blob: 1784921c645dad35d4a5f485f75ae890f6803ad2 [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;
Vadim Bendebury56193272014-10-13 15:51:48 -070062my $min_conf_desc_length = 4;
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";
Peter Ujfalusi0ee3e7b2021-11-08 18:33:34 -080066my $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
77sub help {
78 my ($exitcode) = @_;
79
80 print << "EOM";
81Usage: $P [OPTION]... [FILE]...
82Version: $V
83
84Options:
85 -q, --quiet quiet
Dwaipayan Ray52178ce2021-02-26 15:08:26 +053086 -v, --verbose verbose mode
Hannes Eder77f5b102009-09-21 17:04:37 -070087 --no-tree run without a kernel tree
88 --no-signoff do not check for 'Signed-off-by' line
89 --patch treat FILE as patchfile (default)
90 --emacs emacs compile window format
91 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070092 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070093 -g, --git treat FILE as a single commit or git revision range
94 single git commit with:
95 <rev>
96 <rev>^
97 <rev>~n
98 multiple git commits with:
99 <rev1>..<rev2>
100 <rev1>...<rev2>
101 <rev>-<count>
102 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -0700103 -f, --file treat FILE as regular source file
104 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -0700105 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -0700106 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -0700107 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -0700108 --show-types show the specific message type in the output
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700109 --max-line-length=n set the maximum line length, (default $max_line_length)
110 if exceeded, warn on patches
111 requires --strict for use with --file
Vadim Bendebury56193272014-10-13 15:51:48 -0700112 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700113 --tab-size=n set the number of spaces for tab (default $tabsize)
Hannes Eder77f5b102009-09-21 17:04:37 -0700114 --root=PATH PATH to the kernel tree root
115 --no-summary suppress the per-file summary
116 --mailback only produce a report in case of warnings/errors
117 --summary-file include the filename in summary
118 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
119 'values', 'possible', 'type', and 'attr' (default
120 is all off)
121 --test-only=WORD report only warnings/errors containing WORD
122 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700123 --fix EXPERIMENTAL - may create horrible results
124 If correctable single-line errors exist, create
125 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
126 with potential errors corrected to the preferred
127 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800128 --fix-inplace EXPERIMENTAL - may create horrible results
129 Is the same as --fix, but overwrites the input
130 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700131 --ignore-perl-version override checking of perl version. expect
132 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700133 --codespell Use the codespell dictionary for spelling/typos
Peter Ujfalusi0ee3e7b2021-11-08 18:33:34 -0800134 (default:$codespellfile)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700135 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700136 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700137 --color[=WHEN] Use colors 'always', 'never', or only when output
138 is a terminal ('auto'). Default is 'auto'.
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700139 --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default
140 ${CONFIG_})
Hannes Eder77f5b102009-09-21 17:04:37 -0700141 -h, --help, --version display this help and exit
142
143When FILE is - read standard input.
144EOM
145
146 exit($exitcode);
147}
148
Joe Perches3beb42e2016-05-20 17:04:14 -0700149sub uniq {
150 my %seen;
151 return grep { !$seen{$_}++ } @_;
152}
153
154sub list_types {
155 my ($exitcode) = @_;
156
157 my $count = 0;
158
159 local $/ = undef;
160
161 open(my $script, '<', abs_path($P)) or
162 die "$P: Can't read '$P' $!\n";
163
164 my $text = <$script>;
165 close($script);
166
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530167 my %types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700168 # Also catch when type or level is passed through a variable
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530169 while ($text =~ /(?:(\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
170 if (defined($1)) {
171 if (exists($types{$2})) {
172 $types{$2} .= ",$1" if ($types{$2} ne $1);
173 } else {
174 $types{$2} = $1;
175 }
176 } else {
177 $types{$2} = "UNDETERMINED";
178 }
Joe Perches3beb42e2016-05-20 17:04:14 -0700179 }
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530180
Joe Perches3beb42e2016-05-20 17:04:14 -0700181 print("#\tMessage type\n\n");
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530182 if ($color) {
183 print(" ( Color coding: ");
184 print(RED . "ERROR" . RESET);
185 print(" | ");
186 print(YELLOW . "WARNING" . RESET);
187 print(" | ");
188 print(GREEN . "CHECK" . RESET);
189 print(" | ");
190 print("Multiple levels / Undetermined");
191 print(" )\n\n");
192 }
193
194 foreach my $type (sort keys %types) {
195 my $orig_type = $type;
196 if ($color) {
197 my $level = $types{$type};
198 if ($level eq "ERROR") {
199 $type = RED . $type . RESET;
200 } elsif ($level eq "WARN") {
201 $type = YELLOW . $type . RESET;
202 } elsif ($level eq "CHK") {
203 $type = GREEN . $type . RESET;
204 }
205 }
Joe Perches3beb42e2016-05-20 17:04:14 -0700206 print(++$count . "\t" . $type . "\n");
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530207 if ($verbose && exists($verbose_messages{$orig_type})) {
208 my $message = $verbose_messages{$orig_type};
209 $message =~ s/\n/\n\t/g;
210 print("\t" . $message . "\n\n");
211 }
Joe Perches3beb42e2016-05-20 17:04:14 -0700212 }
213
214 exit($exitcode);
215}
216
Joe Perches000d1cc12011-07-25 17:13:25 -0700217my $conf = which_conf($configuration_file);
218if (-f $conf) {
219 my @conf_args;
220 open(my $conffile, '<', "$conf")
221 or warn "$P: Can't find a readable $configuration_file file $!\n";
222
223 while (<$conffile>) {
224 my $line = $_;
225
226 $line =~ s/\s*\n?$//g;
227 $line =~ s/^\s*//g;
228 $line =~ s/\s+/ /g;
229
230 next if ($line =~ m/^\s*#/);
231 next if ($line =~ m/^\s*$/);
232
233 my @words = split(" ", $line);
234 foreach my $word (@words) {
235 last if ($word =~ m/^#/);
236 push (@conf_args, $word);
237 }
238 }
239 close($conffile);
240 unshift(@ARGV, @conf_args) if @conf_args;
241}
242
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530243sub load_docs {
244 open(my $docs, '<', "$docsfile")
245 or warn "$P: Can't read the documentation file $docsfile $!\n";
246
247 my $type = '';
248 my $desc = '';
249 my $in_desc = 0;
250
251 while (<$docs>) {
252 chomp;
253 my $line = $_;
254 $line =~ s/\s+$//;
255
256 if ($line =~ /^\s*\*\*(.+)\*\*$/) {
257 if ($desc ne '') {
258 $verbose_messages{$type} = trim($desc);
259 }
260 $type = $1;
261 $desc = '';
262 $in_desc = 1;
263 } elsif ($in_desc) {
264 if ($line =~ /^(?:\s{4,}|$)/) {
265 $line =~ s/^\s{4}//;
266 $desc .= $line;
267 $desc .= "\n";
268 } else {
269 $verbose_messages{$type} = trim($desc);
270 $type = '';
271 $desc = '';
272 $in_desc = 0;
273 }
274 }
275 }
276
277 if ($desc ne '') {
278 $verbose_messages{$type} = trim($desc);
279 }
280 close($docs);
281}
282
John Brooks737c0762017-07-10 15:52:24 -0700283# Perl's Getopt::Long allows options to take optional arguments after a space.
284# Prevent --color by itself from consuming other arguments
285foreach (@ARGV) {
286 if ($_ eq "--color" || $_ eq "-color") {
287 $_ = "--color=$color";
288 }
289}
290
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700291GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700292 'q|quiet+' => \$quiet,
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530293 'v|verbose!' => \$verbose,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700294 'tree!' => \$tree,
295 'signoff!' => \$chk_signoff,
296 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700297 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800298 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700299 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700300 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700301 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700302 'subjective!' => \$check,
303 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700304 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700305 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700306 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700307 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800308 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700309 'min-conf-desc-length=i' => \$min_conf_desc_length,
Antonio Borneo713a09d2020-04-06 20:11:07 -0700310 'tab-size=i' => \$tabsize,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700311 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800312 'summary!' => \$summary,
313 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800314 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700315 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800316 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700317 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800318 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700319 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700320 'codespell!' => \$codespell,
Peter Ujfalusi0ee3e7b2021-11-08 18:33:34 -0800321 'codespellfile=s' => \$user_codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700322 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700323 'color=s' => \$color,
324 'no-color' => \$color, #keep old behaviors of -nocolor
325 'nocolor' => \$color, #keep old behaviors of -nocolor
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700326 'kconfig-prefix=s' => \${CONFIG_},
Hannes Eder77f5b102009-09-21 17:04:37 -0700327 'h|help' => \$help,
328 'version' => \$help
Peter Ujfalusi0ee3e7b2021-11-08 18:33:34 -0800329) or $help = 2;
Hannes Eder77f5b102009-09-21 17:04:37 -0700330
Peter Ujfalusi0ee3e7b2021-11-08 18:33:34 -0800331if ($user_codespellfile) {
332 # Use the user provided codespell file unconditionally
333 $codespellfile = $user_codespellfile;
334} elsif (!(-f $codespellfile)) {
335 # If /usr/share/codespell/dictionary.txt is not present, try to find it
336 # under codespell's install directory: <codespell_root>/data/dictionary.txt
337 if (($codespell || $help) && which("codespell") ne "" && which("python") ne "") {
338 my $python_codespell_dict = << "EOF";
339
340import os.path as op
341import codespell_lib
342codespell_dir = op.dirname(codespell_lib.__file__)
343codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt')
344print(codespell_file, end='')
345EOF
346
347 my $codespell_dict = `python -c "$python_codespell_dict" 2> /dev/null`;
348 $codespellfile = $codespell_dict if (-f $codespell_dict);
349 }
350}
351
352# $help is 1 if either -h, --help or --version is passed as option - exitcode: 0
353# $help is 2 if invalid option is passed - exitcode: 1
354help($help - 1) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700355
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530356die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
357die "$P: --verbose cannot be used with --terse\n" if ($verbose && $terse);
358
359if ($color =~ /^[01]$/) {
360 $color = !$color;
361} elsif ($color =~ /^always$/i) {
362 $color = 1;
363} elsif ($color =~ /^never$/i) {
364 $color = 0;
365} elsif ($color =~ /^auto$/i) {
366 $color = (-t STDOUT);
367} else {
368 die "$P: Invalid color mode: $color\n";
369}
370
371load_docs() if ($verbose);
Joe Perches3beb42e2016-05-20 17:04:14 -0700372list_types(0) if ($list_types);
373
Joe Perches9624b8d2014-01-23 15:54:44 -0800374$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700375$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800376
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700377my $exit = 0;
378
Joe Perches5b579802018-08-21 21:57:33 -0700379my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700380if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700381 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700382 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700383 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700384}
385
Allen Hubbe45107ff2016-08-02 14:04:47 -0700386#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700387if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700388 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700389}
390
Antonio Borneo713a09d2020-04-06 20:11:07 -0700391# skip TAB size 1 to avoid additional checks on $tabsize - 1
Joe Perches32f30ca2020-06-04 16:50:40 -0700392die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
Antonio Borneo713a09d2020-04-06 20:11:07 -0700393
Joe Perches91bfe482013-09-11 14:23:59 -0700394sub hash_save_array_words {
395 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700396
Joe Perches91bfe482013-09-11 14:23:59 -0700397 my @array = split(/,/, join(',', @$arrayRef));
398 foreach my $word (@array) {
399 $word =~ s/\s*\n?$//g;
400 $word =~ s/^\s*//g;
401 $word =~ s/\s+/ /g;
402 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700403
Joe Perches91bfe482013-09-11 14:23:59 -0700404 next if ($word =~ m/^\s*#/);
405 next if ($word =~ m/^\s*$/);
406
407 $hashRef->{$word}++;
408 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700409}
410
Joe Perches91bfe482013-09-11 14:23:59 -0700411sub hash_show_words {
412 my ($hashRef, $prefix) = @_;
413
Joe Perches3c816e42015-06-25 15:03:29 -0700414 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700415 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700416 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700417 print " $word";
418 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700419 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700420 }
421}
422
423hash_save_array_words(\%ignore_type, \@ignore);
424hash_save_array_words(\%use_type, \@use);
425
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800426my $dbg_values = 0;
427my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700428my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700429my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800430for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800431 ## no critic
432 eval "\${dbg_$key} = '$debug{$key}';";
433 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800434}
435
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700436my $rpt_cleaners = 0;
437
Andy Whitcroft8905a672007-11-28 16:21:06 -0800438if ($terse) {
439 $emacs = 1;
440 $quiet++;
441}
442
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700443if ($tree) {
444 if (defined $root) {
445 if (!top_of_kernel_tree($root)) {
446 die "$P: $root: --root does not point at a valid tree\n";
447 }
448 } else {
449 if (top_of_kernel_tree('.')) {
450 $root = '.';
451 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
452 top_of_kernel_tree($1)) {
453 $root = $1;
454 }
455 }
456
457 if (!defined $root) {
458 print "Must be run from the top-level dir. of a kernel tree\n";
459 exit(2);
460 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700461}
462
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700463my $emitted_corrupt = 0;
464
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700465our $Ident = qr{
466 [A-Za-z_][A-Za-z\d_]*
467 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
468 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700469our $Storage = qr{extern|static|asmlinkage};
470our $Sparse = qr{
471 __user|
472 __kernel|
473 __force|
474 __iomem|
475 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800476 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700477 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700478 __refconst|
479 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800480 __rcu|
481 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700482 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800483our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
484our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
485our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
486our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
487our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700488
Wolfram Sang52131292010-03-05 13:43:51 -0800489# Notes to $Attribute:
490# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700491our $Attribute = qr{
492 const|
Joe Perchesb5e87362021-02-25 17:21:40 -0800493 volatile|
Joe Perches03f1df72010-10-26 14:23:16 -0700494 __percpu|
495 __nocast|
496 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200497 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700498 __packed__|
499 __packed2__|
500 __naked|
501 __maybe_unused|
502 __always_unused|
503 __noreturn|
504 __used|
505 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800506 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700507 __noclone|
508 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700509 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700510 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700511 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700512 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700513 ____cacheline_aligned|
514 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800515 ____cacheline_internodealigned_in_smp|
Kees Cook86cffec2021-11-05 13:36:19 -0700516 __weak|
517 __alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\)
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700518 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700519our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700520our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700521our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
522our $Lval = qr{$Ident(?:$Member)*};
523
Joe Perches95e2c602013-07-03 15:05:20 -0700524our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
525our $Binary = qr{(?i)0b[01]+$Int_type?};
526our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
527our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700528our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesd2af5aa2021-09-07 19:59:51 -0700529our $String = qr{(?:\b[Lu])?"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800530our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
531our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
532our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800533our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700534our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800535our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700536our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700537our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700538our $Operators = qr{
539 <=|>=|==|!=|
540 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700541 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700542 }x;
543
Joe Perches91cb5192014-04-03 14:49:32 -0700544our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
545
Joe Perchesab7e23f2015-04-16 12:44:22 -0700546our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800547our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700548our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700549our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800550our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700551our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800552our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700553our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800554
Joe Perches15662b32011-10-31 17:13:12 -0700555our $NON_ASCII_UTF8 = qr{
556 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700557 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
558 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
559 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
560 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
561 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
562 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
563}x;
564
Joe Perches15662b32011-10-31 17:13:12 -0700565our $UTF8 = qr{
566 [\x09\x0A\x0D\x20-\x7E] # ASCII
567 | $NON_ASCII_UTF8
568}x;
569
Joe Perchese6176fa2015-06-25 15:02:49 -0700570our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800571our $typeOtherOSTypedefs = qr{(?x:
572 u_(?:char|short|int|long) | # bsd
573 u(?:nchar|short|int|long) # sysv
574)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700575our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700576 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700577 atomic_t
578)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700579our $typeTypedefs = qr{(?x:
580 $typeC99Typedefs\b|
581 $typeOtherOSTypedefs\b|
582 $typeKernelTypedefs\b
583)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700584
Joe Perches6d32f7a2015-11-06 16:31:37 -0800585our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
586
Joe Perches691e6692010-03-05 13:43:51 -0800587our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800588 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700589 (?:[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 -0800590 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700591 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700592 panic|
Joe Perches06668722013-11-12 15:10:07 -0800593 MODULE_[A-Z_]+|
594 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800595)};
596
Joe Perchese29a70f2019-03-07 16:28:35 -0800597our $allocFunctions = qr{(?x:
598 (?:(?:devm_)?
Joe Perches58f02262021-02-25 17:22:01 -0800599 (?:kv|k|v)[czm]alloc(?:_array)?(?:_node)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800600 kstrdup(?:_const)? |
601 kmemdup(?:_nul)?) |
Christophe JAILLET461e1562020-04-20 18:13:58 -0700602 (?:\w+)?alloc_skb(?:_ip_align)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800603 # dev_alloc_skb/netdev_alloc_skb, et al
604 dma_alloc_coherent
605)};
606
Joe Perches20112472011-07-25 17:13:23 -0700607our $signature_tags = qr{(?xi:
608 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800609 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700610 Acked-by:|
611 Tested-by:|
612 Reviewed-by:|
613 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700614 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700615 To:|
616 Cc:
617)};
618
Joe Perchesadb2da82021-02-25 17:21:50 -0800619our $tracing_logging_tags = qr{(?xi:
620 [=-]*> |
621 <[=-]* |
622 \[ |
623 \] |
624 start |
625 called |
626 entered |
627 entry |
628 enter |
629 in |
630 inside |
631 here |
632 begin |
633 exit |
634 end |
635 done |
636 leave |
637 completed |
638 out |
639 return |
640 [\.\!:\s]*
641)};
642
Aditya Srivastava831242a2020-12-15 20:45:12 -0800643sub edit_distance_min {
644 my (@arr) = @_;
645 my $len = scalar @arr;
646 if ((scalar @arr) < 1) {
647 # if underflow, return
648 return;
649 }
650 my $min = $arr[0];
651 for my $i (0 .. ($len-1)) {
652 if ($arr[$i] < $min) {
653 $min = $arr[$i];
654 }
655 }
656 return $min;
657}
658
659sub get_edit_distance {
660 my ($str1, $str2) = @_;
661 $str1 = lc($str1);
662 $str2 = lc($str2);
663 $str1 =~ s/-//g;
664 $str2 =~ s/-//g;
665 my $len1 = length($str1);
666 my $len2 = length($str2);
667 # two dimensional array storing minimum edit distance
668 my @distance;
669 for my $i (0 .. $len1) {
670 for my $j (0 .. $len2) {
671 if ($i == 0) {
672 $distance[$i][$j] = $j;
673 } elsif ($j == 0) {
674 $distance[$i][$j] = $i;
675 } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
676 $distance[$i][$j] = $distance[$i - 1][$j - 1];
677 } else {
678 my $dist1 = $distance[$i][$j - 1]; #insert distance
679 my $dist2 = $distance[$i - 1][$j]; # remove
680 my $dist3 = $distance[$i - 1][$j - 1]; #replace
681 $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
682 }
683 }
684 }
685 return $distance[$len1][$len2];
686}
687
688sub find_standard_signature {
689 my ($sign_off) = @_;
690 my @standard_signature_tags = (
691 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
692 'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
693 );
694 foreach my $signature (@standard_signature_tags) {
695 return $signature if (get_edit_distance($sign_off, $signature) <= 2);
696 }
697
698 return "";
699}
700
Joe Perches18130872014-08-06 16:11:22 -0700701our @typeListMisordered = (
702 qr{char\s+(?:un)?signed},
703 qr{int\s+(?:(?:un)?signed\s+)?short\s},
704 qr{int\s+short(?:\s+(?:un)?signed)},
705 qr{short\s+int(?:\s+(?:un)?signed)},
706 qr{(?:un)?signed\s+int\s+short},
707 qr{short\s+(?:un)?signed},
708 qr{long\s+int\s+(?:un)?signed},
709 qr{int\s+long\s+(?:un)?signed},
710 qr{long\s+(?:un)?signed\s+int},
711 qr{int\s+(?:un)?signed\s+long},
712 qr{int\s+(?:un)?signed},
713 qr{int\s+long\s+long\s+(?:un)?signed},
714 qr{long\s+long\s+int\s+(?:un)?signed},
715 qr{long\s+long\s+(?:un)?signed\s+int},
716 qr{long\s+long\s+(?:un)?signed},
717 qr{long\s+(?:un)?signed},
718);
719
Andy Whitcroft8905a672007-11-28 16:21:06 -0800720our @typeList = (
721 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700722 qr{(?:(?:un)?signed\s+)?char},
723 qr{(?:(?:un)?signed\s+)?short\s+int},
724 qr{(?:(?:un)?signed\s+)?short},
725 qr{(?:(?:un)?signed\s+)?int},
726 qr{(?:(?:un)?signed\s+)?long\s+int},
727 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
728 qr{(?:(?:un)?signed\s+)?long\s+long},
729 qr{(?:(?:un)?signed\s+)?long},
730 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800731 qr{float},
732 qr{double},
733 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800734 qr{struct\s+$Ident},
735 qr{union\s+$Ident},
736 qr{enum\s+$Ident},
737 qr{${Ident}_t},
738 qr{${Ident}_handler},
739 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700740 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800741);
Joe Perches938224b2016-01-20 14:59:15 -0800742
743our $C90_int_types = qr{(?x:
744 long\s+long\s+int\s+(?:un)?signed|
745 long\s+long\s+(?:un)?signed\s+int|
746 long\s+long\s+(?:un)?signed|
747 (?:(?:un)?signed\s+)?long\s+long\s+int|
748 (?:(?:un)?signed\s+)?long\s+long|
749 int\s+long\s+long\s+(?:un)?signed|
750 int\s+(?:(?:un)?signed\s+)?long\s+long|
751
752 long\s+int\s+(?:un)?signed|
753 long\s+(?:un)?signed\s+int|
754 long\s+(?:un)?signed|
755 (?:(?:un)?signed\s+)?long\s+int|
756 (?:(?:un)?signed\s+)?long|
757 int\s+long\s+(?:un)?signed|
758 int\s+(?:(?:un)?signed\s+)?long|
759
760 int\s+(?:un)?signed|
761 (?:(?:un)?signed\s+)?int
762)};
763
Alex Dowad485ff232015-06-25 15:02:52 -0700764our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700765our @typeListWithAttr = (
766 @typeList,
767 qr{struct\s+$InitAttribute\s+$Ident},
768 qr{union\s+$InitAttribute\s+$Ident},
769);
770
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700771our @modifierList = (
772 qr{fastcall},
773);
Alex Dowad485ff232015-06-25 15:02:52 -0700774our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800775
Joe Perches24358802014-04-03 14:49:13 -0700776our @mode_permission_funcs = (
777 ["module_param", 3],
778 ["module_param_(?:array|named|string)", 4],
779 ["module_param_array_named", 5],
780 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
781 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700782 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
783 ["IIO_DEV_ATTR_[A-Z_]+", 1],
784 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
785 ["SENSOR_TEMPLATE(?:_2|)", 3],
786 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700787);
788
Joe Perches1a3dcf22020-08-11 18:35:16 -0700789my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
790
Joe Perches515a2352014-04-03 14:49:24 -0700791#Create a search pattern for all these functions to speed up a loop below
792our $mode_perms_search = "";
793foreach my $entry (@mode_permission_funcs) {
794 $mode_perms_search .= '|' if ($mode_perms_search ne "");
795 $mode_perms_search .= $entry->[0];
796}
Joe Perches00180462018-02-06 15:38:55 -0800797$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700798
Joe Perches9189c7e2018-09-07 15:26:18 -0700799our %deprecated_apis = (
800 "synchronize_rcu_bh" => "synchronize_rcu",
801 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
802 "call_rcu_bh" => "call_rcu",
803 "rcu_barrier_bh" => "rcu_barrier",
804 "synchronize_sched" => "synchronize_rcu",
805 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
806 "call_rcu_sched" => "call_rcu",
807 "rcu_barrier_sched" => "rcu_barrier",
808 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
809 "cond_synchronize_sched" => "cond_synchronize_rcu",
810);
811
812#Create a search pattern for all these strings to speed up a loop below
813our $deprecated_apis_search = "";
814foreach my $entry (keys %deprecated_apis) {
815 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
816 $deprecated_apis_search .= $entry;
817}
818$deprecated_apis_search = "(?:${deprecated_apis_search})";
819
Joe Perchesb392c642015-04-16 12:44:16 -0700820our $mode_perms_world_writable = qr{
821 S_IWUGO |
822 S_IWOTH |
823 S_IRWXUGO |
824 S_IALLUGO |
825 0[0-7][0-7][2367]
826}x;
827
Joe Perchesf90774e2016-10-11 13:51:47 -0700828our %mode_permission_string_types = (
829 "S_IRWXU" => 0700,
830 "S_IRUSR" => 0400,
831 "S_IWUSR" => 0200,
832 "S_IXUSR" => 0100,
833 "S_IRWXG" => 0070,
834 "S_IRGRP" => 0040,
835 "S_IWGRP" => 0020,
836 "S_IXGRP" => 0010,
837 "S_IRWXO" => 0007,
838 "S_IROTH" => 0004,
839 "S_IWOTH" => 0002,
840 "S_IXOTH" => 0001,
841 "S_IRWXUGO" => 0777,
842 "S_IRUGO" => 0444,
843 "S_IWUGO" => 0222,
844 "S_IXUGO" => 0111,
845);
846
847#Create a search pattern for all these strings to speed up a loop below
848our $mode_perms_string_search = "";
849foreach my $entry (keys %mode_permission_string_types) {
850 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
851 $mode_perms_string_search .= $entry;
852}
Joe Perches00180462018-02-06 15:38:55 -0800853our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
854our $multi_mode_perms_string_search = qr{
855 ${single_mode_perms_string_search}
856 (?:\s*\|\s*${single_mode_perms_string_search})*
857}x;
858
859sub perms_to_octal {
860 my ($string) = @_;
861
862 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
863
864 my $val = "";
865 my $oval = "";
866 my $to = 0;
867 my $curpos = 0;
868 my $lastpos = 0;
869 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
870 $curpos = pos($string);
871 my $match = $2;
872 my $omatch = $1;
873 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
874 $lastpos = $curpos;
875 $to |= $mode_permission_string_types{$match};
876 $val .= '\s*\|\s*' if ($val ne "");
877 $val .= $match;
878 $oval .= $omatch;
879 }
880 $oval =~ s/^\s*\|\s*//;
881 $oval =~ s/\s*\|\s*$//;
882 return sprintf("%04o", $to);
883}
Joe Perchesf90774e2016-10-11 13:51:47 -0700884
Wolfram Sang7840a942010-08-09 17:20:57 -0700885our $allowed_asm_includes = qr{(?x:
886 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700887 memory|
888 time|
889 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700890)};
891# memory.h: ARM has a custom one
892
Kees Cook66b47b42014-10-13 15:51:57 -0700893# Load common spelling mistakes and build regular expression list.
894my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700895my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700896
Joe Perches36061e32014-12-10 15:51:43 -0800897if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800898 while (<$spelling>) {
899 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700900
Joe Perches36061e32014-12-10 15:51:43 -0800901 $line =~ s/\s*\n?$//g;
902 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700903
Joe Perches36061e32014-12-10 15:51:43 -0800904 next if ($line =~ m/^\s*#/);
905 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700906
Joe Perches36061e32014-12-10 15:51:43 -0800907 my ($suspect, $fix) = split(/\|\|/, $line);
908
Joe Perches36061e32014-12-10 15:51:43 -0800909 $spelling_fix{$suspect} = $fix;
910 }
911 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800912} else {
913 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700914}
Kees Cook66b47b42014-10-13 15:51:57 -0700915
Joe Perchesebfd7d62015-04-16 12:44:14 -0700916if ($codespell) {
917 if (open(my $spelling, '<', $codespellfile)) {
918 while (<$spelling>) {
919 my $line = $_;
920
921 $line =~ s/\s*\n?$//g;
922 $line =~ s/^\s*//g;
923
924 next if ($line =~ m/^\s*#/);
925 next if ($line =~ m/^\s*$/);
926 next if ($line =~ m/, disabled/i);
927
928 $line =~ s/,.*$//;
929
930 my ($suspect, $fix) = split(/->/, $line);
931
932 $spelling_fix{$suspect} = $fix;
933 }
934 close($spelling);
935 } else {
936 warn "No codespell typos will be found - file '$codespellfile': $!\n";
937 }
938}
939
940$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
941
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700942sub read_words {
943 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700944
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700945 if (open(my $words, '<', $file)) {
946 while (<$words>) {
947 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700948
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700949 $line =~ s/\s*\n?$//g;
950 $line =~ s/^\s*//g;
951
952 next if ($line =~ m/^\s*#/);
953 next if ($line =~ m/^\s*$/);
954 if ($line =~ /\s/) {
955 print("$file: '$line' invalid - ignored\n");
956 next;
957 }
958
Quentin Monnetced69da12020-08-11 18:35:13 -0700959 $$wordsRef .= '|' if (defined $$wordsRef);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700960 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700961 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700962 close($file);
963 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700964 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700965
966 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700967}
968
Quentin Monnetced69da12020-08-11 18:35:13 -0700969my $const_structs;
970if (show_type("CONST_STRUCT")) {
971 read_words(\$const_structs, $conststructsfile)
972 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
973}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700974
Quentin Monnetced69da12020-08-11 18:35:13 -0700975if (defined($typedefsfile)) {
976 my $typeOtherTypedefs;
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700977 read_words(\$typeOtherTypedefs, $typedefsfile)
978 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
Quentin Monnetced69da12020-08-11 18:35:13 -0700979 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700980}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700981
Andy Whitcroft8905a672007-11-28 16:21:06 -0800982sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700983 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
984 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700985 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700986 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700987 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700988 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700989 (?:$typeTypedefs\b)|
990 (?:${all}\b)
991 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800992 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700993 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800994 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800995 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700996 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700997 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800998 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700999 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -08001000 }x;
Joe Perches18130872014-08-06 16:11:22 -07001001 $NonptrTypeMisordered = qr{
1002 (?:$Modifier\s+|const\s+)*
1003 (?:
1004 (?:${Misordered}\b)
1005 )
1006 (?:\s+$Modifier|\s+const)*
1007 }x;
Joe Perches8716de32013-09-11 14:24:05 -07001008 $NonptrTypeWithAttr = qr{
1009 (?:$Modifier\s+|const\s+)*
1010 (?:
1011 (?:typeof|__typeof__)\s*\([^\)]*\)|
1012 (?:$typeTypedefs\b)|
1013 (?:${allWithAttr}\b)
1014 )
1015 (?:\s+$Modifier|\s+const)*
1016 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001017 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001018 $NonptrType
Antonio Borneo7b184962020-04-06 20:11:04 -07001019 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001020 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -08001021 }x;
Joe Perches18130872014-08-06 16:11:22 -07001022 $TypeMisordered = qr{
1023 $NonptrTypeMisordered
Antonio Borneo7b184962020-04-06 20:11:04 -07001024 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Perches18130872014-08-06 16:11:22 -07001025 (?:\s+$Inline|\s+$Modifier)*
1026 }x;
Joe Perches91cb5192014-04-03 14:49:32 -07001027 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -07001028 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -08001029}
1030build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001031
Joe Perches7d2367a2011-07-25 17:13:22 -07001032our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001033
1034# Using $balanced_parens, $LvalOrFunc, or $FuncArg
1035# requires at least perl version v5.10.0
1036# Any use must be runtime checked with $^V
1037
1038our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -07001039our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -08001040our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -07001041
Joe Perchesf8422302014-08-06 16:11:31 -07001042our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -07001043 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -07001044 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -07001045 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -07001046)};
1047
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08001048our %allow_repeated_words = (
1049 add => '',
1050 added => '',
1051 bad => '',
1052 be => '',
1053);
1054
Joe Perches7d2367a2011-07-25 17:13:22 -07001055sub deparenthesize {
1056 my ($string) = @_;
1057 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -07001058
1059 while ($string =~ /^\s*\(.*\)\s*$/) {
1060 $string =~ s@^\s*\(\s*@@;
1061 $string =~ s@\s*\)\s*$@@;
1062 }
1063
Joe Perches7d2367a2011-07-25 17:13:22 -07001064 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -07001065
Joe Perches7d2367a2011-07-25 17:13:22 -07001066 return $string;
1067}
1068
Joe Perches34456862013-07-03 15:05:34 -07001069sub seed_camelcase_file {
1070 my ($file) = @_;
1071
1072 return if (!(-f $file));
1073
1074 local $/;
1075
1076 open(my $include_file, '<', "$file")
1077 or warn "$P: Can't read '$file' $!\n";
1078 my $text = <$include_file>;
1079 close($include_file);
1080
1081 my @lines = split('\n', $text);
1082
1083 foreach my $line (@lines) {
1084 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
1085 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
1086 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -08001087 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
1088 $camelcase{$1} = 1;
1089 } 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 -07001090 $camelcase{$1} = 1;
1091 }
1092 }
1093}
1094
Joe Perchescd28b112019-12-04 16:52:09 -08001095our %maintained_status = ();
1096
Joe Perches85b0ee12016-10-11 13:51:44 -07001097sub is_maintained_obsolete {
1098 my ($filename) = @_;
1099
Jerome Forissierf2c19c22016-12-12 16:46:23 -08001100 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -07001101
Joe Perchescd28b112019-12-04 16:52:09 -08001102 if (!exists($maintained_status{$filename})) {
1103 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
1104 }
Joe Perches85b0ee12016-10-11 13:51:44 -07001105
Joe Perchescd28b112019-12-04 16:52:09 -08001106 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -07001107}
1108
Joe Perches3b6e8ac2018-08-21 21:57:47 -07001109sub is_SPDX_License_valid {
1110 my ($license) = @_;
1111
Guenter Roeckf9363b32021-06-30 18:56:19 -07001112 return 1 if (!$tree || which("python3") eq "" || !(-x "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -07001113
Joe Perches56294112018-08-21 21:58:04 -07001114 my $root_path = abs_path($root);
Guenter Roeckf9363b32021-06-30 18:56:19 -07001115 my $status = `cd "$root_path"; echo "$license" | scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -07001116 return 0 if ($status ne "");
1117 return 1;
1118}
1119
Joe Perches34456862013-07-03 15:05:34 -07001120my $camelcase_seeded = 0;
1121sub seed_camelcase_includes {
1122 return if ($camelcase_seeded);
1123
1124 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -07001125 my $camelcase_cache = "";
1126 my @include_files = ();
1127
1128 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -07001129
Joe Perches0f7f6352020-10-24 16:59:04 -07001130 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -07001131 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -07001132 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -07001133 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -07001134 } else {
Joe Perchesc707a812013-07-08 16:00:43 -07001135 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -07001136 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -07001137 @include_files = split('\n', $files);
1138 foreach my $file (@include_files) {
1139 my $date = POSIX::strftime("%Y%m%d%H%M",
1140 localtime((stat $file)[9]));
1141 $last_mod_date = $date if ($last_mod_date < $date);
1142 }
1143 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -07001144 }
Joe Perchesc707a812013-07-08 16:00:43 -07001145
1146 if ($camelcase_cache ne "" && -f $camelcase_cache) {
1147 open(my $camelcase_file, '<', "$camelcase_cache")
1148 or warn "$P: Can't read '$camelcase_cache' $!\n";
1149 while (<$camelcase_file>) {
1150 chomp;
1151 $camelcase{$_} = 1;
1152 }
1153 close($camelcase_file);
1154
1155 return;
1156 }
1157
Joe Perches0f7f6352020-10-24 16:59:04 -07001158 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -07001159 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -07001160 @include_files = split('\n', $files);
1161 }
1162
Joe Perches34456862013-07-03 15:05:34 -07001163 foreach my $file (@include_files) {
1164 seed_camelcase_file($file);
1165 }
Joe Perches351b2a12013-07-03 15:05:36 -07001166
Joe Perchesc707a812013-07-08 16:00:43 -07001167 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -07001168 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -07001169 open(my $camelcase_file, '>', "$camelcase_cache")
1170 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -07001171 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1172 print $camelcase_file ("$_\n");
1173 }
1174 close($camelcase_file);
1175 }
Joe Perches34456862013-07-03 15:05:34 -07001176}
1177
Joe Perchesf5f61322020-10-15 20:12:12 -07001178sub git_is_single_file {
1179 my ($filename) = @_;
1180
1181 return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1182
1183 my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1184 my $count = $output =~ tr/\n//;
1185 return $count eq 1 && $output =~ m{^${filename}$};
1186}
1187
Joe Perchesd311cd42014-08-06 16:10:57 -07001188sub git_commit_info {
1189 my ($commit, $id, $desc) = @_;
1190
Joe Perches0f7f6352020-10-24 16:59:04 -07001191 return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
Joe Perchesd311cd42014-08-06 16:10:57 -07001192
Joe Perchesdbbf8692019-09-25 16:46:52 -07001193 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -07001194 $output =~ s/^\s*//gm;
1195 my @lines = split("\n", $output);
1196
Joe Perches0d7835f2015-02-13 14:38:35 -08001197 return ($id, $desc) if ($#lines < 0);
1198
Sean Christopherson5a7f4452019-09-25 16:46:49 -07001199 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -07001200# Maybe one day convert this block of bash into something that returns
1201# all matching commit ids, but it's very slow...
1202#
1203# echo "checking commits $1..."
1204# git rev-list --remotes | grep -i "^$1" |
1205# while read line ; do
1206# git log --format='%H %s' -1 $line |
1207# echo "commit $(cut -c 1-12,41-)"
1208# done
Joe Perches4ce9f972021-09-07 19:59:57 -07001209 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ ||
1210 $lines[0] =~ /^fatal: bad object $commit/) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07001211 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -07001212 } else {
1213 $id = substr($lines[0], 0, 12);
1214 $desc = substr($lines[0], 41);
1215 }
1216
1217 return ($id, $desc);
1218}
1219
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001220$chk_signoff = 0 if ($file);
1221
Andy Whitcroft00df3442007-06-08 13:47:06 -07001222my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001223my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001224my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001225my @fixed_inserted = ();
1226my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001227my $fixlinenr = -1;
1228
Du, Changbin4a593c32016-05-20 17:04:16 -07001229# If input is git commits, extract all commits from the commit expressions.
1230# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
Joe Perches0f7f6352020-10-24 16:59:04 -07001231die "$P: No git repository found\n" if ($git && !-e "$gitroot");
Du, Changbin4a593c32016-05-20 17:04:16 -07001232
1233if ($git) {
1234 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001235 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001236 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001237 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1238 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001239 } elsif ($commit_expr =~ m/\.\./) {
1240 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001241 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001242 $git_range = "-1 $commit_expr";
1243 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001244 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001245 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001246 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1247 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001248 my $sha1 = $1;
1249 my $subject = $2;
1250 unshift(@commits, $sha1);
1251 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001252 }
1253 }
1254 die "$P: no git commits after extraction!\n" if (@commits == 0);
1255 @ARGV = @commits;
1256}
1257
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001258my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001259$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001260for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001261 my $FILE;
Joe Perchesf5f61322020-10-15 20:12:12 -07001262 my $is_git_file = git_is_single_file($filename);
1263 my $oldfile = $file;
1264 $file = 1 if ($is_git_file);
Du, Changbin4a593c32016-05-20 17:04:16 -07001265 if ($git) {
1266 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1267 die "$P: $filename: git format-patch failed - $!\n";
1268 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001269 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001270 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001271 } elsif ($filename eq '-') {
1272 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001273 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001274 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001275 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001276 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001277 if ($filename eq '-') {
1278 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001279 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001280 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001281 } else {
1282 $vname = $filename;
1283 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001284 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001285 chomp;
1286 push(@rawlines, $_);
Geert Uytterhoevenc7f574d2020-06-04 16:50:43 -07001287 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001288 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001289 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001290
1291 if ($#ARGV > 0 && $quiet == 0) {
1292 print '-' x length($vname) . "\n";
1293 print "$vname\n";
1294 print '-' x length($vname) . "\n";
1295 }
1296
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001297 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001298 $exit = 1;
1299 }
1300 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001301 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001302 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001303 @fixed_inserted = ();
1304 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001305 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001306 @modifierListFile = ();
1307 @typeListFile = ();
1308 build_types();
Joe Perchesf5f61322020-10-15 20:12:12 -07001309 $file = $oldfile if ($is_git_file);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001310}
1311
Joe Perchesd8469f12015-06-25 15:03:00 -07001312if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001313 hash_show_words(\%use_type, "Used");
1314 hash_show_words(\%ignore_type, "Ignored");
1315
Joe Perches5b579802018-08-21 21:57:33 -07001316 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001317 print << "EOM"
1318
1319NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001320 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001321EOM
1322 }
1323 if ($exit) {
1324 print << "EOM"
1325
1326NOTE: If any of the errors are false positives, please report
1327 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1328EOM
1329 }
1330}
1331
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001332exit($exit);
1333
1334sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001335 my ($root) = @_;
1336
1337 my @tree_check = (
1338 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1339 "README", "Documentation", "arch", "include", "drivers",
1340 "fs", "init", "ipc", "kernel", "lib", "scripts",
1341 );
1342
1343 foreach my $check (@tree_check) {
1344 if (! -e $root . '/' . $check) {
1345 return 0;
1346 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001347 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001348 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001349}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001350
Joe Perches20112472011-07-25 17:13:23 -07001351sub parse_email {
1352 my ($formatted_email) = @_;
1353
1354 my $name = "";
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001355 my $quoted = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001356 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001357 my $address = "";
1358 my $comment = "";
1359
1360 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1361 $name = $1;
1362 $address = $2;
1363 $comment = $3 if defined $3;
1364 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1365 $address = $1;
1366 $comment = $2 if defined $2;
1367 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1368 $address = $1;
1369 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001370 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001371 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001372 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001373 $name =~ s/^\"|\"$//g;
1374 # If there's a name left after stripping spaces and
1375 # leading quotes, and the address doesn't have both
1376 # leading and trailing angle brackets, the address
1377 # is invalid. ie:
1378 # "joe smith joe@smith.com" bad
1379 # "joe smith <joe@smith.com" bad
1380 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1381 $name = "";
1382 $address = "";
1383 $comment = "";
1384 }
1385 }
1386
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001387 # Extract comments from names excluding quoted parts
1388 # "John D. (Doe)" - Do not extract
1389 if ($name =~ s/\"(.+)\"//) {
1390 $quoted = $1;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001391 }
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001392 while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1393 $name_comment .= trim($1);
1394 }
1395 $name =~ s/^[ \"]+|[ \"]+$//g;
1396 $name = trim("$quoted $name");
1397
Joe Perches3705ce52013-07-03 15:05:31 -07001398 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001399 $address =~ s/^\<|\>$//g;
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001400 $comment = trim($comment);
Joe Perches20112472011-07-25 17:13:23 -07001401
1402 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1403 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1404 $name = "\"$name\"";
1405 }
1406
Joe Perchesdfa05c22020-04-06 20:10:48 -07001407 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001408}
1409
1410sub format_email {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001411 my ($name, $name_comment, $address, $comment) = @_;
Joe Perches20112472011-07-25 17:13:23 -07001412
1413 my $formatted_email;
1414
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001415 $name =~ s/^[ \"]+|[ \"]+$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001416 $address = trim($address);
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001417 $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
Joe Perches20112472011-07-25 17:13:23 -07001418
1419 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1420 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1421 $name = "\"$name\"";
1422 }
1423
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001424 $name_comment = trim($name_comment);
1425 $name_comment = " $name_comment" if ($name_comment ne "");
1426 $comment = trim($comment);
1427 $comment = " $comment" if ($comment ne "");
1428
Joe Perches20112472011-07-25 17:13:23 -07001429 if ("$name" eq "") {
1430 $formatted_email = "$address";
1431 } else {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001432 $formatted_email = "$name$name_comment <$address>";
Joe Perches20112472011-07-25 17:13:23 -07001433 }
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001434 $formatted_email .= "$comment";
Joe Perches20112472011-07-25 17:13:23 -07001435 return $formatted_email;
1436}
1437
Joe Perchesdfa05c22020-04-06 20:10:48 -07001438sub reformat_email {
1439 my ($email) = @_;
1440
1441 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001442 return format_email($email_name, $name_comment, $email_address, $comment);
Joe Perchesdfa05c22020-04-06 20:10:48 -07001443}
1444
1445sub same_email_addresses {
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001446 my ($email1, $email2) = @_;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001447
1448 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1449 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1450
1451 return $email1_name eq $email2_name &&
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001452 $email1_address eq $email2_address &&
1453 $name1_comment eq $name2_comment &&
1454 $comment1 eq $comment2;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001455}
1456
Joe Perchesd311cd42014-08-06 16:10:57 -07001457sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001458 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001459
Joe Perchesbd474ca2014-08-06 16:11:10 -07001460 foreach my $path (split(/:/, $ENV{PATH})) {
1461 if (-e "$path/$bin") {
1462 return "$path/$bin";
1463 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001464 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001465
Joe Perchesbd474ca2014-08-06 16:11:10 -07001466 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001467}
1468
Joe Perches000d1cc12011-07-25 17:13:25 -07001469sub which_conf {
1470 my ($conf) = @_;
1471
1472 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1473 if (-e "$path/$conf") {
1474 return "$path/$conf";
1475 }
1476 }
1477
1478 return "";
1479}
1480
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001481sub expand_tabs {
1482 my ($str) = @_;
1483
1484 my $res = '';
1485 my $n = 0;
1486 for my $c (split(//, $str)) {
1487 if ($c eq "\t") {
1488 $res .= ' ';
1489 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001490 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001491 $res .= ' ';
1492 }
1493 next;
1494 }
1495 $res .= $c;
1496 $n++;
1497 }
1498
1499 return $res;
1500}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001501sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001502 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001503 return $res;
1504}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001505
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001506sub line_stats {
1507 my ($line) = @_;
1508
1509 # Drop the diff line leader and expand tabs
1510 $line =~ s/^.//;
1511 $line = expand_tabs($line);
1512
1513 # Pick the indent from the front of the line.
1514 my ($white) = ($line =~ /^(\s*)/);
1515
1516 return (length($line), length($white));
1517}
1518
Andy Whitcroft773647a2008-03-28 14:15:58 -07001519my $sanitise_quote = '';
1520
1521sub sanitise_line_reset {
1522 my ($in_comment) = @_;
1523
1524 if ($in_comment) {
1525 $sanitise_quote = '*/';
1526 } else {
1527 $sanitise_quote = '';
1528 }
1529}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001530sub sanitise_line {
1531 my ($line) = @_;
1532
1533 my $res = '';
1534 my $l = '';
1535
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001536 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001537 my $off = 0;
1538 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001539
Andy Whitcroft773647a2008-03-28 14:15:58 -07001540 # Always copy over the diff marker.
1541 $res = substr($line, 0, 1);
1542
1543 for ($off = 1; $off < length($line); $off++) {
1544 $c = substr($line, $off, 1);
1545
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001546 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001547 # and end, all to $;.
1548 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1549 $sanitise_quote = '*/';
1550
1551 substr($res, $off, 2, "$;$;");
1552 $off++;
1553 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001554 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001555 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001556 $sanitise_quote = '';
1557 substr($res, $off, 2, "$;$;");
1558 $off++;
1559 next;
1560 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001561 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1562 $sanitise_quote = '//';
1563
1564 substr($res, $off, 2, $sanitise_quote);
1565 $off++;
1566 next;
1567 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001568
1569 # A \ in a string means ignore the next character.
1570 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1571 $c eq "\\") {
1572 substr($res, $off, 2, 'XX');
1573 $off++;
1574 next;
1575 }
1576 # Regular quotes.
1577 if ($c eq "'" || $c eq '"') {
1578 if ($sanitise_quote eq '') {
1579 $sanitise_quote = $c;
1580
1581 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001582 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001583 } elsif ($sanitise_quote eq $c) {
1584 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001585 }
1586 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001587
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001588 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001589 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1590 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001591 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1592 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001593 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1594 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001595 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001596 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001597 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001598 }
1599
Daniel Walker113f04a2009-09-21 17:04:35 -07001600 if ($sanitise_quote eq '//') {
1601 $sanitise_quote = '';
1602 }
1603
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001604 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001605 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001606 my $clean = 'X' x length($1);
1607 $res =~ s@\<.*\>@<$clean>@;
1608
1609 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001610 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001611 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001612 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001613 }
1614
Joe Perchesdadf6802016-08-02 14:04:33 -07001615 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1616 my $match = $1;
1617 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1618 }
1619
Andy Whitcroft00df3442007-06-08 13:47:06 -07001620 return $res;
1621}
1622
Joe Perchesa6962d72013-04-29 16:18:13 -07001623sub get_quoted_string {
1624 my ($line, $rawline) = @_;
1625
Joe Perches478b1792018-04-10 16:33:34 -07001626 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001627 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001628 return substr($rawline, $-[0], $+[0] - $-[0]);
1629}
1630
Andy Whitcroft8905a672007-11-28 16:21:06 -08001631sub ctx_statement_block {
1632 my ($linenr, $remain, $off) = @_;
1633 my $line = $linenr - 1;
1634 my $blk = '';
1635 my $soff = $off;
1636 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001637 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001638
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001639 my $loff = 0;
1640
Andy Whitcroft8905a672007-11-28 16:21:06 -08001641 my $type = '';
1642 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001643 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001644 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001645 my $c;
1646 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001647
1648 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001649 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001650 @stack = (['', 0]) if ($#stack == -1);
1651
Andy Whitcroft773647a2008-03-28 14:15:58 -07001652 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001653 # If we are about to drop off the end, pull in more
1654 # context.
1655 if ($off >= $len) {
1656 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001657 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001658 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001659 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001660 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001661 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001662 $len = length($blk);
1663 $line++;
1664 last;
1665 }
1666 # Bail if there is no further context.
1667 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001668 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001669 last;
1670 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001671 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1672 $level++;
1673 $type = '#';
1674 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001675 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001676 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001677 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001678 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001679
Andy Whitcroft773647a2008-03-28 14:15:58 -07001680 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001681
1682 # Handle nested #if/#else.
1683 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1684 push(@stack, [ $type, $level ]);
1685 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1686 ($type, $level) = @{$stack[$#stack - 1]};
1687 } elsif ($remainder =~ /^#\s*endif\b/) {
1688 ($type, $level) = @{pop(@stack)};
1689 }
1690
Andy Whitcroft8905a672007-11-28 16:21:06 -08001691 # Statement ends at the ';' or a close '}' at the
1692 # outermost level.
1693 if ($level == 0 && $c eq ';') {
1694 last;
1695 }
1696
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001697 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001698 if ($level == 0 && $coff_set == 0 &&
1699 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1700 $remainder =~ /^(else)(?:\s|{)/ &&
1701 $remainder !~ /^else\s+if\b/) {
1702 $coff = $off + length($1) - 1;
1703 $coff_set = 1;
1704 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1705 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001706 }
1707
Andy Whitcroft8905a672007-11-28 16:21:06 -08001708 if (($type eq '' || $type eq '(') && $c eq '(') {
1709 $level++;
1710 $type = '(';
1711 }
1712 if ($type eq '(' && $c eq ')') {
1713 $level--;
1714 $type = ($level != 0)? '(' : '';
1715
1716 if ($level == 0 && $coff < $soff) {
1717 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001718 $coff_set = 1;
1719 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001720 }
1721 }
1722 if (($type eq '' || $type eq '{') && $c eq '{') {
1723 $level++;
1724 $type = '{';
1725 }
1726 if ($type eq '{' && $c eq '}') {
1727 $level--;
1728 $type = ($level != 0)? '{' : '';
1729
1730 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001731 if (substr($blk, $off + 1, 1) eq ';') {
1732 $off++;
1733 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001734 last;
1735 }
1736 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001737 # Preprocessor commands end at the newline unless escaped.
1738 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1739 $level--;
1740 $type = '';
1741 $off++;
1742 last;
1743 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001744 $off++;
1745 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001746 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001747 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001748 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001749 $line++;
1750 $remain--;
1751 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001752
1753 my $statement = substr($blk, $soff, $off - $soff + 1);
1754 my $condition = substr($blk, $soff, $coff - $soff + 1);
1755
1756 #warn "STATEMENT<$statement>\n";
1757 #warn "CONDITION<$condition>\n";
1758
Andy Whitcroft773647a2008-03-28 14:15:58 -07001759 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001760
1761 return ($statement, $condition,
1762 $line, $remain + 1, $off - $loff + 1, $level);
1763}
1764
Andy Whitcroftcf655042008-03-04 14:28:20 -08001765sub statement_lines {
1766 my ($stmt) = @_;
1767
1768 # Strip the diff line prefixes and rip blank lines at start and end.
1769 $stmt =~ s/(^|\n)./$1/g;
1770 $stmt =~ s/^\s*//;
1771 $stmt =~ s/\s*$//;
1772
1773 my @stmt_lines = ($stmt =~ /\n/g);
1774
1775 return $#stmt_lines + 2;
1776}
1777
1778sub statement_rawlines {
1779 my ($stmt) = @_;
1780
1781 my @stmt_lines = ($stmt =~ /\n/g);
1782
1783 return $#stmt_lines + 2;
1784}
1785
1786sub statement_block_size {
1787 my ($stmt) = @_;
1788
1789 $stmt =~ s/(^|\n)./$1/g;
1790 $stmt =~ s/^\s*{//;
1791 $stmt =~ s/}\s*$//;
1792 $stmt =~ s/^\s*//;
1793 $stmt =~ s/\s*$//;
1794
1795 my @stmt_lines = ($stmt =~ /\n/g);
1796 my @stmt_statements = ($stmt =~ /;/g);
1797
1798 my $stmt_lines = $#stmt_lines + 2;
1799 my $stmt_statements = $#stmt_statements + 1;
1800
1801 if ($stmt_lines > $stmt_statements) {
1802 return $stmt_lines;
1803 } else {
1804 return $stmt_statements;
1805 }
1806}
1807
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001808sub ctx_statement_full {
1809 my ($linenr, $remain, $off) = @_;
1810 my ($statement, $condition, $level);
1811
1812 my (@chunks);
1813
Andy Whitcroftcf655042008-03-04 14:28:20 -08001814 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001815 ($statement, $condition, $linenr, $remain, $off, $level) =
1816 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001817 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001818 push(@chunks, [ $condition, $statement ]);
1819 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1820 return ($level, $linenr, @chunks);
1821 }
1822
1823 # Pull in the following conditional/block pairs and see if they
1824 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001825 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001826 ($statement, $condition, $linenr, $remain, $off, $level) =
1827 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001828 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001829 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001830 #print "C: push\n";
1831 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001832 }
1833
1834 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001835}
1836
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001837sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001838 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001839 my $line;
1840 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001841 my $blk = '';
1842 my @o;
1843 my @c;
1844 my @res = ();
1845
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001846 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001847 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001848 for ($line = $start; $remain > 0; $line++) {
1849 next if ($rawlines[$line] =~ /^-/);
1850 $remain--;
1851
1852 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001853
1854 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001855 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001856 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001857 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001858 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001859 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001860 $level = pop(@stack);
1861 }
1862
Andy Whitcroft01464f32010-10-26 14:23:19 -07001863 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001864 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1865 if ($off > 0) {
1866 $off--;
1867 next;
1868 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001869
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001870 if ($c eq $close && $level > 0) {
1871 $level--;
1872 last if ($level == 0);
1873 } elsif ($c eq $open) {
1874 $level++;
1875 }
1876 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001877
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001878 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001879 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001880 }
1881
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001882 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001883 }
1884
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001885 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001886}
1887sub ctx_block_outer {
1888 my ($linenr, $remain) = @_;
1889
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001890 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1891 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001892}
1893sub ctx_block {
1894 my ($linenr, $remain) = @_;
1895
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001896 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1897 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001898}
1899sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001900 my ($linenr, $remain, $off) = @_;
1901
1902 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1903 return @r;
1904}
1905sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001906 my ($linenr, $remain) = @_;
1907
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001908 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001909}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001910sub ctx_statement_level {
1911 my ($linenr, $remain, $off) = @_;
1912
1913 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1914}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001915
1916sub ctx_locate_comment {
1917 my ($first_line, $end_line) = @_;
1918
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001919 # If c99 comment on the current line, or the line before or after
1920 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1921 return $current_comment if (defined $current_comment);
1922 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1923 return $current_comment if (defined $current_comment);
1924 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1925 return $current_comment if (defined $current_comment);
1926
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001927 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001928 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001929 return $current_comment if (defined $current_comment);
1930
1931 # Look through the context and try and figure out if there is a
1932 # comment.
1933 my $in_comment = 0;
1934 $current_comment = '';
1935 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001936 my $line = $rawlines[$linenr - 1];
1937 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001938 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1939 $in_comment = 1;
1940 }
1941 if ($line =~ m@/\*@) {
1942 $in_comment = 1;
1943 }
1944 if (!$in_comment && $current_comment ne '') {
1945 $current_comment = '';
1946 }
1947 $current_comment .= $line . "\n" if ($in_comment);
1948 if ($line =~ m@\*/@) {
1949 $in_comment = 0;
1950 }
1951 }
1952
1953 chomp($current_comment);
1954 return($current_comment);
1955}
1956sub ctx_has_comment {
1957 my ($first_line, $end_line) = @_;
1958 my $cmt = ctx_locate_comment($first_line, $end_line);
1959
Andy Whitcroft00df3442007-06-08 13:47:06 -07001960 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001961 ##print "CMMT: $cmt\n";
1962
1963 return ($cmt ne '');
1964}
1965
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001966sub raw_line {
1967 my ($linenr, $cnt) = @_;
1968
1969 my $offset = $linenr - 1;
1970 $cnt++;
1971
1972 my $line;
1973 while ($cnt) {
1974 $line = $rawlines[$offset++];
1975 next if (defined($line) && $line =~ /^-/);
1976 $cnt--;
1977 }
1978
1979 return $line;
1980}
1981
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001982sub get_stat_real {
1983 my ($linenr, $lc) = @_;
1984
1985 my $stat_real = raw_line($linenr, 0);
1986 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1987 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1988 }
1989
1990 return $stat_real;
1991}
1992
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001993sub get_stat_here {
1994 my ($linenr, $cnt, $here) = @_;
1995
1996 my $herectx = $here . "\n";
1997 for (my $n = 0; $n < $cnt; $n++) {
1998 $herectx .= raw_line($linenr, $n) . "\n";
1999 }
2000
2001 return $herectx;
2002}
2003
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002004sub cat_vet {
2005 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002006 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002007
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002008 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002009 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
2010 $res .= $1;
2011 if ($2 ne '') {
2012 $coded = sprintf("^%c", unpack('C', $2) + 64);
2013 $res .= $coded;
2014 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002015 }
2016 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002017
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002018 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002019}
2020
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002021my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002022my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002023my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002024my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002025
2026sub annotate_reset {
2027 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002028 $av_pending = '_';
2029 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002030 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002031}
2032
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002033sub annotate_values {
2034 my ($stream, $type) = @_;
2035
2036 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002037 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002038 my $cur = $stream;
2039
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002040 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002041
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002042 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002043 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002044 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002045 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002046 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002047 print "WS($1)\n" if ($dbg_values > 1);
2048 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002049 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002050 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002051 }
2052
Florian Micklerc023e4732011-01-12 16:59:58 -08002053 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07002054 print "CAST($1)\n" if ($dbg_values > 1);
2055 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08002056 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07002057
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07002058 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002059 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002060 $type = 'T';
2061
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07002062 } elsif ($cur =~ /^($Modifier)\s*/) {
2063 print "MODIFIER($1)\n" if ($dbg_values > 1);
2064 $type = 'T';
2065
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002066 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002067 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002068 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002069 push(@av_paren_type, $type);
2070 if ($2 ne '') {
2071 $av_pending = 'N';
2072 }
2073 $type = 'E';
2074
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002075 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002076 print "UNDEF($1)\n" if ($dbg_values > 1);
2077 $av_preprocessor = 1;
2078 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002079
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002080 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002081 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002082 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002083
2084 push(@av_paren_type, $type);
2085 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002086 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08002087
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002088 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002089 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
2090 $av_preprocessor = 1;
2091
2092 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
2093
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002094 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08002095
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002096 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002097 print "PRE_END($1)\n" if ($dbg_values > 1);
2098
2099 $av_preprocessor = 1;
2100
2101 # Assume all arms of the conditional end as this
2102 # one does, and continue as if the #endif was not here.
2103 pop(@av_paren_type);
2104 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002105 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002106
2107 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002108 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002109
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002110 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
2111 print "ATTR($1)\n" if ($dbg_values > 1);
2112 $av_pending = $type;
2113 $type = 'N';
2114
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002115 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002116 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002117 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002118 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002119 }
2120 $type = 'N';
2121
Andy Whitcroft14b111c2008-10-15 22:02:16 -07002122 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002123 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07002124 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002125 $type = 'N';
2126
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002127 } elsif ($cur =~/^(case)/o) {
2128 print "CASE($1)\n" if ($dbg_values > 1);
2129 $av_pend_colon = 'C';
2130 $type = 'N';
2131
Andy Whitcroft14b111c2008-10-15 22:02:16 -07002132 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002133 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002134 $type = 'N';
2135
2136 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002137 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002138 push(@av_paren_type, $av_pending);
2139 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002140 $type = 'N';
2141
2142 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002143 my $new_type = pop(@av_paren_type);
2144 if ($new_type ne '_') {
2145 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002146 print "PAREN('$1') -> $type\n"
2147 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002148 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002149 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002150 }
2151
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07002152 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002153 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07002154 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08002155 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002156
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002157 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
2158 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002159 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002160 } elsif ($type eq 'E') {
2161 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002162 }
2163 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
2164 $type = 'V';
2165
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002166 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002167 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002168 $type = 'V';
2169
2170 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002171 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002172 $type = 'N';
2173
Andy Whitcroftcf655042008-03-04 14:28:20 -08002174 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002175 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002176 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002177 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002178
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002179 } elsif ($cur =~/^(,)/) {
2180 print "COMMA($1)\n" if ($dbg_values > 1);
2181 $type = 'C';
2182
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002183 } elsif ($cur =~ /^(\?)/o) {
2184 print "QUESTION($1)\n" if ($dbg_values > 1);
2185 $type = 'N';
2186
2187 } elsif ($cur =~ /^(:)/o) {
2188 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
2189
2190 substr($var, length($res), 1, $av_pend_colon);
2191 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
2192 $type = 'E';
2193 } else {
2194 $type = 'N';
2195 }
2196 $av_pend_colon = 'O';
2197
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002198 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002199 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002200 $type = 'N';
2201
Andy Whitcroft0d413862008-10-15 22:02:16 -07002202 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002203 my $variant;
2204
2205 print "OPV($1)\n" if ($dbg_values > 1);
2206 if ($type eq 'V') {
2207 $variant = 'B';
2208 } else {
2209 $variant = 'U';
2210 }
2211
2212 substr($var, length($res), 1, $variant);
2213 $type = 'N';
2214
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002215 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002216 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002217 if ($1 ne '++' && $1 ne '--') {
2218 $type = 'N';
2219 }
2220
2221 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002222 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002223 }
2224 if (defined $1) {
2225 $cur = substr($cur, length($1));
2226 $res .= $type x length($1);
2227 }
2228 }
2229
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002230 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002231}
2232
Andy Whitcroft8905a672007-11-28 16:21:06 -08002233sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002234 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002235 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002236 ^(?:
2237 $Modifier|
2238 $Storage|
2239 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002240 DEFINE_\S+
2241 )$|
2242 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002243 goto|
2244 return|
2245 case|
2246 else|
2247 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002248 do|
2249 \#|
2250 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002251 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002252 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002253 )}x;
2254 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2255 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002256 # Check for modifiers.
2257 $possible =~ s/\s*$Storage\s*//g;
2258 $possible =~ s/\s*$Sparse\s*//g;
2259 if ($possible =~ /^\s*$/) {
2260
2261 } elsif ($possible =~ /\s/) {
2262 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002263 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002264 if ($modifier !~ $notPermitted) {
2265 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002266 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002267 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002268 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002269
2270 } else {
2271 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002272 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002273 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002274 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002275 } else {
2276 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002277 }
2278}
2279
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002280my $prefix = '';
2281
Joe Perches000d1cc12011-07-25 17:13:25 -07002282sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002283 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002284
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002285 $type =~ tr/[a-z]/[A-Z]/;
2286
Joe Perchescbec18a2014-04-03 14:49:19 -07002287 return defined $use_type{$type} if (scalar keys %use_type > 0);
2288
2289 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002290}
2291
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002292sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002293 my ($level, $type, $msg) = @_;
2294
2295 if (!show_type($type) ||
2296 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002297 return 0;
2298 }
Joe Perches57230292015-06-25 15:03:03 -07002299 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002300 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002301 if ($level eq 'ERROR') {
2302 $output .= RED;
2303 } elsif ($level eq 'WARNING') {
2304 $output .= YELLOW;
2305 } else {
2306 $output .= GREEN;
2307 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002308 }
Joe Perches57230292015-06-25 15:03:03 -07002309 $output .= $prefix . $level . ':';
2310 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002311 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002312 $output .= "$type:";
2313 }
John Brooks737c0762017-07-10 15:52:24 -07002314 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002315 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002316
2317 if ($showfile) {
2318 my @lines = split("\n", $output, -1);
2319 splice(@lines, 1, 1);
2320 $output = join("\n", @lines);
2321 }
Dwaipayan Ray52178ce2021-02-26 15:08:26 +05302322
2323 if ($terse) {
2324 $output = (split('\n', $output))[0] . "\n";
2325 }
2326
2327 if ($verbose && exists($verbose_messages{$type}) &&
2328 !exists($verbose_emitted{$type})) {
2329 $output .= $verbose_messages{$type} . "\n\n";
2330 $verbose_emitted{$type} = 1;
2331 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002332
Joe Perches57230292015-06-25 15:03:03 -07002333 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002334
2335 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002336}
Joe Perchescbec18a2014-04-03 14:49:19 -07002337
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002338sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002339 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002340}
Joe Perches000d1cc12011-07-25 17:13:25 -07002341
Joe Perchesd752fcc2014-08-06 16:11:05 -07002342sub fixup_current_range {
2343 my ($lineRef, $offset, $length) = @_;
2344
2345 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2346 my $o = $1;
2347 my $l = $2;
2348 my $no = $o + $offset;
2349 my $nl = $l + $length;
2350 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2351 }
2352}
2353
2354sub fix_inserted_deleted_lines {
2355 my ($linesRef, $insertedRef, $deletedRef) = @_;
2356
2357 my $range_last_linenr = 0;
2358 my $delta_offset = 0;
2359
2360 my $old_linenr = 0;
2361 my $new_linenr = 0;
2362
2363 my $next_insert = 0;
2364 my $next_delete = 0;
2365
2366 my @lines = ();
2367
2368 my $inserted = @{$insertedRef}[$next_insert++];
2369 my $deleted = @{$deletedRef}[$next_delete++];
2370
2371 foreach my $old_line (@{$linesRef}) {
2372 my $save_line = 1;
2373 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002374 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002375 $delta_offset = 0;
2376 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2377 $range_last_linenr = $new_linenr;
2378 fixup_current_range(\$line, $delta_offset, 0);
2379 }
2380
2381 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2382 $deleted = @{$deletedRef}[$next_delete++];
2383 $save_line = 0;
2384 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2385 }
2386
2387 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2388 push(@lines, ${$inserted}{'LINE'});
2389 $inserted = @{$insertedRef}[$next_insert++];
2390 $new_linenr++;
2391 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2392 }
2393
2394 if ($save_line) {
2395 push(@lines, $line);
2396 $new_linenr++;
2397 }
2398
2399 $old_linenr++;
2400 }
2401
2402 return @lines;
2403}
2404
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002405sub fix_insert_line {
2406 my ($linenr, $line) = @_;
2407
2408 my $inserted = {
2409 LINENR => $linenr,
2410 LINE => $line,
2411 };
2412 push(@fixed_inserted, $inserted);
2413}
2414
2415sub fix_delete_line {
2416 my ($linenr, $line) = @_;
2417
2418 my $deleted = {
2419 LINENR => $linenr,
2420 LINE => $line,
2421 };
2422
2423 push(@fixed_deleted, $deleted);
2424}
2425
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002426sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002427 my ($type, $msg) = @_;
2428
2429 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002430 our $clean = 0;
2431 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002432 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002433 }
Joe Perches3705ce52013-07-03 15:05:31 -07002434 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002435}
2436sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002437 my ($type, $msg) = @_;
2438
2439 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002440 our $clean = 0;
2441 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002442 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002443 }
Joe Perches3705ce52013-07-03 15:05:31 -07002444 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002445}
2446sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002447 my ($type, $msg) = @_;
2448
2449 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002450 our $clean = 0;
2451 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002452 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002453 }
Joe Perches3705ce52013-07-03 15:05:31 -07002454 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002455}
2456
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002457sub check_absolute_file {
2458 my ($absolute, $herecurr) = @_;
2459 my $file = $absolute;
2460
2461 ##print "absolute<$absolute>\n";
2462
2463 # See if any suffix of this path is a path within the tree.
2464 while ($file =~ s@^[^/]*/@@) {
2465 if (-f "$root/$file") {
2466 ##print "file<$file>\n";
2467 last;
2468 }
2469 }
2470 if (! -f _) {
2471 return 0;
2472 }
2473
2474 # It is, so see if the prefix is acceptable.
2475 my $prefix = $absolute;
2476 substr($prefix, -length($file)) = '';
2477
2478 ##print "prefix<$prefix>\n";
2479 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002480 WARN("USE_RELATIVE_PATH",
2481 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002482 }
2483}
2484
Joe Perches3705ce52013-07-03 15:05:31 -07002485sub trim {
2486 my ($string) = @_;
2487
Joe Perchesb34c6482013-09-11 14:24:01 -07002488 $string =~ s/^\s+|\s+$//g;
2489
2490 return $string;
2491}
2492
2493sub ltrim {
2494 my ($string) = @_;
2495
2496 $string =~ s/^\s+//;
2497
2498 return $string;
2499}
2500
2501sub rtrim {
2502 my ($string) = @_;
2503
2504 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002505
2506 return $string;
2507}
2508
Joe Perches52ea8502013-11-12 15:10:09 -08002509sub string_find_replace {
2510 my ($string, $find, $replace) = @_;
2511
2512 $string =~ s/$find/$replace/g;
2513
2514 return $string;
2515}
2516
Joe Perches3705ce52013-07-03 15:05:31 -07002517sub tabify {
2518 my ($leading) = @_;
2519
Antonio Borneo713a09d2020-04-06 20:11:07 -07002520 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002521 my $max_spaces_before_tab = $source_indent - 1;
2522 my $spaces_to_tab = " " x $source_indent;
2523
2524 #convert leading spaces to tabs
2525 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2526 #Remove spaces before a tab
2527 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2528
2529 return "$leading";
2530}
2531
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002532sub pos_last_openparen {
2533 my ($line) = @_;
2534
2535 my $pos = 0;
2536
2537 my $opens = $line =~ tr/\(/\(/;
2538 my $closes = $line =~ tr/\)/\)/;
2539
2540 my $last_openparen = 0;
2541
2542 if (($opens == 0) || ($closes >= $opens)) {
2543 return -1;
2544 }
2545
2546 my $len = length($line);
2547
2548 for ($pos = 0; $pos < $len; $pos++) {
2549 my $string = substr($line, $pos);
2550 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2551 $pos += length($1) - 1;
2552 } elsif (substr($line, $pos, 1) eq '(') {
2553 $last_openparen = $pos;
2554 } elsif (index($string, '(') == -1) {
2555 last;
2556 }
2557 }
2558
Joe Perches91cb5192014-04-03 14:49:32 -07002559 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002560}
2561
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002562sub get_raw_comment {
2563 my ($line, $rawline) = @_;
2564 my $comment = '';
2565
2566 for my $i (0 .. (length($line) - 1)) {
2567 if (substr($line, $i, 1) eq "$;") {
2568 $comment .= substr($rawline, $i, 1);
2569 }
2570 }
2571
2572 return $comment;
2573}
2574
Song Liu5b8f82e2021-02-25 17:22:08 -08002575sub exclude_global_initialisers {
2576 my ($realfile) = @_;
2577
2578 # Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
2579 return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ ||
2580 $realfile =~ m@^samples/bpf/.*_kern\.c$@ ||
2581 $realfile =~ m@/bpf/.*\.bpf\.c$@;
2582}
2583
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002584sub process {
2585 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002586
2587 my $linenr=0;
2588 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002589 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002590 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002591 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002592
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002593 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002594 my $indent;
2595 my $previndent=0;
2596 my $stashindent=0;
2597
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002598 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002599 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002600 my $author = '';
2601 my $authorsignoff = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002602 my $author_sob = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002603 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002604 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002605 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002606 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002607 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002608 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002609 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002610 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002611 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002612 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002613 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002614 my $non_utf8_charset = 0;
2615
Joe Perches4ce9f972021-09-07 19:59:57 -07002616 my $last_git_commit_id_linenr = -1;
2617
Joe Perches365dd4e2014-08-06 16:10:42 -07002618 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002619 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002620
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002621 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002622 our $cnt_lines = 0;
2623 our $cnt_error = 0;
2624 our $cnt_warn = 0;
2625 our $cnt_chk = 0;
2626
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002627 # Trace the real file/line as we go.
2628 my $realfile = '';
2629 my $realline = 0;
2630 my $realcnt = 0;
2631 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002632 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002633 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002634 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002635 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002636 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002637
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002638 my $prev_values = 'E';
2639
2640 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002641 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002642 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002643 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002644 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002645
Joe Perches7e51f192013-09-11 14:23:57 -07002646 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002647
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002648 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002649 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002650 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002651 my @setup_docs = ();
2652 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002653
Joe Perchesd8b07712013-11-12 15:10:06 -08002654 my $camelcase_file_seeded = 0;
2655
Rob Herring9f3a8992018-04-10 16:33:13 -07002656 my $checklicenseline = 1;
2657
Andy Whitcroft773647a2008-03-28 14:15:58 -07002658 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002659 my $line;
2660 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002661 $linenr++;
2662 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002663
Joe Perches3705ce52013-07-03 15:05:31 -07002664 push(@fixed, $rawline) if ($fix);
2665
Andy Whitcroft773647a2008-03-28 14:15:58 -07002666 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002667 $setup_docs = 0;
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07002668 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002669 $setup_docs = 1;
2670 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002671 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002672 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002673 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002674 $realline=$1-1;
2675 if (defined $2) {
2676 $realcnt=$3+1;
2677 } else {
2678 $realcnt=1+1;
2679 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002680 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002681
2682 # Guestimate if this is a continuing comment. Run
2683 # the context looking for a comment "edge". If this
2684 # edge is a close comment then we must be in a comment
2685 # at context start.
2686 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002687 my $cnt = $realcnt;
2688 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2689 next if (defined $rawlines[$ln - 1] &&
2690 $rawlines[$ln - 1] =~ /^-/);
2691 $cnt--;
2692 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002693 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002694 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2695 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2696 ($edge) = $1;
2697 last;
2698 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002699 }
2700 if (defined $edge && $edge eq '*/') {
2701 $in_comment = 1;
2702 }
2703
2704 # Guestimate if this is a continuing comment. If this
2705 # is the start of a diff block and this line starts
2706 # ' *' then it is very likely a comment.
2707 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002708 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002709 {
2710 $in_comment = 1;
2711 }
2712
2713 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2714 sanitise_line_reset($in_comment);
2715
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002716 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002717 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002718 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002719 $line = sanitise_line($rawline);
2720 }
2721 push(@lines, $line);
2722
2723 if ($realcnt > 1) {
2724 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2725 } else {
2726 $realcnt = 0;
2727 }
2728
2729 #print "==>$rawline\n";
2730 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002731
2732 if ($setup_docs && $line =~ /^\+/) {
2733 push(@setup_docs, $line);
2734 }
2735 }
2736
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002737 $prefix = '';
2738
Andy Whitcroft773647a2008-03-28 14:15:58 -07002739 $realcnt = 0;
2740 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002741 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002742 foreach my $line (@lines) {
2743 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002744 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002745 my $sline = $line; #copy of $line
2746 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002747
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002748 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002749 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002750
Joe Perches12c253a2018-06-07 17:10:58 -07002751# check if it's a mode change, rename or start of a patch
2752 if (!$in_commit_log &&
2753 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2754 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2755 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2756 $is_patch = 1;
2757 }
2758
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002759#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002760 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002761 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2762 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002763 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002764 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002765 $realline=$1-1;
2766 if (defined $2) {
2767 $realcnt=$3+1;
2768 } else {
2769 $realcnt=1+1;
2770 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002771 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002772 $prev_values = 'E';
2773
Andy Whitcroft773647a2008-03-28 14:15:58 -07002774 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002775 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002776 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002777 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002778 if ($context =~ /\b(\w+)\s*\(/) {
2779 $context_function = $1;
2780 } else {
2781 undef $context_function;
2782 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002783 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002784
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002785# track the line number as we move through the hunk, note that
2786# new versions of GNU diff omit the leading space on completely
2787# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002788 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002789 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002790 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002791
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002792 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002793 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002794
2795 # Track the previous line.
2796 ($prevline, $stashline) = ($stashline, $line);
2797 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002798 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2799
Andy Whitcroft773647a2008-03-28 14:15:58 -07002800 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002801
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002802 } elsif ($realcnt == 1) {
2803 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002804 }
2805
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002806 my $hunk_line = ($realcnt != 0);
2807
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002808 $here = "#$linenr: " if (!$file);
2809 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002810
Joe Perches2ac73b42014-06-04 16:12:05 -07002811 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002812 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002813 if ($line =~ /^diff --git.*?(\S+)$/) {
2814 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002815 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002816 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002817 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002818 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002819 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002820 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002821 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002822
2823 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002824 if (!$file && $tree && $p1_prefix ne '' &&
2825 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002826 WARN("PATCH_PREFIX",
2827 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002828 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002829
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002830 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002831 ERROR("MODIFIED_INCLUDE_ASM",
2832 "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 -07002833 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002834 $found_file = 1;
2835 }
2836
Joe Perches34d88152015-06-25 15:03:05 -07002837#make up the handle for any error we report on this line
2838 if ($showfile) {
2839 $prefix = "$realfile:$realline: "
2840 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002841 if ($file) {
2842 $prefix = "$filename:$realline: ";
2843 } else {
2844 $prefix = "$filename:$linenr: ";
2845 }
Joe Perches34d88152015-06-25 15:03:05 -07002846 }
2847
Joe Perches2ac73b42014-06-04 16:12:05 -07002848 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002849 if (is_maintained_obsolete($realfile)) {
2850 WARN("OBSOLETE",
2851 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2852 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002853 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002854 $check = 1;
2855 } else {
2856 $check = $check_orig;
2857 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002858 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002859
2860 if ($realfile !~ /^MAINTAINERS/) {
2861 my $last_binding_patch = $is_binding_patch;
2862
2863 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2864
2865 if (($last_binding_patch != -1) &&
2866 ($last_binding_patch ^ $is_binding_patch)) {
2867 WARN("DT_SPLIT_BINDING_PATCH",
Mauro Carvalho Chehab858e6842020-04-15 16:45:25 +02002868 "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 -07002869 }
2870 }
2871
Andy Whitcroft773647a2008-03-28 14:15:58 -07002872 next;
2873 }
2874
Randy Dunlap389834b2007-06-08 13:47:03 -07002875 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002876
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002877 my $hereline = "$here\n$rawline\n";
2878 my $herecurr = "$here\n$rawline\n";
2879 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002880
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002881 $cnt_lines++ if ($realcnt != 0);
2882
Joe Perches490b2922018-08-21 21:58:01 -07002883# Verify the existence of a commit log if appropriate
2884# 2 is used because a $signature is counted in $commit_log_lines
2885 if ($in_commit_log) {
2886 if ($line !~ /^\s*$/) {
2887 $commit_log_lines++; #could be a $signature
2888 }
2889 } elsif ($has_commit_log && $commit_log_lines < 2) {
2890 WARN("COMMIT_MESSAGE",
2891 "Missing commit description - Add an appropriate one\n");
2892 $commit_log_lines = 2; #warn only once
2893 }
2894
Joe Perchese518e9a2015-06-25 15:03:27 -07002895# Check if the commit log has what seems like a diff which can confuse patch
2896 if ($in_commit_log && !$commit_log_has_diff &&
Mrinal Pandey13e45412020-09-04 16:35:52 -07002897 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2898 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
Joe Perchese518e9a2015-06-25 15:03:27 -07002899 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2900 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2901 ERROR("DIFF_IN_COMMIT_MSG",
2902 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2903 $commit_log_has_diff = 1;
2904 }
2905
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002906# Check for incorrect file permissions
2907 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2908 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002909 if ($realfile !~ m@scripts/@ &&
2910 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002911 ERROR("EXECUTE_PERMISSIONS",
2912 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002913 }
2914 }
2915
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002916# Check the patch for a From:
2917 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2918 $author = $1;
Dwaipayan Raye7f929f2020-10-15 20:12:15 -07002919 my $curline = $linenr;
2920 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2921 $author .= $1;
2922 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002923 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2924 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002925 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002926 }
2927
Joe Perches20112472011-07-25 17:13:23 -07002928# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002929 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002930 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002931 $in_commit_log = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002932 if ($author ne '' && $authorsignoff != 1) {
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002933 if (same_email_addresses($1, $author)) {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002934 $authorsignoff = 1;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002935 } else {
2936 my $ctx = $1;
2937 my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
2938 my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
2939
Mimi Zohar046fc742021-09-07 19:59:54 -07002940 if (lc $email_address eq lc $author_address && $email_name eq $author_name) {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002941 $author_sob = $ctx;
2942 $authorsignoff = 2;
Mimi Zohar046fc742021-09-07 19:59:54 -07002943 } elsif (lc $email_address eq lc $author_address) {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002944 $author_sob = $ctx;
2945 $authorsignoff = 3;
2946 } elsif ($email_name eq $author_name) {
2947 $author_sob = $ctx;
2948 $authorsignoff = 4;
2949
2950 my $address1 = $email_address;
2951 my $address2 = $author_address;
2952
2953 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
2954 $address1 = "$1$2";
2955 }
2956 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
2957 $address2 = "$1$2";
2958 }
2959 if ($address1 eq $address2) {
2960 $authorsignoff = 5;
2961 }
2962 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002963 }
2964 }
Joe Perches20112472011-07-25 17:13:23 -07002965 }
2966
Joe Perches44d303e2020-04-06 20:11:10 -07002967# Check for patch separator
2968 if ($line =~ /^---$/) {
2969 $has_patch_separator = 1;
2970 $in_commit_log = 0;
2971 }
2972
Joe Perchese0d975b2014-12-10 15:51:49 -08002973# Check if MAINTAINERS is being updated. If so, there's probably no need to
2974# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2975 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2976 $reported_maintainer_file = 1;
2977 }
2978
Joe Perches20112472011-07-25 17:13:23 -07002979# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002980 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002981 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002982 my $space_before = $1;
2983 my $sign_off = $2;
2984 my $space_after = $3;
2985 my $email = $4;
2986 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2987
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002988 if ($sign_off !~ /$signature_tags/) {
Aditya Srivastava831242a2020-12-15 20:45:12 -08002989 my $suggested_signature = find_standard_signature($sign_off);
2990 if ($suggested_signature eq "") {
2991 WARN("BAD_SIGN_OFF",
2992 "Non-standard signature: $sign_off\n" . $herecurr);
2993 } else {
2994 if (WARN("BAD_SIGN_OFF",
2995 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
2996 $fix) {
2997 $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
2998 }
2999 }
Joe Perchesce0338df3c2012-07-30 14:41:18 -07003000 }
Joe Perches20112472011-07-25 17:13:23 -07003001 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07003002 if (WARN("BAD_SIGN_OFF",
3003 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
3004 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003005 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07003006 "$ucfirst_sign_off $email";
3007 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003008 }
Joe Perches20112472011-07-25 17:13:23 -07003009 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07003010 if (WARN("BAD_SIGN_OFF",
3011 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
3012 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003013 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07003014 "$ucfirst_sign_off $email";
3015 }
3016
Joe Perches20112472011-07-25 17:13:23 -07003017 }
3018 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07003019 if (WARN("BAD_SIGN_OFF",
3020 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
3021 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003022 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07003023 "$ucfirst_sign_off $email";
3024 }
Joe Perches20112472011-07-25 17:13:23 -07003025 }
3026
Joe Perchesdfa05c22020-04-06 20:10:48 -07003027 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07003028 my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
Joe Perches20112472011-07-25 17:13:23 -07003029 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07003030 ERROR("BAD_SIGN_OFF",
3031 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07003032 } else {
3033 my $dequoted = $suggested_email;
3034 $dequoted =~ s/^"//;
3035 $dequoted =~ s/" </ </;
3036 # Don't force email to have quotes
3037 # Allow just an angle bracketed address
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08003038 if (!same_email_addresses($email, $suggested_email)) {
3039 if (WARN("BAD_SIGN_OFF",
3040 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
3041 $fix) {
3042 $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
3043 }
3044 }
3045
3046 # Address part shouldn't have comments
3047 my $stripped_address = $email_address;
3048 $stripped_address =~ s/\([^\(\)]*\)//g;
3049 if ($email_address ne $stripped_address) {
3050 if (WARN("BAD_SIGN_OFF",
3051 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
3052 $fix) {
3053 $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
3054 }
3055 }
3056
3057 # Only one name comment should be allowed
3058 my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
3059 if ($comment_count > 1) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003060 WARN("BAD_SIGN_OFF",
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08003061 "Use a single name comment in email: '$email'\n" . $herecurr);
3062 }
3063
3064
3065 # stable@vger.kernel.org or stable@kernel.org shouldn't
Dwaipayan Raye73d2712020-12-15 20:44:56 -08003066 # have an email name. In addition comments should strictly
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08003067 # begin with a #
3068 if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
3069 if (($comment ne "" && $comment !~ /^#.+/) ||
3070 ($email_name ne "")) {
3071 my $cur_name = $email_name;
3072 my $new_comment = $comment;
3073 $cur_name =~ s/[a-zA-Z\s\-\"]+//g;
3074
3075 # Remove brackets enclosing comment text
3076 # and # from start of comments to get comment text
3077 $new_comment =~ s/^\((.*)\)$/$1/;
3078 $new_comment =~ s/^\[(.*)\]$/$1/;
3079 $new_comment =~ s/^[\s\#]+|\s+$//g;
3080
3081 $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
3082 $new_comment = " # $new_comment" if ($new_comment ne "");
3083 my $new_email = "$email_address$new_comment";
3084
3085 if (WARN("BAD_STABLE_ADDRESS_STYLE",
3086 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
3087 $fix) {
3088 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3089 }
3090 }
3091 } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
3092 my $new_comment = $comment;
3093
3094 # Extract comment text from within brackets or
3095 # c89 style /*...*/ comments
3096 $new_comment =~ s/^\[(.*)\]$/$1/;
3097 $new_comment =~ s/^\/\*(.*)\*\/$/$1/;
3098
3099 $new_comment = trim($new_comment);
3100 $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
3101 $new_comment = "($new_comment)" if ($new_comment ne "");
3102 my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
3103
3104 if (WARN("BAD_SIGN_OFF",
3105 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
3106 $fix) {
3107 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3108 }
Joe Perches20112472011-07-25 17:13:23 -07003109 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003110 }
Joe Perches7e51f192013-09-11 14:23:57 -07003111
3112# Check for duplicate signatures
3113 my $sig_nospace = $line;
3114 $sig_nospace =~ s/\s//g;
3115 $sig_nospace = lc($sig_nospace);
3116 if (defined $signatures{$sig_nospace}) {
3117 WARN("BAD_SIGN_OFF",
3118 "Duplicate signature\n" . $herecurr);
3119 } else {
3120 $signatures{$sig_nospace} = 1;
3121 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07003122
3123# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
3124 if ($sign_off =~ /^co-developed-by:$/i) {
3125 if ($email eq $author) {
3126 WARN("BAD_SIGN_OFF",
3127 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
3128 }
3129 if (!defined $lines[$linenr]) {
3130 WARN("BAD_SIGN_OFF",
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08003131 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07003132 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
3133 WARN("BAD_SIGN_OFF",
3134 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
3135 } elsif ($1 ne $email) {
3136 WARN("BAD_SIGN_OFF",
3137 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
3138 }
3139 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003140 }
3141
Joe Perchesa2fe16b2015-02-13 14:39:02 -08003142# Check email subject for common tools that don't need to be mentioned
3143 if ($in_header_lines &&
3144 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
3145 WARN("EMAIL_SUBJECT",
3146 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
3147 }
3148
Joe Perches44d303e2020-04-06 20:11:10 -07003149# Check for Gerrit Change-Ids not in any patch context
3150 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Aditya Srivastava7580c5b2020-12-15 20:44:47 -08003151 if (ERROR("GERRIT_CHANGE_ID",
3152 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
3153 $fix) {
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08003154 fix_delete_line($fixlinenr, $rawline);
3155 }
Christopher Covington7ebd05e2014-04-03 14:49:31 -07003156 }
3157
Joe Perches369c8dd2015-11-06 16:31:34 -08003158# Check if the commit log is in a possible stack dump
3159 if ($in_commit_log && !$commit_log_possible_stack_dump &&
3160 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3161 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3162 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07003163 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3164 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3165 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3166 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08003167 $commit_log_possible_stack_dump = 1;
3168 }
3169
Joe Perches2a076f42015-04-16 12:44:28 -07003170# Check for line lengths > 75 in commit log, warn once
3171 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08003172 length($line) > 75 &&
3173 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3174 # file delta changes
3175 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
3176 # filename then :
Aditya Srivastava27b379a2020-12-15 20:44:59 -08003177 $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
3178 # A Fixes: or Link: line or signature tag line
Joe Perches369c8dd2015-11-06 16:31:34 -08003179 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07003180 WARN("COMMIT_LOG_LONG_LINE",
3181 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
3182 $commit_log_long_line = 1;
3183 }
3184
Joe Perchesbf4daf12015-09-09 15:37:50 -07003185# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08003186 if ($in_commit_log && $commit_log_possible_stack_dump &&
3187 $line =~ /^\s*$/) {
3188 $commit_log_possible_stack_dump = 0;
3189 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07003190
Dwaipayan Ray084a6172020-12-15 20:45:18 -08003191# Check for lines starting with a #
3192 if ($in_commit_log && $line =~ /^#/) {
3193 if (WARN("COMMIT_COMMENT_SYMBOL",
3194 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3195 $fix) {
3196 $fixed[$fixlinenr] =~ s/^/ /;
3197 }
3198 }
3199
Joe Perches0d7835f2015-02-13 14:38:35 -08003200# Check for git id commit length and improperly formed commit descriptions
Joe Perches4ce9f972021-09-07 19:59:57 -07003201# A correctly formed commit description is:
3202# commit <SHA-1 hash length 12+ chars> ("Complete commit subject")
3203# with the commit subject '("' prefix and '")' suffix
3204# This is a fairly compilicated block as it tests for what appears to be
3205# bare SHA-1 hash with minimum length of 5. It also avoids several types of
3206# possible SHA-1 matches.
3207# A commit match can span multiple lines so this block attempts to find a
3208# complete typical commit on a maximum of 3 lines
3209 if ($perl_version_ok &&
3210 $in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07003211 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07003212 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perches4ce9f972021-09-07 19:59:57 -07003213 (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
3214 ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) ||
Joe Perchesaab38f52016-08-02 14:04:36 -07003215 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08003216 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3217 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07003218 my $init_char = "c";
3219 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08003220 my $short = 1;
3221 my $long = 0;
3222 my $case = 1;
3223 my $space = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08003224 my $id = '0123456789ab';
3225 my $orig_desc = "commit description";
3226 my $description = "";
Joe Perches4ce9f972021-09-07 19:59:57 -07003227 my $herectx = $herecurr;
3228 my $has_parens = 0;
3229 my $has_quotes = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08003230
Joe Perches4ce9f972021-09-07 19:59:57 -07003231 my $input = $line;
3232 if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) {
3233 for (my $n = 0; $n < 2; $n++) {
3234 if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) {
3235 $orig_desc = $1;
3236 $has_parens = 1;
3237 # Always strip leading/trailing parens then double quotes if existing
3238 $orig_desc = substr($orig_desc, 1, -1);
3239 if ($orig_desc =~ /^".*"$/) {
3240 $orig_desc = substr($orig_desc, 1, -1);
3241 $has_quotes = 1;
3242 }
3243 last;
3244 }
3245 last if ($#lines < $linenr + $n);
3246 $input .= " " . trim($rawlines[$linenr + $n]);
3247 $herectx .= "$rawlines[$linenr + $n]\n";
3248 }
3249 $herectx = $herecurr if (!$has_parens);
Joe Perchesfe043ea2015-09-09 15:37:25 -07003250 }
3251
Joe Perches4ce9f972021-09-07 19:59:57 -07003252 if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3253 $init_char = $1;
3254 $orig_commit = lc($2);
3255 $short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i);
3256 $long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i);
3257 $space = 0 if ($input =~ /\bcommit [0-9a-f]/i);
3258 $case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
3259 } elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) {
3260 $orig_commit = lc($1);
Joe Perches0d7835f2015-02-13 14:38:35 -08003261 }
3262
3263 ($id, $description) = git_commit_info($orig_commit,
3264 $id, $orig_desc);
3265
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07003266 if (defined($id) &&
Joe Perches4ce9f972021-09-07 19:59:57 -07003267 ($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) &&
3268 $last_git_commit_id_linenr != $linenr - 1) {
Joe Perches0d7835f2015-02-13 14:38:35 -08003269 ERROR("GIT_COMMIT_ID",
Joe Perches4ce9f972021-09-07 19:59:57 -07003270 "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 -08003271 }
Joe Perches4ce9f972021-09-07 19:59:57 -07003272 #don't report the next line if this line ends in commit and the sha1 hash is the next line
3273 $last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i);
Joe Perchesd311cd42014-08-06 16:10:57 -07003274 }
3275
Joe Perches13f19372014-08-06 16:10:59 -07003276# Check for added, moved or deleted files
3277 if (!$reported_maintainer_file && !$in_commit_log &&
3278 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
3279 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
3280 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
3281 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08003282 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07003283 $reported_maintainer_file = 1;
3284 WARN("FILE_PATH_CHANGES",
3285 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
3286 }
3287
Rob Herringe400edb2019-09-12 15:18:20 +01003288# Check for adding new DT bindings not in schema format
3289 if (!$in_commit_log &&
3290 ($line =~ /^new file mode\s*\d+\s*$/) &&
3291 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3292 WARN("DT_SCHEMA_BINDING_PATCH",
Mauro Carvalho Chehab56ddc4c2021-04-01 14:17:49 +02003293 "DT bindings should be in DT schema format. See: Documentation/devicetree/bindings/writing-schema.rst\n");
Rob Herringe400edb2019-09-12 15:18:20 +01003294 }
3295
Andy Whitcroft00df3442007-06-08 13:47:06 -07003296# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08003297 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003298 ERROR("CORRUPTED_PATCH",
3299 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003300 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003301 }
3302
3303# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3304 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003305 $rawline !~ m/^$UTF8*$/) {
3306 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3307
3308 my $blank = copy_spacing($rawline);
3309 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3310 my $hereptr = "$hereline$ptr\n";
3311
Joe Perches34d99212011-07-25 17:13:26 -07003312 CHK("INVALID_UTF8",
3313 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003314 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003315
Joe Perches15662b32011-10-31 17:13:12 -07003316# Check if it's the start of a commit log
3317# (not a header line and we haven't seen the patch filename)
3318 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07003319 !($rawline =~ /^\s+(?:\S|$)/ ||
3320 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07003321 $in_header_lines = 0;
3322 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07003323 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07003324 }
3325
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003326# Check if there is UTF-8 in a commit log when a mail header has explicitly
3327# declined it, i.e defined some charset where it is missing.
3328 if ($in_header_lines &&
3329 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3330 $1 !~ /utf-8/i) {
3331 $non_utf8_charset = 1;
3332 }
3333
3334 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07003335 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003336 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07003337 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3338 }
3339
Joe Perchesd6430f72016-12-12 16:46:28 -08003340# Check for absolute kernel paths in commit message
3341 if ($tree && $in_commit_log) {
3342 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3343 my $file = $1;
3344
3345 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3346 check_absolute_file($1, $herecurr)) {
3347 #
3348 } else {
3349 check_absolute_file($file, $herecurr);
3350 }
3351 }
3352 }
3353
Kees Cook66b47b42014-10-13 15:51:57 -07003354# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07003355 if (defined($misspellings) &&
3356 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003357 while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07003358 my $typo = $1;
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003359 my $blank = copy_spacing($rawline);
3360 my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
3361 my $hereptr = "$hereline$ptr\n";
Kees Cook66b47b42014-10-13 15:51:57 -07003362 my $typo_fix = $spelling_fix{lc($typo)};
3363 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3364 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07003365 my $msg_level = \&WARN;
3366 $msg_level = \&CHK if ($file);
3367 if (&{$msg_level}("TYPO_SPELLING",
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003368 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07003369 $fix) {
3370 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3371 }
3372 }
3373 }
3374
Matteo Crocea8dd86b2019-09-25 16:46:38 -07003375# check for invalid commit id
3376 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3377 my $id;
3378 my $description;
3379 ($id, $description) = git_commit_info($2, undef, undef);
3380 if (!defined($id)) {
3381 WARN("UNKNOWN_COMMIT_ID",
3382 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3383 }
3384 }
3385
Joe Perches310cd062020-10-15 20:11:52 -07003386# check for repeated words separated by a single space
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003387# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3388 if (($rawline =~ /^\+/ || $in_commit_log) &&
3389 $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003390 pos($rawline) = 1 if (!$in_commit_log);
Joe Perches310cd062020-10-15 20:11:52 -07003391 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3392
3393 my $first = $1;
3394 my $second = $2;
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003395 my $start_pos = $-[1];
3396 my $end_pos = $+[2];
Joe Perches310cd062020-10-15 20:11:52 -07003397 if ($first =~ /(?:struct|union|enum)/) {
3398 pos($rawline) += length($first) + length($second) + 1;
3399 next;
3400 }
3401
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003402 next if (lc($first) ne lc($second));
Joe Perches310cd062020-10-15 20:11:52 -07003403 next if ($first eq 'long');
3404
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003405 # check for character before and after the word matches
3406 my $start_char = '';
3407 my $end_char = '';
3408 $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
3409 $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3410
3411 next if ($start_char =~ /^\S$/);
3412 next if (index(" \t.,;?!", $end_char) == -1);
3413
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08003414 # avoid repeating hex occurrences like 'ff ff fe 09 ...'
3415 if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3416 next if (!exists($allow_repeated_words{lc($first)}));
3417 }
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003418
Joe Perches310cd062020-10-15 20:11:52 -07003419 if (WARN("REPEATED_WORD",
3420 "Possible repeated word: '$first'\n" . $herecurr) &&
3421 $fix) {
3422 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3423 }
3424 }
3425
3426 # if it's a repeated word on consecutive lines in a comment block
3427 if ($prevline =~ /$;+\s*$/ &&
3428 $prevrawline =~ /($word_pattern)\s*$/) {
3429 my $last_word = $1;
3430 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3431 if (WARN("REPEATED_WORD",
3432 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3433 $fix) {
3434 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3435 }
3436 }
3437 }
3438 }
3439
Andy Whitcroft306708542008-10-15 22:02:28 -07003440# ignore non-hunk lines and lines being removed
3441 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003442
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003443#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003444 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003445 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07003446 if (ERROR("DOS_LINE_ENDINGS",
3447 "DOS line endings\n" . $herevet) &&
3448 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003449 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003450 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003451 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3452 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003453 if (ERROR("TRAILING_WHITESPACE",
3454 "trailing whitespace\n" . $herevet) &&
3455 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003456 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003457 }
3458
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003459 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003460 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07003461
Josh Triplett4783f892013-11-12 15:10:12 -08003462# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003463 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003464 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003465 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3466 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003467 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003468 my $msg_level = \&ERROR;
3469 $msg_level = \&CHK if ($file);
3470 &{$msg_level}("FSF_MAILING_ADDRESS",
3471 "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 -08003472 }
3473
Andi Kleen33549572010-05-24 14:33:29 -07003474# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003475# Only applies when adding the entry originally, after that we do not have
3476# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003477 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003478 # 'choice' is usually the last thing on the line (though
3479 # Kconfig supports named choices), so use a word boundary
3480 # (\b) rather than a whitespace character (\s)
3481 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003482 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003483 my $cnt = $realcnt;
3484 my $ln = $linenr + 1;
3485 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003486 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003487 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003488 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003489 $f = $lines[$ln - 1];
3490 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3491 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003492
3493 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003494 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003495
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003496 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003497 $is_start = 1;
Masahiro Yamada22a4ac02020-06-17 12:02:20 +09003498 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003499 $length = -1;
3500 }
3501
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003502 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003503 $f =~ s/#.*//;
3504 $f =~ s/^\s+//;
3505 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003506
3507 # This only checks context lines in the patch
3508 # and so hopefully shouldn't trigger false
3509 # positives, even though some of these are
3510 # common words in help texts
3511 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3512 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003513 $is_end = 1;
3514 last;
3515 }
Andi Kleen33549572010-05-24 14:33:29 -07003516 $length++;
3517 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003518 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3519 WARN("CONFIG_DESCRIPTION",
3520 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3521 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003522 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003523 }
3524
Joe Perches7ccf41a2020-06-04 16:50:33 -07003525# check MAINTAINERS entries
3526 if ($realfile =~ /^MAINTAINERS$/) {
3527# check MAINTAINERS entries for the right form
3528 if ($rawline =~ /^\+[A-Z]:/ &&
3529 $rawline !~ /^\+[A-Z]:\t\S/) {
3530 if (WARN("MAINTAINERS_STYLE",
3531 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3532 $fix) {
3533 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3534 }
3535 }
3536# check MAINTAINERS entries for the right ordering too
3537 my $preferred_order = 'MRLSWQBCPTFXNK';
3538 if ($rawline =~ /^\+[A-Z]:/ &&
3539 $prevrawline =~ /^[\+ ][A-Z]:/) {
3540 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3541 my $cur = $1;
3542 my $curval = $2;
3543 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3544 my $prev = $1;
3545 my $prevval = $2;
3546 my $curindex = index($preferred_order, $cur);
3547 my $previndex = index($preferred_order, $prev);
3548 if ($curindex < 0) {
3549 WARN("MAINTAINERS_STYLE",
3550 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3551 } else {
3552 if ($previndex >= 0 && $curindex < $previndex) {
3553 WARN("MAINTAINERS_STYLE",
3554 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3555 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3556 ($prev eq 'X' && $cur eq 'X')) &&
3557 ($prevval cmp $curval) > 0) {
3558 WARN("MAINTAINERS_STYLE",
3559 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3560 }
3561 }
Joe Perches628f91a2017-07-10 15:52:07 -07003562 }
3563 }
3564
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003565 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3566 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3567 my $flag = $1;
3568 my $replacement = {
3569 'EXTRA_AFLAGS' => 'asflags-y',
3570 'EXTRA_CFLAGS' => 'ccflags-y',
3571 'EXTRA_CPPFLAGS' => 'cppflags-y',
3572 'EXTRA_LDFLAGS' => 'ldflags-y',
3573 };
3574
3575 WARN("DEPRECATED_VARIABLE",
3576 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3577 }
3578
Rob Herringbff5da42014-01-23 15:54:51 -08003579# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003580 if (defined $root &&
3581 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3582 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3583
Rob Herringbff5da42014-01-23 15:54:51 -08003584 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3585
Florian Vaussardcc933192014-04-03 14:49:27 -07003586 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003587 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003588
Rob Herringbff5da42014-01-23 15:54:51 -08003589 foreach my $compat (@compats) {
3590 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003591 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3592 my $compat3 = $compat;
3593 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3594 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003595 if ( $? >> 8 ) {
3596 WARN("UNDOCUMENTED_DT_STRING",
3597 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3598 }
3599
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003600 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3601 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003602 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003603 if ( $? >> 8 ) {
3604 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003605 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003606 }
3607 }
3608 }
3609
Rob Herring9f3a8992018-04-10 16:33:13 -07003610# check for using SPDX license tag at beginning of files
3611 if ($realline == $checklicenseline) {
3612 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3613 $checklicenseline = 2;
3614 } elsif ($rawline =~ /^\+/) {
3615 my $comment = "";
3616 if ($realfile =~ /\.(h|s|S)$/) {
3617 $comment = '/*';
3618 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3619 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003620 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003621 $comment = '#';
3622 } elsif ($realfile =~ /\.rst$/) {
3623 $comment = '..';
3624 }
3625
Joe Perchesfdf13692019-03-07 16:28:32 -08003626# check SPDX comment style for .[chsS] files
3627 if ($realfile =~ /\.[chsS]$/ &&
3628 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003629 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003630 WARN("SPDX_LICENSE_TAG",
3631 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3632 }
3633
Rob Herring9f3a8992018-04-10 16:33:13 -07003634 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003635 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3636 WARN("SPDX_LICENSE_TAG",
3637 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003638 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003639 my $spdx_license = $1;
3640 if (!is_SPDX_License_valid($spdx_license)) {
3641 WARN("SPDX_LICENSE_TAG",
3642 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3643 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003644 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3645 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3646 my $msg_level = \&WARN;
3647 $msg_level = \&CHK if ($file);
3648 if (&{$msg_level}("SPDX_LICENSE_TAG",
3649
3650 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3651 $fix) {
3652 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3653 }
3654 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003655 }
3656 }
3657 }
3658
Joe Perchesa0154cd2020-10-15 20:12:19 -07003659# check for embedded filenames
3660 if ($rawline =~ /^\+.*\Q$realfile\E/) {
3661 WARN("EMBEDDED_FILENAME",
3662 "It's generally not useful to have the filename in the file\n" . $herecurr);
3663 }
3664
Andy Whitcroft5368df202008-10-15 22:02:27 -07003665# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003666 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003667
Joe Perchesa8da38a2019-03-07 16:28:42 -08003668# check for using SPDX-License-Identifier on the wrong line number
3669 if ($realline != $checklicenseline &&
3670 $rawline =~ /\bSPDX-License-Identifier:/ &&
3671 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3672 WARN("SPDX_LICENSE_TAG",
3673 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3674 }
3675
Joe Perches47e0c882015-06-25 15:02:57 -07003676# line length limit (with some exclusions)
3677#
3678# There are a few types of lines that may extend beyond $max_line_length:
3679# logging functions like pr_info that end in a string
3680# lines with a single string
3681# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003682# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003683#
3684# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003685# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003686# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3687# LONG_LINE all other lines longer than $max_line_length
3688#
3689# if LONG_LINE is ignored, the other 2 types are also ignored
3690#
3691
Joe Perchesb4749e92015-07-17 16:24:01 -07003692 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003693 my $msg_type = "LONG_LINE";
3694
3695 # Check the allowed long line types first
3696
3697 # logging functions that end in a string that starts
3698 # before $max_line_length
3699 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3700 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3701 $msg_type = "";
3702
3703 # lines with only strings (w/ possible termination)
3704 # #defines with only strings
3705 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3706 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3707 $msg_type = "";
3708
Joe Perchescc147502017-11-17 15:28:44 -08003709 # More special cases
3710 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3711 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003712 $msg_type = "";
3713
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003714 # URL ($rawline is used in case the URL is in a comment)
3715 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3716 $msg_type = "";
3717
Joe Perches47e0c882015-06-25 15:02:57 -07003718 # Otherwise set the alternate message types
3719
3720 # a comment starts before $max_line_length
3721 } elsif ($line =~ /($;[\s$;]*)$/ &&
3722 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3723 $msg_type = "LONG_LINE_COMMENT"
3724
3725 # a quoted string starts before $max_line_length
3726 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3727 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3728 $msg_type = "LONG_LINE_STRING"
3729 }
3730
3731 if ($msg_type ne "" &&
3732 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003733 my $msg_level = \&WARN;
3734 $msg_level = \&CHK if ($file);
3735 &{$msg_level}($msg_type,
3736 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003737 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003738 }
3739
Andy Whitcroft8905a672007-11-28 16:21:06 -08003740# check for adding lines without a newline.
3741 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Tom Rix47ca69b2020-12-15 20:44:40 -08003742 if (WARN("MISSING_EOF_NEWLINE",
3743 "adding a line without newline at end of file\n" . $herecurr) &&
3744 $fix) {
3745 fix_delete_line($fixlinenr+1, "No newline at end of file");
3746 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003747 }
3748
Aditya Srivastavade932452021-02-25 17:21:57 -08003749# check for .L prefix local symbols in .S files
3750 if ($realfile =~ /\.S$/ &&
3751 $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) {
3752 WARN("AVOID_L_PREFIX",
3753 "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/asm-annotations.rst\n" . $herecurr);
3754 }
3755
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003756# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003757 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003758
3759# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003760# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003761 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3762 $rawline =~ /^\+\s* \s*/) {
3763 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003764 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003765 if (ERROR("CODE_INDENT",
3766 "code indent should use tabs where possible\n" . $herevet) &&
3767 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003768 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003769 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003770 }
3771
Alberto Panizzo08e44362010-03-05 13:43:54 -08003772# check for space before tabs.
3773 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3774 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003775 if (WARN("SPACE_BEFORE_TAB",
3776 "please, no space before tabs\n" . $herevet) &&
3777 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003778 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003779 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003780 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003781 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003782 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003783 }
3784
Joe Perches6a487212018-04-10 16:34:04 -07003785# check for assignments on the start of a line
3786 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
Aditya Srivastavada7355a2020-12-15 20:45:06 -08003787 my $operator = $1;
3788 if (CHK("ASSIGNMENT_CONTINUATIONS",
3789 "Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3790 $fix && $prevrawline =~ /^\+/) {
3791 # add assignment operator to the previous line, remove from current line
3792 $fixed[$fixlinenr - 1] .= " $operator";
3793 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3794 }
Joe Perches6a487212018-04-10 16:34:04 -07003795 }
3796
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003797# check for && or || at the start of a line
3798 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
Aditya Srivastava8e08f072020-12-15 20:45:09 -08003799 my $operator = $1;
3800 if (CHK("LOGICAL_CONTINUATIONS",
3801 "Logical continuations should be on the previous line\n" . $hereprev) &&
3802 $fix && $prevrawline =~ /^\+/) {
3803 # insert logical operator at last non-comment, non-whitepsace char on previous line
3804 $prevline =~ /[\s$;]*$/;
3805 my $line_end = substr($prevrawline, $-[0]);
3806 $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
3807 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3808 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003809 }
3810
Joe Perchesa91e8992016-05-20 17:04:05 -07003811# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003812 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003813 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003814 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003815 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003816 if (WARN("TABSTOP",
3817 "Statements should start on a tabstop\n" . $herecurr) &&
3818 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003819 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003820 }
3821 }
3822 }
3823
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003824# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003825 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003826 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003827 $prevline =~ /^\+(\t*)(.*)$/;
3828 my $oldindent = $1;
3829 my $rest = $2;
3830
3831 my $pos = pos_last_openparen($rest);
3832 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003833 $line =~ /^(\+| )([ \t]*)/;
3834 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003835
3836 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003837 "\t" x ($pos / $tabsize) .
3838 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003839 my $goodspaceindent = $oldindent . " " x $pos;
3840
3841 if ($newindent ne $goodtabindent &&
3842 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003843
3844 if (CHK("PARENTHESIS_ALIGNMENT",
3845 "Alignment should match open parenthesis\n" . $hereprev) &&
3846 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003847 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003848 s/^\+[ \t]*/\+$goodtabindent/;
3849 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003850 }
3851 }
3852 }
3853
Joe Perches6ab3a972015-04-16 12:44:05 -07003854# check for space after cast like "(int) foo" or "(struct foo) bar"
3855# avoid checking a few false positives:
3856# "sizeof(<type>)" or "__alignof__(<type>)"
3857# function pointer declarations like "(*foo)(int) = bar;"
3858# structure definitions like "(struct foo) { 0 };"
3859# multiline macros that define functions
3860# known attributes or the __attribute__ keyword
3861 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3862 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003863 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003864 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003865 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003866 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003867 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003868 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003869 }
3870
Joe Perches86406b12015-09-09 15:37:41 -07003871# Block comment styles
3872# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003873 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003874 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003875 $rawline =~ /^\+[ \t]*\*/ &&
Łukasz Stelmachc70735c2020-10-15 20:12:25 -07003876 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
Joe Perches05880602012-10-04 17:13:35 -07003877 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3878 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3879 }
3880
Joe Perches86406b12015-09-09 15:37:41 -07003881# Block comments use * on subsequent lines
3882 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3883 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003884 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003885 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003886 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003887 WARN("BLOCK_COMMENT_STYLE",
3888 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003889 }
3890
Joe Perches86406b12015-09-09 15:37:41 -07003891# Block comments use */ on trailing lines
3892 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003893 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3894 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3895 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003896 WARN("BLOCK_COMMENT_STYLE",
3897 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003898 }
3899
Joe Perches08eb9b82016-10-11 13:51:50 -07003900# Block comment * alignment
3901 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003902 $line =~ /^\+[ \t]*$;/ && #leading comment
3903 $rawline =~ /^\+[ \t]*\*/ && #leading *
3904 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003905 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003906 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3907 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003908 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003909 if (defined($1)) {
3910 $oldindent = expand_tabs($1);
3911 } else {
3912 $prevrawline =~ m@^\+(.*/?)\*@;
3913 $oldindent = expand_tabs($1);
3914 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003915 $rawline =~ m@^\+([ \t]*)\*@;
3916 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003917 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003918 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003919 WARN("BLOCK_COMMENT_STYLE",
3920 "Block comments should align the * on each line\n" . $hereprev);
3921 }
3922 }
3923
Joe Perches7f619192014-08-06 16:10:39 -07003924# check for missing blank lines after struct/union declarations
3925# with exceptions for various attributes and macros
3926 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3927 $line =~ /^\+/ &&
3928 !($line =~ /^\+\s*$/ ||
3929 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3930 $line =~ /^\+\s*MODULE_/i ||
3931 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3932 $line =~ /^\+[a-z_]*init/ ||
3933 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3934 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003935 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003936 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003937 if (CHK("LINE_SPACING",
3938 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3939 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003940 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003941 }
Joe Perches7f619192014-08-06 16:10:39 -07003942 }
3943
Joe Perches365dd4e2014-08-06 16:10:42 -07003944# check for multiple consecutive blank lines
3945 if ($prevline =~ /^[\+ ]\s*$/ &&
3946 $line =~ /^\+\s*$/ &&
3947 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003948 if (CHK("LINE_SPACING",
3949 "Please don't use multiple blank lines\n" . $hereprev) &&
3950 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003951 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003952 }
3953
Joe Perches365dd4e2014-08-06 16:10:42 -07003954 $last_blank_line = $linenr;
3955 }
3956
Joe Perches3b617e32014-04-03 14:49:28 -07003957# check for missing blank lines after declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08003958# (declarations must have the same indentation and not be at the start of line)
3959 if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
3960 # use temporaries
3961 my $sl = $sline;
3962 my $pl = $prevline;
3963 # remove $Attribute/$Sparse uses to simplify comparisons
3964 $sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3965 $pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3966 if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003967 # function pointer declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08003968 $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003969 # foo bar; where foo is some local typedef or #define
Joe Perchesb5e87362021-02-25 17:21:40 -08003970 $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003971 # known declaration macros
Joe Perchesb5e87362021-02-25 17:21:40 -08003972 $pl =~ /^\+\s+$declaration_macros/) &&
Joe Perches3f7bac02014-06-04 16:12:04 -07003973 # for "else if" which can look like "$Ident $Ident"
Joe Perchesb5e87362021-02-25 17:21:40 -08003974 !($pl =~ /^\+\s+$c90_Keywords\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003975 # other possible extensions of declaration lines
Joe Perchesb5e87362021-02-25 17:21:40 -08003976 $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003977 # not starting a section or a macro "\" extended line
Joe Perchesb5e87362021-02-25 17:21:40 -08003978 $pl =~ /(?:\{\s*|\\)$/) &&
Joe Perches3f7bac02014-06-04 16:12:04 -07003979 # looks like a declaration
Joe Perchesb5e87362021-02-25 17:21:40 -08003980 !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003981 # function pointer declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08003982 $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003983 # foo bar; where foo is some local typedef or #define
Joe Perchesb5e87362021-02-25 17:21:40 -08003984 $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003985 # known declaration macros
Joe Perchesb5e87362021-02-25 17:21:40 -08003986 $sl =~ /^\+\s+$declaration_macros/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003987 # start of struct or union or enum
Joe Perchesb5e87362021-02-25 17:21:40 -08003988 $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003989 # start or end of block or continuation of declaration
Joe Perchesb5e87362021-02-25 17:21:40 -08003990 $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003991 # bitfield continuation
Joe Perchesb5e87362021-02-25 17:21:40 -08003992 $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003993 # other possible extensions of declaration lines
Joe Perchesb5e87362021-02-25 17:21:40 -08003994 $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
3995 if (WARN("LINE_SPACING",
3996 "Missing a blank line after declarations\n" . $hereprev) &&
3997 $fix) {
3998 fix_insert_line($fixlinenr, "\+");
3999 }
Joe Perchesd752fcc2014-08-06 16:11:05 -07004000 }
Joe Perches3b617e32014-04-03 14:49:28 -07004001 }
4002
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07004003# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07004004# Exceptions:
4005# 1) within comments
4006# 2) indented preprocessor commands
4007# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07004008 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07004009 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004010 if (WARN("LEADING_SPACE",
4011 "please, no spaces at the start of a line\n" . $herevet) &&
4012 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004013 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07004014 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07004015 }
4016
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07004017# check we are in a valid C source file if not then ignore this hunk
4018 next if ($realfile !~ /\.(h|c)$/);
4019
Joe Perches5751a242017-11-17 15:28:52 -08004020# check for unusual line ending [ or (
4021 if ($line =~ /^\+.*([\[\(])\s*$/) {
4022 CHK("OPEN_ENDED_LINE",
4023 "Lines should not end with a '$1'\n" . $herecurr);
4024 }
4025
Joe Perches4dbed762017-05-08 15:55:39 -07004026# check if this appears to be the start function declaration, save the name
4027 if ($sline =~ /^\+\{\s*$/ &&
4028 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
4029 $context_function = $1;
4030 }
4031
4032# check if this appears to be the end of function declaration
4033 if ($sline =~ /^\+\}\s*$/) {
4034 undef $context_function;
4035 }
4036
Joe Perches032a4c02014-08-06 16:10:29 -07004037# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07004038# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07004039# if the previous line is a break or return and is indented 1 tab more...
4040 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
4041 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07004042 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
4043 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
4044 defined $lines[$linenr] &&
4045 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07004046 WARN("UNNECESSARY_ELSE",
4047 "else is not generally useful after a break or return\n" . $hereprev);
4048 }
4049 }
4050
Joe Perchesc00df192014-08-06 16:11:01 -07004051# check indentation of a line with a break;
Joe Perchesdc58bc52020-12-15 20:44:33 -08004052# if the previous line is a goto, return or break
4053# and is indented the same # of tabs
Joe Perchesc00df192014-08-06 16:11:01 -07004054 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
4055 my $tabs = $1;
Joe Perchesdc58bc52020-12-15 20:44:33 -08004056 if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
4057 if (WARN("UNNECESSARY_BREAK",
4058 "break is not useful after a $1\n" . $hereprev) &&
4059 $fix) {
4060 fix_delete_line($fixlinenr, $rawline);
4061 }
Joe Perchesc00df192014-08-06 16:11:01 -07004062 }
4063 }
4064
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004065# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08004066 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004067 WARN("CVS_KEYWORD",
4068 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004069 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004070
Joe Perches56e77d72013-02-21 16:44:14 -08004071# check for old HOTPLUG __dev<foo> section markings
4072 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
4073 WARN("HOTPLUG_SECTION",
4074 "Using $1 is unnecessary\n" . $herecurr);
4075 }
4076
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004077# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004078 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
4079 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004080#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07004081 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07004082 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004083 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004084 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004085 $stat =~ s/\n./\n /g;
4086 $cond =~ s/\n./\n /g;
4087
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004088#print "linenr<$linenr> <$stat>\n";
4089 # If this statement has no statement boundaries within
4090 # it there is no point in retrying a statement scan
4091 # until we hit end of it.
4092 my $frag = $stat; $frag =~ s/;+\s*$//;
4093 if ($frag !~ /(?:{|;)/) {
4094#print "skip<$line_nr_next>\n";
4095 $suppress_statement = $line_nr_next;
4096 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004097
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004098 # Find the real next line.
4099 $realline_next = $line_nr_next;
4100 if (defined $realline_next &&
4101 (!defined $lines[$realline_next - 1] ||
4102 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
4103 $realline_next++;
4104 }
4105
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004106 my $s = $stat;
4107 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004108
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004109 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004110 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004111
4112 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004113 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004114
Andy Whitcroft463f2862009-09-21 17:04:34 -07004115 } elsif ($s =~ /^.\s*else\b/s) {
4116
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004117 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07004118 } 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 -07004119 my $type = $1;
4120 $type =~ s/\s+/ /g;
4121 possible($type, "A:" . $s);
4122
Andy Whitcroft8905a672007-11-28 16:21:06 -08004123 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07004124 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004125 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004126 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004127
4128 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08004129 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004130 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004131 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004132
4133 # Check for any sort of function declaration.
4134 # int foo(something bar, other baz);
4135 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004136 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 -08004137 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004138
Andy Whitcroftcf655042008-03-04 14:28:20 -08004139 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004140 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004141 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004142
Andy Whitcroft8905a672007-11-28 16:21:06 -08004143 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004144 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08004145
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004146 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004147 }
4148 }
4149 }
4150
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004151 }
4152
Andy Whitcroft653d4872007-06-23 17:16:34 -07004153#
4154# Checks which may be anchored in the context.
4155#
4156
4157# Check for switch () and associated case and default
4158# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07004159 if ($line=~/\bswitch\s*\(.*\)/) {
4160 my $err = '';
4161 my $sep = '';
4162 my @ctx = ctx_block_outer($linenr, $realcnt);
4163 shift(@ctx);
4164 for my $ctx (@ctx) {
4165 my ($clen, $cindent) = line_stats($ctx);
4166 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
4167 $indent != $cindent) {
4168 $err .= "$sep$ctx\n";
4169 $sep = '';
4170 } else {
4171 $sep = "[...]\n";
4172 }
4173 }
4174 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004175 ERROR("SWITCH_CASE_INDENT_LEVEL",
4176 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004177 }
4178 }
4179
4180# if/while/etc brace do not go on next line, unless defining a do while loop,
4181# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07004182 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 -07004183 my $pre_ctx = "$1$2";
4184
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004185 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08004186
4187 if ($line =~ /^\+\t{6,}/) {
4188 WARN("DEEP_INDENTATION",
4189 "Too many leading tabs - consider code refactoring\n" . $herecurr);
4190 }
4191
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004192 my $ctx_cnt = $realcnt - $#ctx - 1;
4193 my $ctx = join("\n", @ctx);
4194
Andy Whitcroft548596d2008-07-23 21:29:01 -07004195 my $ctx_ln = $linenr;
4196 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004197
Andy Whitcroft548596d2008-07-23 21:29:01 -07004198 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4199 defined $lines[$ctx_ln - 1] &&
4200 $lines[$ctx_ln - 1] =~ /^-/)) {
4201 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4202 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004203 $ctx_ln++;
4204 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07004205
Andy Whitcroft53210162008-07-23 21:29:03 -07004206 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
4207 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07004208
Joe Perchesd752fcc2014-08-06 16:11:05 -07004209 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004210 ERROR("OPEN_BRACE",
4211 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07004212 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07004213 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004214 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4215 $ctx =~ /\)\s*\;\s*$/ &&
4216 defined $lines[$ctx_ln - 1])
4217 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004218 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
4219 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004220 WARN("TRAILING_SEMICOLON",
4221 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07004222 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004223 }
4224 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004225 }
4226
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004227# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07004228 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 -08004229 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4230 ctx_statement_block($linenr, $realcnt, 0)
4231 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004232 my ($s, $c) = ($stat, $cond);
4233
4234 substr($s, 0, length($c), '');
4235
Joe Perches9f5af482015-09-09 15:37:30 -07004236 # remove inline comments
4237 $s =~ s/$;/ /g;
4238 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004239
4240 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07004241 my @newlines = ($c =~ /\n/gs);
4242 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004243
Joe Perches9f5af482015-09-09 15:37:30 -07004244 # Make sure we remove the line prefixes as we have
4245 # none on the first line, and are going to readd them
4246 # where necessary.
4247 $s =~ s/\n./\n/gs;
4248 while ($s =~ /\n\s+\\\n/) {
4249 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
4250 }
4251
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004252 # We want to check the first line inside the block
4253 # starting at the end of the conditional, so remove:
4254 # 1) any blank line termination
4255 # 2) any opening brace { on end of the line
4256 # 3) any do (...) {
4257 my $continuation = 0;
4258 my $check = 0;
4259 $s =~ s/^.*\bdo\b//;
4260 $s =~ s/^\s*{//;
4261 if ($s =~ s/^\s*\\//) {
4262 $continuation = 1;
4263 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004264 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004265 $check = 1;
4266 $cond_lines++;
4267 }
4268
4269 # Also ignore a loop construct at the end of a
4270 # preprocessor statement.
4271 if (($prevline =~ /^.\s*#\s*define\s/ ||
4272 $prevline =~ /\\\s*$/) && $continuation == 0) {
4273 $check = 0;
4274 }
4275
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004276 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07004277 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004278 while ($cond_ptr != $cond_lines) {
4279 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004280
Andy Whitcroftf16fa282008-10-15 22:02:32 -07004281 # If we see an #else/#elif then the code
4282 # is not linear.
4283 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4284 $check = 0;
4285 }
4286
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004287 # Ignore:
4288 # 1) blank lines, they should be at 0,
4289 # 2) preprocessor lines, and
4290 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07004291 if ($continuation ||
4292 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004293 $s =~ /^\s*#\s*?/ ||
4294 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07004295 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07004296 if ($s =~ s/^.*?\n//) {
4297 $cond_lines++;
4298 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004299 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004300 }
4301
4302 my (undef, $sindent) = line_stats("+" . $s);
4303 my $stat_real = raw_line($linenr, $cond_lines);
4304
4305 # Check if either of these lines are modified, else
4306 # this is not this patch's fault.
4307 if (!defined($stat_real) ||
4308 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
4309 $check = 0;
4310 }
4311 if (defined($stat_real) && $cond_lines > 1) {
4312 $stat_real = "[...]\n$stat_real";
4313 }
4314
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004315 #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 -07004316
Joe Perches9f5af482015-09-09 15:37:30 -07004317 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07004318 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07004319 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07004320 ($sindent == $indent &&
4321 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07004322 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004323 WARN("SUSPECT_CODE_INDENT",
4324 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004325 }
4326 }
4327
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004328 # Track the 'values' across context and added lines.
4329 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004330 my ($curr_values, $curr_vars) =
4331 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004332 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004333 if ($dbg_values) {
4334 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004335 print "$linenr > .$outline\n";
4336 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004337 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004338 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004339 $prev_values = substr($curr_values, -1);
4340
Andy Whitcroft00df3442007-06-08 13:47:06 -07004341#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07004342 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07004343
Joe Perches99ca38c2020-10-15 20:12:09 -07004344# check for self assignments used to avoid compiler warnings
4345# e.g.: int foo = foo, *bar = NULL;
4346# struct foo bar = *(&(bar));
4347 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
4348 my $var = $1;
4349 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
4350 WARN("SELF_ASSIGNMENT",
4351 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
4352 }
4353 }
4354
Joe Perches11ca40a2016-12-12 16:46:31 -08004355# check for dereferences that span multiple lines
4356 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
4357 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
4358 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
4359 my $ref = $1;
4360 $line =~ /^.\s*($Lval)/;
4361 $ref .= $1;
4362 $ref =~ s/\s//g;
4363 WARN("MULTILINE_DEREFERENCE",
4364 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
4365 }
4366
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004367# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07004368 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 -07004369 my $type = $1;
4370 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07004371 $var = "" if (!defined $var);
4372 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004373 my $sign = $1;
4374 my $pointer = $2;
4375
4376 $pointer = "" if (!defined $pointer);
4377
4378 if (WARN("UNSPECIFIED_INT",
4379 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4380 $fix) {
4381 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07004382 my $comp_pointer = $pointer;
4383 $comp_pointer =~ s/\s//g;
4384 $decl .= $comp_pointer;
4385 $decl = rtrim($decl) if ($var eq "");
4386 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004387 }
4388 }
4389 }
4390
Andy Whitcroft653d4872007-06-23 17:16:34 -07004391# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07004392 if ($dbg_type) {
4393 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004394 ERROR("TEST_TYPE",
4395 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004396 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004397 ERROR("TEST_NOT_TYPE",
4398 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004399 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004400 next;
4401 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004402# TEST: allow direct testing of the attribute matcher.
4403 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004404 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004405 ERROR("TEST_ATTR",
4406 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004407 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004408 ERROR("TEST_NOT_ATTR",
4409 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004410 }
4411 next;
4412 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004413
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004414# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07004415 if ($line =~ /^.\s*{/ &&
4416 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07004417 if (ERROR("OPEN_BRACE",
4418 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004419 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4420 fix_delete_line($fixlinenr - 1, $prevrawline);
4421 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004422 my $fixedline = $prevrawline;
4423 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004424 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004425 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004426 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004427 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004428 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004429 }
4430
Andy Whitcroft653d4872007-06-23 17:16:34 -07004431#
4432# Checks which are anchored on the added line.
4433#
4434
4435# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004436 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07004437 my $path = $1;
4438 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004439 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08004440 "malformed #include filename\n" . $herecurr);
4441 }
4442 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4443 ERROR("UAPI_INCLUDE",
4444 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004445 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004446 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004447
4448# no C99 // comments
4449 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07004450 if (ERROR("C99_COMMENTS",
4451 "do not use C99 // comments\n" . $herecurr) &&
4452 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004453 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07004454 if ($line =~ /\/\/(.*)$/) {
4455 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07004456 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07004457 }
4458 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004459 }
4460 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004461 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004462 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004463
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004464# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4465# the whole statement.
4466#print "APW <$lines[$realline_next - 1]>\n";
4467 if (defined $realline_next &&
4468 exists $lines[$realline_next - 1] &&
4469 !defined $suppress_export{$realline_next} &&
Christoph Hellwig36794822021-02-02 13:13:34 +01004470 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004471 # Handle definitions which produce identifiers with
4472 # a prefix:
4473 # XXX(foo);
4474 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004475 my $name = $1;
Joe Perches70a11652021-11-08 18:33:31 -08004476 $name =~ s/^\s*($Ident).*/$1/;
Andy Whitcroft87a53872012-01-10 15:10:04 -08004477 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004478 $name =~ /^${Ident}_$2/) {
4479#print "FOO C name<$name>\n";
4480 $suppress_export{$realline_next} = 1;
4481
4482 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004483 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07004484 ^.DEFINE_$Ident\(\Q$name\E\)|
4485 ^.DECLARE_$Ident\(\Q$name\E\)|
4486 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004487 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4488 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07004489 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004490#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4491 $suppress_export{$realline_next} = 2;
4492 } else {
4493 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004494 }
4495 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004496 if (!defined $suppress_export{$linenr} &&
4497 $prevline =~ /^.\s*$/ &&
Christoph Hellwig36794822021-02-02 13:13:34 +01004498 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004499#print "FOO B <$lines[$linenr - 1]>\n";
4500 $suppress_export{$linenr} = 2;
4501 }
4502 if (defined $suppress_export{$linenr} &&
4503 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004504 WARN("EXPORT_SYMBOL",
4505 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004506 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004507
Joe Eloff5150bda2010-08-09 17:21:00 -07004508# check for global initialisers.
Song Liu5b8f82e2021-02-25 17:22:08 -08004509 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
4510 !exclude_global_initialisers($realfile)) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004511 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004512 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004513 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004514 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004515 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004516 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004517# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004518 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004519 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004520 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004521 $herecurr) &&
4522 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004523 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004524 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004525 }
4526
Joe Perches18130872014-08-06 16:11:22 -07004527# check for misordered declarations of char/short/int/long with signed/unsigned
4528 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4529 my $tmp = trim($1);
4530 WARN("MISORDERED_TYPE",
4531 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4532 }
4533
Joe Perches809e0822018-08-21 21:58:12 -07004534# check for unnecessary <signed> int declarations of short/long/long long
4535 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4536 my $type = trim($1);
4537 next if ($type !~ /\bint\b/);
4538 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4539 my $new_type = $type;
4540 $new_type =~ s/\b\s*int\s*\b/ /;
4541 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4542 $new_type =~ s/^const\s+//;
4543 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4544 $new_type = "const $new_type" if ($type =~ /^const\b/);
4545 $new_type =~ s/\s+/ /g;
4546 $new_type = trim($new_type);
4547 if (WARN("UNNECESSARY_INT",
4548 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4549 $fix) {
4550 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4551 }
4552 }
4553
Joe Perchescb710ec2010-10-26 14:23:20 -07004554# check for static const char * arrays.
4555 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004556 WARN("STATIC_CONST_CHAR_ARRAY",
4557 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004558 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004559 }
4560
4561# check for initialized const char arrays that should be static const
4562 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4563 if (WARN("STATIC_CONST_CHAR_ARRAY",
4564 "const array should probably be static const\n" . $herecurr) &&
4565 $fix) {
4566 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4567 }
4568 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004569
4570# check for static char foo[] = "bar" declarations.
4571 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004572 WARN("STATIC_CONST_CHAR_ARRAY",
4573 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004574 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004575 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004576
Joe Perchesab7e23f2015-04-16 12:44:22 -07004577# check for const <foo> const where <foo> is not a pointer or array type
4578 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4579 my $found = $1;
4580 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4581 WARN("CONST_CONST",
4582 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4583 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4584 WARN("CONST_CONST",
4585 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4586 }
4587 }
4588
Joe Perches73169762020-12-15 20:44:30 -08004589# check for const static or static <non ptr type> const declarations
4590# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
4591 if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
4592 $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
4593 if (WARN("STATIC_CONST",
4594 "Move const after static - use 'static const $1'\n" . $herecurr) &&
4595 $fix) {
4596 $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
4597 $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
4598 }
4599 }
4600
Joe Perches9b0fa602014-04-03 14:49:18 -07004601# check for non-global char *foo[] = {"bar", ...} declarations.
4602 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4603 WARN("STATIC_CONST_CHAR_ARRAY",
4604 "char * array declaration might be better as static const\n" .
4605 $herecurr);
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08004606 }
Joe Perches9b0fa602014-04-03 14:49:18 -07004607
Joe Perchesb598b672015-04-16 12:44:36 -07004608# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4609 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4610 my $array = $1;
4611 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4612 my $array_div = $1;
4613 if (WARN("ARRAY_SIZE",
4614 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4615 $fix) {
4616 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4617 }
4618 }
4619 }
4620
Joe Perchesb36190c2014-01-27 17:07:18 -08004621# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004622 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004623 if (ERROR("FUNCTION_WITHOUT_ARGS",
4624 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4625 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004626 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004627 }
4628 }
4629
Andy Whitcroft653d4872007-06-23 17:16:34 -07004630# check for new typedefs, only function parameters and sparse annotations
4631# make sense.
4632 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004633 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004634 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004635 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004636 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004637 WARN("NEW_TYPEDEFS",
4638 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004639 }
4640
4641# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004642 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004643 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4644 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004645 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004646
Andy Whitcroft65863862009-01-06 14:41:21 -08004647 # Should start with a space.
4648 $to =~ s/^(\S)/ $1/;
4649 # Should not end with a space.
4650 $to =~ s/\s+$//;
4651 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004652 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004653 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004654
Joe Perches3705ce52013-07-03 15:05:31 -07004655## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004656 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004657 if (ERROR("POINTER_LOCATION",
4658 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4659 $fix) {
4660 my $sub_from = $ident;
4661 my $sub_to = $ident;
4662 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004663 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004664 s@\Q$sub_from\E@$sub_to@;
4665 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004666 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004667 }
4668 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4669 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004670 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004671
Andy Whitcroft65863862009-01-06 14:41:21 -08004672 # Should start with a space.
4673 $to =~ s/^(\S)/ $1/;
4674 # Should not end with a space.
4675 $to =~ s/\s+$//;
4676 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004677 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004678 }
4679 # Modifiers should have spaces.
4680 $to =~ s/(\b$Modifier$)/$1 /;
4681
Joe Perches3705ce52013-07-03 15:05:31 -07004682## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004683 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004684 if (ERROR("POINTER_LOCATION",
4685 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4686 $fix) {
4687
4688 my $sub_from = $match;
4689 my $sub_to = $match;
4690 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004691 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004692 s@\Q$sub_from\E@$sub_to@;
4693 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004694 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004695 }
4696
Joe Perches9d3e3c72015-09-09 15:37:27 -07004697# avoid BUG() or BUG_ON()
4698 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004699 my $msg_level = \&WARN;
4700 $msg_level = \&CHK if ($file);
4701 &{$msg_level}("AVOID_BUG",
4702 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
Joe Perches9d3e3c72015-09-09 15:37:27 -07004703 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004704
Joe Perches9d3e3c72015-09-09 15:37:27 -07004705# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004706 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004707 WARN("LINUX_VERSION_CODE",
4708 "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 -08004709 }
4710
Joe Perches17441222011-06-15 15:08:17 -07004711# check for uses of printk_ratelimit
4712 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004713 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004714 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004715 }
4716
Joe Percheseeef5732017-11-17 15:28:41 -08004717# printk should use KERN_* levels
4718 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4719 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4720 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004721 }
4722
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004723# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4724 if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4725 my $printk = $1;
4726 my $modifier = $2;
4727 my $orig = $3;
4728 $modifier = "" if (!defined($modifier));
Joe Perches243f3802012-05-31 16:26:09 -07004729 my $level = lc($orig);
4730 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004731 my $level2 = $level;
4732 $level2 = "dbg" if ($level eq "debug");
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004733 $level .= $modifier;
4734 $level2 .= $modifier;
Joe Perches243f3802012-05-31 16:26:09 -07004735 WARN("PREFER_PR_LEVEL",
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004736 "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 -07004737 }
4738
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004739# prefer dev_<level> to dev_printk(KERN_<LEVEL>
Joe Perchesdc139312013-02-21 16:44:13 -08004740 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4741 my $orig = $1;
4742 my $level = lc($orig);
4743 $level = "warn" if ($level eq "warning");
4744 $level = "dbg" if ($level eq "debug");
4745 WARN("PREFER_DEV_LEVEL",
4746 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4747 }
4748
Nicolas Boichat8020b252020-10-15 20:12:02 -07004749# trace_printk should not be used in production code.
4750 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4751 WARN("TRACE_PRINTK",
4752 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4753 }
4754
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004755# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4756# number of false positives, but assembly files are not checked, so at
4757# least the arch entry code will not trigger this warning.
4758 if ($line =~ /\bENOSYS\b/) {
4759 WARN("ENOSYS",
4760 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4761 }
4762
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004763# ENOTSUPP is not a standard error code and should be avoided in new patches.
4764# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4765# Similarly to ENOSYS warning a small number of false positives is expected.
4766 if (!$file && $line =~ /\bENOTSUPP\b/) {
4767 if (WARN("ENOTSUPP",
4768 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4769 $fix) {
4770 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4771 }
4772 }
4773
Andy Whitcroft653d4872007-06-23 17:16:34 -07004774# function brace can't be on same line, except for #defines of do while,
4775# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004776 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004777 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4778 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4779 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004780 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004781 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004782 $fix) {
4783 fix_delete_line($fixlinenr, $rawline);
4784 my $fixed_line = $rawline;
Dwaipayan Ray03f49352020-12-15 20:45:02 -08004785 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
Joe Perches8d182472014-08-06 16:11:12 -07004786 my $line1 = $1;
4787 my $line2 = $2;
4788 fix_insert_line($fixlinenr, ltrim($line1));
4789 fix_insert_line($fixlinenr, "\+{");
4790 if ($line2 !~ /^\s*$/) {
4791 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4792 }
4793 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004794 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004795
Andy Whitcroft8905a672007-11-28 16:21:06 -08004796# open braces for enum, union and struct go on the same line.
4797 if ($line =~ /^.\s*{/ &&
4798 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004799 if (ERROR("OPEN_BRACE",
4800 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4801 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4802 fix_delete_line($fixlinenr - 1, $prevrawline);
4803 fix_delete_line($fixlinenr, $rawline);
4804 my $fixedline = rtrim($prevrawline) . " {";
4805 fix_insert_line($fixlinenr, $fixedline);
4806 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004807 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004808 if ($fixedline !~ /^\+\s*$/) {
4809 fix_insert_line($fixlinenr, $fixedline);
4810 }
4811 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004812 }
4813
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004814# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004815 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4816 if (WARN("SPACING",
4817 "missing space after $1 definition\n" . $herecurr) &&
4818 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004819 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004820 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4821 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004822 }
4823
Joe Perches31070b52014-01-23 15:54:49 -08004824# Function pointer declarations
4825# check spacing between type, funcptr, and args
4826# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004827 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004828 my $declare = $1;
4829 my $pre_pointer_space = $2;
4830 my $post_pointer_space = $3;
4831 my $funcname = $4;
4832 my $post_funcname_space = $5;
4833 my $pre_args_space = $6;
4834
Joe Perches91f72e92014-04-03 14:49:12 -07004835# the $Declare variable will capture all spaces after the type
4836# so check it for a missing trailing missing space but pointer return types
4837# don't need a space so don't warn for those.
4838 my $post_declare_space = "";
4839 if ($declare =~ /(\s+)$/) {
4840 $post_declare_space = $1;
4841 $declare = rtrim($declare);
4842 }
4843 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004844 WARN("SPACING",
4845 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004846 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004847 }
4848
4849# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004850# This test is not currently implemented because these declarations are
4851# equivalent to
4852# int foo(int bar, ...)
4853# and this is form shouldn't/doesn't generate a checkpatch warning.
4854#
4855# elsif ($declare =~ /\s{2,}$/) {
4856# WARN("SPACING",
4857# "Multiple spaces after return type\n" . $herecurr);
4858# }
Joe Perches31070b52014-01-23 15:54:49 -08004859
4860# unnecessary space "type ( *funcptr)(args...)"
4861 if (defined $pre_pointer_space &&
4862 $pre_pointer_space =~ /^\s/) {
4863 WARN("SPACING",
4864 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4865 }
4866
4867# unnecessary space "type (* funcptr)(args...)"
4868 if (defined $post_pointer_space &&
4869 $post_pointer_space =~ /^\s/) {
4870 WARN("SPACING",
4871 "Unnecessary space before function pointer name\n" . $herecurr);
4872 }
4873
4874# unnecessary space "type (*funcptr )(args...)"
4875 if (defined $post_funcname_space &&
4876 $post_funcname_space =~ /^\s/) {
4877 WARN("SPACING",
4878 "Unnecessary space after function pointer name\n" . $herecurr);
4879 }
4880
4881# unnecessary space "type (*funcptr) (args...)"
4882 if (defined $pre_args_space &&
4883 $pre_args_space =~ /^\s/) {
4884 WARN("SPACING",
4885 "Unnecessary space before function pointer arguments\n" . $herecurr);
4886 }
4887
4888 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004889 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004890 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004891 }
4892 }
4893
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004894# check for spacing round square brackets; allowed:
4895# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004896# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4897# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004898 while ($line =~ /(.*?\s)\[/g) {
4899 my ($where, $prefix) = ($-[1], $1);
4900 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004901 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004902 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004903 if (ERROR("BRACKET_SPACE",
4904 "space prohibited before open square bracket '['\n" . $herecurr) &&
4905 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004906 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004907 s/^(\+.*?)\s+\[/$1\[/;
4908 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004909 }
4910 }
4911
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004912# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004913 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004914 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004915 my $ctx_before = substr($line, 0, $-[1]);
4916 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004917
4918 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004919 if ($name =~ /^(?:
4920 if|for|while|switch|return|case|
4921 volatile|__volatile__|
4922 __attribute__|format|__extension__|
4923 asm|__asm__)$/x)
4924 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004925 # cpp #define statements have non-optional spaces, ie
4926 # if there is a space between the name and the open
4927 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004928 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004929
4930 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004931 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004932
4933 # If this whole things ends with a type its most
4934 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004935 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004936
4937 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004938 if (WARN("SPACING",
4939 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4940 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004941 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004942 s/\b$name\s+\(/$name\(/;
4943 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004944 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004945 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004946
Andy Whitcroft653d4872007-06-23 17:16:34 -07004947# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004948 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004949 my $fixed_line = "";
4950 my $line_fixed = 0;
4951
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004952 my $ops = qr{
4953 <<=|>>=|<=|>=|==|!=|
4954 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4955 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004956 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004957 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004958 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004959 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004960
4961## print("element count: <" . $#elements . ">\n");
4962## foreach my $el (@elements) {
4963## print("el: <$el>\n");
4964## }
4965
4966 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004967 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004968
Joe Perches3705ce52013-07-03 15:05:31 -07004969 foreach my $el (@elements) {
4970 push(@fix_elements, substr($rawline, $off, length($el)));
4971 $off += length($el);
4972 }
4973
4974 $off = 0;
4975
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004976 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004977 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004978
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004979 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004980
4981 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4982
4983## print("n: <$n> good: <$good>\n");
4984
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004985 $off += length($elements[$n]);
4986
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004987 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004988 my $ca = substr($opline, 0, $off);
4989 my $cc = '';
4990 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4991 $cc = substr($opline, $off + length($elements[$n + 1]));
4992 }
4993 my $cb = "$ca$;$cc";
4994
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004995 my $a = '';
4996 $a = 'V' if ($elements[$n] ne '');
4997 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004998 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004999 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
5000 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07005001 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005002
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005003 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005004
5005 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005006 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005007 $c = 'V' if ($elements[$n + 2] ne '');
5008 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005009 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005010 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
5011 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08005012 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005013 } else {
5014 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005015 }
5016
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005017 my $ctx = "${a}x${c}";
5018
5019 my $at = "(ctx:$ctx)";
5020
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005021 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005022 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005023
Andy Whitcroft74048ed2008-07-23 21:29:10 -07005024 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005025 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005026
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005027 # Get the full operator variant.
5028 my $opv = $op . substr($curr_vars, $off, 1);
5029
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005030 # Ignore operators passed as parameters.
5031 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07005032 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005033
Andy Whitcroftcf655042008-03-04 14:28:20 -08005034# # Ignore comments
5035# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005036
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005037 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005038 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005039 if ($ctx !~ /.x[WEBC]/ &&
5040 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005041 if (ERROR("SPACING",
5042 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005043 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07005044 $line_fixed = 1;
5045 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005046 }
5047
5048 # // is a comment
5049 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005050
Joe Perchesb00e4812014-04-03 14:49:33 -07005051 # : when part of a bitfield
5052 } elsif ($opv eq ':B') {
5053 # skip the bitfield test for now
5054
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005055 # No spaces for:
5056 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07005057 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005058 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005059 if (ERROR("SPACING",
5060 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005061 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005062 if (defined $fix_elements[$n + 2]) {
5063 $fix_elements[$n + 2] =~ s/^\s+//;
5064 }
Joe Perchesb34c6482013-09-11 14:24:01 -07005065 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07005066 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005067 }
5068
Joe Perches23810972014-12-10 15:51:32 -08005069 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005070 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08005071 my $rtrim_before = 0;
5072 my $space_after = 0;
5073 if ($ctx =~ /Wx./) {
5074 if (ERROR("SPACING",
5075 "space prohibited before that '$op' $at\n" . $hereptr)) {
5076 $line_fixed = 1;
5077 $rtrim_before = 1;
5078 }
5079 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005080 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005081 if (ERROR("SPACING",
5082 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07005083 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07005084 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08005085 $space_after = 1;
5086 }
5087 }
5088 if ($rtrim_before || $space_after) {
5089 if ($rtrim_before) {
5090 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
5091 } else {
5092 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
5093 }
5094 if ($space_after) {
5095 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07005096 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005097 }
5098
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005099 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07005100 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005101 #warn "'*' is part of type\n";
5102
5103 # unary operators should have a space before and
5104 # none after. May be left adjacent to another
5105 # unary operator, or a cast
5106 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07005107 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07005108 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005109 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005110 if (ERROR("SPACING",
5111 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005112 if ($n != $last_after + 2) {
5113 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
5114 $line_fixed = 1;
5115 }
Joe Perches3705ce52013-07-03 15:05:31 -07005116 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005117 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08005118 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005119 # A unary '*' may be const
5120
5121 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005122 if (ERROR("SPACING",
5123 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005124 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005125 if (defined $fix_elements[$n + 2]) {
5126 $fix_elements[$n + 2] =~ s/^\s+//;
5127 }
Joe Perchesb34c6482013-09-11 14:24:01 -07005128 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07005129 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005130 }
5131
5132 # unary ++ and unary -- are allowed no space on one side.
5133 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005134 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005135 if (ERROR("SPACING",
5136 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005137 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07005138 $line_fixed = 1;
5139 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005140 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005141 if ($ctx =~ /Wx[BE]/ ||
5142 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07005143 if (ERROR("SPACING",
5144 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005145 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005146 $line_fixed = 1;
5147 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005148 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005149 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005150 if (ERROR("SPACING",
5151 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005152 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005153 if (defined $fix_elements[$n + 2]) {
5154 $fix_elements[$n + 2] =~ s/^\s+//;
5155 }
Joe Perchesb34c6482013-09-11 14:24:01 -07005156 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07005157 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005158 }
5159
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005160 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005161 } elsif ($op eq '<<' or $op eq '>>' or
5162 $op eq '&' or $op eq '^' or $op eq '|' or
5163 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005164 $op eq '*' or $op eq '/' or
5165 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005166 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08005167 if ($check) {
5168 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
5169 if (CHK("SPACING",
5170 "spaces preferred around that '$op' $at\n" . $hereptr)) {
5171 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5172 $fix_elements[$n + 2] =~ s/^\s+//;
5173 $line_fixed = 1;
5174 }
5175 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
5176 if (CHK("SPACING",
5177 "space preferred before that '$op' $at\n" . $hereptr)) {
5178 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
5179 $line_fixed = 1;
5180 }
5181 }
5182 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005183 if (ERROR("SPACING",
5184 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005185 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5186 if (defined $fix_elements[$n + 2]) {
5187 $fix_elements[$n + 2] =~ s/^\s+//;
5188 }
Joe Perches3705ce52013-07-03 15:05:31 -07005189 $line_fixed = 1;
5190 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005191 }
5192
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005193 # A colon needs no spaces before when it is
5194 # terminating a case value or a label.
5195 } elsif ($opv eq ':C' || $opv eq ':L') {
Chris Down263afd32021-02-25 17:22:04 -08005196 if ($ctx =~ /Wx./ and $realfile !~ m@.*\.lds\.h$@) {
Joe Perches3705ce52013-07-03 15:05:31 -07005197 if (ERROR("SPACING",
5198 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005199 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005200 $line_fixed = 1;
5201 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005202 }
5203
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005204 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08005205 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005206 my $ok = 0;
5207
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005208 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005209 if (($op eq '<' &&
5210 $cc =~ /^\S+\@\S+>/) ||
5211 ($op eq '>' &&
5212 $ca =~ /<\S+\@\S+$/))
5213 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07005214 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005215 }
5216
Joe Perchese0df7e12015-04-16 12:44:53 -07005217 # for asm volatile statements
5218 # ignore a colon with another
5219 # colon immediately before or after
5220 if (($op eq ':') &&
5221 ($ca =~ /:$/ || $cc =~ /^:/)) {
5222 $ok = 1;
5223 }
5224
Joe Perches84731622013-11-12 15:10:05 -08005225 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005226 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07005227 my $msg_level = \&ERROR;
5228 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08005229
Jean Delvare0675a8f2017-09-08 16:16:07 -07005230 if (&{$msg_level}("SPACING",
5231 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005232 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5233 if (defined $fix_elements[$n + 2]) {
5234 $fix_elements[$n + 2] =~ s/^\s+//;
5235 }
Joe Perches3705ce52013-07-03 15:05:31 -07005236 $line_fixed = 1;
5237 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005238 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005239 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005240 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005241
5242## print("n: <$n> GOOD: <$good>\n");
5243
5244 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005245 }
Joe Perches3705ce52013-07-03 15:05:31 -07005246
5247 if (($#elements % 2) == 0) {
5248 $fixed_line = $fixed_line . $fix_elements[$#elements];
5249 }
5250
Joe Perches194f66f2014-08-06 16:11:03 -07005251 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5252 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07005253 }
5254
5255
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005256 }
5257
Joe Perches786b6322013-07-03 15:05:32 -07005258# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08005259 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07005260 if (WARN("SPACING",
5261 "space prohibited before semicolon\n" . $herecurr) &&
5262 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005263 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07005264 s/^(\+.*\S)\s+;/$1;/;
5265 }
5266 }
5267
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005268# check for multiple assignments
5269 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005270 CHK("MULTIPLE_ASSIGNMENTS",
5271 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005272 }
5273
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005274## # check for multiple declarations, allowing for a function declaration
5275## # continuation.
5276## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
5277## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
5278##
5279## # Remove any bracketed sections to ensure we do not
Dwaipayan Raye73d2712020-12-15 20:44:56 -08005280## # falsely report the parameters of functions.
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005281## my $ln = $line;
5282## while ($ln =~ s/\([^\(\)]*\)//g) {
5283## }
5284## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005285## WARN("MULTIPLE_DECLARATION",
5286## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005287## }
5288## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005289
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005290#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07005291 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07005292 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005293 if (ERROR("SPACING",
5294 "space required before the open brace '{'\n" . $herecurr) &&
5295 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07005296 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07005297 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005298 }
5299
Joe Perchesc4a62ef2013-07-03 15:05:28 -07005300## # check for blank lines before declarations
5301## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5302## $prevrawline =~ /^.\s*$/) {
5303## WARN("SPACING",
5304## "No blank lines before declarations\n" . $hereprev);
5305## }
5306##
5307
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005308# closing brace should have a space following it when it has anything
5309# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07005310 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005311 if (ERROR("SPACING",
5312 "space required after that close brace '}'\n" . $herecurr) &&
5313 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005314 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07005315 s/}((?!(?:,|;|\)))\S)/} $1/;
5316 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005317 }
5318
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005319# check spacing on square brackets
5320 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005321 if (ERROR("SPACING",
5322 "space prohibited after that open square bracket '['\n" . $herecurr) &&
5323 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005324 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005325 s/\[\s+/\[/;
5326 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005327 }
5328 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005329 if (ERROR("SPACING",
5330 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
5331 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005332 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005333 s/\s+\]/\]/;
5334 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005335 }
5336
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005337# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005338 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
5339 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005340 if (ERROR("SPACING",
5341 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
5342 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005343 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005344 s/\(\s+/\(/;
5345 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005346 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005347 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005348 $line !~ /for\s*\(.*;\s+\)/ &&
5349 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005350 if (ERROR("SPACING",
5351 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
5352 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005353 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005354 s/\s+\)/\)/;
5355 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005356 }
5357
Joe Perchese2826fd2014-08-06 16:10:48 -07005358# check unnecessary parentheses around addressof/dereference single $Lvals
5359# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5360
5361 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08005362 my $var = $1;
5363 if (CHK("UNNECESSARY_PARENTHESES",
5364 "Unnecessary parentheses around $var\n" . $herecurr) &&
5365 $fix) {
5366 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5367 }
5368 }
5369
5370# check for unnecessary parentheses around function pointer uses
5371# ie: (foo->bar)(); should be foo->bar();
5372# but not "if (foo->bar) (" to avoid some false positives
5373 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5374 my $var = $2;
5375 if (CHK("UNNECESSARY_PARENTHESES",
5376 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5377 $fix) {
5378 my $var2 = deparenthesize($var);
5379 $var2 =~ s/\s//g;
5380 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5381 }
5382 }
Joe Perchese2826fd2014-08-06 16:10:48 -07005383
Joe Perches63b7c732017-09-08 16:16:01 -07005384# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08005385# when !drivers/staging or command-line uses --strict
5386 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07005387 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07005388 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
5389 my $if_stat = $1;
5390 my $test = substr($2, 1, -1);
5391 my $herectx;
5392 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
5393 my $match = $1;
5394 # avoid parentheses around potential macro args
5395 next if ($match =~ /^\s*\w+\s*$/);
5396 if (!defined($herectx)) {
5397 $herectx = $here . "\n";
5398 my $cnt = statement_rawlines($if_stat);
5399 for (my $n = 0; $n < $cnt; $n++) {
5400 my $rl = raw_line($linenr, $n);
5401 $herectx .= $rl . "\n";
5402 last if $rl =~ /^[ \+].*\{/;
5403 }
5404 }
5405 CHK("UNNECESSARY_PARENTHESES",
5406 "Unnecessary parentheses around '$match'\n" . $herectx);
5407 }
5408 }
5409
Joe Perches69078652021-06-30 18:56:22 -07005410# check that goto labels aren't indented (allow a single space indentation)
5411# and ignore bitfield definitions like foo:1
5412# Strictly, labels can have whitespace after the identifier and before the :
5413# but this is not allowed here as many ?: uses would appear to be labels
5414 if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ &&
5415 $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ &&
5416 $sline !~ /^.\s+default:/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005417 if (WARN("INDENTED_LABEL",
5418 "labels should not be indented\n" . $herecurr) &&
5419 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005420 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005421 s/^(.)\s+/$1/;
5422 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005423 }
5424
Joe Perches40873ab2020-10-15 20:11:56 -07005425# check if a statement with a comma should be two statements like:
5426# foo = bar(), /* comma should be semicolon */
5427# bar = baz();
5428 if (defined($stat) &&
5429 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5430 my $cnt = statement_rawlines($stat);
5431 my $herectx = get_stat_here($linenr, $cnt, $here);
5432 WARN("SUSPECT_COMMA_SEMICOLON",
5433 "Possible comma where semicolon could be used\n" . $herectx);
5434 }
5435
Joe Perches5b9553a2014-04-03 14:49:21 -07005436# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08005437 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005438 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07005439 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07005440 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5441 my $value = $1;
5442 $value = deparenthesize($value);
5443 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5444 ERROR("RETURN_PARENTHESES",
5445 "return is not a function, parentheses are not required\n" . $herecurr);
5446 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005447 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005448 ERROR("SPACING",
5449 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005450 }
5451 }
Joe Perches507e5142013-11-12 15:10:13 -08005452
Joe Perchesb43ae212014-06-23 13:22:07 -07005453# unnecessary return in a void function
5454# at end-of-function, with the previous line a single leading tab, then return;
5455# and the line before that not a goto label target like "out:"
5456 if ($sline =~ /^[ \+]}\s*$/ &&
5457 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5458 $linenr >= 3 &&
5459 $lines[$linenr - 3] =~ /^[ +]/ &&
5460 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07005461 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07005462 "void function return statements are not generally useful\n" . $hereprev);
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08005463 }
Joe Perches9819cf22014-06-04 16:12:09 -07005464
Joe Perches189248d2014-01-23 15:54:47 -08005465# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07005466 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08005467 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5468 my $openparens = $1;
5469 my $count = $openparens =~ tr@\(@\(@;
5470 my $msg = "";
5471 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5472 my $comp = $4; #Not $1 because of $LvalOrFunc
5473 $msg = " - maybe == should be = ?" if ($comp eq "==");
5474 WARN("UNNECESSARY_PARENTHESES",
5475 "Unnecessary parentheses$msg\n" . $herecurr);
5476 }
5477 }
5478
Joe Perchesc5595fa2015-09-09 15:37:58 -07005479# comparisons with a constant or upper case identifier on the left
5480# avoid cases like "foo + BAR < baz"
5481# only fix matches surrounded by parentheses to avoid incorrect
5482# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07005483 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005484 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5485 my $lead = $1;
5486 my $const = $2;
5487 my $comp = $3;
5488 my $to = $4;
5489 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07005490 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005491 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5492 WARN("CONSTANT_COMPARISON",
5493 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5494 $fix) {
5495 if ($comp eq "<") {
5496 $newcomp = ">";
5497 } elsif ($comp eq "<=") {
5498 $newcomp = ">=";
5499 } elsif ($comp eq ">") {
5500 $newcomp = "<";
5501 } elsif ($comp eq ">=") {
5502 $newcomp = "<=";
5503 }
5504 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5505 }
5506 }
5507
Joe Perchesf34e4a42015-04-16 12:44:19 -07005508# Return of what appears to be an errno should normally be negative
5509 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005510 my $name = $1;
Guenter Roeck46b85bf2021-06-30 18:56:25 -07005511 if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005512 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07005513 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005514 }
5515 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005516
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005517# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07005518 if ($line =~ /\b(if|while|for|switch)\(/) {
5519 if (ERROR("SPACING",
5520 "space required before the open parenthesis '('\n" . $herecurr) &&
5521 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005522 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005523 s/\b(if|while|for|switch)\(/$1 \(/;
5524 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005525 }
5526
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07005527# Check for illegal assignment in if conditional -- and check for trailing
5528# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005529 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08005530 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5531 ctx_statement_block($linenr, $realcnt, 0)
5532 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005533 my ($stat_next) = ctx_statement_block($line_nr_next,
5534 $remain_next, $off_next);
5535 $stat_next =~ s/\n./\n /g;
5536 ##print "stat<$stat> stat_next<$stat_next>\n";
5537
5538 if ($stat_next =~ /^\s*while\b/) {
5539 # If the statement carries leading newlines,
5540 # then count those as offsets.
5541 my ($whitespace) =
5542 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5543 my $offset =
5544 statement_rawlines($whitespace) - 1;
5545
5546 $suppress_whiletrailers{$line_nr_next +
5547 $offset} = 1;
5548 }
5549 }
5550 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005551 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005552 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005553 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005554
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005555 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches65b64b32020-08-11 18:35:10 -07005556 if (ERROR("ASSIGN_IN_IF",
5557 "do not use assignment in if condition\n" . $herecurr) &&
5558 $fix && $perl_version_ok) {
5559 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5560 my $space = $1;
5561 my $not = $2;
5562 my $statement = $3;
5563 my $assigned = $4;
5564 my $test = $8;
5565 my $against = $9;
5566 my $brace = $15;
5567 fix_delete_line($fixlinenr, $rawline);
5568 fix_insert_line($fixlinenr, "$space$statement;");
5569 my $newline = "${space}if (";
5570 $newline .= '!' if defined($not);
5571 $newline .= '(' if (defined $not && defined($test) && defined($against));
5572 $newline .= "$assigned";
5573 $newline .= " $test $against" if (defined($test) && defined($against));
5574 $newline .= ')' if (defined $not && defined($test) && defined($against));
5575 $newline .= ')';
5576 $newline .= " {" if (defined($brace));
5577 fix_insert_line($fixlinenr + 1, $newline);
5578 }
5579 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005580 }
5581
5582 # Find out what is on the end of the line after the
5583 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005584 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005585 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005586 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005587 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5588 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005589 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005590 # Find out how long the conditional actually is.
5591 my @newlines = ($c =~ /\n/gs);
5592 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005593 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005594
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005595 $stat_real = raw_line($linenr, $cond_lines)
5596 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005597 if (defined($stat_real) && $cond_lines > 1) {
5598 $stat_real = "[...]\n$stat_real";
5599 }
5600
Joe Perches000d1cc12011-07-25 17:13:25 -07005601 ERROR("TRAILING_STATEMENTS",
5602 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005603 }
5604 }
5605
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005606# Check for bitwise tests written as boolean
5607 if ($line =~ /
5608 (?:
5609 (?:\[|\(|\&\&|\|\|)
5610 \s*0[xX][0-9]+\s*
5611 (?:\&\&|\|\|)
5612 |
5613 (?:\&\&|\|\|)
5614 \s*0[xX][0-9]+\s*
5615 (?:\&\&|\|\||\)|\])
5616 )/x)
5617 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005618 WARN("HEXADECIMAL_BOOLEAN_TEST",
5619 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005620 }
5621
Andy Whitcroft8905a672007-11-28 16:21:06 -08005622# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005623 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5624 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005625 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005626 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005627 ERROR("TRAILING_STATEMENTS",
5628 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005629 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005630 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005631# if should not continue a brace
5632 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005633 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005634 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005635 $herecurr);
5636 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005637# case and default should not have general statements after them
5638 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5639 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005640 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005641 \s*return\s+
5642 )/xg)
5643 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005644 ERROR("TRAILING_STATEMENTS",
5645 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005646 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005647
5648 # Check for }<nl>else {, these must be at the same
5649 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005650 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5651 $previndent == $indent) {
5652 if (ERROR("ELSE_AFTER_BRACE",
5653 "else should follow close brace '}'\n" . $hereprev) &&
5654 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5655 fix_delete_line($fixlinenr - 1, $prevrawline);
5656 fix_delete_line($fixlinenr, $rawline);
5657 my $fixedline = $prevrawline;
5658 $fixedline =~ s/}\s*$//;
5659 if ($fixedline !~ /^\+\s*$/) {
5660 fix_insert_line($fixlinenr, $fixedline);
5661 }
5662 $fixedline = $rawline;
5663 $fixedline =~ s/^(.\s*)else/$1} else/;
5664 fix_insert_line($fixlinenr, $fixedline);
5665 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005666 }
5667
Joe Perches8b8856f2014-08-06 16:11:14 -07005668 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5669 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005670 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5671
5672 # Find out what is on the end of the line after the
5673 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005674 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005675 $s =~ s/\n.*//g;
5676
5677 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005678 if (ERROR("WHILE_AFTER_BRACE",
5679 "while should follow close brace '}'\n" . $hereprev) &&
5680 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5681 fix_delete_line($fixlinenr - 1, $prevrawline);
5682 fix_delete_line($fixlinenr, $rawline);
5683 my $fixedline = $prevrawline;
5684 my $trailing = $rawline;
5685 $trailing =~ s/^\+//;
5686 $trailing = trim($trailing);
5687 $fixedline =~ s/}\s*$/} $trailing/;
5688 fix_insert_line($fixlinenr, $fixedline);
5689 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005690 }
5691 }
5692
Joe Perches95e2c602013-07-03 15:05:20 -07005693#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005694 while ($line =~ m{($Constant|$Lval)}g) {
5695 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005696
Joe Perches95e2c602013-07-03 15:05:20 -07005697#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005698 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005699 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Łukasz Stelmach4104a202020-12-15 20:44:27 -08005700#Ignore some autogenerated defines and enum values
5701 $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005702#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005703 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005704#Ignore SI style variants like nS, mV and dB
5705#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5706 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005707#Ignore some three character SI units explicitly, like MiB and KHz
5708 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005709 while ($var =~ m{($Ident)}g) {
5710 my $word = $1;
5711 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005712 if ($check) {
5713 seed_camelcase_includes();
5714 if (!$file && !$camelcase_file_seeded) {
5715 seed_camelcase_file($realfile);
5716 $camelcase_file_seeded = 1;
5717 }
5718 }
Joe Perches7e781f62013-09-11 14:23:55 -07005719 if (!defined $camelcase{$word}) {
5720 $camelcase{$word} = 1;
5721 CHK("CAMELCASE",
5722 "Avoid CamelCase: <$word>\n" . $herecurr);
5723 }
Joe Perches34456862013-07-03 15:05:34 -07005724 }
Joe Perches323c1262012-12-17 16:02:07 -08005725 }
5726 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005727
5728#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005729 if ($line =~ /\#\s*define.*\\\s+$/) {
5730 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5731 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5732 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005733 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005734 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005735 }
5736
Fabian Frederick0e212e02015-04-16 12:44:25 -07005737# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5738# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005739 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005740 my $file = "$1.h";
5741 my $checkfile = "include/linux/$file";
5742 if (-f "$root/$checkfile" &&
5743 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005744 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005745 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005746 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5747 if ($asminclude > 0) {
5748 if ($realfile =~ m{^arch/}) {
5749 CHK("ARCH_INCLUDE_LINUX",
5750 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5751 } else {
5752 WARN("INCLUDE_LINUX",
5753 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5754 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005755 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005756 }
5757 }
5758
Andy Whitcroft653d4872007-06-23 17:16:34 -07005759# multi-statement macros should be enclosed in a do while loop, grab the
5760# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005761# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07005762 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5763 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005764 my $ln = $linenr;
5765 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005766 my ($off, $dstat, $dcond, $rest);
5767 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005768 my $has_flow_statement = 0;
5769 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005770 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005771 ctx_statement_block($linenr, $realcnt, 0);
5772 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005773 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005774 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005775
Joe Perches08a28432014-10-13 15:51:55 -07005776 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005777 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005778
Joe Perchesf59b64b2016-10-11 13:52:08 -07005779 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5780 my $define_args = $1;
5781 my $define_stmt = $dstat;
5782 my @def_args = ();
5783
5784 if (defined $define_args && $define_args ne "") {
5785 $define_args = substr($define_args, 1, length($define_args) - 2);
5786 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005787 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005788 @def_args = split(",", $define_args);
5789 }
5790
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005791 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005792 $dstat =~ s/\\\n.//g;
5793 $dstat =~ s/^\s*//s;
5794 $dstat =~ s/\s*$//s;
5795
5796 # Flatten any parentheses and braces
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005797 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5798 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5799 $dstat =~ s/.\[[^\[\]]*\]/1u/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005800 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005801 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005802
Antonio Borneo342d3d22020-04-06 20:11:01 -07005803 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005804 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5805 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005806 {
5807 }
5808
Joe Perches42e15292016-03-15 14:58:01 -07005809 # Make asm volatile uses seem like a generic function
5810 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5811
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005812 my $exceptions = qr{
5813 $Declare|
5814 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005815 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005816 DECLARE_PER_CPU|
5817 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005818 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005819 union|
5820 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005821 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005822 ^\"|\"$|
5823 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005824 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005825 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005826
5827 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005828 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005829 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005830
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005831 if ($dstat ne '' &&
5832 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5833 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005834 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005835 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005836 $dstat !~ /$exceptions/ &&
5837 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005838 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005839 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005840 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005841 $dstat !~ /^for\s*$Constant$/ && # for (...)
5842 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5843 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005844 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005845 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005846 {
Joe Perchese7955562017-05-08 15:55:48 -07005847 if ($dstat =~ /^\s*if\b/) {
5848 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5849 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5850 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005851 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5852 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5853 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005854 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005855 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005856 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005857
5858 }
Joe Perches52076492016-10-11 13:52:14 -07005859
5860 # Make $define_stmt single line, comment-free, etc
5861 my @stmt_array = split('\n', $define_stmt);
5862 my $first = 1;
5863 $define_stmt = "";
5864 foreach my $l (@stmt_array) {
5865 $l =~ s/\\$//;
5866 if ($first) {
5867 $define_stmt = $l;
5868 $first = 0;
5869 } elsif ($l =~ /^[\+ ]/) {
5870 $define_stmt .= substr($l, 1);
5871 }
5872 }
5873 $define_stmt =~ s/$;//g;
5874 $define_stmt =~ s/\s+/ /g;
5875 $define_stmt = trim($define_stmt);
5876
Joe Perchesf59b64b2016-10-11 13:52:08 -07005877# check if any macro arguments are reused (ignore '...' and 'type')
5878 foreach my $arg (@def_args) {
5879 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005880 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005881 my $tmp_stmt = $define_stmt;
Vincent Mailhol7b844342021-05-06 18:03:58 -07005882 $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 -07005883 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5884 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005885 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005886 if ($use_cnt > 1) {
5887 CHK("MACRO_ARG_REUSE",
5888 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005889 }
5890# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005891 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005892 ((defined($1) && $1 ne ',') ||
5893 (defined($2) && $2 ne ','))) {
5894 CHK("MACRO_ARG_PRECEDENCE",
5895 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005896 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005897 }
Joe Perches5023d342012-12-17 16:01:47 -08005898
Joe Perches08a28432014-10-13 15:51:55 -07005899# check for macros with flow control, but without ## concatenation
5900# ## concatenation is commonly a macro that defines a function so ignore those
5901 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005902 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005903 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005904
Joe Perches08a28432014-10-13 15:51:55 -07005905 WARN("MACRO_WITH_FLOW_CONTROL",
5906 "Macros with flow control statements should be avoided\n" . "$herectx");
5907 }
5908
Joe Perches481eb482012-12-17 16:01:56 -08005909# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005910
5911 } else {
5912 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005913 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5914 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005915 $line =~ /^\+.*\\$/) {
5916 WARN("LINE_CONTINUATIONS",
5917 "Avoid unnecessary line continuations\n" . $herecurr);
5918 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005919 }
5920
Joe Perchesb13edf72012-07-30 14:41:24 -07005921# do {} while (0) macro tests:
5922# single-statement macros do not need to be enclosed in do while (0) loop,
5923# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005924 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005925 $realfile !~ m@/vmlinux.lds.h$@ &&
5926 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5927 my $ln = $linenr;
5928 my $cnt = $realcnt;
5929 my ($off, $dstat, $dcond, $rest);
5930 my $ctx = '';
5931 ($dstat, $dcond, $ln, $cnt, $off) =
5932 ctx_statement_block($linenr, $realcnt, 0);
5933 $ctx = $dstat;
5934
5935 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005936 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005937
5938 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5939 my $stmts = $2;
5940 my $semis = $3;
5941
5942 $ctx =~ s/\n*$//;
5943 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005944 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005945
Joe Perchesac8e97f2012-08-21 16:15:53 -07005946 if (($stmts =~ tr/;/;/) == 1 &&
5947 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005948 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5949 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5950 }
5951 if (defined $semis && $semis ne "") {
5952 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5953 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5954 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005955 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5956 $ctx =~ s/\n*$//;
5957 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005958 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005959
5960 WARN("TRAILING_SEMICOLON",
5961 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005962 }
5963 }
5964
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005965# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005966 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5967 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005968 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005969 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005970 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5971 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005972 my @allowed = ();
5973 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005974 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005975 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005976 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005977 for my $chunk (@chunks) {
5978 my ($cond, $block) = @{$chunk};
5979
Andy Whitcroft773647a2008-03-28 14:15:58 -07005980 # If the condition carries leading newlines, then count those as offsets.
5981 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5982 my $offset = statement_rawlines($whitespace) - 1;
5983
Joe Perchesaad4f612012-03-23 15:02:19 -07005984 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005985 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5986
5987 # We have looked at and allowed this specific line.
5988 $suppress_ifbraces{$ln + $offset} = 1;
5989
5990 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005991 $ln += statement_rawlines($block) - 1;
5992
Andy Whitcroft773647a2008-03-28 14:15:58 -07005993 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005994
5995 $seen++ if ($block =~ /^\s*{/);
5996
Joe Perchesaad4f612012-03-23 15:02:19 -07005997 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005998 if (statement_lines($cond) > 1) {
5999 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07006000 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006001 }
6002 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08006003 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07006004 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006005 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08006006 if (statement_block_size($block) > 1) {
6007 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07006008 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006009 }
Joe Perchesaad4f612012-03-23 15:02:19 -07006010 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006011 }
Joe Perchesaad4f612012-03-23 15:02:19 -07006012 if ($seen) {
6013 my $sum_allowed = 0;
6014 foreach (@allowed) {
6015 $sum_allowed += $_;
6016 }
6017 if ($sum_allowed == 0) {
6018 WARN("BRACES",
6019 "braces {} are not necessary for any arm of this statement\n" . $herectx);
6020 } elsif ($sum_allowed != $allow &&
6021 $seen != $allow) {
6022 CHK("BRACES",
6023 "braces {} should be used on all arms of this statement\n" . $herectx);
6024 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006025 }
6026 }
6027 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006028 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006029 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08006030 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006031
Andy Whitcroftcf655042008-03-04 14:28:20 -08006032 # Check the pre-context.
6033 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
6034 #print "APW: ALLOWED: pre<$1>\n";
6035 $allowed = 1;
6036 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006037
6038 my ($level, $endln, @chunks) =
6039 ctx_statement_full($linenr, $realcnt, $-[0]);
6040
Andy Whitcroftcf655042008-03-04 14:28:20 -08006041 # Check the condition.
6042 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07006043 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08006044 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07006045 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08006046 }
6047 if (statement_lines($cond) > 1) {
6048 #print "APW: ALLOWED: cond<$cond>\n";
6049 $allowed = 1;
6050 }
6051 if ($block =~/\b(?:if|for|while)\b/) {
6052 #print "APW: ALLOWED: block<$block>\n";
6053 $allowed = 1;
6054 }
6055 if (statement_block_size($block) > 1) {
6056 #print "APW: ALLOWED: lines block<$block>\n";
6057 $allowed = 1;
6058 }
6059 # Check the post-context.
6060 if (defined $chunks[1]) {
6061 my ($cond, $block) = @{$chunks[1]};
6062 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07006063 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006064 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08006065 if ($block =~ /^\s*\{/) {
6066 #print "APW: ALLOWED: chunk-1 block<$block>\n";
6067 $allowed = 1;
6068 }
6069 }
6070 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07006071 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006072 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08006073
Joe Perches000d1cc12011-07-25 17:13:25 -07006074 WARN("BRACES",
6075 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006076 }
6077 }
6078
Joe Perchese4c5bab2017-02-24 15:01:41 -08006079# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08006080 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
6081 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08006082 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
6083 }
6084
Joe Perches0979ae62012-12-17 16:01:59 -08006085# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07006086 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08006087 if (CHK("BRACES",
6088 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
6089 $fix && $prevrawline =~ /^\+/) {
6090 fix_delete_line($fixlinenr - 1, $prevrawline);
6091 }
Joe Perches0979ae62012-12-17 16:01:59 -08006092 }
Joe Perches77b9a532013-07-03 15:05:29 -07006093 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08006094 if (CHK("BRACES",
6095 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
6096 $fix) {
6097 fix_delete_line($fixlinenr, $rawline);
6098 }
Joe Perches0979ae62012-12-17 16:01:59 -08006099 }
6100
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006101# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07006102 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
6103 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006104 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006105 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006106 }
6107
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006108# Check for user-visible strings broken across lines, which breaks the ability
6109# to grep for the string. Make exceptions when the previous string ends in a
6110# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
6111# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07006112 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006113 $prevline =~ /"\s*$/ &&
6114 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
6115 if (WARN("SPLIT_STRING",
6116 "quoted string split across lines\n" . $hereprev) &&
6117 $fix &&
6118 $prevrawline =~ /^\+.*"\s*$/ &&
6119 $last_coalesced_string_linenr != $linenr - 1) {
6120 my $extracted_string = get_quoted_string($line, $rawline);
6121 my $comma_close = "";
6122 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
6123 $comma_close = $1;
6124 }
6125
6126 fix_delete_line($fixlinenr - 1, $prevrawline);
6127 fix_delete_line($fixlinenr, $rawline);
6128 my $fixedline = $prevrawline;
6129 $fixedline =~ s/"\s*$//;
6130 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
6131 fix_insert_line($fixlinenr - 1, $fixedline);
6132 $fixedline = $rawline;
6133 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
6134 if ($fixedline !~ /\+\s*$/) {
6135 fix_insert_line($fixlinenr, $fixedline);
6136 }
6137 $last_coalesced_string_linenr = $linenr;
6138 }
6139 }
6140
6141# check for missing a space in a string concatenation
6142 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
6143 WARN('MISSING_SPACE',
6144 "break quoted strings at a space character\n" . $hereprev);
6145 }
6146
Joe Perchese4b7d302017-05-08 15:55:51 -07006147# check for an embedded function name in a string when the function is known
6148# This does not work very well for -f --file checking as it depends on patch
6149# context providing the function name or a single line form for in-file
6150# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08006151 if ($line =~ /^\+.*$String/ &&
6152 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07006153 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
6154 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08006155 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07006156 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08006157 }
6158
Joe Perchesadb2da82021-02-25 17:21:50 -08006159# check for unnecessary function tracing like uses
6160# This does not use $logFunctions because there are many instances like
6161# 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions
6162 if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) {
6163 if (WARN("TRACING_LOGGING",
6164 "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) &&
6165 $fix) {
6166 fix_delete_line($fixlinenr, $rawline);
6167 }
6168 }
6169
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006170# check for spaces before a quoted newline
6171 if ($rawline =~ /^.*\".*\s\\n/) {
6172 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
6173 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
6174 $fix) {
6175 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
6176 }
6177
6178 }
6179
Joe Perchesf17dba42014-10-13 15:51:51 -07006180# concatenated string without spaces between elements
Joe Perchesd2af5aa2021-09-07 19:59:51 -07006181 if ($line =~ /$String[A-Z_]/ ||
6182 ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) {
Joe Perches79682c02018-08-21 21:57:29 -07006183 if (CHK("CONCATENATED_STRING",
6184 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
6185 $fix) {
6186 while ($line =~ /($String)/g) {
6187 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6188 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
6189 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
6190 }
6191 }
Joe Perchesf17dba42014-10-13 15:51:51 -07006192 }
6193
Joe Perches90ad30e2014-12-10 15:51:59 -08006194# uncoalesced string fragments
Joe Perchesd2af5aa2021-09-07 19:59:51 -07006195 if ($line =~ /$String\s*[Lu]?"/) {
Joe Perches79682c02018-08-21 21:57:29 -07006196 if (WARN("STRING_FRAGMENTS",
6197 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
6198 $fix) {
6199 while ($line =~ /($String)(?=\s*")/g) {
6200 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6201 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
6202 }
6203 }
Joe Perches90ad30e2014-12-10 15:51:59 -08006204 }
6205
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006206# check for non-standard and hex prefixed decimal printf formats
6207 my $show_L = 1; #don't show the same defect twice
6208 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006209 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006210 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006211 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006212 # check for %L
6213 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006214 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006215 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6216 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006217 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006218 # check for %Z
6219 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6220 WARN("PRINTF_Z",
6221 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6222 $show_Z = 0;
6223 }
6224 # check for 0x<decimal>
6225 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6226 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07006227 "Prefixing 0x with decimal output is defective\n" . $herecurr);
6228 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006229 }
6230
6231# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08006232 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006233 WARN("LINE_CONTINUATIONS",
6234 "Avoid line continuations in quoted strings\n" . $herecurr);
6235 }
6236
Andy Whitcroft00df3442007-06-08 13:47:06 -07006237# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006238 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07006239 WARN("IF_0",
6240 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
6241 }
6242
6243# warn about #if 1
6244 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
6245 WARN("IF_1",
6246 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006247 }
6248
Andy Whitcroft03df4b52012-12-17 16:01:52 -08006249# check for needless "if (<foo>) fn(<foo>)" uses
6250 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07006251 my $tested = quotemeta($1);
6252 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6253 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6254 my $func = $1;
6255 if (WARN('NEEDLESS_IF',
6256 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6257 $fix) {
6258 my $do_fix = 1;
6259 my $leading_tabs = "";
6260 my $new_leading_tabs = "";
6261 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6262 $leading_tabs = $1;
6263 } else {
6264 $do_fix = 0;
6265 }
6266 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6267 $new_leading_tabs = $1;
6268 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6269 $do_fix = 0;
6270 }
6271 } else {
6272 $do_fix = 0;
6273 }
6274 if ($do_fix) {
6275 fix_delete_line($fixlinenr - 1, $prevrawline);
6276 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6277 }
6278 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07006279 }
6280 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006281
Joe Perchesebfdc402014-08-06 16:10:27 -07006282# check for unnecessary "Out of Memory" messages
6283 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6284 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6285 (defined $1 || defined $3) &&
6286 $linenr > 3) {
6287 my $testval = $2;
6288 my $testline = $lines[$linenr - 3];
6289
6290 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6291# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6292
Joe Perchese29a70f2019-03-07 16:28:35 -08006293 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6294 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07006295 WARN("OOM_MESSAGE",
6296 "Possible unnecessary 'out of memory' message\n" . $hereprev);
6297 }
6298 }
6299
Joe Perchesf78d98f2014-10-13 15:52:01 -07006300# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08006301 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07006302 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6303 my $level = $1;
6304 if (WARN("UNNECESSARY_KERN_LEVEL",
6305 "Possible unnecessary $level\n" . $herecurr) &&
6306 $fix) {
6307 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
6308 }
6309 }
6310
Joe Perches45c55e92017-02-24 15:01:31 -08006311# check for logging continuations
6312 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
6313 WARN("LOGGING_CONTINUATION",
6314 "Avoid logging continuation uses where feasible\n" . $herecurr);
6315 }
6316
Dwaipayan Ray70eb2272020-12-15 20:45:15 -08006317# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
6318 if (defined $stat &&
6319 $line =~ /\b$logFunctions\s*\(/ &&
6320 index($stat, '"') >= 0) {
6321 my $lc = $stat =~ tr@\n@@;
6322 $lc = $lc + $linenr;
6323 my $stat_real = get_stat_real($linenr, $lc);
6324 pos($stat_real) = index($stat_real, '"');
6325 while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
6326 my $pspec = $1;
6327 my $h = $2;
6328 my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
6329 if (WARN("UNNECESSARY_MODIFIER",
6330 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
6331 $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
6332 my $nspec = $pspec;
6333 $nspec =~ s/h//g;
6334 $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
6335 }
6336 }
6337 }
6338
Joe Perchesabb08a52014-12-10 15:51:46 -08006339# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07006340 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08006341 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6342 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6343 WARN("MASK_THEN_SHIFT",
6344 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6345 }
6346
Joe Perchesb75ac612014-12-10 15:52:02 -08006347# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07006348 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08006349 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6350 my $val = $1;
6351 my $equal = "!";
6352 $equal = "" if ($4 eq "!=");
6353 if (CHK("COMPARISON_TO_NULL",
6354 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6355 $fix) {
6356 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6357 }
6358 }
6359 }
6360
Joe Perches8716de32013-09-11 14:24:05 -07006361# check for bad placement of section $InitAttribute (e.g.: __initdata)
6362 if ($line =~ /(\b$InitAttribute\b)/) {
6363 my $attr = $1;
6364 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
6365 my $ptr = $1;
6366 my $var = $2;
6367 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
6368 ERROR("MISPLACED_INIT",
6369 "$attr should be placed after $var\n" . $herecurr)) ||
6370 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
6371 WARN("MISPLACED_INIT",
6372 "$attr should be placed after $var\n" . $herecurr))) &&
6373 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006374 $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 -07006375 }
6376 }
6377 }
6378
Joe Perchese970b8842013-11-12 15:10:10 -08006379# check for $InitAttributeData (ie: __initdata) with const
6380 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6381 my $attr = $1;
6382 $attr =~ /($InitAttributePrefix)(.*)/;
6383 my $attr_prefix = $1;
6384 my $attr_type = $2;
6385 if (ERROR("INIT_ATTRIBUTE",
6386 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6387 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006388 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006389 s/$InitAttributeData/${attr_prefix}initconst/;
6390 }
6391 }
6392
6393# check for $InitAttributeConst (ie: __initconst) without const
6394 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6395 my $attr = $1;
6396 if (ERROR("INIT_ATTRIBUTE",
6397 "Use of $attr requires a separate use of const\n" . $herecurr) &&
6398 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006399 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006400 /(^\+\s*(?:static\s+))/;
6401 $lead = rtrim($1);
6402 $lead = "$lead " if ($lead !~ /^\+$/);
6403 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07006404 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08006405 }
6406 }
6407
Joe Perchesc17893c2015-04-16 12:44:42 -07006408# check for __read_mostly with const non-pointer (should just be const)
6409 if ($line =~ /\b__read_mostly\b/ &&
6410 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6411 if (ERROR("CONST_READ_MOSTLY",
6412 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6413 $fix) {
6414 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6415 }
6416 }
6417
Joe Perchesfbdb8132014-04-03 14:49:14 -07006418# don't use __constant_<foo> functions outside of include/uapi/
6419 if ($realfile !~ m@^include/uapi/@ &&
6420 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6421 my $constant_func = $1;
6422 my $func = $constant_func;
6423 $func =~ s/^__constant_//;
6424 if (WARN("CONSTANT_CONVERSION",
6425 "$constant_func should be $func\n" . $herecurr) &&
6426 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006427 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07006428 }
6429 }
6430
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006431# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08006432 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07006433 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006434 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07006435 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006436 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006437 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07006438 }
6439 if ($delay > 2000) {
6440 WARN("LONG_UDELAY",
6441 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006442 }
6443 }
6444
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006445# warn about unexpectedly long msleep's
6446 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6447 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006448 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006449 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006450 }
6451 }
6452
Joe Perches36ec1932013-07-03 15:05:25 -07006453# check for comparisons of jiffies
6454 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6455 WARN("JIFFIES_COMPARISON",
6456 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6457 }
6458
Joe Perches9d7a34a2013-07-03 15:05:26 -07006459# check for comparisons of get_jiffies_64()
6460 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6461 WARN("JIFFIES_COMPARISON",
6462 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6463 }
6464
Andy Whitcroft00df3442007-06-08 13:47:06 -07006465# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006466# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07006467# print "#ifdef in C files should be avoided\n";
6468# print "$herecurr";
6469# $clean = 0;
6470# }
6471
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006472# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006473 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07006474 if (ERROR("SPACING",
6475 "exactly one space required after that #$1\n" . $herecurr) &&
6476 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006477 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07006478 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6479 }
6480
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006481 }
6482
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006483# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006484 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6485 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006486 my $which = $1;
6487 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006488 CHK("UNCOMMENTED_DEFINITION",
6489 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006490 }
6491 }
6492# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006493
6494 my $barriers = qr{
6495 mb|
6496 rmb|
Will Deaconad83ec62019-11-07 14:49:00 +00006497 wmb
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006498 }x;
6499 my $barrier_stems = qr{
6500 mb__before_atomic|
6501 mb__after_atomic|
6502 store_release|
6503 load_acquire|
6504 store_mb|
6505 (?:$barriers)
6506 }x;
6507 my $all_barriers = qr{
6508 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02006509 smp_(?:$barrier_stems)|
6510 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006511 }x;
6512
6513 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006514 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08006515 WARN("MEMORY_BARRIER",
6516 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006517 }
6518 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006519
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02006520 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6521
6522 if ($realfile !~ m@^include/asm-generic/@ &&
6523 $realfile !~ m@/barrier\.h$@ &&
6524 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6525 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6526 WARN("MEMORY_BARRIER",
6527 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6528 }
6529
Joe Perchescb426e92015-06-25 15:02:46 -07006530# check for waitqueue_active without a comment.
6531 if ($line =~ /\bwaitqueue_active\s*\(/) {
6532 if (!ctx_has_comment($first_line, $linenr)) {
6533 WARN("WAITQUEUE_ACTIVE",
6534 "waitqueue_active without comment\n" . $herecurr);
6535 }
6536 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006537
Marco Elver5099a722020-04-01 12:17:14 +02006538# check for data_race without a comment.
6539 if ($line =~ /\bdata_race\s*\(/) {
6540 if (!ctx_has_comment($first_line, $linenr)) {
6541 WARN("DATA_RACE",
6542 "data_race without comment\n" . $herecurr);
6543 }
6544 }
6545
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006546# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006547 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006548 CHK("ARCH_DEFINES",
6549 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006550 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006551
Joe Perches596ed452017-07-12 14:37:02 -07006552# check that the storage class is not after a type
6553 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006554 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07006555 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6556 }
6557# Check that the storage class is at the beginning of a declaration
6558 if ($line =~ /\b$Storage\b/ &&
6559 $line !~ /^.\s*$Storage/ &&
6560 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6561 $1 !~ /[\,\)]\s*$/) {
6562 WARN("STORAGE_CLASS",
6563 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07006564 }
6565
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006566# check the location of the inline attribute, that it is between
6567# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006568 if ($line =~ /\b$Type\s+$Inline\b/ ||
6569 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006570 ERROR("INLINE_LOCATION",
6571 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006572 }
6573
Andy Whitcroft8905a672007-11-28 16:21:06 -08006574# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08006575 if ($realfile !~ m@\binclude/uapi/@ &&
6576 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006577 if (WARN("INLINE",
6578 "plain inline is preferred over $1\n" . $herecurr) &&
6579 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006580 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006581
6582 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006583 }
6584
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006585# Check for compiler attributes
Joe Perches2b7ab452013-11-12 15:10:14 -08006586 if ($realfile !~ m@\binclude/uapi/@ &&
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006587 $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
6588 my $attr = $1;
6589 $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
Joe Perches3d130fd2011-01-12 17:00:00 -08006590
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006591 my %attr_list = (
Joe Perches0830aab2020-12-15 20:44:50 -08006592 "alias" => "__alias",
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006593 "aligned" => "__aligned",
6594 "always_inline" => "__always_inline",
6595 "assume_aligned" => "__assume_aligned",
6596 "cold" => "__cold",
6597 "const" => "__attribute_const__",
6598 "copy" => "__copy",
6599 "designated_init" => "__designated_init",
6600 "externally_visible" => "__visible",
6601 "format" => "printf|scanf",
6602 "gnu_inline" => "__gnu_inline",
6603 "malloc" => "__malloc",
6604 "mode" => "__mode",
6605 "no_caller_saved_registers" => "__no_caller_saved_registers",
6606 "noclone" => "__noclone",
6607 "noinline" => "noinline",
6608 "nonstring" => "__nonstring",
6609 "noreturn" => "__noreturn",
6610 "packed" => "__packed",
6611 "pure" => "__pure",
Joe Perches339f29d2020-12-15 20:44:43 -08006612 "section" => "__section",
Joe Perches0830aab2020-12-15 20:44:50 -08006613 "used" => "__used",
6614 "weak" => "__weak"
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006615 );
Joe Perches39b7e282011-07-25 17:13:24 -07006616
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006617 while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
Joe Perches339f29d2020-12-15 20:44:43 -08006618 my $orig_attr = $1;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006619 my $params = '';
6620 $params = $2 if defined($2);
Joe Perches339f29d2020-12-15 20:44:43 -08006621 my $curr_attr = $orig_attr;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006622 $curr_attr =~ s/^[\s_]+|[\s_]+$//g;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006623 if (exists($attr_list{$curr_attr})) {
Joe Perches339f29d2020-12-15 20:44:43 -08006624 my $new = $attr_list{$curr_attr};
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006625 if ($curr_attr eq "format" && $params) {
6626 $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
Joe Perches339f29d2020-12-15 20:44:43 -08006627 $new = "__$1\($2";
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006628 } else {
Joe Perches339f29d2020-12-15 20:44:43 -08006629 $new = "$new$params";
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006630 }
Joe Perches339f29d2020-12-15 20:44:43 -08006631 if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6632 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
6633 $fix) {
6634 my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6635 $fixed[$fixlinenr] =~ s/$remove//;
6636 $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6637 $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6638 $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
6639 }
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006640 }
6641 }
6642
6643 # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
6644 if ($attr =~ /^_*unused/) {
6645 WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6646 "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
Joe Perchesd5e616f2013-09-11 14:23:54 -07006647 }
Joe Perches6061d942012-03-23 15:02:16 -07006648 }
6649
Joe Perches619a9082014-12-10 15:51:35 -08006650# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006651 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006652 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6653 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6654 $line =~ /\b__weak\b/)) {
6655 ERROR("WEAK_DECLARATION",
6656 "Using weak declarations can have unintended link defects\n" . $herecurr);
6657 }
6658
Tomas Winklerfd39f902016-12-12 16:46:34 -08006659# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006660 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006661 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006662 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6663 my $type = $1;
6664 if ($type =~ /\b($typeC99Typedefs)\b/) {
6665 $type = $1;
6666 my $kernel_type = 'u';
6667 $kernel_type = 's' if ($type =~ /^_*[si]/);
6668 $type =~ /(\d+)/;
6669 $kernel_type .= $1;
6670 if (CHK("PREFER_KERNEL_TYPES",
6671 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6672 $fix) {
6673 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6674 }
6675 }
6676 }
6677
Joe Perches938224b2016-01-20 14:59:15 -08006678# check for cast of C90 native int or longer types constants
6679 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6680 my $cast = $1;
6681 my $const = $2;
Joe Perches0972b8b2021-02-25 17:21:54 -08006682 my $suffix = "";
6683 my $newconst = $const;
6684 $newconst =~ s/${Int_type}$//;
6685 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6686 if ($cast =~ /\blong\s+long\b/) {
6687 $suffix .= 'LL';
6688 } elsif ($cast =~ /\blong\b/) {
6689 $suffix .= 'L';
6690 }
Joe Perches938224b2016-01-20 14:59:15 -08006691 if (WARN("TYPECAST_INT_CONSTANT",
Joe Perches0972b8b2021-02-25 17:21:54 -08006692 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
Joe Perches938224b2016-01-20 14:59:15 -08006693 $fix) {
Joe Perches938224b2016-01-20 14:59:15 -08006694 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6695 }
6696 }
6697
Joe Perches8f53a9b2010-03-05 13:43:48 -08006698# check for sizeof(&)
6699 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006700 WARN("SIZEOF_ADDRESS",
6701 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006702 }
6703
Joe Perches66c80b62012-07-30 14:41:22 -07006704# check for sizeof without parenthesis
6705 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006706 if (WARN("SIZEOF_PARENTHESIS",
6707 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6708 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006709 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006710 }
Joe Perches66c80b62012-07-30 14:41:22 -07006711 }
6712
Joe Perches88982fe2012-12-17 16:02:00 -08006713# check for struct spinlock declarations
6714 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6715 WARN("USE_SPINLOCK_T",
6716 "struct spinlock should be spinlock_t\n" . $herecurr);
6717 }
6718
Joe Perchesa6962d72013-04-29 16:18:13 -07006719# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006720 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006721 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006722 $fmt =~ s/%%//g;
6723 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006724 if (WARN("PREFER_SEQ_PUTS",
6725 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6726 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006727 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006728 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006729 }
6730 }
6731
Joe Perches478b1792018-04-10 16:33:34 -07006732# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006733 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006734 defined $stat &&
6735 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6736 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006737 my $stat_real;
6738
Joe Perches0b523762017-05-08 15:55:36 -07006739 my $lc = $stat =~ tr@\n@@;
6740 $lc = $lc + $linenr;
6741 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006742 my $specifier;
6743 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006744 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006745 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006746 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6747 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006748
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006749 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006750 $specifier = $1;
6751 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006752 $qualifier = $3;
Sakari Ailusaf612e42021-02-16 17:57:20 +02006753 if ($extension !~ /[4SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006754 ($extension eq "f" &&
Sakari Ailusaf612e42021-02-16 17:57:20 +02006755 defined $qualifier && $qualifier !~ /^w/) ||
6756 ($extension eq "4" &&
6757 defined $qualifier && $qualifier !~ /^cc/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006758 $bad_specifier = $specifier;
6759 last;
6760 }
6761 if ($extension eq "x" && !defined($stat_real)) {
6762 if (!defined($stat_real)) {
6763 $stat_real = get_stat_real($linenr, $lc);
6764 }
6765 WARN("VSPRINTF_SPECIFIER_PX",
6766 "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");
6767 }
6768 }
6769 if ($bad_specifier ne "") {
6770 my $stat_real = get_stat_real($linenr, $lc);
6771 my $ext_type = "Invalid";
6772 my $use = "";
6773 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006774 $use = " - use %pS instead";
6775 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6776 }
6777
6778 WARN("VSPRINTF_POINTER_EXTENSION",
6779 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6780 }
Joe Perches0b523762017-05-08 15:55:36 -07006781 }
6782 }
6783
Andy Whitcroft554e1652012-01-10 15:09:57 -08006784# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006785 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006786 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006787 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006788
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006789 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006790 my $ms_val = $7;
6791 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006792
Andy Whitcroft554e1652012-01-10 15:09:57 -08006793 if ($ms_size =~ /^(0x|)0$/i) {
6794 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006795 "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 -08006796 } elsif ($ms_size =~ /^(0x|)1$/i) {
6797 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006798 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6799 }
6800 }
6801
Joe Perches98a9bba2014-01-23 15:54:52 -08006802# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006803# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006804# defined $stat &&
6805# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6806# if (WARN("PREFER_ETHER_ADDR_COPY",
6807# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6808# $fix) {
6809# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6810# }
6811# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006812
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006813# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006814# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006815# defined $stat &&
6816# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6817# WARN("PREFER_ETHER_ADDR_EQUAL",
6818# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6819# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006820
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006821# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6822# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006823# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006824# defined $stat &&
6825# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6826#
6827# my $ms_val = $7;
6828#
6829# if ($ms_val =~ /^(?:0x|)0+$/i) {
6830# if (WARN("PREFER_ETH_ZERO_ADDR",
6831# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6832# $fix) {
6833# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6834# }
6835# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6836# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6837# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6838# $fix) {
6839# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6840# }
6841# }
6842# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006843
Joe Perches5dbdb2d2020-12-29 15:14:31 -08006844# strlcpy uses that should likely be strscpy
6845 if ($line =~ /\bstrlcpy\s*\(/) {
6846 WARN("STRLCPY",
6847 "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr);
6848 }
6849
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006850# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006851 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006852 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006853 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006854 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006855 my $call = $1;
6856 my $cast1 = deparenthesize($2);
6857 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006858 my $cast2 = deparenthesize($7);
6859 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006860 my $cast;
6861
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006862 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006863 $cast = "$cast1 or $cast2";
6864 } elsif ($cast1 ne "") {
6865 $cast = $cast1;
6866 } else {
6867 $cast = $cast2;
6868 }
6869 WARN("MINMAX",
6870 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006871 }
6872 }
6873
Joe Perches4a273192012-07-30 14:41:20 -07006874# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006875 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006876 defined $stat &&
6877 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6878 my $min = $1;
6879 my $max = $7;
6880 if ($min eq $max) {
6881 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006882 "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 -07006883 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6884 $min > $max) {
6885 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006886 "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 -07006887 }
6888 }
6889
Joe Perches823b7942013-11-12 15:10:15 -08006890# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006891 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006892 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006893 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006894 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6895 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6896 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6897 my $lc = $stat =~ tr@\n@@;
6898 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006899 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006900 WARN("NAKED_SSCANF",
6901 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6902 }
6903
Joe Perchesafc819a2014-06-04 16:12:08 -07006904# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006905 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006906 defined $stat &&
6907 $line =~ /\bsscanf\b/) {
6908 my $lc = $stat =~ tr@\n@@;
6909 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006910 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006911 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6912 my $format = $6;
6913 my $count = $format =~ tr@%@%@;
6914 if ($count == 1 &&
6915 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6916 WARN("SSCANF_TO_KSTRTO",
6917 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6918 }
6919 }
6920 }
6921
Joe Perches70dc8a42013-09-11 14:23:58 -07006922# check for new externs in .h files.
6923 if ($realfile =~ /\.h$/ &&
6924 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006925 if (CHK("AVOID_EXTERNS",
6926 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006927 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006928 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006929 }
6930 }
6931
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006932# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006933 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006934 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006935 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006936 my $function_name = $1;
6937 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006938
6939 my $s = $stat;
6940 if (defined $cond) {
6941 substr($s, 0, length($cond), '');
6942 }
Kees Cookd8b44b52020-06-03 14:18:09 -07006943 if ($s =~ /^\s*;/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006944 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006945 WARN("AVOID_EXTERNS",
6946 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006947 }
6948
6949 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006950 WARN("FUNCTION_ARGUMENTS",
6951 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006952 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006953
6954 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6955 $stat =~ /^.\s*extern\s+/)
6956 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006957 WARN("AVOID_EXTERNS",
6958 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006959 }
6960
Joe Perchesa0ad7592017-07-10 15:52:19 -07006961# check for function declarations that have arguments without identifier names
6962 if (defined $stat &&
Kees Cookd8b44b52020-06-03 14:18:09 -07006963 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6964 $1 ne "void") {
6965 my $args = trim($1);
Joe Perchesca0d8922016-10-11 13:52:16 -07006966 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6967 my $arg = trim($1);
Kees Cookd8b44b52020-06-03 14:18:09 -07006968 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006969 WARN("FUNCTION_ARGUMENTS",
6970 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6971 }
6972 }
6973 }
6974
Joe Perchesa0ad7592017-07-10 15:52:19 -07006975# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006976 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006977 defined $stat &&
6978 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6979 $context_function = $1;
6980
6981# check for multiline function definition with misplaced open brace
6982 my $ok = 0;
6983 my $cnt = statement_rawlines($stat);
6984 my $herectx = $here . "\n";
6985 for (my $n = 0; $n < $cnt; $n++) {
6986 my $rl = raw_line($linenr, $n);
6987 $herectx .= $rl . "\n";
6988 $ok = 1 if ($rl =~ /^[ \+]\{/);
6989 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6990 last if $rl =~ /^[ \+].*\{/;
6991 }
6992 if (!$ok) {
6993 ERROR("OPEN_BRACE",
6994 "open brace '{' following function definitions go on the next line\n" . $herectx);
6995 }
6996 }
6997
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006998# checks for new __setup's
6999 if ($rawline =~ /\b__setup\("([^"]*)"/) {
7000 my $name = $1;
7001
7002 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007003 CHK("UNDOCUMENTED_SETUP",
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07007004 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07007005 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07007006 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07007007
Joe Perchese29a70f2019-03-07 16:28:35 -08007008# check for pointless casting of alloc functions
7009 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007010 WARN("UNNECESSARY_CASTS",
7011 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07007012 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007013
Joe Perchesa640d252013-07-03 15:05:21 -07007014# alloc style
7015# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07007016 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08007017 $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 -07007018 CHK("ALLOC_SIZEOF_STRUCT",
7019 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
7020 }
7021
Joe Perches60a55362014-06-04 16:12:07 -07007022# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07007023 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07007024 defined $stat &&
7025 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
Joe Perches60a55362014-06-04 16:12:07 -07007026 my $oldfunc = $3;
7027 my $a1 = $4;
7028 my $a2 = $10;
7029 my $newfunc = "kmalloc_array";
7030 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07007031 my $r1 = $a1;
7032 my $r2 = $a2;
7033 if ($a1 =~ /^sizeof\s*\S/) {
7034 $r1 = $a2;
7035 $r2 = $a1;
7036 }
7037 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
7038 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07007039 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07007040 my $herectx = get_stat_here($linenr, $cnt, $here);
7041
Joe Perches60a55362014-06-04 16:12:07 -07007042 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07007043 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
7044 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07007045 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07007046 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
Joe Perches60a55362014-06-04 16:12:07 -07007047 }
7048 }
7049 }
7050
Joe Perches972fdea2013-04-29 16:18:12 -07007051# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07007052 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07007053 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
7054 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07007055 WARN("KREALLOC_ARG_REUSE",
7056 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
7057 }
7058
Joe Perches5ce59ae2013-02-21 16:44:18 -08007059# check for alloc argument mismatch
Christophe JAILLET7e6cdd72021-05-06 18:04:01 -07007060 if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
Joe Perches5ce59ae2013-02-21 16:44:18 -08007061 WARN("ALLOC_ARRAY_ARGS",
7062 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
7063 }
7064
Joe Perchescaf2a542011-01-12 16:59:56 -08007065# check for multiple semicolons
7066 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07007067 if (WARN("ONE_SEMICOLON",
7068 "Statements terminations use 1 semicolon\n" . $herecurr) &&
7069 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07007070 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07007071 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007072 }
7073
Tomas Winklercec3aaa2016-08-02 14:04:39 -07007074# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
7075 if ($realfile !~ m@^include/uapi/@ &&
7076 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08007077 my $ull = "";
7078 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
7079 if (CHK("BIT_MACRO",
7080 "Prefer using the BIT$ull macro\n" . $herecurr) &&
7081 $fix) {
7082 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
7083 }
7084 }
7085
Joe Perches50161262020-08-11 18:35:07 -07007086# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
Jerome Forissier3e89ad82020-10-15 20:11:49 -07007087 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
Joe Perches50161262020-08-11 18:35:07 -07007088 WARN("IS_ENABLED_CONFIG",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07007089 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
Joe Perches50161262020-08-11 18:35:07 -07007090 }
7091
Joe Perches2d632742016-05-20 17:04:00 -07007092# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
Jerome Forissier3e89ad82020-10-15 20:11:49 -07007093 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 -07007094 my $config = $1;
7095 if (WARN("PREFER_IS_ENABLED",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07007096 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
Joe Perches2d632742016-05-20 17:04:00 -07007097 $fix) {
7098 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
7099 }
7100 }
7101
Joe Perchesf36d3eb2020-04-06 20:10:58 -07007102# check for /* fallthrough */ like comment, prefer fallthrough;
7103 my @fallthroughs = (
7104 'fallthrough',
7105 '@fallthrough@',
7106 'lint -fallthrough[ \t]*',
7107 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
7108 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
7109 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7110 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7111 );
7112 if ($raw_comment ne '') {
7113 foreach my $ft (@fallthroughs) {
7114 if ($raw_comment =~ /$ft/) {
7115 my $msg_level = \&WARN;
7116 $msg_level = \&CHK if ($file);
7117 &{$msg_level}("PREFER_FALLTHROUGH",
7118 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
7119 last;
7120 }
7121 }
7122 }
7123
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007124# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07007125 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007126 defined $stat &&
7127 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007128 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07007129 my $herectx = get_stat_here($linenr, $cnt, $here);
7130
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007131 WARN("DEFAULT_NO_BREAK",
7132 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08007133 }
7134
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007135# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07007136 if ($line =~ /\b__FUNCTION__\b/) {
7137 if (WARN("USE_FUNC",
7138 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
7139 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07007140 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07007141 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007142 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07007143
Joe Perches62ec8182015-02-13 14:38:18 -08007144# check for uses of __DATE__, __TIME__, __TIMESTAMP__
7145 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
7146 ERROR("DATE_TIME",
7147 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
7148 }
7149
Joe Perches2c924882012-03-23 15:02:20 -07007150# check for use of yield()
7151 if ($line =~ /\byield\s*\(\s*\)/) {
7152 WARN("YIELD",
7153 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
7154 }
7155
Joe Perches179f8f42013-07-03 15:05:30 -07007156# check for comparisons against true and false
7157 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
7158 my $lead = $1;
7159 my $arg = $2;
7160 my $test = $3;
7161 my $otype = $4;
7162 my $trail = $5;
7163 my $op = "!";
7164
7165 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
7166
7167 my $type = lc($otype);
7168 if ($type =~ /^(?:true|false)$/) {
7169 if (("$test" eq "==" && "$type" eq "true") ||
7170 ("$test" eq "!=" && "$type" eq "false")) {
7171 $op = "";
7172 }
7173
7174 CHK("BOOL_COMPARISON",
7175 "Using comparison to $otype is error prone\n" . $herecurr);
7176
7177## maybe suggesting a correct construct would better
7178## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
7179
7180 }
7181 }
7182
Thomas Gleixner4882720b2010-09-07 14:34:01 +00007183# check for semaphores initialized locked
7184 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007185 WARN("CONSIDER_COMPLETION",
7186 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07007187 }
Joe Perches6712d852012-03-23 15:02:20 -07007188
Joe Perches67d0a072011-10-31 17:13:10 -07007189# recommend kstrto* over simple_strto* and strict_strto*
7190 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007191 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07007192 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07007193 }
Joe Perches6712d852012-03-23 15:02:20 -07007194
Fabian Frederickae3ccc42014-06-04 16:12:10 -07007195# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07007196 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007197 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07007198 "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 -07007199 }
Joe Perches6712d852012-03-23 15:02:20 -07007200
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08007201# check for spin_is_locked(), suggest lockdep instead
7202 if ($line =~ /\bspin_is_locked\(/) {
7203 WARN("USE_LOCKDEP",
7204 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
7205 }
7206
Joe Perches9189c7e2018-09-07 15:26:18 -07007207# check for deprecated apis
7208 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
7209 my $deprecated_api = $1;
7210 my $new_api = $deprecated_apis{$deprecated_api};
7211 WARN("DEPRECATED_API",
7212 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
7213 }
7214
Joe Perches0f3c5aa2015-02-13 14:39:05 -08007215# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07007216# and avoid what seem like struct definitions 'struct foo {'
Quentin Monnetced69da12020-08-11 18:35:13 -07007217 if (defined($const_structs) &&
7218 $line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07007219 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007220 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07007221 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08007222 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07007223
7224# use of NR_CPUS is usually wrong
7225# ignore definitions of NR_CPUS and usage to define arrays as likely right
Peng Wang35cdcbf2021-02-25 17:21:44 -08007226# ignore designated initializers using NR_CPUS
Andy Whitcroft773647a2008-03-28 14:15:58 -07007227 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07007228 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7229 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07007230 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7231 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
Peng Wang35cdcbf2021-02-25 17:21:44 -08007232 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
7233 $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07007234 {
Joe Perches000d1cc12011-07-25 17:13:25 -07007235 WARN("NR_CPUS",
7236 "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 -07007237 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07007238
Joe Perches52ea8502013-11-12 15:10:09 -08007239# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
7240 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
7241 ERROR("DEFINE_ARCH_HAS",
7242 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
7243 }
7244
Joe Perchesacd93622015-02-13 14:38:38 -08007245# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07007246 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08007247 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7248 WARN("LIKELY_MISUSE",
7249 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7250 }
7251
Joe Perchesfbe74542021-05-06 18:03:55 -07007252# return sysfs_emit(foo, fmt, ...) fmt without newline
7253 if ($line =~ /\breturn\s+sysfs_emit\s*\(\s*$FuncArg\s*,\s*($String)/ &&
7254 substr($rawline, $-[6], $+[6] - $-[6]) !~ /\\n"$/) {
7255 my $offset = $+[6] - 1;
7256 if (WARN("SYSFS_EMIT",
7257 "return sysfs_emit(...) formats should include a terminating newline\n" . $herecurr) &&
7258 $fix) {
7259 substr($fixed[$fixlinenr], $offset, 0) = '\\n';
7260 }
7261 }
7262
Denis Efremovde3f1862019-09-25 16:49:25 -07007263# nested likely/unlikely calls
7264 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7265 WARN("LIKELY_MISUSE",
7266 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7267 }
7268
Andy Whitcroft691d77b2009-01-06 14:41:16 -08007269# whine mightly about in_atomic
7270 if ($line =~ /\bin_atomic\s*\(/) {
7271 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007272 ERROR("IN_ATOMIC",
7273 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08007274 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007275 WARN("IN_ATOMIC",
7276 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08007277 }
7278 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01007279
7280# check for lockdep_set_novalidate_class
7281 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
7282 $line =~ /__lockdep_no_validate__\s*\)/ ) {
7283 if ($realfile !~ m@^kernel/lockdep@ &&
7284 $realfile !~ m@^include/linux/lockdep@ &&
7285 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007286 ERROR("LOCKDEP",
7287 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01007288 }
7289 }
Dave Jones88f88312011-01-12 16:59:59 -08007290
Joe Perchesb392c642015-04-16 12:44:16 -07007291 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7292 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007293 WARN("EXPORTED_WORLD_WRITABLE",
7294 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08007295 }
Joe Perches24358802014-04-03 14:49:13 -07007296
Joe Perches00180462018-02-06 15:38:55 -08007297# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
7298# and whether or not function naming is typical and if
7299# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07007300 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08007301 defined $stat &&
7302 $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*\)/) {
7303 my $var = $1;
7304 my $perms = $2;
7305 my $show = $3;
7306 my $store = $4;
7307 my $octal_perms = perms_to_octal($perms);
7308 if ($show =~ /^${var}_show$/ &&
7309 $store =~ /^${var}_store$/ &&
7310 $octal_perms eq "0644") {
7311 if (WARN("DEVICE_ATTR_RW",
7312 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
7313 $fix) {
7314 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
7315 }
7316 } elsif ($show =~ /^${var}_show$/ &&
7317 $store =~ /^NULL$/ &&
7318 $octal_perms eq "0444") {
7319 if (WARN("DEVICE_ATTR_RO",
7320 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
7321 $fix) {
7322 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
7323 }
7324 } elsif ($show =~ /^NULL$/ &&
7325 $store =~ /^${var}_store$/ &&
7326 $octal_perms eq "0200") {
7327 if (WARN("DEVICE_ATTR_WO",
7328 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
7329 $fix) {
7330 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
7331 }
7332 } elsif ($octal_perms eq "0644" ||
7333 $octal_perms eq "0444" ||
7334 $octal_perms eq "0200") {
7335 my $newshow = "$show";
7336 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
7337 my $newstore = $store;
7338 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
7339 my $rename = "";
7340 if ($show ne $newshow) {
7341 $rename .= " '$show' to '$newshow'";
7342 }
7343 if ($store ne $newstore) {
7344 $rename .= " '$store' to '$newstore'";
7345 }
7346 WARN("DEVICE_ATTR_FUNCTIONS",
7347 "Consider renaming function(s)$rename\n" . $herecurr);
7348 } else {
7349 WARN("DEVICE_ATTR_PERMS",
7350 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
7351 }
7352 }
7353
Joe Perches515a2352014-04-03 14:49:24 -07007354# Mode permission misuses where it seems decimal should be octal
7355# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08007356# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
7357# specific definition of not visible in sysfs.
7358# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
7359# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07007360 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07007361 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07007362 $line =~ /$mode_perms_search/) {
7363 foreach my $entry (@mode_permission_funcs) {
7364 my $func = $entry->[0];
7365 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07007366
Joe Perches459cf0a2016-10-11 13:52:19 -07007367 my $lc = $stat =~ tr@\n@@;
7368 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07007369 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07007370
Joe Perches515a2352014-04-03 14:49:24 -07007371 my $skip_args = "";
7372 if ($arg_pos > 1) {
7373 $arg_pos--;
7374 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
7375 }
Joe Perchesf90774e2016-10-11 13:51:47 -07007376 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07007377 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07007378 my $val = $1;
7379 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08007380 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
7381 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
7382 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07007383 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07007384 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07007385 }
7386 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08007387 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07007388 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07007389 }
Joe Perches24358802014-04-03 14:49:13 -07007390 }
7391 }
7392 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007393
Joe Perches459cf0a2016-10-11 13:52:19 -07007394# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07007395 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08007396 my $oval = $1;
7397 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07007398 if (WARN("SYMBOLIC_PERMS",
7399 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7400 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08007401 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07007402 }
7403 }
7404
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007405# validate content of MODULE_LICENSE against list from include/linux/module.h
7406 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
7407 my $extracted_string = get_quoted_string($line, $rawline);
7408 my $valid_licenses = qr{
7409 GPL|
7410 GPL\ v2|
7411 GPL\ and\ additional\ rights|
7412 Dual\ BSD/GPL|
7413 Dual\ MIT/GPL|
7414 Dual\ MPL/GPL|
7415 Proprietary
7416 }x;
7417 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
7418 WARN("MODULE_LICENSE",
7419 "unknown module license " . $extracted_string . "\n" . $herecurr);
7420 }
7421 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07007422
7423# check for sysctl duplicate constants
7424 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
7425 WARN("DUPLICATED_SYSCTL_CONST",
7426 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
7427 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007428 }
7429
7430 # If we have no input at all, then there is nothing to report on
7431 # so just keep quiet.
7432 if ($#rawlines == -1) {
7433 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007434 }
7435
Andy Whitcroft8905a672007-11-28 16:21:06 -08007436 # In mailback mode only produce a report in the negative, for
7437 # things that appear to be patches.
7438 if ($mailback && ($clean == 1 || !$is_patch)) {
7439 exit(0);
7440 }
7441
Dwaipayan Raye73d2712020-12-15 20:44:56 -08007442 # This is not a patch, and we are in 'no-patch' mode so
Andy Whitcroft8905a672007-11-28 16:21:06 -08007443 # just keep quiet.
7444 if (!$chk_patch && !$is_patch) {
7445 exit(0);
7446 }
7447
Stafford Hornea08ffbe2017-10-03 16:16:51 -07007448 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007449 ERROR("NOT_UNIFIED_DIFF",
7450 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007451 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007452 if ($is_patch && $has_commit_log && $chk_signoff) {
7453 if ($signoff == 0) {
7454 ERROR("MISSING_SIGN_OFF",
7455 "Missing Signed-off-by: line(s)\n");
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07007456 } elsif ($authorsignoff != 1) {
7457 # authorsignoff values:
7458 # 0 -> missing sign off
7459 # 1 -> sign off identical
7460 # 2 -> names and addresses match, comments mismatch
7461 # 3 -> addresses match, names different
7462 # 4 -> names match, addresses different
7463 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
7464
7465 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
7466
7467 if ($authorsignoff == 0) {
7468 ERROR("NO_AUTHOR_SIGN_OFF",
7469 "Missing Signed-off-by: line by nominal patch author '$author'\n");
7470 } elsif ($authorsignoff == 2) {
7471 CHK("FROM_SIGN_OFF_MISMATCH",
7472 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7473 } elsif ($authorsignoff == 3) {
7474 WARN("FROM_SIGN_OFF_MISMATCH",
7475 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7476 } elsif ($authorsignoff == 4) {
7477 WARN("FROM_SIGN_OFF_MISMATCH",
7478 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7479 } elsif ($authorsignoff == 5) {
7480 WARN("FROM_SIGN_OFF_MISMATCH",
7481 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7482 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007483 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007484 }
7485
Andy Whitcroft8905a672007-11-28 16:21:06 -08007486 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007487 if ($summary && !($clean == 1 && $quiet == 1)) {
7488 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08007489 print "total: $cnt_error errors, $cnt_warn warnings, " .
7490 (($check)? "$cnt_chk checks, " : "") .
7491 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07007492 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08007493
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007494 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07007495 # If there were any defects found and not already fixing them
7496 if (!$clean and !$fix) {
7497 print << "EOM"
7498
7499NOTE: For some of the reported defects, checkpatch may be able to
7500 mechanically convert to the typical style using --fix or --fix-inplace.
7501EOM
7502 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007503 # If there were whitespace errors which cleanpatch can fix
7504 # then suggest that.
7505 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07007506 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07007507 print << "EOM"
7508
7509NOTE: Whitespace errors detected.
7510 You may wish to use scripts/cleanpatch or scripts/cleanfile
7511EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007512 }
7513 }
7514
Joe Perchesd752fcc2014-08-06 16:11:05 -07007515 if ($clean == 0 && $fix &&
7516 ("@rawlines" ne "@fixed" ||
7517 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08007518 my $newfile = $filename;
7519 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07007520 my $linecount = 0;
7521 my $f;
7522
Joe Perchesd752fcc2014-08-06 16:11:05 -07007523 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7524
Joe Perches3705ce52013-07-03 15:05:31 -07007525 open($f, '>', $newfile)
7526 or die "$P: Can't open $newfile for write\n";
7527 foreach my $fixed_line (@fixed) {
7528 $linecount++;
7529 if ($file) {
7530 if ($linecount > 3) {
7531 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07007532 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07007533 }
7534 } else {
7535 print $f $fixed_line . "\n";
7536 }
7537 }
7538 close($f);
7539
7540 if (!$quiet) {
7541 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07007542
Joe Perches3705ce52013-07-03 15:05:31 -07007543Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
7544
7545Do _NOT_ trust the results written to this file.
7546Do _NOT_ submit these changes without inspecting them for correctness.
7547
7548This EXPERIMENTAL file is simply a convenience to help rewrite patches.
7549No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07007550EOM
7551 }
7552 }
7553
Joe Perchesd8469f12015-06-25 15:03:00 -07007554 if ($quiet == 0) {
7555 print "\n";
7556 if ($clean == 1) {
7557 print "$vname has no obvious style problems and is ready for submission.\n";
7558 } else {
7559 print "$vname has style problems, please review.\n";
7560 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007561 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007562 return $clean;
7563}