blob: 91798b07c6cb54fdca3a50c923d36604d80b8212 [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";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070066my $conststructsfile = "$D/const_structs.checkpatch";
Dwaipayan Ray52178ce2021-02-26 15:08:26 +053067my $docsfile = "$D/../Documentation/dev-tools/checkpatch.rst";
Quentin Monnetced69da12020-08-11 18:35:13 -070068my $typedefsfile;
John Brooks737c0762017-07-10 15:52:24 -070069my $color = "auto";
Vadim Bendebury98005e82019-03-07 16:28:38 -080070my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
Joe Perchesdbbf8692019-09-25 16:46:52 -070071# git output parsing needs US English output, so first set backtick child process LANGUAGE
72my $git_command ='export LANGUAGE=en_US.UTF-8; git';
Antonio Borneo713a09d2020-04-06 20:11:07 -070073my $tabsize = 8;
Jerome Forissier3e89ad82020-10-15 20:11:49 -070074my ${CONFIG_} = "CONFIG_";
Hannes Eder77f5b102009-09-21 17:04:37 -070075
76sub help {
77 my ($exitcode) = @_;
78
79 print << "EOM";
80Usage: $P [OPTION]... [FILE]...
81Version: $V
82
83Options:
84 -q, --quiet quiet
Dwaipayan Ray52178ce2021-02-26 15:08:26 +053085 -v, --verbose verbose mode
Hannes Eder77f5b102009-09-21 17:04:37 -070086 --no-tree run without a kernel tree
87 --no-signoff do not check for 'Signed-off-by' line
88 --patch treat FILE as patchfile (default)
89 --emacs emacs compile window format
90 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070091 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070092 -g, --git treat FILE as a single commit or git revision range
93 single git commit with:
94 <rev>
95 <rev>^
96 <rev>~n
97 multiple git commits with:
98 <rev1>..<rev2>
99 <rev1>...<rev2>
100 <rev>-<count>
101 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -0700102 -f, --file treat FILE as regular source file
103 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -0700104 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -0700105 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -0700106 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -0700107 --show-types show the specific message type in the output
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700108 --max-line-length=n set the maximum line length, (default $max_line_length)
109 if exceeded, warn on patches
110 requires --strict for use with --file
Vadim Bendebury56193272014-10-13 15:51:48 -0700111 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700112 --tab-size=n set the number of spaces for tab (default $tabsize)
Hannes Eder77f5b102009-09-21 17:04:37 -0700113 --root=PATH PATH to the kernel tree root
114 --no-summary suppress the per-file summary
115 --mailback only produce a report in case of warnings/errors
116 --summary-file include the filename in summary
117 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
118 'values', 'possible', 'type', and 'attr' (default
119 is all off)
120 --test-only=WORD report only warnings/errors containing WORD
121 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700122 --fix EXPERIMENTAL - may create horrible results
123 If correctable single-line errors exist, create
124 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
125 with potential errors corrected to the preferred
126 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800127 --fix-inplace EXPERIMENTAL - may create horrible results
128 Is the same as --fix, but overwrites the input
129 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700130 --ignore-perl-version override checking of perl version. expect
131 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700132 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700133 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700134 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700135 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700136 --color[=WHEN] Use colors 'always', 'never', or only when output
137 is a terminal ('auto'). Default is 'auto'.
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700138 --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default
139 ${CONFIG_})
Hannes Eder77f5b102009-09-21 17:04:37 -0700140 -h, --help, --version display this help and exit
141
142When FILE is - read standard input.
143EOM
144
145 exit($exitcode);
146}
147
Joe Perches3beb42e2016-05-20 17:04:14 -0700148sub uniq {
149 my %seen;
150 return grep { !$seen{$_}++ } @_;
151}
152
153sub list_types {
154 my ($exitcode) = @_;
155
156 my $count = 0;
157
158 local $/ = undef;
159
160 open(my $script, '<', abs_path($P)) or
161 die "$P: Can't read '$P' $!\n";
162
163 my $text = <$script>;
164 close($script);
165
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530166 my %types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700167 # Also catch when type or level is passed through a variable
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530168 while ($text =~ /(?:(\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
169 if (defined($1)) {
170 if (exists($types{$2})) {
171 $types{$2} .= ",$1" if ($types{$2} ne $1);
172 } else {
173 $types{$2} = $1;
174 }
175 } else {
176 $types{$2} = "UNDETERMINED";
177 }
Joe Perches3beb42e2016-05-20 17:04:14 -0700178 }
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530179
Joe Perches3beb42e2016-05-20 17:04:14 -0700180 print("#\tMessage type\n\n");
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530181 if ($color) {
182 print(" ( Color coding: ");
183 print(RED . "ERROR" . RESET);
184 print(" | ");
185 print(YELLOW . "WARNING" . RESET);
186 print(" | ");
187 print(GREEN . "CHECK" . RESET);
188 print(" | ");
189 print("Multiple levels / Undetermined");
190 print(" )\n\n");
191 }
192
193 foreach my $type (sort keys %types) {
194 my $orig_type = $type;
195 if ($color) {
196 my $level = $types{$type};
197 if ($level eq "ERROR") {
198 $type = RED . $type . RESET;
199 } elsif ($level eq "WARN") {
200 $type = YELLOW . $type . RESET;
201 } elsif ($level eq "CHK") {
202 $type = GREEN . $type . RESET;
203 }
204 }
Joe Perches3beb42e2016-05-20 17:04:14 -0700205 print(++$count . "\t" . $type . "\n");
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530206 if ($verbose && exists($verbose_messages{$orig_type})) {
207 my $message = $verbose_messages{$orig_type};
208 $message =~ s/\n/\n\t/g;
209 print("\t" . $message . "\n\n");
210 }
Joe Perches3beb42e2016-05-20 17:04:14 -0700211 }
212
213 exit($exitcode);
214}
215
Joe Perches000d1cc12011-07-25 17:13:25 -0700216my $conf = which_conf($configuration_file);
217if (-f $conf) {
218 my @conf_args;
219 open(my $conffile, '<', "$conf")
220 or warn "$P: Can't find a readable $configuration_file file $!\n";
221
222 while (<$conffile>) {
223 my $line = $_;
224
225 $line =~ s/\s*\n?$//g;
226 $line =~ s/^\s*//g;
227 $line =~ s/\s+/ /g;
228
229 next if ($line =~ m/^\s*#/);
230 next if ($line =~ m/^\s*$/);
231
232 my @words = split(" ", $line);
233 foreach my $word (@words) {
234 last if ($word =~ m/^#/);
235 push (@conf_args, $word);
236 }
237 }
238 close($conffile);
239 unshift(@ARGV, @conf_args) if @conf_args;
240}
241
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530242sub load_docs {
243 open(my $docs, '<', "$docsfile")
244 or warn "$P: Can't read the documentation file $docsfile $!\n";
245
246 my $type = '';
247 my $desc = '';
248 my $in_desc = 0;
249
250 while (<$docs>) {
251 chomp;
252 my $line = $_;
253 $line =~ s/\s+$//;
254
255 if ($line =~ /^\s*\*\*(.+)\*\*$/) {
256 if ($desc ne '') {
257 $verbose_messages{$type} = trim($desc);
258 }
259 $type = $1;
260 $desc = '';
261 $in_desc = 1;
262 } elsif ($in_desc) {
263 if ($line =~ /^(?:\s{4,}|$)/) {
264 $line =~ s/^\s{4}//;
265 $desc .= $line;
266 $desc .= "\n";
267 } else {
268 $verbose_messages{$type} = trim($desc);
269 $type = '';
270 $desc = '';
271 $in_desc = 0;
272 }
273 }
274 }
275
276 if ($desc ne '') {
277 $verbose_messages{$type} = trim($desc);
278 }
279 close($docs);
280}
281
John Brooks737c0762017-07-10 15:52:24 -0700282# Perl's Getopt::Long allows options to take optional arguments after a space.
283# Prevent --color by itself from consuming other arguments
284foreach (@ARGV) {
285 if ($_ eq "--color" || $_ eq "-color") {
286 $_ = "--color=$color";
287 }
288}
289
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700290GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700291 'q|quiet+' => \$quiet,
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530292 'v|verbose!' => \$verbose,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700293 'tree!' => \$tree,
294 'signoff!' => \$chk_signoff,
295 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700296 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800297 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700298 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700299 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700300 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700301 'subjective!' => \$check,
302 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700303 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700304 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700305 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700306 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800307 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700308 'min-conf-desc-length=i' => \$min_conf_desc_length,
Antonio Borneo713a09d2020-04-06 20:11:07 -0700309 'tab-size=i' => \$tabsize,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700310 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800311 'summary!' => \$summary,
312 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800313 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700314 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800315 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700316 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800317 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700318 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700319 'codespell!' => \$codespell,
320 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700321 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700322 'color=s' => \$color,
323 'no-color' => \$color, #keep old behaviors of -nocolor
324 'nocolor' => \$color, #keep old behaviors of -nocolor
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700325 'kconfig-prefix=s' => \${CONFIG_},
Hannes Eder77f5b102009-09-21 17:04:37 -0700326 'h|help' => \$help,
327 'version' => \$help
328) or help(1);
329
330help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700331
Dwaipayan Ray52178ce2021-02-26 15:08:26 +0530332die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
333die "$P: --verbose cannot be used with --terse\n" if ($verbose && $terse);
334
335if ($color =~ /^[01]$/) {
336 $color = !$color;
337} elsif ($color =~ /^always$/i) {
338 $color = 1;
339} elsif ($color =~ /^never$/i) {
340 $color = 0;
341} elsif ($color =~ /^auto$/i) {
342 $color = (-t STDOUT);
343} else {
344 die "$P: Invalid color mode: $color\n";
345}
346
347load_docs() if ($verbose);
Joe Perches3beb42e2016-05-20 17:04:14 -0700348list_types(0) if ($list_types);
349
Joe Perches9624b8d2014-01-23 15:54:44 -0800350$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700351$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800352
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700353my $exit = 0;
354
Joe Perches5b579802018-08-21 21:57:33 -0700355my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700356if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700357 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700358 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700359 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700360}
361
Allen Hubbe45107ff2016-08-02 14:04:47 -0700362#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700363if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700364 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700365}
366
Antonio Borneo713a09d2020-04-06 20:11:07 -0700367# skip TAB size 1 to avoid additional checks on $tabsize - 1
Joe Perches32f30ca2020-06-04 16:50:40 -0700368die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
Antonio Borneo713a09d2020-04-06 20:11:07 -0700369
Joe Perches91bfe482013-09-11 14:23:59 -0700370sub hash_save_array_words {
371 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700372
Joe Perches91bfe482013-09-11 14:23:59 -0700373 my @array = split(/,/, join(',', @$arrayRef));
374 foreach my $word (@array) {
375 $word =~ s/\s*\n?$//g;
376 $word =~ s/^\s*//g;
377 $word =~ s/\s+/ /g;
378 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700379
Joe Perches91bfe482013-09-11 14:23:59 -0700380 next if ($word =~ m/^\s*#/);
381 next if ($word =~ m/^\s*$/);
382
383 $hashRef->{$word}++;
384 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700385}
386
Joe Perches91bfe482013-09-11 14:23:59 -0700387sub hash_show_words {
388 my ($hashRef, $prefix) = @_;
389
Joe Perches3c816e42015-06-25 15:03:29 -0700390 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700391 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700392 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700393 print " $word";
394 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700395 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700396 }
397}
398
399hash_save_array_words(\%ignore_type, \@ignore);
400hash_save_array_words(\%use_type, \@use);
401
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800402my $dbg_values = 0;
403my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700404my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700405my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800406for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800407 ## no critic
408 eval "\${dbg_$key} = '$debug{$key}';";
409 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800410}
411
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700412my $rpt_cleaners = 0;
413
Andy Whitcroft8905a672007-11-28 16:21:06 -0800414if ($terse) {
415 $emacs = 1;
416 $quiet++;
417}
418
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700419if ($tree) {
420 if (defined $root) {
421 if (!top_of_kernel_tree($root)) {
422 die "$P: $root: --root does not point at a valid tree\n";
423 }
424 } else {
425 if (top_of_kernel_tree('.')) {
426 $root = '.';
427 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
428 top_of_kernel_tree($1)) {
429 $root = $1;
430 }
431 }
432
433 if (!defined $root) {
434 print "Must be run from the top-level dir. of a kernel tree\n";
435 exit(2);
436 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700437}
438
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700439my $emitted_corrupt = 0;
440
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700441our $Ident = qr{
442 [A-Za-z_][A-Za-z\d_]*
443 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
444 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700445our $Storage = qr{extern|static|asmlinkage};
446our $Sparse = qr{
447 __user|
448 __kernel|
449 __force|
450 __iomem|
451 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800452 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700453 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700454 __refconst|
455 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800456 __rcu|
457 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700458 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800459our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
460our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
461our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
462our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
463our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700464
Wolfram Sang52131292010-03-05 13:43:51 -0800465# Notes to $Attribute:
466# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700467our $Attribute = qr{
468 const|
Joe Perchesb5e87362021-02-25 17:21:40 -0800469 volatile|
Joe Perches03f1df72010-10-26 14:23:16 -0700470 __percpu|
471 __nocast|
472 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200473 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700474 __packed__|
475 __packed2__|
476 __naked|
477 __maybe_unused|
478 __always_unused|
479 __noreturn|
480 __used|
481 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800482 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700483 __noclone|
484 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700485 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700486 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700487 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700488 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700489 ____cacheline_aligned|
490 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800491 ____cacheline_internodealigned_in_smp|
Kees Cook86cffec2021-11-05 13:36:19 -0700492 __weak|
493 __alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\)
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700494 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700495our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700496our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700497our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
498our $Lval = qr{$Ident(?:$Member)*};
499
Joe Perches95e2c602013-07-03 15:05:20 -0700500our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
501our $Binary = qr{(?i)0b[01]+$Int_type?};
502our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
503our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700504our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesd2af5aa2021-09-07 19:59:51 -0700505our $String = qr{(?:\b[Lu])?"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800506our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
507our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
508our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800509our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700510our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800511our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700512our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700513our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700514our $Operators = qr{
515 <=|>=|==|!=|
516 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700517 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700518 }x;
519
Joe Perches91cb5192014-04-03 14:49:32 -0700520our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
521
Joe Perchesab7e23f2015-04-16 12:44:22 -0700522our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800523our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700524our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700525our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800526our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700527our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800528our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700529our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800530
Joe Perches15662b32011-10-31 17:13:12 -0700531our $NON_ASCII_UTF8 = qr{
532 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700533 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
534 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
535 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
536 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
537 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
538 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
539}x;
540
Joe Perches15662b32011-10-31 17:13:12 -0700541our $UTF8 = qr{
542 [\x09\x0A\x0D\x20-\x7E] # ASCII
543 | $NON_ASCII_UTF8
544}x;
545
Joe Perchese6176fa2015-06-25 15:02:49 -0700546our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800547our $typeOtherOSTypedefs = qr{(?x:
548 u_(?:char|short|int|long) | # bsd
549 u(?:nchar|short|int|long) # sysv
550)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700551our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700552 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700553 atomic_t
554)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700555our $typeTypedefs = qr{(?x:
556 $typeC99Typedefs\b|
557 $typeOtherOSTypedefs\b|
558 $typeKernelTypedefs\b
559)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700560
Joe Perches6d32f7a2015-11-06 16:31:37 -0800561our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
562
Joe Perches691e6692010-03-05 13:43:51 -0800563our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800564 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700565 (?:[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 -0800566 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700567 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700568 panic|
Joe Perches06668722013-11-12 15:10:07 -0800569 MODULE_[A-Z_]+|
570 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800571)};
572
Joe Perchese29a70f2019-03-07 16:28:35 -0800573our $allocFunctions = qr{(?x:
574 (?:(?:devm_)?
Joe Perches58f02262021-02-25 17:22:01 -0800575 (?:kv|k|v)[czm]alloc(?:_array)?(?:_node)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800576 kstrdup(?:_const)? |
577 kmemdup(?:_nul)?) |
Christophe JAILLET461e1562020-04-20 18:13:58 -0700578 (?:\w+)?alloc_skb(?:_ip_align)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800579 # dev_alloc_skb/netdev_alloc_skb, et al
580 dma_alloc_coherent
581)};
582
Joe Perches20112472011-07-25 17:13:23 -0700583our $signature_tags = qr{(?xi:
584 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800585 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700586 Acked-by:|
587 Tested-by:|
588 Reviewed-by:|
589 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700590 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700591 To:|
592 Cc:
593)};
594
Joe Perchesadb2da82021-02-25 17:21:50 -0800595our $tracing_logging_tags = qr{(?xi:
596 [=-]*> |
597 <[=-]* |
598 \[ |
599 \] |
600 start |
601 called |
602 entered |
603 entry |
604 enter |
605 in |
606 inside |
607 here |
608 begin |
609 exit |
610 end |
611 done |
612 leave |
613 completed |
614 out |
615 return |
616 [\.\!:\s]*
617)};
618
Aditya Srivastava831242a2020-12-15 20:45:12 -0800619sub edit_distance_min {
620 my (@arr) = @_;
621 my $len = scalar @arr;
622 if ((scalar @arr) < 1) {
623 # if underflow, return
624 return;
625 }
626 my $min = $arr[0];
627 for my $i (0 .. ($len-1)) {
628 if ($arr[$i] < $min) {
629 $min = $arr[$i];
630 }
631 }
632 return $min;
633}
634
635sub get_edit_distance {
636 my ($str1, $str2) = @_;
637 $str1 = lc($str1);
638 $str2 = lc($str2);
639 $str1 =~ s/-//g;
640 $str2 =~ s/-//g;
641 my $len1 = length($str1);
642 my $len2 = length($str2);
643 # two dimensional array storing minimum edit distance
644 my @distance;
645 for my $i (0 .. $len1) {
646 for my $j (0 .. $len2) {
647 if ($i == 0) {
648 $distance[$i][$j] = $j;
649 } elsif ($j == 0) {
650 $distance[$i][$j] = $i;
651 } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
652 $distance[$i][$j] = $distance[$i - 1][$j - 1];
653 } else {
654 my $dist1 = $distance[$i][$j - 1]; #insert distance
655 my $dist2 = $distance[$i - 1][$j]; # remove
656 my $dist3 = $distance[$i - 1][$j - 1]; #replace
657 $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
658 }
659 }
660 }
661 return $distance[$len1][$len2];
662}
663
664sub find_standard_signature {
665 my ($sign_off) = @_;
666 my @standard_signature_tags = (
667 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
668 'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
669 );
670 foreach my $signature (@standard_signature_tags) {
671 return $signature if (get_edit_distance($sign_off, $signature) <= 2);
672 }
673
674 return "";
675}
676
Joe Perches18130872014-08-06 16:11:22 -0700677our @typeListMisordered = (
678 qr{char\s+(?:un)?signed},
679 qr{int\s+(?:(?:un)?signed\s+)?short\s},
680 qr{int\s+short(?:\s+(?:un)?signed)},
681 qr{short\s+int(?:\s+(?:un)?signed)},
682 qr{(?:un)?signed\s+int\s+short},
683 qr{short\s+(?:un)?signed},
684 qr{long\s+int\s+(?:un)?signed},
685 qr{int\s+long\s+(?:un)?signed},
686 qr{long\s+(?:un)?signed\s+int},
687 qr{int\s+(?:un)?signed\s+long},
688 qr{int\s+(?:un)?signed},
689 qr{int\s+long\s+long\s+(?:un)?signed},
690 qr{long\s+long\s+int\s+(?:un)?signed},
691 qr{long\s+long\s+(?:un)?signed\s+int},
692 qr{long\s+long\s+(?:un)?signed},
693 qr{long\s+(?:un)?signed},
694);
695
Andy Whitcroft8905a672007-11-28 16:21:06 -0800696our @typeList = (
697 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700698 qr{(?:(?:un)?signed\s+)?char},
699 qr{(?:(?:un)?signed\s+)?short\s+int},
700 qr{(?:(?:un)?signed\s+)?short},
701 qr{(?:(?:un)?signed\s+)?int},
702 qr{(?:(?:un)?signed\s+)?long\s+int},
703 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
704 qr{(?:(?:un)?signed\s+)?long\s+long},
705 qr{(?:(?:un)?signed\s+)?long},
706 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800707 qr{float},
708 qr{double},
709 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800710 qr{struct\s+$Ident},
711 qr{union\s+$Ident},
712 qr{enum\s+$Ident},
713 qr{${Ident}_t},
714 qr{${Ident}_handler},
715 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700716 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800717);
Joe Perches938224b2016-01-20 14:59:15 -0800718
719our $C90_int_types = qr{(?x:
720 long\s+long\s+int\s+(?:un)?signed|
721 long\s+long\s+(?:un)?signed\s+int|
722 long\s+long\s+(?:un)?signed|
723 (?:(?:un)?signed\s+)?long\s+long\s+int|
724 (?:(?:un)?signed\s+)?long\s+long|
725 int\s+long\s+long\s+(?:un)?signed|
726 int\s+(?:(?:un)?signed\s+)?long\s+long|
727
728 long\s+int\s+(?:un)?signed|
729 long\s+(?:un)?signed\s+int|
730 long\s+(?:un)?signed|
731 (?:(?:un)?signed\s+)?long\s+int|
732 (?:(?:un)?signed\s+)?long|
733 int\s+long\s+(?:un)?signed|
734 int\s+(?:(?:un)?signed\s+)?long|
735
736 int\s+(?:un)?signed|
737 (?:(?:un)?signed\s+)?int
738)};
739
Alex Dowad485ff232015-06-25 15:02:52 -0700740our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700741our @typeListWithAttr = (
742 @typeList,
743 qr{struct\s+$InitAttribute\s+$Ident},
744 qr{union\s+$InitAttribute\s+$Ident},
745);
746
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700747our @modifierList = (
748 qr{fastcall},
749);
Alex Dowad485ff232015-06-25 15:02:52 -0700750our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800751
Joe Perches24358802014-04-03 14:49:13 -0700752our @mode_permission_funcs = (
753 ["module_param", 3],
754 ["module_param_(?:array|named|string)", 4],
755 ["module_param_array_named", 5],
756 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
757 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700758 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
759 ["IIO_DEV_ATTR_[A-Z_]+", 1],
760 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
761 ["SENSOR_TEMPLATE(?:_2|)", 3],
762 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700763);
764
Joe Perches1a3dcf22020-08-11 18:35:16 -0700765my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
766
Joe Perches515a2352014-04-03 14:49:24 -0700767#Create a search pattern for all these functions to speed up a loop below
768our $mode_perms_search = "";
769foreach my $entry (@mode_permission_funcs) {
770 $mode_perms_search .= '|' if ($mode_perms_search ne "");
771 $mode_perms_search .= $entry->[0];
772}
Joe Perches00180462018-02-06 15:38:55 -0800773$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700774
Joe Perches9189c7e2018-09-07 15:26:18 -0700775our %deprecated_apis = (
776 "synchronize_rcu_bh" => "synchronize_rcu",
777 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
778 "call_rcu_bh" => "call_rcu",
779 "rcu_barrier_bh" => "rcu_barrier",
780 "synchronize_sched" => "synchronize_rcu",
781 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
782 "call_rcu_sched" => "call_rcu",
783 "rcu_barrier_sched" => "rcu_barrier",
784 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
785 "cond_synchronize_sched" => "cond_synchronize_rcu",
786);
787
788#Create a search pattern for all these strings to speed up a loop below
789our $deprecated_apis_search = "";
790foreach my $entry (keys %deprecated_apis) {
791 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
792 $deprecated_apis_search .= $entry;
793}
794$deprecated_apis_search = "(?:${deprecated_apis_search})";
795
Joe Perchesb392c642015-04-16 12:44:16 -0700796our $mode_perms_world_writable = qr{
797 S_IWUGO |
798 S_IWOTH |
799 S_IRWXUGO |
800 S_IALLUGO |
801 0[0-7][0-7][2367]
802}x;
803
Joe Perchesf90774e2016-10-11 13:51:47 -0700804our %mode_permission_string_types = (
805 "S_IRWXU" => 0700,
806 "S_IRUSR" => 0400,
807 "S_IWUSR" => 0200,
808 "S_IXUSR" => 0100,
809 "S_IRWXG" => 0070,
810 "S_IRGRP" => 0040,
811 "S_IWGRP" => 0020,
812 "S_IXGRP" => 0010,
813 "S_IRWXO" => 0007,
814 "S_IROTH" => 0004,
815 "S_IWOTH" => 0002,
816 "S_IXOTH" => 0001,
817 "S_IRWXUGO" => 0777,
818 "S_IRUGO" => 0444,
819 "S_IWUGO" => 0222,
820 "S_IXUGO" => 0111,
821);
822
823#Create a search pattern for all these strings to speed up a loop below
824our $mode_perms_string_search = "";
825foreach my $entry (keys %mode_permission_string_types) {
826 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
827 $mode_perms_string_search .= $entry;
828}
Joe Perches00180462018-02-06 15:38:55 -0800829our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
830our $multi_mode_perms_string_search = qr{
831 ${single_mode_perms_string_search}
832 (?:\s*\|\s*${single_mode_perms_string_search})*
833}x;
834
835sub perms_to_octal {
836 my ($string) = @_;
837
838 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
839
840 my $val = "";
841 my $oval = "";
842 my $to = 0;
843 my $curpos = 0;
844 my $lastpos = 0;
845 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
846 $curpos = pos($string);
847 my $match = $2;
848 my $omatch = $1;
849 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
850 $lastpos = $curpos;
851 $to |= $mode_permission_string_types{$match};
852 $val .= '\s*\|\s*' if ($val ne "");
853 $val .= $match;
854 $oval .= $omatch;
855 }
856 $oval =~ s/^\s*\|\s*//;
857 $oval =~ s/\s*\|\s*$//;
858 return sprintf("%04o", $to);
859}
Joe Perchesf90774e2016-10-11 13:51:47 -0700860
Wolfram Sang7840a942010-08-09 17:20:57 -0700861our $allowed_asm_includes = qr{(?x:
862 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700863 memory|
864 time|
865 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700866)};
867# memory.h: ARM has a custom one
868
Kees Cook66b47b42014-10-13 15:51:57 -0700869# Load common spelling mistakes and build regular expression list.
870my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700871my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700872
Joe Perches36061e32014-12-10 15:51:43 -0800873if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800874 while (<$spelling>) {
875 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700876
Joe Perches36061e32014-12-10 15:51:43 -0800877 $line =~ s/\s*\n?$//g;
878 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700879
Joe Perches36061e32014-12-10 15:51:43 -0800880 next if ($line =~ m/^\s*#/);
881 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700882
Joe Perches36061e32014-12-10 15:51:43 -0800883 my ($suspect, $fix) = split(/\|\|/, $line);
884
Joe Perches36061e32014-12-10 15:51:43 -0800885 $spelling_fix{$suspect} = $fix;
886 }
887 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800888} else {
889 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700890}
Kees Cook66b47b42014-10-13 15:51:57 -0700891
Joe Perchesebfd7d62015-04-16 12:44:14 -0700892if ($codespell) {
893 if (open(my $spelling, '<', $codespellfile)) {
894 while (<$spelling>) {
895 my $line = $_;
896
897 $line =~ s/\s*\n?$//g;
898 $line =~ s/^\s*//g;
899
900 next if ($line =~ m/^\s*#/);
901 next if ($line =~ m/^\s*$/);
902 next if ($line =~ m/, disabled/i);
903
904 $line =~ s/,.*$//;
905
906 my ($suspect, $fix) = split(/->/, $line);
907
908 $spelling_fix{$suspect} = $fix;
909 }
910 close($spelling);
911 } else {
912 warn "No codespell typos will be found - file '$codespellfile': $!\n";
913 }
914}
915
916$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
917
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700918sub read_words {
919 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700920
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700921 if (open(my $words, '<', $file)) {
922 while (<$words>) {
923 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700924
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700925 $line =~ s/\s*\n?$//g;
926 $line =~ s/^\s*//g;
927
928 next if ($line =~ m/^\s*#/);
929 next if ($line =~ m/^\s*$/);
930 if ($line =~ /\s/) {
931 print("$file: '$line' invalid - ignored\n");
932 next;
933 }
934
Quentin Monnetced69da12020-08-11 18:35:13 -0700935 $$wordsRef .= '|' if (defined $$wordsRef);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700936 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700937 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700938 close($file);
939 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700940 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700941
942 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700943}
944
Quentin Monnetced69da12020-08-11 18:35:13 -0700945my $const_structs;
946if (show_type("CONST_STRUCT")) {
947 read_words(\$const_structs, $conststructsfile)
948 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
949}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700950
Quentin Monnetced69da12020-08-11 18:35:13 -0700951if (defined($typedefsfile)) {
952 my $typeOtherTypedefs;
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700953 read_words(\$typeOtherTypedefs, $typedefsfile)
954 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
Quentin Monnetced69da12020-08-11 18:35:13 -0700955 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700956}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700957
Andy Whitcroft8905a672007-11-28 16:21:06 -0800958sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700959 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
960 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700961 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700962 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700963 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700964 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700965 (?:$typeTypedefs\b)|
966 (?:${all}\b)
967 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800968 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700969 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800970 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800971 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700972 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700973 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800974 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700975 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800976 }x;
Joe Perches18130872014-08-06 16:11:22 -0700977 $NonptrTypeMisordered = qr{
978 (?:$Modifier\s+|const\s+)*
979 (?:
980 (?:${Misordered}\b)
981 )
982 (?:\s+$Modifier|\s+const)*
983 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700984 $NonptrTypeWithAttr = qr{
985 (?:$Modifier\s+|const\s+)*
986 (?:
987 (?:typeof|__typeof__)\s*\([^\)]*\)|
988 (?:$typeTypedefs\b)|
989 (?:${allWithAttr}\b)
990 )
991 (?:\s+$Modifier|\s+const)*
992 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800993 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700994 $NonptrType
Antonio Borneo7b184962020-04-06 20:11:04 -0700995 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700996 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800997 }x;
Joe Perches18130872014-08-06 16:11:22 -0700998 $TypeMisordered = qr{
999 $NonptrTypeMisordered
Antonio Borneo7b184962020-04-06 20:11:04 -07001000 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Perches18130872014-08-06 16:11:22 -07001001 (?:\s+$Inline|\s+$Modifier)*
1002 }x;
Joe Perches91cb5192014-04-03 14:49:32 -07001003 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -07001004 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -08001005}
1006build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001007
Joe Perches7d2367a2011-07-25 17:13:22 -07001008our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001009
1010# Using $balanced_parens, $LvalOrFunc, or $FuncArg
1011# requires at least perl version v5.10.0
1012# Any use must be runtime checked with $^V
1013
1014our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -07001015our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -08001016our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -07001017
Joe Perchesf8422302014-08-06 16:11:31 -07001018our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -07001019 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -07001020 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -07001021 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -07001022)};
1023
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08001024our %allow_repeated_words = (
1025 add => '',
1026 added => '',
1027 bad => '',
1028 be => '',
1029);
1030
Joe Perches7d2367a2011-07-25 17:13:22 -07001031sub deparenthesize {
1032 my ($string) = @_;
1033 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -07001034
1035 while ($string =~ /^\s*\(.*\)\s*$/) {
1036 $string =~ s@^\s*\(\s*@@;
1037 $string =~ s@\s*\)\s*$@@;
1038 }
1039
Joe Perches7d2367a2011-07-25 17:13:22 -07001040 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -07001041
Joe Perches7d2367a2011-07-25 17:13:22 -07001042 return $string;
1043}
1044
Joe Perches34456862013-07-03 15:05:34 -07001045sub seed_camelcase_file {
1046 my ($file) = @_;
1047
1048 return if (!(-f $file));
1049
1050 local $/;
1051
1052 open(my $include_file, '<', "$file")
1053 or warn "$P: Can't read '$file' $!\n";
1054 my $text = <$include_file>;
1055 close($include_file);
1056
1057 my @lines = split('\n', $text);
1058
1059 foreach my $line (@lines) {
1060 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
1061 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
1062 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -08001063 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
1064 $camelcase{$1} = 1;
1065 } 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 -07001066 $camelcase{$1} = 1;
1067 }
1068 }
1069}
1070
Joe Perchescd28b112019-12-04 16:52:09 -08001071our %maintained_status = ();
1072
Joe Perches85b0ee12016-10-11 13:51:44 -07001073sub is_maintained_obsolete {
1074 my ($filename) = @_;
1075
Jerome Forissierf2c19c22016-12-12 16:46:23 -08001076 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -07001077
Joe Perchescd28b112019-12-04 16:52:09 -08001078 if (!exists($maintained_status{$filename})) {
1079 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
1080 }
Joe Perches85b0ee12016-10-11 13:51:44 -07001081
Joe Perchescd28b112019-12-04 16:52:09 -08001082 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -07001083}
1084
Joe Perches3b6e8ac2018-08-21 21:57:47 -07001085sub is_SPDX_License_valid {
1086 my ($license) = @_;
1087
Guenter Roeckf9363b32021-06-30 18:56:19 -07001088 return 1 if (!$tree || which("python3") eq "" || !(-x "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -07001089
Joe Perches56294112018-08-21 21:58:04 -07001090 my $root_path = abs_path($root);
Guenter Roeckf9363b32021-06-30 18:56:19 -07001091 my $status = `cd "$root_path"; echo "$license" | scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -07001092 return 0 if ($status ne "");
1093 return 1;
1094}
1095
Joe Perches34456862013-07-03 15:05:34 -07001096my $camelcase_seeded = 0;
1097sub seed_camelcase_includes {
1098 return if ($camelcase_seeded);
1099
1100 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -07001101 my $camelcase_cache = "";
1102 my @include_files = ();
1103
1104 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -07001105
Joe Perches0f7f6352020-10-24 16:59:04 -07001106 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -07001107 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -07001108 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -07001109 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -07001110 } else {
Joe Perchesc707a812013-07-08 16:00:43 -07001111 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -07001112 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -07001113 @include_files = split('\n', $files);
1114 foreach my $file (@include_files) {
1115 my $date = POSIX::strftime("%Y%m%d%H%M",
1116 localtime((stat $file)[9]));
1117 $last_mod_date = $date if ($last_mod_date < $date);
1118 }
1119 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -07001120 }
Joe Perchesc707a812013-07-08 16:00:43 -07001121
1122 if ($camelcase_cache ne "" && -f $camelcase_cache) {
1123 open(my $camelcase_file, '<', "$camelcase_cache")
1124 or warn "$P: Can't read '$camelcase_cache' $!\n";
1125 while (<$camelcase_file>) {
1126 chomp;
1127 $camelcase{$_} = 1;
1128 }
1129 close($camelcase_file);
1130
1131 return;
1132 }
1133
Joe Perches0f7f6352020-10-24 16:59:04 -07001134 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -07001135 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -07001136 @include_files = split('\n', $files);
1137 }
1138
Joe Perches34456862013-07-03 15:05:34 -07001139 foreach my $file (@include_files) {
1140 seed_camelcase_file($file);
1141 }
Joe Perches351b2a12013-07-03 15:05:36 -07001142
Joe Perchesc707a812013-07-08 16:00:43 -07001143 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -07001144 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -07001145 open(my $camelcase_file, '>', "$camelcase_cache")
1146 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -07001147 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1148 print $camelcase_file ("$_\n");
1149 }
1150 close($camelcase_file);
1151 }
Joe Perches34456862013-07-03 15:05:34 -07001152}
1153
Joe Perchesf5f61322020-10-15 20:12:12 -07001154sub git_is_single_file {
1155 my ($filename) = @_;
1156
1157 return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1158
1159 my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1160 my $count = $output =~ tr/\n//;
1161 return $count eq 1 && $output =~ m{^${filename}$};
1162}
1163
Joe Perchesd311cd42014-08-06 16:10:57 -07001164sub git_commit_info {
1165 my ($commit, $id, $desc) = @_;
1166
Joe Perches0f7f6352020-10-24 16:59:04 -07001167 return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
Joe Perchesd311cd42014-08-06 16:10:57 -07001168
Joe Perchesdbbf8692019-09-25 16:46:52 -07001169 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -07001170 $output =~ s/^\s*//gm;
1171 my @lines = split("\n", $output);
1172
Joe Perches0d7835f2015-02-13 14:38:35 -08001173 return ($id, $desc) if ($#lines < 0);
1174
Sean Christopherson5a7f4452019-09-25 16:46:49 -07001175 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -07001176# Maybe one day convert this block of bash into something that returns
1177# all matching commit ids, but it's very slow...
1178#
1179# echo "checking commits $1..."
1180# git rev-list --remotes | grep -i "^$1" |
1181# while read line ; do
1182# git log --format='%H %s' -1 $line |
1183# echo "commit $(cut -c 1-12,41-)"
1184# done
Joe Perches4ce9f972021-09-07 19:59:57 -07001185 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ ||
1186 $lines[0] =~ /^fatal: bad object $commit/) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07001187 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -07001188 } else {
1189 $id = substr($lines[0], 0, 12);
1190 $desc = substr($lines[0], 41);
1191 }
1192
1193 return ($id, $desc);
1194}
1195
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001196$chk_signoff = 0 if ($file);
1197
Andy Whitcroft00df3442007-06-08 13:47:06 -07001198my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001199my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001200my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001201my @fixed_inserted = ();
1202my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001203my $fixlinenr = -1;
1204
Du, Changbin4a593c32016-05-20 17:04:16 -07001205# If input is git commits, extract all commits from the commit expressions.
1206# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
Joe Perches0f7f6352020-10-24 16:59:04 -07001207die "$P: No git repository found\n" if ($git && !-e "$gitroot");
Du, Changbin4a593c32016-05-20 17:04:16 -07001208
1209if ($git) {
1210 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001211 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001212 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001213 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1214 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001215 } elsif ($commit_expr =~ m/\.\./) {
1216 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001217 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001218 $git_range = "-1 $commit_expr";
1219 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001220 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001221 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001222 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1223 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001224 my $sha1 = $1;
1225 my $subject = $2;
1226 unshift(@commits, $sha1);
1227 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001228 }
1229 }
1230 die "$P: no git commits after extraction!\n" if (@commits == 0);
1231 @ARGV = @commits;
1232}
1233
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001234my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001235$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001236for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001237 my $FILE;
Joe Perchesf5f61322020-10-15 20:12:12 -07001238 my $is_git_file = git_is_single_file($filename);
1239 my $oldfile = $file;
1240 $file = 1 if ($is_git_file);
Du, Changbin4a593c32016-05-20 17:04:16 -07001241 if ($git) {
1242 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1243 die "$P: $filename: git format-patch failed - $!\n";
1244 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001245 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001246 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001247 } elsif ($filename eq '-') {
1248 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001249 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001250 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001251 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001252 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001253 if ($filename eq '-') {
1254 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001255 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001256 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001257 } else {
1258 $vname = $filename;
1259 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001260 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001261 chomp;
1262 push(@rawlines, $_);
Geert Uytterhoevenc7f574d2020-06-04 16:50:43 -07001263 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001264 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001265 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001266
1267 if ($#ARGV > 0 && $quiet == 0) {
1268 print '-' x length($vname) . "\n";
1269 print "$vname\n";
1270 print '-' x length($vname) . "\n";
1271 }
1272
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001273 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001274 $exit = 1;
1275 }
1276 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001277 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001278 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001279 @fixed_inserted = ();
1280 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001281 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001282 @modifierListFile = ();
1283 @typeListFile = ();
1284 build_types();
Joe Perchesf5f61322020-10-15 20:12:12 -07001285 $file = $oldfile if ($is_git_file);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001286}
1287
Joe Perchesd8469f12015-06-25 15:03:00 -07001288if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001289 hash_show_words(\%use_type, "Used");
1290 hash_show_words(\%ignore_type, "Ignored");
1291
Joe Perches5b579802018-08-21 21:57:33 -07001292 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001293 print << "EOM"
1294
1295NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001296 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001297EOM
1298 }
1299 if ($exit) {
1300 print << "EOM"
1301
1302NOTE: If any of the errors are false positives, please report
1303 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1304EOM
1305 }
1306}
1307
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001308exit($exit);
1309
1310sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001311 my ($root) = @_;
1312
1313 my @tree_check = (
1314 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1315 "README", "Documentation", "arch", "include", "drivers",
1316 "fs", "init", "ipc", "kernel", "lib", "scripts",
1317 );
1318
1319 foreach my $check (@tree_check) {
1320 if (! -e $root . '/' . $check) {
1321 return 0;
1322 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001323 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001324 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001325}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001326
Joe Perches20112472011-07-25 17:13:23 -07001327sub parse_email {
1328 my ($formatted_email) = @_;
1329
1330 my $name = "";
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001331 my $quoted = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001332 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001333 my $address = "";
1334 my $comment = "";
1335
1336 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1337 $name = $1;
1338 $address = $2;
1339 $comment = $3 if defined $3;
1340 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1341 $address = $1;
1342 $comment = $2 if defined $2;
1343 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1344 $address = $1;
1345 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001346 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001347 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001348 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001349 $name =~ s/^\"|\"$//g;
1350 # If there's a name left after stripping spaces and
1351 # leading quotes, and the address doesn't have both
1352 # leading and trailing angle brackets, the address
1353 # is invalid. ie:
1354 # "joe smith joe@smith.com" bad
1355 # "joe smith <joe@smith.com" bad
1356 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1357 $name = "";
1358 $address = "";
1359 $comment = "";
1360 }
1361 }
1362
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001363 # Extract comments from names excluding quoted parts
1364 # "John D. (Doe)" - Do not extract
1365 if ($name =~ s/\"(.+)\"//) {
1366 $quoted = $1;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001367 }
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001368 while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1369 $name_comment .= trim($1);
1370 }
1371 $name =~ s/^[ \"]+|[ \"]+$//g;
1372 $name = trim("$quoted $name");
1373
Joe Perches3705ce52013-07-03 15:05:31 -07001374 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001375 $address =~ s/^\<|\>$//g;
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001376 $comment = trim($comment);
Joe Perches20112472011-07-25 17:13:23 -07001377
1378 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1379 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1380 $name = "\"$name\"";
1381 }
1382
Joe Perchesdfa05c22020-04-06 20:10:48 -07001383 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001384}
1385
1386sub format_email {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001387 my ($name, $name_comment, $address, $comment) = @_;
Joe Perches20112472011-07-25 17:13:23 -07001388
1389 my $formatted_email;
1390
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001391 $name =~ s/^[ \"]+|[ \"]+$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001392 $address = trim($address);
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001393 $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
Joe Perches20112472011-07-25 17:13:23 -07001394
1395 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1396 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1397 $name = "\"$name\"";
1398 }
1399
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001400 $name_comment = trim($name_comment);
1401 $name_comment = " $name_comment" if ($name_comment ne "");
1402 $comment = trim($comment);
1403 $comment = " $comment" if ($comment ne "");
1404
Joe Perches20112472011-07-25 17:13:23 -07001405 if ("$name" eq "") {
1406 $formatted_email = "$address";
1407 } else {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001408 $formatted_email = "$name$name_comment <$address>";
Joe Perches20112472011-07-25 17:13:23 -07001409 }
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001410 $formatted_email .= "$comment";
Joe Perches20112472011-07-25 17:13:23 -07001411 return $formatted_email;
1412}
1413
Joe Perchesdfa05c22020-04-06 20:10:48 -07001414sub reformat_email {
1415 my ($email) = @_;
1416
1417 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001418 return format_email($email_name, $name_comment, $email_address, $comment);
Joe Perchesdfa05c22020-04-06 20:10:48 -07001419}
1420
1421sub same_email_addresses {
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001422 my ($email1, $email2) = @_;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001423
1424 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1425 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1426
1427 return $email1_name eq $email2_name &&
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001428 $email1_address eq $email2_address &&
1429 $name1_comment eq $name2_comment &&
1430 $comment1 eq $comment2;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001431}
1432
Joe Perchesd311cd42014-08-06 16:10:57 -07001433sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001434 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001435
Joe Perchesbd474ca2014-08-06 16:11:10 -07001436 foreach my $path (split(/:/, $ENV{PATH})) {
1437 if (-e "$path/$bin") {
1438 return "$path/$bin";
1439 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001440 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001441
Joe Perchesbd474ca2014-08-06 16:11:10 -07001442 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001443}
1444
Joe Perches000d1cc12011-07-25 17:13:25 -07001445sub which_conf {
1446 my ($conf) = @_;
1447
1448 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1449 if (-e "$path/$conf") {
1450 return "$path/$conf";
1451 }
1452 }
1453
1454 return "";
1455}
1456
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001457sub expand_tabs {
1458 my ($str) = @_;
1459
1460 my $res = '';
1461 my $n = 0;
1462 for my $c (split(//, $str)) {
1463 if ($c eq "\t") {
1464 $res .= ' ';
1465 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001466 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001467 $res .= ' ';
1468 }
1469 next;
1470 }
1471 $res .= $c;
1472 $n++;
1473 }
1474
1475 return $res;
1476}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001477sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001478 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001479 return $res;
1480}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001481
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001482sub line_stats {
1483 my ($line) = @_;
1484
1485 # Drop the diff line leader and expand tabs
1486 $line =~ s/^.//;
1487 $line = expand_tabs($line);
1488
1489 # Pick the indent from the front of the line.
1490 my ($white) = ($line =~ /^(\s*)/);
1491
1492 return (length($line), length($white));
1493}
1494
Andy Whitcroft773647a2008-03-28 14:15:58 -07001495my $sanitise_quote = '';
1496
1497sub sanitise_line_reset {
1498 my ($in_comment) = @_;
1499
1500 if ($in_comment) {
1501 $sanitise_quote = '*/';
1502 } else {
1503 $sanitise_quote = '';
1504 }
1505}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001506sub sanitise_line {
1507 my ($line) = @_;
1508
1509 my $res = '';
1510 my $l = '';
1511
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001512 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001513 my $off = 0;
1514 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001515
Andy Whitcroft773647a2008-03-28 14:15:58 -07001516 # Always copy over the diff marker.
1517 $res = substr($line, 0, 1);
1518
1519 for ($off = 1; $off < length($line); $off++) {
1520 $c = substr($line, $off, 1);
1521
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001522 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001523 # and end, all to $;.
1524 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1525 $sanitise_quote = '*/';
1526
1527 substr($res, $off, 2, "$;$;");
1528 $off++;
1529 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001530 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001531 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001532 $sanitise_quote = '';
1533 substr($res, $off, 2, "$;$;");
1534 $off++;
1535 next;
1536 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001537 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1538 $sanitise_quote = '//';
1539
1540 substr($res, $off, 2, $sanitise_quote);
1541 $off++;
1542 next;
1543 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001544
1545 # A \ in a string means ignore the next character.
1546 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1547 $c eq "\\") {
1548 substr($res, $off, 2, 'XX');
1549 $off++;
1550 next;
1551 }
1552 # Regular quotes.
1553 if ($c eq "'" || $c eq '"') {
1554 if ($sanitise_quote eq '') {
1555 $sanitise_quote = $c;
1556
1557 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001558 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001559 } elsif ($sanitise_quote eq $c) {
1560 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001561 }
1562 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001563
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001564 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001565 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1566 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001567 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1568 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001569 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1570 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001571 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001572 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001573 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001574 }
1575
Daniel Walker113f04a2009-09-21 17:04:35 -07001576 if ($sanitise_quote eq '//') {
1577 $sanitise_quote = '';
1578 }
1579
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001580 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001581 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001582 my $clean = 'X' x length($1);
1583 $res =~ s@\<.*\>@<$clean>@;
1584
1585 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001586 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001587 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001588 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001589 }
1590
Joe Perchesdadf6802016-08-02 14:04:33 -07001591 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1592 my $match = $1;
1593 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1594 }
1595
Andy Whitcroft00df3442007-06-08 13:47:06 -07001596 return $res;
1597}
1598
Joe Perchesa6962d72013-04-29 16:18:13 -07001599sub get_quoted_string {
1600 my ($line, $rawline) = @_;
1601
Joe Perches478b1792018-04-10 16:33:34 -07001602 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001603 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001604 return substr($rawline, $-[0], $+[0] - $-[0]);
1605}
1606
Andy Whitcroft8905a672007-11-28 16:21:06 -08001607sub ctx_statement_block {
1608 my ($linenr, $remain, $off) = @_;
1609 my $line = $linenr - 1;
1610 my $blk = '';
1611 my $soff = $off;
1612 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001613 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001614
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001615 my $loff = 0;
1616
Andy Whitcroft8905a672007-11-28 16:21:06 -08001617 my $type = '';
1618 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001619 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001620 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001621 my $c;
1622 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001623
1624 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001625 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001626 @stack = (['', 0]) if ($#stack == -1);
1627
Andy Whitcroft773647a2008-03-28 14:15:58 -07001628 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001629 # If we are about to drop off the end, pull in more
1630 # context.
1631 if ($off >= $len) {
1632 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001633 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001634 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001635 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001636 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001637 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001638 $len = length($blk);
1639 $line++;
1640 last;
1641 }
1642 # Bail if there is no further context.
1643 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001644 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001645 last;
1646 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001647 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1648 $level++;
1649 $type = '#';
1650 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001651 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001652 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001653 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001654 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001655
Andy Whitcroft773647a2008-03-28 14:15:58 -07001656 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001657
1658 # Handle nested #if/#else.
1659 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1660 push(@stack, [ $type, $level ]);
1661 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1662 ($type, $level) = @{$stack[$#stack - 1]};
1663 } elsif ($remainder =~ /^#\s*endif\b/) {
1664 ($type, $level) = @{pop(@stack)};
1665 }
1666
Andy Whitcroft8905a672007-11-28 16:21:06 -08001667 # Statement ends at the ';' or a close '}' at the
1668 # outermost level.
1669 if ($level == 0 && $c eq ';') {
1670 last;
1671 }
1672
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001673 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001674 if ($level == 0 && $coff_set == 0 &&
1675 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1676 $remainder =~ /^(else)(?:\s|{)/ &&
1677 $remainder !~ /^else\s+if\b/) {
1678 $coff = $off + length($1) - 1;
1679 $coff_set = 1;
1680 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1681 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001682 }
1683
Andy Whitcroft8905a672007-11-28 16:21:06 -08001684 if (($type eq '' || $type eq '(') && $c eq '(') {
1685 $level++;
1686 $type = '(';
1687 }
1688 if ($type eq '(' && $c eq ')') {
1689 $level--;
1690 $type = ($level != 0)? '(' : '';
1691
1692 if ($level == 0 && $coff < $soff) {
1693 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001694 $coff_set = 1;
1695 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001696 }
1697 }
1698 if (($type eq '' || $type eq '{') && $c eq '{') {
1699 $level++;
1700 $type = '{';
1701 }
1702 if ($type eq '{' && $c eq '}') {
1703 $level--;
1704 $type = ($level != 0)? '{' : '';
1705
1706 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001707 if (substr($blk, $off + 1, 1) eq ';') {
1708 $off++;
1709 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001710 last;
1711 }
1712 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001713 # Preprocessor commands end at the newline unless escaped.
1714 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1715 $level--;
1716 $type = '';
1717 $off++;
1718 last;
1719 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001720 $off++;
1721 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001722 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001723 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001724 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001725 $line++;
1726 $remain--;
1727 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001728
1729 my $statement = substr($blk, $soff, $off - $soff + 1);
1730 my $condition = substr($blk, $soff, $coff - $soff + 1);
1731
1732 #warn "STATEMENT<$statement>\n";
1733 #warn "CONDITION<$condition>\n";
1734
Andy Whitcroft773647a2008-03-28 14:15:58 -07001735 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001736
1737 return ($statement, $condition,
1738 $line, $remain + 1, $off - $loff + 1, $level);
1739}
1740
Andy Whitcroftcf655042008-03-04 14:28:20 -08001741sub statement_lines {
1742 my ($stmt) = @_;
1743
1744 # Strip the diff line prefixes and rip blank lines at start and end.
1745 $stmt =~ s/(^|\n)./$1/g;
1746 $stmt =~ s/^\s*//;
1747 $stmt =~ s/\s*$//;
1748
1749 my @stmt_lines = ($stmt =~ /\n/g);
1750
1751 return $#stmt_lines + 2;
1752}
1753
1754sub statement_rawlines {
1755 my ($stmt) = @_;
1756
1757 my @stmt_lines = ($stmt =~ /\n/g);
1758
1759 return $#stmt_lines + 2;
1760}
1761
1762sub statement_block_size {
1763 my ($stmt) = @_;
1764
1765 $stmt =~ s/(^|\n)./$1/g;
1766 $stmt =~ s/^\s*{//;
1767 $stmt =~ s/}\s*$//;
1768 $stmt =~ s/^\s*//;
1769 $stmt =~ s/\s*$//;
1770
1771 my @stmt_lines = ($stmt =~ /\n/g);
1772 my @stmt_statements = ($stmt =~ /;/g);
1773
1774 my $stmt_lines = $#stmt_lines + 2;
1775 my $stmt_statements = $#stmt_statements + 1;
1776
1777 if ($stmt_lines > $stmt_statements) {
1778 return $stmt_lines;
1779 } else {
1780 return $stmt_statements;
1781 }
1782}
1783
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001784sub ctx_statement_full {
1785 my ($linenr, $remain, $off) = @_;
1786 my ($statement, $condition, $level);
1787
1788 my (@chunks);
1789
Andy Whitcroftcf655042008-03-04 14:28:20 -08001790 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001791 ($statement, $condition, $linenr, $remain, $off, $level) =
1792 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001793 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001794 push(@chunks, [ $condition, $statement ]);
1795 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1796 return ($level, $linenr, @chunks);
1797 }
1798
1799 # Pull in the following conditional/block pairs and see if they
1800 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001801 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001802 ($statement, $condition, $linenr, $remain, $off, $level) =
1803 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001804 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001805 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001806 #print "C: push\n";
1807 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001808 }
1809
1810 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001811}
1812
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001813sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001814 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001815 my $line;
1816 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001817 my $blk = '';
1818 my @o;
1819 my @c;
1820 my @res = ();
1821
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001822 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001823 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001824 for ($line = $start; $remain > 0; $line++) {
1825 next if ($rawlines[$line] =~ /^-/);
1826 $remain--;
1827
1828 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001829
1830 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001831 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001832 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001833 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001834 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001835 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001836 $level = pop(@stack);
1837 }
1838
Andy Whitcroft01464f32010-10-26 14:23:19 -07001839 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001840 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1841 if ($off > 0) {
1842 $off--;
1843 next;
1844 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001845
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001846 if ($c eq $close && $level > 0) {
1847 $level--;
1848 last if ($level == 0);
1849 } elsif ($c eq $open) {
1850 $level++;
1851 }
1852 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001853
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001854 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001855 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001856 }
1857
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001858 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001859 }
1860
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001861 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001862}
1863sub ctx_block_outer {
1864 my ($linenr, $remain) = @_;
1865
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001866 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1867 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001868}
1869sub ctx_block {
1870 my ($linenr, $remain) = @_;
1871
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001872 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1873 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001874}
1875sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001876 my ($linenr, $remain, $off) = @_;
1877
1878 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1879 return @r;
1880}
1881sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001882 my ($linenr, $remain) = @_;
1883
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001884 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001885}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001886sub ctx_statement_level {
1887 my ($linenr, $remain, $off) = @_;
1888
1889 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1890}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001891
1892sub ctx_locate_comment {
1893 my ($first_line, $end_line) = @_;
1894
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001895 # If c99 comment on the current line, or the line before or after
1896 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1897 return $current_comment if (defined $current_comment);
1898 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1899 return $current_comment if (defined $current_comment);
1900 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1901 return $current_comment if (defined $current_comment);
1902
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001903 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001904 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001905 return $current_comment if (defined $current_comment);
1906
1907 # Look through the context and try and figure out if there is a
1908 # comment.
1909 my $in_comment = 0;
1910 $current_comment = '';
1911 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001912 my $line = $rawlines[$linenr - 1];
1913 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001914 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1915 $in_comment = 1;
1916 }
1917 if ($line =~ m@/\*@) {
1918 $in_comment = 1;
1919 }
1920 if (!$in_comment && $current_comment ne '') {
1921 $current_comment = '';
1922 }
1923 $current_comment .= $line . "\n" if ($in_comment);
1924 if ($line =~ m@\*/@) {
1925 $in_comment = 0;
1926 }
1927 }
1928
1929 chomp($current_comment);
1930 return($current_comment);
1931}
1932sub ctx_has_comment {
1933 my ($first_line, $end_line) = @_;
1934 my $cmt = ctx_locate_comment($first_line, $end_line);
1935
Andy Whitcroft00df3442007-06-08 13:47:06 -07001936 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001937 ##print "CMMT: $cmt\n";
1938
1939 return ($cmt ne '');
1940}
1941
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001942sub raw_line {
1943 my ($linenr, $cnt) = @_;
1944
1945 my $offset = $linenr - 1;
1946 $cnt++;
1947
1948 my $line;
1949 while ($cnt) {
1950 $line = $rawlines[$offset++];
1951 next if (defined($line) && $line =~ /^-/);
1952 $cnt--;
1953 }
1954
1955 return $line;
1956}
1957
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001958sub get_stat_real {
1959 my ($linenr, $lc) = @_;
1960
1961 my $stat_real = raw_line($linenr, 0);
1962 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1963 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1964 }
1965
1966 return $stat_real;
1967}
1968
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001969sub get_stat_here {
1970 my ($linenr, $cnt, $here) = @_;
1971
1972 my $herectx = $here . "\n";
1973 for (my $n = 0; $n < $cnt; $n++) {
1974 $herectx .= raw_line($linenr, $n) . "\n";
1975 }
1976
1977 return $herectx;
1978}
1979
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001980sub cat_vet {
1981 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001982 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001983
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001984 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001985 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1986 $res .= $1;
1987 if ($2 ne '') {
1988 $coded = sprintf("^%c", unpack('C', $2) + 64);
1989 $res .= $coded;
1990 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001991 }
1992 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001993
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001994 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001995}
1996
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001997my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001998my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001999my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002000my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002001
2002sub annotate_reset {
2003 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002004 $av_pending = '_';
2005 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002006 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002007}
2008
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002009sub annotate_values {
2010 my ($stream, $type) = @_;
2011
2012 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002013 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002014 my $cur = $stream;
2015
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002016 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002017
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002018 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002019 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002020 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002021 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002022 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002023 print "WS($1)\n" if ($dbg_values > 1);
2024 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002025 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002026 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002027 }
2028
Florian Micklerc023e4732011-01-12 16:59:58 -08002029 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07002030 print "CAST($1)\n" if ($dbg_values > 1);
2031 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08002032 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07002033
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07002034 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002035 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002036 $type = 'T';
2037
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07002038 } elsif ($cur =~ /^($Modifier)\s*/) {
2039 print "MODIFIER($1)\n" if ($dbg_values > 1);
2040 $type = 'T';
2041
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002042 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002043 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002044 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002045 push(@av_paren_type, $type);
2046 if ($2 ne '') {
2047 $av_pending = 'N';
2048 }
2049 $type = 'E';
2050
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002051 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002052 print "UNDEF($1)\n" if ($dbg_values > 1);
2053 $av_preprocessor = 1;
2054 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002055
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002056 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002057 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002058 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002059
2060 push(@av_paren_type, $type);
2061 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002062 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08002063
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002064 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002065 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
2066 $av_preprocessor = 1;
2067
2068 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
2069
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002070 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08002071
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002072 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002073 print "PRE_END($1)\n" if ($dbg_values > 1);
2074
2075 $av_preprocessor = 1;
2076
2077 # Assume all arms of the conditional end as this
2078 # one does, and continue as if the #endif was not here.
2079 pop(@av_paren_type);
2080 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002081 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002082
2083 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002084 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002085
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002086 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
2087 print "ATTR($1)\n" if ($dbg_values > 1);
2088 $av_pending = $type;
2089 $type = 'N';
2090
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002091 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002092 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002093 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002094 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002095 }
2096 $type = 'N';
2097
Andy Whitcroft14b111c2008-10-15 22:02:16 -07002098 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002099 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07002100 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002101 $type = 'N';
2102
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002103 } elsif ($cur =~/^(case)/o) {
2104 print "CASE($1)\n" if ($dbg_values > 1);
2105 $av_pend_colon = 'C';
2106 $type = 'N';
2107
Andy Whitcroft14b111c2008-10-15 22:02:16 -07002108 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002109 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002110 $type = 'N';
2111
2112 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002113 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002114 push(@av_paren_type, $av_pending);
2115 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002116 $type = 'N';
2117
2118 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002119 my $new_type = pop(@av_paren_type);
2120 if ($new_type ne '_') {
2121 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002122 print "PAREN('$1') -> $type\n"
2123 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002124 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002125 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002126 }
2127
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07002128 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002129 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07002130 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08002131 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002132
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002133 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
2134 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002135 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002136 } elsif ($type eq 'E') {
2137 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002138 }
2139 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
2140 $type = 'V';
2141
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002142 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002143 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002144 $type = 'V';
2145
2146 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002147 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002148 $type = 'N';
2149
Andy Whitcroftcf655042008-03-04 14:28:20 -08002150 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002151 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002152 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002153 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002154
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002155 } elsif ($cur =~/^(,)/) {
2156 print "COMMA($1)\n" if ($dbg_values > 1);
2157 $type = 'C';
2158
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002159 } elsif ($cur =~ /^(\?)/o) {
2160 print "QUESTION($1)\n" if ($dbg_values > 1);
2161 $type = 'N';
2162
2163 } elsif ($cur =~ /^(:)/o) {
2164 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
2165
2166 substr($var, length($res), 1, $av_pend_colon);
2167 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
2168 $type = 'E';
2169 } else {
2170 $type = 'N';
2171 }
2172 $av_pend_colon = 'O';
2173
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002174 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002175 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002176 $type = 'N';
2177
Andy Whitcroft0d413862008-10-15 22:02:16 -07002178 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002179 my $variant;
2180
2181 print "OPV($1)\n" if ($dbg_values > 1);
2182 if ($type eq 'V') {
2183 $variant = 'B';
2184 } else {
2185 $variant = 'U';
2186 }
2187
2188 substr($var, length($res), 1, $variant);
2189 $type = 'N';
2190
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002191 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002192 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002193 if ($1 ne '++' && $1 ne '--') {
2194 $type = 'N';
2195 }
2196
2197 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002198 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002199 }
2200 if (defined $1) {
2201 $cur = substr($cur, length($1));
2202 $res .= $type x length($1);
2203 }
2204 }
2205
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002206 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002207}
2208
Andy Whitcroft8905a672007-11-28 16:21:06 -08002209sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002210 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002211 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002212 ^(?:
2213 $Modifier|
2214 $Storage|
2215 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002216 DEFINE_\S+
2217 )$|
2218 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002219 goto|
2220 return|
2221 case|
2222 else|
2223 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002224 do|
2225 \#|
2226 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002227 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002228 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002229 )}x;
2230 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2231 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002232 # Check for modifiers.
2233 $possible =~ s/\s*$Storage\s*//g;
2234 $possible =~ s/\s*$Sparse\s*//g;
2235 if ($possible =~ /^\s*$/) {
2236
2237 } elsif ($possible =~ /\s/) {
2238 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002239 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002240 if ($modifier !~ $notPermitted) {
2241 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002242 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002243 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002244 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002245
2246 } else {
2247 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002248 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002249 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002250 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002251 } else {
2252 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002253 }
2254}
2255
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002256my $prefix = '';
2257
Joe Perches000d1cc12011-07-25 17:13:25 -07002258sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002259 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002260
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002261 $type =~ tr/[a-z]/[A-Z]/;
2262
Joe Perchescbec18a2014-04-03 14:49:19 -07002263 return defined $use_type{$type} if (scalar keys %use_type > 0);
2264
2265 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002266}
2267
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002268sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002269 my ($level, $type, $msg) = @_;
2270
2271 if (!show_type($type) ||
2272 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002273 return 0;
2274 }
Joe Perches57230292015-06-25 15:03:03 -07002275 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002276 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002277 if ($level eq 'ERROR') {
2278 $output .= RED;
2279 } elsif ($level eq 'WARNING') {
2280 $output .= YELLOW;
2281 } else {
2282 $output .= GREEN;
2283 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002284 }
Joe Perches57230292015-06-25 15:03:03 -07002285 $output .= $prefix . $level . ':';
2286 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002287 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002288 $output .= "$type:";
2289 }
John Brooks737c0762017-07-10 15:52:24 -07002290 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002291 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002292
2293 if ($showfile) {
2294 my @lines = split("\n", $output, -1);
2295 splice(@lines, 1, 1);
2296 $output = join("\n", @lines);
2297 }
Dwaipayan Ray52178ce2021-02-26 15:08:26 +05302298
2299 if ($terse) {
2300 $output = (split('\n', $output))[0] . "\n";
2301 }
2302
2303 if ($verbose && exists($verbose_messages{$type}) &&
2304 !exists($verbose_emitted{$type})) {
2305 $output .= $verbose_messages{$type} . "\n\n";
2306 $verbose_emitted{$type} = 1;
2307 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002308
Joe Perches57230292015-06-25 15:03:03 -07002309 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002310
2311 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002312}
Joe Perchescbec18a2014-04-03 14:49:19 -07002313
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002314sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002315 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002316}
Joe Perches000d1cc12011-07-25 17:13:25 -07002317
Joe Perchesd752fcc2014-08-06 16:11:05 -07002318sub fixup_current_range {
2319 my ($lineRef, $offset, $length) = @_;
2320
2321 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2322 my $o = $1;
2323 my $l = $2;
2324 my $no = $o + $offset;
2325 my $nl = $l + $length;
2326 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2327 }
2328}
2329
2330sub fix_inserted_deleted_lines {
2331 my ($linesRef, $insertedRef, $deletedRef) = @_;
2332
2333 my $range_last_linenr = 0;
2334 my $delta_offset = 0;
2335
2336 my $old_linenr = 0;
2337 my $new_linenr = 0;
2338
2339 my $next_insert = 0;
2340 my $next_delete = 0;
2341
2342 my @lines = ();
2343
2344 my $inserted = @{$insertedRef}[$next_insert++];
2345 my $deleted = @{$deletedRef}[$next_delete++];
2346
2347 foreach my $old_line (@{$linesRef}) {
2348 my $save_line = 1;
2349 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002350 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002351 $delta_offset = 0;
2352 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2353 $range_last_linenr = $new_linenr;
2354 fixup_current_range(\$line, $delta_offset, 0);
2355 }
2356
2357 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2358 $deleted = @{$deletedRef}[$next_delete++];
2359 $save_line = 0;
2360 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2361 }
2362
2363 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2364 push(@lines, ${$inserted}{'LINE'});
2365 $inserted = @{$insertedRef}[$next_insert++];
2366 $new_linenr++;
2367 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2368 }
2369
2370 if ($save_line) {
2371 push(@lines, $line);
2372 $new_linenr++;
2373 }
2374
2375 $old_linenr++;
2376 }
2377
2378 return @lines;
2379}
2380
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002381sub fix_insert_line {
2382 my ($linenr, $line) = @_;
2383
2384 my $inserted = {
2385 LINENR => $linenr,
2386 LINE => $line,
2387 };
2388 push(@fixed_inserted, $inserted);
2389}
2390
2391sub fix_delete_line {
2392 my ($linenr, $line) = @_;
2393
2394 my $deleted = {
2395 LINENR => $linenr,
2396 LINE => $line,
2397 };
2398
2399 push(@fixed_deleted, $deleted);
2400}
2401
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002402sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002403 my ($type, $msg) = @_;
2404
2405 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002406 our $clean = 0;
2407 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002408 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002409 }
Joe Perches3705ce52013-07-03 15:05:31 -07002410 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002411}
2412sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002413 my ($type, $msg) = @_;
2414
2415 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002416 our $clean = 0;
2417 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002418 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002419 }
Joe Perches3705ce52013-07-03 15:05:31 -07002420 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002421}
2422sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002423 my ($type, $msg) = @_;
2424
2425 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002426 our $clean = 0;
2427 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002428 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002429 }
Joe Perches3705ce52013-07-03 15:05:31 -07002430 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002431}
2432
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002433sub check_absolute_file {
2434 my ($absolute, $herecurr) = @_;
2435 my $file = $absolute;
2436
2437 ##print "absolute<$absolute>\n";
2438
2439 # See if any suffix of this path is a path within the tree.
2440 while ($file =~ s@^[^/]*/@@) {
2441 if (-f "$root/$file") {
2442 ##print "file<$file>\n";
2443 last;
2444 }
2445 }
2446 if (! -f _) {
2447 return 0;
2448 }
2449
2450 # It is, so see if the prefix is acceptable.
2451 my $prefix = $absolute;
2452 substr($prefix, -length($file)) = '';
2453
2454 ##print "prefix<$prefix>\n";
2455 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002456 WARN("USE_RELATIVE_PATH",
2457 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002458 }
2459}
2460
Joe Perches3705ce52013-07-03 15:05:31 -07002461sub trim {
2462 my ($string) = @_;
2463
Joe Perchesb34c6482013-09-11 14:24:01 -07002464 $string =~ s/^\s+|\s+$//g;
2465
2466 return $string;
2467}
2468
2469sub ltrim {
2470 my ($string) = @_;
2471
2472 $string =~ s/^\s+//;
2473
2474 return $string;
2475}
2476
2477sub rtrim {
2478 my ($string) = @_;
2479
2480 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002481
2482 return $string;
2483}
2484
Joe Perches52ea8502013-11-12 15:10:09 -08002485sub string_find_replace {
2486 my ($string, $find, $replace) = @_;
2487
2488 $string =~ s/$find/$replace/g;
2489
2490 return $string;
2491}
2492
Joe Perches3705ce52013-07-03 15:05:31 -07002493sub tabify {
2494 my ($leading) = @_;
2495
Antonio Borneo713a09d2020-04-06 20:11:07 -07002496 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002497 my $max_spaces_before_tab = $source_indent - 1;
2498 my $spaces_to_tab = " " x $source_indent;
2499
2500 #convert leading spaces to tabs
2501 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2502 #Remove spaces before a tab
2503 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2504
2505 return "$leading";
2506}
2507
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002508sub pos_last_openparen {
2509 my ($line) = @_;
2510
2511 my $pos = 0;
2512
2513 my $opens = $line =~ tr/\(/\(/;
2514 my $closes = $line =~ tr/\)/\)/;
2515
2516 my $last_openparen = 0;
2517
2518 if (($opens == 0) || ($closes >= $opens)) {
2519 return -1;
2520 }
2521
2522 my $len = length($line);
2523
2524 for ($pos = 0; $pos < $len; $pos++) {
2525 my $string = substr($line, $pos);
2526 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2527 $pos += length($1) - 1;
2528 } elsif (substr($line, $pos, 1) eq '(') {
2529 $last_openparen = $pos;
2530 } elsif (index($string, '(') == -1) {
2531 last;
2532 }
2533 }
2534
Joe Perches91cb5192014-04-03 14:49:32 -07002535 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002536}
2537
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002538sub get_raw_comment {
2539 my ($line, $rawline) = @_;
2540 my $comment = '';
2541
2542 for my $i (0 .. (length($line) - 1)) {
2543 if (substr($line, $i, 1) eq "$;") {
2544 $comment .= substr($rawline, $i, 1);
2545 }
2546 }
2547
2548 return $comment;
2549}
2550
Song Liu5b8f82e2021-02-25 17:22:08 -08002551sub exclude_global_initialisers {
2552 my ($realfile) = @_;
2553
2554 # Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
2555 return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ ||
2556 $realfile =~ m@^samples/bpf/.*_kern\.c$@ ||
2557 $realfile =~ m@/bpf/.*\.bpf\.c$@;
2558}
2559
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002560sub process {
2561 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002562
2563 my $linenr=0;
2564 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002565 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002566 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002567 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002568
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002569 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002570 my $indent;
2571 my $previndent=0;
2572 my $stashindent=0;
2573
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002574 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002575 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002576 my $author = '';
2577 my $authorsignoff = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002578 my $author_sob = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002579 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002580 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002581 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002582 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002583 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002584 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002585 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002586 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002587 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002588 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002589 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002590 my $non_utf8_charset = 0;
2591
Joe Perches4ce9f972021-09-07 19:59:57 -07002592 my $last_git_commit_id_linenr = -1;
2593
Joe Perches365dd4e2014-08-06 16:10:42 -07002594 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002595 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002596
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002597 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002598 our $cnt_lines = 0;
2599 our $cnt_error = 0;
2600 our $cnt_warn = 0;
2601 our $cnt_chk = 0;
2602
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002603 # Trace the real file/line as we go.
2604 my $realfile = '';
2605 my $realline = 0;
2606 my $realcnt = 0;
2607 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002608 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002609 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002610 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002611 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002612 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002613
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002614 my $prev_values = 'E';
2615
2616 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002617 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002618 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002619 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002620 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002621
Joe Perches7e51f192013-09-11 14:23:57 -07002622 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002623
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002624 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002625 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002626 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002627 my @setup_docs = ();
2628 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002629
Joe Perchesd8b07712013-11-12 15:10:06 -08002630 my $camelcase_file_seeded = 0;
2631
Rob Herring9f3a8992018-04-10 16:33:13 -07002632 my $checklicenseline = 1;
2633
Andy Whitcroft773647a2008-03-28 14:15:58 -07002634 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002635 my $line;
2636 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002637 $linenr++;
2638 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002639
Joe Perches3705ce52013-07-03 15:05:31 -07002640 push(@fixed, $rawline) if ($fix);
2641
Andy Whitcroft773647a2008-03-28 14:15:58 -07002642 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002643 $setup_docs = 0;
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07002644 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002645 $setup_docs = 1;
2646 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002647 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002648 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002649 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002650 $realline=$1-1;
2651 if (defined $2) {
2652 $realcnt=$3+1;
2653 } else {
2654 $realcnt=1+1;
2655 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002656 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002657
2658 # Guestimate if this is a continuing comment. Run
2659 # the context looking for a comment "edge". If this
2660 # edge is a close comment then we must be in a comment
2661 # at context start.
2662 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002663 my $cnt = $realcnt;
2664 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2665 next if (defined $rawlines[$ln - 1] &&
2666 $rawlines[$ln - 1] =~ /^-/);
2667 $cnt--;
2668 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002669 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002670 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2671 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2672 ($edge) = $1;
2673 last;
2674 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002675 }
2676 if (defined $edge && $edge eq '*/') {
2677 $in_comment = 1;
2678 }
2679
2680 # Guestimate if this is a continuing comment. If this
2681 # is the start of a diff block and this line starts
2682 # ' *' then it is very likely a comment.
2683 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002684 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002685 {
2686 $in_comment = 1;
2687 }
2688
2689 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2690 sanitise_line_reset($in_comment);
2691
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002692 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002693 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002694 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002695 $line = sanitise_line($rawline);
2696 }
2697 push(@lines, $line);
2698
2699 if ($realcnt > 1) {
2700 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2701 } else {
2702 $realcnt = 0;
2703 }
2704
2705 #print "==>$rawline\n";
2706 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002707
2708 if ($setup_docs && $line =~ /^\+/) {
2709 push(@setup_docs, $line);
2710 }
2711 }
2712
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002713 $prefix = '';
2714
Andy Whitcroft773647a2008-03-28 14:15:58 -07002715 $realcnt = 0;
2716 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002717 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002718 foreach my $line (@lines) {
2719 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002720 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002721 my $sline = $line; #copy of $line
2722 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002723
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002724 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002725 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002726
Joe Perches12c253a2018-06-07 17:10:58 -07002727# check if it's a mode change, rename or start of a patch
2728 if (!$in_commit_log &&
2729 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2730 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2731 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2732 $is_patch = 1;
2733 }
2734
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002735#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002736 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002737 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2738 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002739 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002740 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002741 $realline=$1-1;
2742 if (defined $2) {
2743 $realcnt=$3+1;
2744 } else {
2745 $realcnt=1+1;
2746 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002747 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002748 $prev_values = 'E';
2749
Andy Whitcroft773647a2008-03-28 14:15:58 -07002750 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002751 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002752 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002753 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002754 if ($context =~ /\b(\w+)\s*\(/) {
2755 $context_function = $1;
2756 } else {
2757 undef $context_function;
2758 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002759 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002760
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002761# track the line number as we move through the hunk, note that
2762# new versions of GNU diff omit the leading space on completely
2763# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002764 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002765 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002766 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002767
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002768 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002769 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002770
2771 # Track the previous line.
2772 ($prevline, $stashline) = ($stashline, $line);
2773 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002774 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2775
Andy Whitcroft773647a2008-03-28 14:15:58 -07002776 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002777
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002778 } elsif ($realcnt == 1) {
2779 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002780 }
2781
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002782 my $hunk_line = ($realcnt != 0);
2783
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002784 $here = "#$linenr: " if (!$file);
2785 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002786
Joe Perches2ac73b42014-06-04 16:12:05 -07002787 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002788 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002789 if ($line =~ /^diff --git.*?(\S+)$/) {
2790 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002791 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002792 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002793 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002794 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002795 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002796 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002797 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002798
2799 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002800 if (!$file && $tree && $p1_prefix ne '' &&
2801 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002802 WARN("PATCH_PREFIX",
2803 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002804 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002805
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002806 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002807 ERROR("MODIFIED_INCLUDE_ASM",
2808 "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 -07002809 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002810 $found_file = 1;
2811 }
2812
Joe Perches34d88152015-06-25 15:03:05 -07002813#make up the handle for any error we report on this line
2814 if ($showfile) {
2815 $prefix = "$realfile:$realline: "
2816 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002817 if ($file) {
2818 $prefix = "$filename:$realline: ";
2819 } else {
2820 $prefix = "$filename:$linenr: ";
2821 }
Joe Perches34d88152015-06-25 15:03:05 -07002822 }
2823
Joe Perches2ac73b42014-06-04 16:12:05 -07002824 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002825 if (is_maintained_obsolete($realfile)) {
2826 WARN("OBSOLETE",
2827 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2828 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002829 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002830 $check = 1;
2831 } else {
2832 $check = $check_orig;
2833 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002834 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002835
2836 if ($realfile !~ /^MAINTAINERS/) {
2837 my $last_binding_patch = $is_binding_patch;
2838
2839 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2840
2841 if (($last_binding_patch != -1) &&
2842 ($last_binding_patch ^ $is_binding_patch)) {
2843 WARN("DT_SPLIT_BINDING_PATCH",
Mauro Carvalho Chehab858e6842020-04-15 16:45:25 +02002844 "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 -07002845 }
2846 }
2847
Andy Whitcroft773647a2008-03-28 14:15:58 -07002848 next;
2849 }
2850
Randy Dunlap389834b2007-06-08 13:47:03 -07002851 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002852
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002853 my $hereline = "$here\n$rawline\n";
2854 my $herecurr = "$here\n$rawline\n";
2855 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002856
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002857 $cnt_lines++ if ($realcnt != 0);
2858
Joe Perches490b2922018-08-21 21:58:01 -07002859# Verify the existence of a commit log if appropriate
2860# 2 is used because a $signature is counted in $commit_log_lines
2861 if ($in_commit_log) {
2862 if ($line !~ /^\s*$/) {
2863 $commit_log_lines++; #could be a $signature
2864 }
2865 } elsif ($has_commit_log && $commit_log_lines < 2) {
2866 WARN("COMMIT_MESSAGE",
2867 "Missing commit description - Add an appropriate one\n");
2868 $commit_log_lines = 2; #warn only once
2869 }
2870
Joe Perchese518e9a2015-06-25 15:03:27 -07002871# Check if the commit log has what seems like a diff which can confuse patch
2872 if ($in_commit_log && !$commit_log_has_diff &&
Mrinal Pandey13e45412020-09-04 16:35:52 -07002873 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2874 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
Joe Perchese518e9a2015-06-25 15:03:27 -07002875 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2876 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2877 ERROR("DIFF_IN_COMMIT_MSG",
2878 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2879 $commit_log_has_diff = 1;
2880 }
2881
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002882# Check for incorrect file permissions
2883 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2884 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002885 if ($realfile !~ m@scripts/@ &&
2886 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002887 ERROR("EXECUTE_PERMISSIONS",
2888 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002889 }
2890 }
2891
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002892# Check the patch for a From:
2893 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2894 $author = $1;
Dwaipayan Raye7f929f2020-10-15 20:12:15 -07002895 my $curline = $linenr;
2896 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2897 $author .= $1;
2898 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002899 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2900 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002901 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002902 }
2903
Joe Perches20112472011-07-25 17:13:23 -07002904# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002905 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002906 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002907 $in_commit_log = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002908 if ($author ne '' && $authorsignoff != 1) {
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002909 if (same_email_addresses($1, $author)) {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002910 $authorsignoff = 1;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002911 } else {
2912 my $ctx = $1;
2913 my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
2914 my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
2915
Mimi Zohar046fc742021-09-07 19:59:54 -07002916 if (lc $email_address eq lc $author_address && $email_name eq $author_name) {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002917 $author_sob = $ctx;
2918 $authorsignoff = 2;
Mimi Zohar046fc742021-09-07 19:59:54 -07002919 } elsif (lc $email_address eq lc $author_address) {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002920 $author_sob = $ctx;
2921 $authorsignoff = 3;
2922 } elsif ($email_name eq $author_name) {
2923 $author_sob = $ctx;
2924 $authorsignoff = 4;
2925
2926 my $address1 = $email_address;
2927 my $address2 = $author_address;
2928
2929 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
2930 $address1 = "$1$2";
2931 }
2932 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
2933 $address2 = "$1$2";
2934 }
2935 if ($address1 eq $address2) {
2936 $authorsignoff = 5;
2937 }
2938 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002939 }
2940 }
Joe Perches20112472011-07-25 17:13:23 -07002941 }
2942
Joe Perches44d303e2020-04-06 20:11:10 -07002943# Check for patch separator
2944 if ($line =~ /^---$/) {
2945 $has_patch_separator = 1;
2946 $in_commit_log = 0;
2947 }
2948
Joe Perchese0d975b2014-12-10 15:51:49 -08002949# Check if MAINTAINERS is being updated. If so, there's probably no need to
2950# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2951 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2952 $reported_maintainer_file = 1;
2953 }
2954
Joe Perches20112472011-07-25 17:13:23 -07002955# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002956 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002957 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002958 my $space_before = $1;
2959 my $sign_off = $2;
2960 my $space_after = $3;
2961 my $email = $4;
2962 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2963
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002964 if ($sign_off !~ /$signature_tags/) {
Aditya Srivastava831242a2020-12-15 20:45:12 -08002965 my $suggested_signature = find_standard_signature($sign_off);
2966 if ($suggested_signature eq "") {
2967 WARN("BAD_SIGN_OFF",
2968 "Non-standard signature: $sign_off\n" . $herecurr);
2969 } else {
2970 if (WARN("BAD_SIGN_OFF",
2971 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
2972 $fix) {
2973 $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
2974 }
2975 }
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002976 }
Joe Perches20112472011-07-25 17:13:23 -07002977 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002978 if (WARN("BAD_SIGN_OFF",
2979 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2980 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002981 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002982 "$ucfirst_sign_off $email";
2983 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002984 }
Joe Perches20112472011-07-25 17:13:23 -07002985 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002986 if (WARN("BAD_SIGN_OFF",
2987 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2988 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002989 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002990 "$ucfirst_sign_off $email";
2991 }
2992
Joe Perches20112472011-07-25 17:13:23 -07002993 }
2994 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002995 if (WARN("BAD_SIGN_OFF",
2996 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2997 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002998 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002999 "$ucfirst_sign_off $email";
3000 }
Joe Perches20112472011-07-25 17:13:23 -07003001 }
3002
Joe Perchesdfa05c22020-04-06 20:10:48 -07003003 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07003004 my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
Joe Perches20112472011-07-25 17:13:23 -07003005 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07003006 ERROR("BAD_SIGN_OFF",
3007 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07003008 } else {
3009 my $dequoted = $suggested_email;
3010 $dequoted =~ s/^"//;
3011 $dequoted =~ s/" </ </;
3012 # Don't force email to have quotes
3013 # Allow just an angle bracketed address
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08003014 if (!same_email_addresses($email, $suggested_email)) {
3015 if (WARN("BAD_SIGN_OFF",
3016 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
3017 $fix) {
3018 $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
3019 }
3020 }
3021
3022 # Address part shouldn't have comments
3023 my $stripped_address = $email_address;
3024 $stripped_address =~ s/\([^\(\)]*\)//g;
3025 if ($email_address ne $stripped_address) {
3026 if (WARN("BAD_SIGN_OFF",
3027 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
3028 $fix) {
3029 $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
3030 }
3031 }
3032
3033 # Only one name comment should be allowed
3034 my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
3035 if ($comment_count > 1) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003036 WARN("BAD_SIGN_OFF",
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08003037 "Use a single name comment in email: '$email'\n" . $herecurr);
3038 }
3039
3040
3041 # stable@vger.kernel.org or stable@kernel.org shouldn't
Dwaipayan Raye73d2712020-12-15 20:44:56 -08003042 # have an email name. In addition comments should strictly
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08003043 # begin with a #
3044 if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
3045 if (($comment ne "" && $comment !~ /^#.+/) ||
3046 ($email_name ne "")) {
3047 my $cur_name = $email_name;
3048 my $new_comment = $comment;
3049 $cur_name =~ s/[a-zA-Z\s\-\"]+//g;
3050
3051 # Remove brackets enclosing comment text
3052 # and # from start of comments to get comment text
3053 $new_comment =~ s/^\((.*)\)$/$1/;
3054 $new_comment =~ s/^\[(.*)\]$/$1/;
3055 $new_comment =~ s/^[\s\#]+|\s+$//g;
3056
3057 $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
3058 $new_comment = " # $new_comment" if ($new_comment ne "");
3059 my $new_email = "$email_address$new_comment";
3060
3061 if (WARN("BAD_STABLE_ADDRESS_STYLE",
3062 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
3063 $fix) {
3064 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3065 }
3066 }
3067 } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
3068 my $new_comment = $comment;
3069
3070 # Extract comment text from within brackets or
3071 # c89 style /*...*/ comments
3072 $new_comment =~ s/^\[(.*)\]$/$1/;
3073 $new_comment =~ s/^\/\*(.*)\*\/$/$1/;
3074
3075 $new_comment = trim($new_comment);
3076 $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
3077 $new_comment = "($new_comment)" if ($new_comment ne "");
3078 my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
3079
3080 if (WARN("BAD_SIGN_OFF",
3081 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
3082 $fix) {
3083 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3084 }
Joe Perches20112472011-07-25 17:13:23 -07003085 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003086 }
Joe Perches7e51f192013-09-11 14:23:57 -07003087
3088# Check for duplicate signatures
3089 my $sig_nospace = $line;
3090 $sig_nospace =~ s/\s//g;
3091 $sig_nospace = lc($sig_nospace);
3092 if (defined $signatures{$sig_nospace}) {
3093 WARN("BAD_SIGN_OFF",
3094 "Duplicate signature\n" . $herecurr);
3095 } else {
3096 $signatures{$sig_nospace} = 1;
3097 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07003098
3099# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
3100 if ($sign_off =~ /^co-developed-by:$/i) {
3101 if ($email eq $author) {
3102 WARN("BAD_SIGN_OFF",
3103 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
3104 }
3105 if (!defined $lines[$linenr]) {
3106 WARN("BAD_SIGN_OFF",
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08003107 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07003108 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
3109 WARN("BAD_SIGN_OFF",
3110 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
3111 } elsif ($1 ne $email) {
3112 WARN("BAD_SIGN_OFF",
3113 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
3114 }
3115 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003116 }
3117
Joe Perchesa2fe16b2015-02-13 14:39:02 -08003118# Check email subject for common tools that don't need to be mentioned
3119 if ($in_header_lines &&
3120 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
3121 WARN("EMAIL_SUBJECT",
3122 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
3123 }
3124
Joe Perches44d303e2020-04-06 20:11:10 -07003125# Check for Gerrit Change-Ids not in any patch context
3126 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Aditya Srivastava7580c5b2020-12-15 20:44:47 -08003127 if (ERROR("GERRIT_CHANGE_ID",
3128 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
3129 $fix) {
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08003130 fix_delete_line($fixlinenr, $rawline);
3131 }
Christopher Covington7ebd05e2014-04-03 14:49:31 -07003132 }
3133
Joe Perches369c8dd2015-11-06 16:31:34 -08003134# Check if the commit log is in a possible stack dump
3135 if ($in_commit_log && !$commit_log_possible_stack_dump &&
3136 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3137 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3138 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07003139 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3140 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3141 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3142 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08003143 $commit_log_possible_stack_dump = 1;
3144 }
3145
Joe Perches2a076f42015-04-16 12:44:28 -07003146# Check for line lengths > 75 in commit log, warn once
3147 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08003148 length($line) > 75 &&
3149 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3150 # file delta changes
3151 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
3152 # filename then :
Aditya Srivastava27b379a2020-12-15 20:44:59 -08003153 $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
3154 # A Fixes: or Link: line or signature tag line
Joe Perches369c8dd2015-11-06 16:31:34 -08003155 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07003156 WARN("COMMIT_LOG_LONG_LINE",
3157 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
3158 $commit_log_long_line = 1;
3159 }
3160
Joe Perchesbf4daf12015-09-09 15:37:50 -07003161# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08003162 if ($in_commit_log && $commit_log_possible_stack_dump &&
3163 $line =~ /^\s*$/) {
3164 $commit_log_possible_stack_dump = 0;
3165 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07003166
Dwaipayan Ray084a6172020-12-15 20:45:18 -08003167# Check for lines starting with a #
3168 if ($in_commit_log && $line =~ /^#/) {
3169 if (WARN("COMMIT_COMMENT_SYMBOL",
3170 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3171 $fix) {
3172 $fixed[$fixlinenr] =~ s/^/ /;
3173 }
3174 }
3175
Joe Perches0d7835f2015-02-13 14:38:35 -08003176# Check for git id commit length and improperly formed commit descriptions
Joe Perches4ce9f972021-09-07 19:59:57 -07003177# A correctly formed commit description is:
3178# commit <SHA-1 hash length 12+ chars> ("Complete commit subject")
3179# with the commit subject '("' prefix and '")' suffix
3180# This is a fairly compilicated block as it tests for what appears to be
3181# bare SHA-1 hash with minimum length of 5. It also avoids several types of
3182# possible SHA-1 matches.
3183# A commit match can span multiple lines so this block attempts to find a
3184# complete typical commit on a maximum of 3 lines
3185 if ($perl_version_ok &&
3186 $in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07003187 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07003188 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perches4ce9f972021-09-07 19:59:57 -07003189 (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
3190 ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) ||
Joe Perchesaab38f52016-08-02 14:04:36 -07003191 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08003192 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3193 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07003194 my $init_char = "c";
3195 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08003196 my $short = 1;
3197 my $long = 0;
3198 my $case = 1;
3199 my $space = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08003200 my $id = '0123456789ab';
3201 my $orig_desc = "commit description";
3202 my $description = "";
Joe Perches4ce9f972021-09-07 19:59:57 -07003203 my $herectx = $herecurr;
3204 my $has_parens = 0;
3205 my $has_quotes = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08003206
Joe Perches4ce9f972021-09-07 19:59:57 -07003207 my $input = $line;
3208 if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) {
3209 for (my $n = 0; $n < 2; $n++) {
3210 if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) {
3211 $orig_desc = $1;
3212 $has_parens = 1;
3213 # Always strip leading/trailing parens then double quotes if existing
3214 $orig_desc = substr($orig_desc, 1, -1);
3215 if ($orig_desc =~ /^".*"$/) {
3216 $orig_desc = substr($orig_desc, 1, -1);
3217 $has_quotes = 1;
3218 }
3219 last;
3220 }
3221 last if ($#lines < $linenr + $n);
3222 $input .= " " . trim($rawlines[$linenr + $n]);
3223 $herectx .= "$rawlines[$linenr + $n]\n";
3224 }
3225 $herectx = $herecurr if (!$has_parens);
Joe Perchesfe043ea2015-09-09 15:37:25 -07003226 }
3227
Joe Perches4ce9f972021-09-07 19:59:57 -07003228 if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3229 $init_char = $1;
3230 $orig_commit = lc($2);
3231 $short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i);
3232 $long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i);
3233 $space = 0 if ($input =~ /\bcommit [0-9a-f]/i);
3234 $case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
3235 } elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) {
3236 $orig_commit = lc($1);
Joe Perches0d7835f2015-02-13 14:38:35 -08003237 }
3238
3239 ($id, $description) = git_commit_info($orig_commit,
3240 $id, $orig_desc);
3241
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07003242 if (defined($id) &&
Joe Perches4ce9f972021-09-07 19:59:57 -07003243 ($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) &&
3244 $last_git_commit_id_linenr != $linenr - 1) {
Joe Perches0d7835f2015-02-13 14:38:35 -08003245 ERROR("GIT_COMMIT_ID",
Joe Perches4ce9f972021-09-07 19:59:57 -07003246 "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 -08003247 }
Joe Perches4ce9f972021-09-07 19:59:57 -07003248 #don't report the next line if this line ends in commit and the sha1 hash is the next line
3249 $last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i);
Joe Perchesd311cd42014-08-06 16:10:57 -07003250 }
3251
Joe Perches13f19372014-08-06 16:10:59 -07003252# Check for added, moved or deleted files
3253 if (!$reported_maintainer_file && !$in_commit_log &&
3254 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
3255 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
3256 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
3257 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08003258 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07003259 $reported_maintainer_file = 1;
3260 WARN("FILE_PATH_CHANGES",
3261 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
3262 }
3263
Rob Herringe400edb2019-09-12 15:18:20 +01003264# Check for adding new DT bindings not in schema format
3265 if (!$in_commit_log &&
3266 ($line =~ /^new file mode\s*\d+\s*$/) &&
3267 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3268 WARN("DT_SCHEMA_BINDING_PATCH",
Mauro Carvalho Chehab56ddc4c2021-04-01 14:17:49 +02003269 "DT bindings should be in DT schema format. See: Documentation/devicetree/bindings/writing-schema.rst\n");
Rob Herringe400edb2019-09-12 15:18:20 +01003270 }
3271
Andy Whitcroft00df3442007-06-08 13:47:06 -07003272# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08003273 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003274 ERROR("CORRUPTED_PATCH",
3275 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003276 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003277 }
3278
3279# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3280 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003281 $rawline !~ m/^$UTF8*$/) {
3282 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3283
3284 my $blank = copy_spacing($rawline);
3285 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3286 my $hereptr = "$hereline$ptr\n";
3287
Joe Perches34d99212011-07-25 17:13:26 -07003288 CHK("INVALID_UTF8",
3289 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003290 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003291
Joe Perches15662b32011-10-31 17:13:12 -07003292# Check if it's the start of a commit log
3293# (not a header line and we haven't seen the patch filename)
3294 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07003295 !($rawline =~ /^\s+(?:\S|$)/ ||
3296 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07003297 $in_header_lines = 0;
3298 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07003299 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07003300 }
3301
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003302# Check if there is UTF-8 in a commit log when a mail header has explicitly
3303# declined it, i.e defined some charset where it is missing.
3304 if ($in_header_lines &&
3305 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3306 $1 !~ /utf-8/i) {
3307 $non_utf8_charset = 1;
3308 }
3309
3310 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07003311 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003312 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07003313 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3314 }
3315
Joe Perchesd6430f72016-12-12 16:46:28 -08003316# Check for absolute kernel paths in commit message
3317 if ($tree && $in_commit_log) {
3318 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3319 my $file = $1;
3320
3321 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3322 check_absolute_file($1, $herecurr)) {
3323 #
3324 } else {
3325 check_absolute_file($file, $herecurr);
3326 }
3327 }
3328 }
3329
Kees Cook66b47b42014-10-13 15:51:57 -07003330# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07003331 if (defined($misspellings) &&
3332 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003333 while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07003334 my $typo = $1;
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003335 my $blank = copy_spacing($rawline);
3336 my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
3337 my $hereptr = "$hereline$ptr\n";
Kees Cook66b47b42014-10-13 15:51:57 -07003338 my $typo_fix = $spelling_fix{lc($typo)};
3339 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3340 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07003341 my $msg_level = \&WARN;
3342 $msg_level = \&CHK if ($file);
3343 if (&{$msg_level}("TYPO_SPELLING",
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003344 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07003345 $fix) {
3346 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3347 }
3348 }
3349 }
3350
Matteo Crocea8dd86b2019-09-25 16:46:38 -07003351# check for invalid commit id
3352 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3353 my $id;
3354 my $description;
3355 ($id, $description) = git_commit_info($2, undef, undef);
3356 if (!defined($id)) {
3357 WARN("UNKNOWN_COMMIT_ID",
3358 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3359 }
3360 }
3361
Joe Perches310cd062020-10-15 20:11:52 -07003362# check for repeated words separated by a single space
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003363# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3364 if (($rawline =~ /^\+/ || $in_commit_log) &&
3365 $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003366 pos($rawline) = 1 if (!$in_commit_log);
Joe Perches310cd062020-10-15 20:11:52 -07003367 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3368
3369 my $first = $1;
3370 my $second = $2;
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003371 my $start_pos = $-[1];
3372 my $end_pos = $+[2];
Joe Perches310cd062020-10-15 20:11:52 -07003373 if ($first =~ /(?:struct|union|enum)/) {
3374 pos($rawline) += length($first) + length($second) + 1;
3375 next;
3376 }
3377
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003378 next if (lc($first) ne lc($second));
Joe Perches310cd062020-10-15 20:11:52 -07003379 next if ($first eq 'long');
3380
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003381 # check for character before and after the word matches
3382 my $start_char = '';
3383 my $end_char = '';
3384 $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
3385 $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3386
3387 next if ($start_char =~ /^\S$/);
3388 next if (index(" \t.,;?!", $end_char) == -1);
3389
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08003390 # avoid repeating hex occurrences like 'ff ff fe 09 ...'
3391 if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3392 next if (!exists($allow_repeated_words{lc($first)}));
3393 }
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003394
Joe Perches310cd062020-10-15 20:11:52 -07003395 if (WARN("REPEATED_WORD",
3396 "Possible repeated word: '$first'\n" . $herecurr) &&
3397 $fix) {
3398 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3399 }
3400 }
3401
3402 # if it's a repeated word on consecutive lines in a comment block
3403 if ($prevline =~ /$;+\s*$/ &&
3404 $prevrawline =~ /($word_pattern)\s*$/) {
3405 my $last_word = $1;
3406 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3407 if (WARN("REPEATED_WORD",
3408 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3409 $fix) {
3410 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3411 }
3412 }
3413 }
3414 }
3415
Andy Whitcroft306708542008-10-15 22:02:28 -07003416# ignore non-hunk lines and lines being removed
3417 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003418
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003419#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003420 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003421 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07003422 if (ERROR("DOS_LINE_ENDINGS",
3423 "DOS line endings\n" . $herevet) &&
3424 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003425 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003426 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003427 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3428 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003429 if (ERROR("TRAILING_WHITESPACE",
3430 "trailing whitespace\n" . $herevet) &&
3431 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003432 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003433 }
3434
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003435 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003436 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07003437
Josh Triplett4783f892013-11-12 15:10:12 -08003438# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003439 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003440 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003441 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3442 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003443 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003444 my $msg_level = \&ERROR;
3445 $msg_level = \&CHK if ($file);
3446 &{$msg_level}("FSF_MAILING_ADDRESS",
3447 "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 -08003448 }
3449
Andi Kleen33549572010-05-24 14:33:29 -07003450# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003451# Only applies when adding the entry originally, after that we do not have
3452# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003453 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003454 # 'choice' is usually the last thing on the line (though
3455 # Kconfig supports named choices), so use a word boundary
3456 # (\b) rather than a whitespace character (\s)
3457 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003458 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003459 my $cnt = $realcnt;
3460 my $ln = $linenr + 1;
3461 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003462 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003463 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003464 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003465 $f = $lines[$ln - 1];
3466 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3467 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003468
3469 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003470 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003471
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003472 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003473 $is_start = 1;
Masahiro Yamada22a4ac02020-06-17 12:02:20 +09003474 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003475 $length = -1;
3476 }
3477
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003478 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003479 $f =~ s/#.*//;
3480 $f =~ s/^\s+//;
3481 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003482
3483 # This only checks context lines in the patch
3484 # and so hopefully shouldn't trigger false
3485 # positives, even though some of these are
3486 # common words in help texts
3487 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3488 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003489 $is_end = 1;
3490 last;
3491 }
Andi Kleen33549572010-05-24 14:33:29 -07003492 $length++;
3493 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003494 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3495 WARN("CONFIG_DESCRIPTION",
3496 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3497 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003498 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003499 }
3500
Joe Perches7ccf41a2020-06-04 16:50:33 -07003501# check MAINTAINERS entries
3502 if ($realfile =~ /^MAINTAINERS$/) {
3503# check MAINTAINERS entries for the right form
3504 if ($rawline =~ /^\+[A-Z]:/ &&
3505 $rawline !~ /^\+[A-Z]:\t\S/) {
3506 if (WARN("MAINTAINERS_STYLE",
3507 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3508 $fix) {
3509 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3510 }
3511 }
3512# check MAINTAINERS entries for the right ordering too
3513 my $preferred_order = 'MRLSWQBCPTFXNK';
3514 if ($rawline =~ /^\+[A-Z]:/ &&
3515 $prevrawline =~ /^[\+ ][A-Z]:/) {
3516 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3517 my $cur = $1;
3518 my $curval = $2;
3519 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3520 my $prev = $1;
3521 my $prevval = $2;
3522 my $curindex = index($preferred_order, $cur);
3523 my $previndex = index($preferred_order, $prev);
3524 if ($curindex < 0) {
3525 WARN("MAINTAINERS_STYLE",
3526 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3527 } else {
3528 if ($previndex >= 0 && $curindex < $previndex) {
3529 WARN("MAINTAINERS_STYLE",
3530 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3531 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3532 ($prev eq 'X' && $cur eq 'X')) &&
3533 ($prevval cmp $curval) > 0) {
3534 WARN("MAINTAINERS_STYLE",
3535 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3536 }
3537 }
Joe Perches628f91a2017-07-10 15:52:07 -07003538 }
3539 }
3540
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003541 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3542 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3543 my $flag = $1;
3544 my $replacement = {
3545 'EXTRA_AFLAGS' => 'asflags-y',
3546 'EXTRA_CFLAGS' => 'ccflags-y',
3547 'EXTRA_CPPFLAGS' => 'cppflags-y',
3548 'EXTRA_LDFLAGS' => 'ldflags-y',
3549 };
3550
3551 WARN("DEPRECATED_VARIABLE",
3552 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3553 }
3554
Rob Herringbff5da42014-01-23 15:54:51 -08003555# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003556 if (defined $root &&
3557 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3558 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3559
Rob Herringbff5da42014-01-23 15:54:51 -08003560 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3561
Florian Vaussardcc933192014-04-03 14:49:27 -07003562 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003563 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003564
Rob Herringbff5da42014-01-23 15:54:51 -08003565 foreach my $compat (@compats) {
3566 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003567 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3568 my $compat3 = $compat;
3569 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3570 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003571 if ( $? >> 8 ) {
3572 WARN("UNDOCUMENTED_DT_STRING",
3573 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3574 }
3575
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003576 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3577 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003578 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003579 if ( $? >> 8 ) {
3580 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003581 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003582 }
3583 }
3584 }
3585
Rob Herring9f3a8992018-04-10 16:33:13 -07003586# check for using SPDX license tag at beginning of files
3587 if ($realline == $checklicenseline) {
3588 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3589 $checklicenseline = 2;
3590 } elsif ($rawline =~ /^\+/) {
3591 my $comment = "";
3592 if ($realfile =~ /\.(h|s|S)$/) {
3593 $comment = '/*';
3594 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3595 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003596 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003597 $comment = '#';
3598 } elsif ($realfile =~ /\.rst$/) {
3599 $comment = '..';
3600 }
3601
Joe Perchesfdf13692019-03-07 16:28:32 -08003602# check SPDX comment style for .[chsS] files
3603 if ($realfile =~ /\.[chsS]$/ &&
3604 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003605 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003606 WARN("SPDX_LICENSE_TAG",
3607 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3608 }
3609
Rob Herring9f3a8992018-04-10 16:33:13 -07003610 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003611 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3612 WARN("SPDX_LICENSE_TAG",
3613 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003614 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003615 my $spdx_license = $1;
3616 if (!is_SPDX_License_valid($spdx_license)) {
3617 WARN("SPDX_LICENSE_TAG",
3618 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3619 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003620 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3621 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3622 my $msg_level = \&WARN;
3623 $msg_level = \&CHK if ($file);
3624 if (&{$msg_level}("SPDX_LICENSE_TAG",
3625
3626 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3627 $fix) {
3628 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3629 }
3630 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003631 }
3632 }
3633 }
3634
Joe Perchesa0154cd2020-10-15 20:12:19 -07003635# check for embedded filenames
3636 if ($rawline =~ /^\+.*\Q$realfile\E/) {
3637 WARN("EMBEDDED_FILENAME",
3638 "It's generally not useful to have the filename in the file\n" . $herecurr);
3639 }
3640
Andy Whitcroft5368df202008-10-15 22:02:27 -07003641# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003642 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003643
Joe Perchesa8da38a2019-03-07 16:28:42 -08003644# check for using SPDX-License-Identifier on the wrong line number
3645 if ($realline != $checklicenseline &&
3646 $rawline =~ /\bSPDX-License-Identifier:/ &&
3647 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3648 WARN("SPDX_LICENSE_TAG",
3649 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3650 }
3651
Joe Perches47e0c882015-06-25 15:02:57 -07003652# line length limit (with some exclusions)
3653#
3654# There are a few types of lines that may extend beyond $max_line_length:
3655# logging functions like pr_info that end in a string
3656# lines with a single string
3657# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003658# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003659#
3660# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003661# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003662# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3663# LONG_LINE all other lines longer than $max_line_length
3664#
3665# if LONG_LINE is ignored, the other 2 types are also ignored
3666#
3667
Joe Perchesb4749e92015-07-17 16:24:01 -07003668 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003669 my $msg_type = "LONG_LINE";
3670
3671 # Check the allowed long line types first
3672
3673 # logging functions that end in a string that starts
3674 # before $max_line_length
3675 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3676 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3677 $msg_type = "";
3678
3679 # lines with only strings (w/ possible termination)
3680 # #defines with only strings
3681 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3682 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3683 $msg_type = "";
3684
Joe Perchescc147502017-11-17 15:28:44 -08003685 # More special cases
3686 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3687 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003688 $msg_type = "";
3689
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003690 # URL ($rawline is used in case the URL is in a comment)
3691 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3692 $msg_type = "";
3693
Joe Perches47e0c882015-06-25 15:02:57 -07003694 # Otherwise set the alternate message types
3695
3696 # a comment starts before $max_line_length
3697 } elsif ($line =~ /($;[\s$;]*)$/ &&
3698 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3699 $msg_type = "LONG_LINE_COMMENT"
3700
3701 # a quoted string starts before $max_line_length
3702 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3703 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3704 $msg_type = "LONG_LINE_STRING"
3705 }
3706
3707 if ($msg_type ne "" &&
3708 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003709 my $msg_level = \&WARN;
3710 $msg_level = \&CHK if ($file);
3711 &{$msg_level}($msg_type,
3712 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003713 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003714 }
3715
Andy Whitcroft8905a672007-11-28 16:21:06 -08003716# check for adding lines without a newline.
3717 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Tom Rix47ca69b2020-12-15 20:44:40 -08003718 if (WARN("MISSING_EOF_NEWLINE",
3719 "adding a line without newline at end of file\n" . $herecurr) &&
3720 $fix) {
3721 fix_delete_line($fixlinenr+1, "No newline at end of file");
3722 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003723 }
3724
Aditya Srivastavade932452021-02-25 17:21:57 -08003725# check for .L prefix local symbols in .S files
3726 if ($realfile =~ /\.S$/ &&
3727 $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) {
3728 WARN("AVOID_L_PREFIX",
3729 "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);
3730 }
3731
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003732# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003733 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003734
3735# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003736# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003737 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3738 $rawline =~ /^\+\s* \s*/) {
3739 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003740 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003741 if (ERROR("CODE_INDENT",
3742 "code indent should use tabs where possible\n" . $herevet) &&
3743 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003744 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003745 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003746 }
3747
Alberto Panizzo08e44362010-03-05 13:43:54 -08003748# check for space before tabs.
3749 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3750 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003751 if (WARN("SPACE_BEFORE_TAB",
3752 "please, no space before tabs\n" . $herevet) &&
3753 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003754 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003755 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003756 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003757 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003758 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003759 }
3760
Joe Perches6a487212018-04-10 16:34:04 -07003761# check for assignments on the start of a line
3762 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
Aditya Srivastavada7355a2020-12-15 20:45:06 -08003763 my $operator = $1;
3764 if (CHK("ASSIGNMENT_CONTINUATIONS",
3765 "Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3766 $fix && $prevrawline =~ /^\+/) {
3767 # add assignment operator to the previous line, remove from current line
3768 $fixed[$fixlinenr - 1] .= " $operator";
3769 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3770 }
Joe Perches6a487212018-04-10 16:34:04 -07003771 }
3772
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003773# check for && or || at the start of a line
3774 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
Aditya Srivastava8e08f072020-12-15 20:45:09 -08003775 my $operator = $1;
3776 if (CHK("LOGICAL_CONTINUATIONS",
3777 "Logical continuations should be on the previous line\n" . $hereprev) &&
3778 $fix && $prevrawline =~ /^\+/) {
3779 # insert logical operator at last non-comment, non-whitepsace char on previous line
3780 $prevline =~ /[\s$;]*$/;
3781 my $line_end = substr($prevrawline, $-[0]);
3782 $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
3783 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3784 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003785 }
3786
Joe Perchesa91e8992016-05-20 17:04:05 -07003787# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003788 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003789 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003790 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003791 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003792 if (WARN("TABSTOP",
3793 "Statements should start on a tabstop\n" . $herecurr) &&
3794 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003795 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003796 }
3797 }
3798 }
3799
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003800# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003801 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003802 $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 -07003803 $prevline =~ /^\+(\t*)(.*)$/;
3804 my $oldindent = $1;
3805 my $rest = $2;
3806
3807 my $pos = pos_last_openparen($rest);
3808 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003809 $line =~ /^(\+| )([ \t]*)/;
3810 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003811
3812 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003813 "\t" x ($pos / $tabsize) .
3814 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003815 my $goodspaceindent = $oldindent . " " x $pos;
3816
3817 if ($newindent ne $goodtabindent &&
3818 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003819
3820 if (CHK("PARENTHESIS_ALIGNMENT",
3821 "Alignment should match open parenthesis\n" . $hereprev) &&
3822 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003823 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003824 s/^\+[ \t]*/\+$goodtabindent/;
3825 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003826 }
3827 }
3828 }
3829
Joe Perches6ab3a972015-04-16 12:44:05 -07003830# check for space after cast like "(int) foo" or "(struct foo) bar"
3831# avoid checking a few false positives:
3832# "sizeof(<type>)" or "__alignof__(<type>)"
3833# function pointer declarations like "(*foo)(int) = bar;"
3834# structure definitions like "(struct foo) { 0 };"
3835# multiline macros that define functions
3836# known attributes or the __attribute__ keyword
3837 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3838 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003839 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003840 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003841 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003842 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003843 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003844 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003845 }
3846
Joe Perches86406b12015-09-09 15:37:41 -07003847# Block comment styles
3848# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003849 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003850 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003851 $rawline =~ /^\+[ \t]*\*/ &&
Łukasz Stelmachc70735c2020-10-15 20:12:25 -07003852 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
Joe Perches05880602012-10-04 17:13:35 -07003853 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3854 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3855 }
3856
Joe Perches86406b12015-09-09 15:37:41 -07003857# Block comments use * on subsequent lines
3858 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3859 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003860 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003861 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003862 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003863 WARN("BLOCK_COMMENT_STYLE",
3864 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003865 }
3866
Joe Perches86406b12015-09-09 15:37:41 -07003867# Block comments use */ on trailing lines
3868 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003869 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3870 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3871 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003872 WARN("BLOCK_COMMENT_STYLE",
3873 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003874 }
3875
Joe Perches08eb9b82016-10-11 13:51:50 -07003876# Block comment * alignment
3877 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003878 $line =~ /^\+[ \t]*$;/ && #leading comment
3879 $rawline =~ /^\+[ \t]*\*/ && #leading *
3880 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003881 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003882 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3883 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003884 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003885 if (defined($1)) {
3886 $oldindent = expand_tabs($1);
3887 } else {
3888 $prevrawline =~ m@^\+(.*/?)\*@;
3889 $oldindent = expand_tabs($1);
3890 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003891 $rawline =~ m@^\+([ \t]*)\*@;
3892 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003893 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003894 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003895 WARN("BLOCK_COMMENT_STYLE",
3896 "Block comments should align the * on each line\n" . $hereprev);
3897 }
3898 }
3899
Joe Perches7f619192014-08-06 16:10:39 -07003900# check for missing blank lines after struct/union declarations
3901# with exceptions for various attributes and macros
3902 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3903 $line =~ /^\+/ &&
3904 !($line =~ /^\+\s*$/ ||
3905 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3906 $line =~ /^\+\s*MODULE_/i ||
3907 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3908 $line =~ /^\+[a-z_]*init/ ||
3909 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3910 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003911 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003912 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003913 if (CHK("LINE_SPACING",
3914 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3915 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003916 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003917 }
Joe Perches7f619192014-08-06 16:10:39 -07003918 }
3919
Joe Perches365dd4e2014-08-06 16:10:42 -07003920# check for multiple consecutive blank lines
3921 if ($prevline =~ /^[\+ ]\s*$/ &&
3922 $line =~ /^\+\s*$/ &&
3923 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003924 if (CHK("LINE_SPACING",
3925 "Please don't use multiple blank lines\n" . $hereprev) &&
3926 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003927 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003928 }
3929
Joe Perches365dd4e2014-08-06 16:10:42 -07003930 $last_blank_line = $linenr;
3931 }
3932
Joe Perches3b617e32014-04-03 14:49:28 -07003933# check for missing blank lines after declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08003934# (declarations must have the same indentation and not be at the start of line)
3935 if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
3936 # use temporaries
3937 my $sl = $sline;
3938 my $pl = $prevline;
3939 # remove $Attribute/$Sparse uses to simplify comparisons
3940 $sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3941 $pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3942 if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003943 # function pointer declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08003944 $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003945 # foo bar; where foo is some local typedef or #define
Joe Perchesb5e87362021-02-25 17:21:40 -08003946 $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003947 # known declaration macros
Joe Perchesb5e87362021-02-25 17:21:40 -08003948 $pl =~ /^\+\s+$declaration_macros/) &&
Joe Perches3f7bac02014-06-04 16:12:04 -07003949 # for "else if" which can look like "$Ident $Ident"
Joe Perchesb5e87362021-02-25 17:21:40 -08003950 !($pl =~ /^\+\s+$c90_Keywords\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003951 # other possible extensions of declaration lines
Joe Perchesb5e87362021-02-25 17:21:40 -08003952 $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003953 # not starting a section or a macro "\" extended line
Joe Perchesb5e87362021-02-25 17:21:40 -08003954 $pl =~ /(?:\{\s*|\\)$/) &&
Joe Perches3f7bac02014-06-04 16:12:04 -07003955 # looks like a declaration
Joe Perchesb5e87362021-02-25 17:21:40 -08003956 !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003957 # function pointer declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08003958 $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003959 # foo bar; where foo is some local typedef or #define
Joe Perchesb5e87362021-02-25 17:21:40 -08003960 $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003961 # known declaration macros
Joe Perchesb5e87362021-02-25 17:21:40 -08003962 $sl =~ /^\+\s+$declaration_macros/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003963 # start of struct or union or enum
Joe Perchesb5e87362021-02-25 17:21:40 -08003964 $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003965 # start or end of block or continuation of declaration
Joe Perchesb5e87362021-02-25 17:21:40 -08003966 $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003967 # bitfield continuation
Joe Perchesb5e87362021-02-25 17:21:40 -08003968 $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003969 # other possible extensions of declaration lines
Joe Perchesb5e87362021-02-25 17:21:40 -08003970 $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
3971 if (WARN("LINE_SPACING",
3972 "Missing a blank line after declarations\n" . $hereprev) &&
3973 $fix) {
3974 fix_insert_line($fixlinenr, "\+");
3975 }
Joe Perchesd752fcc2014-08-06 16:11:05 -07003976 }
Joe Perches3b617e32014-04-03 14:49:28 -07003977 }
3978
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003979# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003980# Exceptions:
3981# 1) within comments
3982# 2) indented preprocessor commands
3983# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003984 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003985 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003986 if (WARN("LEADING_SPACE",
3987 "please, no spaces at the start of a line\n" . $herevet) &&
3988 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003989 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003990 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003991 }
3992
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003993# check we are in a valid C source file if not then ignore this hunk
3994 next if ($realfile !~ /\.(h|c)$/);
3995
Joe Perches5751a242017-11-17 15:28:52 -08003996# check for unusual line ending [ or (
3997 if ($line =~ /^\+.*([\[\(])\s*$/) {
3998 CHK("OPEN_ENDED_LINE",
3999 "Lines should not end with a '$1'\n" . $herecurr);
4000 }
4001
Joe Perches4dbed762017-05-08 15:55:39 -07004002# check if this appears to be the start function declaration, save the name
4003 if ($sline =~ /^\+\{\s*$/ &&
4004 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
4005 $context_function = $1;
4006 }
4007
4008# check if this appears to be the end of function declaration
4009 if ($sline =~ /^\+\}\s*$/) {
4010 undef $context_function;
4011 }
4012
Joe Perches032a4c02014-08-06 16:10:29 -07004013# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07004014# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07004015# if the previous line is a break or return and is indented 1 tab more...
4016 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
4017 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07004018 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
4019 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
4020 defined $lines[$linenr] &&
4021 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07004022 WARN("UNNECESSARY_ELSE",
4023 "else is not generally useful after a break or return\n" . $hereprev);
4024 }
4025 }
4026
Joe Perchesc00df192014-08-06 16:11:01 -07004027# check indentation of a line with a break;
Joe Perchesdc58bc52020-12-15 20:44:33 -08004028# if the previous line is a goto, return or break
4029# and is indented the same # of tabs
Joe Perchesc00df192014-08-06 16:11:01 -07004030 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
4031 my $tabs = $1;
Joe Perchesdc58bc52020-12-15 20:44:33 -08004032 if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
4033 if (WARN("UNNECESSARY_BREAK",
4034 "break is not useful after a $1\n" . $hereprev) &&
4035 $fix) {
4036 fix_delete_line($fixlinenr, $rawline);
4037 }
Joe Perchesc00df192014-08-06 16:11:01 -07004038 }
4039 }
4040
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004041# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08004042 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004043 WARN("CVS_KEYWORD",
4044 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004045 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004046
Joe Perches56e77d72013-02-21 16:44:14 -08004047# check for old HOTPLUG __dev<foo> section markings
4048 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
4049 WARN("HOTPLUG_SECTION",
4050 "Using $1 is unnecessary\n" . $herecurr);
4051 }
4052
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004053# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004054 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
4055 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004056#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07004057 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07004058 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004059 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004060 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004061 $stat =~ s/\n./\n /g;
4062 $cond =~ s/\n./\n /g;
4063
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004064#print "linenr<$linenr> <$stat>\n";
4065 # If this statement has no statement boundaries within
4066 # it there is no point in retrying a statement scan
4067 # until we hit end of it.
4068 my $frag = $stat; $frag =~ s/;+\s*$//;
4069 if ($frag !~ /(?:{|;)/) {
4070#print "skip<$line_nr_next>\n";
4071 $suppress_statement = $line_nr_next;
4072 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004073
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004074 # Find the real next line.
4075 $realline_next = $line_nr_next;
4076 if (defined $realline_next &&
4077 (!defined $lines[$realline_next - 1] ||
4078 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
4079 $realline_next++;
4080 }
4081
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004082 my $s = $stat;
4083 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004084
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004085 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004086 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004087
4088 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004089 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004090
Andy Whitcroft463f2862009-09-21 17:04:34 -07004091 } elsif ($s =~ /^.\s*else\b/s) {
4092
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004093 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07004094 } 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 -07004095 my $type = $1;
4096 $type =~ s/\s+/ /g;
4097 possible($type, "A:" . $s);
4098
Andy Whitcroft8905a672007-11-28 16:21:06 -08004099 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07004100 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004101 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004102 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004103
4104 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08004105 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004106 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004107 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004108
4109 # Check for any sort of function declaration.
4110 # int foo(something bar, other baz);
4111 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004112 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 -08004113 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004114
Andy Whitcroftcf655042008-03-04 14:28:20 -08004115 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004116 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004117 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004118
Andy Whitcroft8905a672007-11-28 16:21:06 -08004119 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004120 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08004121
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004122 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004123 }
4124 }
4125 }
4126
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004127 }
4128
Andy Whitcroft653d4872007-06-23 17:16:34 -07004129#
4130# Checks which may be anchored in the context.
4131#
4132
4133# Check for switch () and associated case and default
4134# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07004135 if ($line=~/\bswitch\s*\(.*\)/) {
4136 my $err = '';
4137 my $sep = '';
4138 my @ctx = ctx_block_outer($linenr, $realcnt);
4139 shift(@ctx);
4140 for my $ctx (@ctx) {
4141 my ($clen, $cindent) = line_stats($ctx);
4142 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
4143 $indent != $cindent) {
4144 $err .= "$sep$ctx\n";
4145 $sep = '';
4146 } else {
4147 $sep = "[...]\n";
4148 }
4149 }
4150 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004151 ERROR("SWITCH_CASE_INDENT_LEVEL",
4152 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004153 }
4154 }
4155
4156# if/while/etc brace do not go on next line, unless defining a do while loop,
4157# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07004158 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 -07004159 my $pre_ctx = "$1$2";
4160
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004161 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08004162
4163 if ($line =~ /^\+\t{6,}/) {
4164 WARN("DEEP_INDENTATION",
4165 "Too many leading tabs - consider code refactoring\n" . $herecurr);
4166 }
4167
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004168 my $ctx_cnt = $realcnt - $#ctx - 1;
4169 my $ctx = join("\n", @ctx);
4170
Andy Whitcroft548596d2008-07-23 21:29:01 -07004171 my $ctx_ln = $linenr;
4172 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004173
Andy Whitcroft548596d2008-07-23 21:29:01 -07004174 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4175 defined $lines[$ctx_ln - 1] &&
4176 $lines[$ctx_ln - 1] =~ /^-/)) {
4177 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4178 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004179 $ctx_ln++;
4180 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07004181
Andy Whitcroft53210162008-07-23 21:29:03 -07004182 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
4183 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07004184
Joe Perchesd752fcc2014-08-06 16:11:05 -07004185 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004186 ERROR("OPEN_BRACE",
4187 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07004188 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07004189 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004190 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4191 $ctx =~ /\)\s*\;\s*$/ &&
4192 defined $lines[$ctx_ln - 1])
4193 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004194 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
4195 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004196 WARN("TRAILING_SEMICOLON",
4197 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07004198 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004199 }
4200 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004201 }
4202
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004203# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07004204 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 -08004205 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4206 ctx_statement_block($linenr, $realcnt, 0)
4207 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004208 my ($s, $c) = ($stat, $cond);
4209
4210 substr($s, 0, length($c), '');
4211
Joe Perches9f5af482015-09-09 15:37:30 -07004212 # remove inline comments
4213 $s =~ s/$;/ /g;
4214 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004215
4216 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07004217 my @newlines = ($c =~ /\n/gs);
4218 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004219
Joe Perches9f5af482015-09-09 15:37:30 -07004220 # Make sure we remove the line prefixes as we have
4221 # none on the first line, and are going to readd them
4222 # where necessary.
4223 $s =~ s/\n./\n/gs;
4224 while ($s =~ /\n\s+\\\n/) {
4225 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
4226 }
4227
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004228 # We want to check the first line inside the block
4229 # starting at the end of the conditional, so remove:
4230 # 1) any blank line termination
4231 # 2) any opening brace { on end of the line
4232 # 3) any do (...) {
4233 my $continuation = 0;
4234 my $check = 0;
4235 $s =~ s/^.*\bdo\b//;
4236 $s =~ s/^\s*{//;
4237 if ($s =~ s/^\s*\\//) {
4238 $continuation = 1;
4239 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004240 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004241 $check = 1;
4242 $cond_lines++;
4243 }
4244
4245 # Also ignore a loop construct at the end of a
4246 # preprocessor statement.
4247 if (($prevline =~ /^.\s*#\s*define\s/ ||
4248 $prevline =~ /\\\s*$/) && $continuation == 0) {
4249 $check = 0;
4250 }
4251
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004252 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07004253 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004254 while ($cond_ptr != $cond_lines) {
4255 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004256
Andy Whitcroftf16fa282008-10-15 22:02:32 -07004257 # If we see an #else/#elif then the code
4258 # is not linear.
4259 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4260 $check = 0;
4261 }
4262
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004263 # Ignore:
4264 # 1) blank lines, they should be at 0,
4265 # 2) preprocessor lines, and
4266 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07004267 if ($continuation ||
4268 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004269 $s =~ /^\s*#\s*?/ ||
4270 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07004271 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07004272 if ($s =~ s/^.*?\n//) {
4273 $cond_lines++;
4274 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004275 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004276 }
4277
4278 my (undef, $sindent) = line_stats("+" . $s);
4279 my $stat_real = raw_line($linenr, $cond_lines);
4280
4281 # Check if either of these lines are modified, else
4282 # this is not this patch's fault.
4283 if (!defined($stat_real) ||
4284 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
4285 $check = 0;
4286 }
4287 if (defined($stat_real) && $cond_lines > 1) {
4288 $stat_real = "[...]\n$stat_real";
4289 }
4290
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004291 #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 -07004292
Joe Perches9f5af482015-09-09 15:37:30 -07004293 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07004294 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07004295 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07004296 ($sindent == $indent &&
4297 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07004298 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004299 WARN("SUSPECT_CODE_INDENT",
4300 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004301 }
4302 }
4303
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004304 # Track the 'values' across context and added lines.
4305 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004306 my ($curr_values, $curr_vars) =
4307 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004308 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004309 if ($dbg_values) {
4310 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004311 print "$linenr > .$outline\n";
4312 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004313 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004314 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004315 $prev_values = substr($curr_values, -1);
4316
Andy Whitcroft00df3442007-06-08 13:47:06 -07004317#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07004318 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07004319
Joe Perches99ca38c2020-10-15 20:12:09 -07004320# check for self assignments used to avoid compiler warnings
4321# e.g.: int foo = foo, *bar = NULL;
4322# struct foo bar = *(&(bar));
4323 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
4324 my $var = $1;
4325 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
4326 WARN("SELF_ASSIGNMENT",
4327 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
4328 }
4329 }
4330
Joe Perches11ca40a2016-12-12 16:46:31 -08004331# check for dereferences that span multiple lines
4332 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
4333 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
4334 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
4335 my $ref = $1;
4336 $line =~ /^.\s*($Lval)/;
4337 $ref .= $1;
4338 $ref =~ s/\s//g;
4339 WARN("MULTILINE_DEREFERENCE",
4340 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
4341 }
4342
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004343# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07004344 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 -07004345 my $type = $1;
4346 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07004347 $var = "" if (!defined $var);
4348 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004349 my $sign = $1;
4350 my $pointer = $2;
4351
4352 $pointer = "" if (!defined $pointer);
4353
4354 if (WARN("UNSPECIFIED_INT",
4355 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4356 $fix) {
4357 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07004358 my $comp_pointer = $pointer;
4359 $comp_pointer =~ s/\s//g;
4360 $decl .= $comp_pointer;
4361 $decl = rtrim($decl) if ($var eq "");
4362 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004363 }
4364 }
4365 }
4366
Andy Whitcroft653d4872007-06-23 17:16:34 -07004367# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07004368 if ($dbg_type) {
4369 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004370 ERROR("TEST_TYPE",
4371 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004372 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004373 ERROR("TEST_NOT_TYPE",
4374 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004375 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004376 next;
4377 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004378# TEST: allow direct testing of the attribute matcher.
4379 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004380 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004381 ERROR("TEST_ATTR",
4382 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004383 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004384 ERROR("TEST_NOT_ATTR",
4385 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004386 }
4387 next;
4388 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004389
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004390# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07004391 if ($line =~ /^.\s*{/ &&
4392 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07004393 if (ERROR("OPEN_BRACE",
4394 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004395 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4396 fix_delete_line($fixlinenr - 1, $prevrawline);
4397 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004398 my $fixedline = $prevrawline;
4399 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004400 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004401 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004402 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004403 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004404 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004405 }
4406
Andy Whitcroft653d4872007-06-23 17:16:34 -07004407#
4408# Checks which are anchored on the added line.
4409#
4410
4411# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004412 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07004413 my $path = $1;
4414 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004415 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08004416 "malformed #include filename\n" . $herecurr);
4417 }
4418 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4419 ERROR("UAPI_INCLUDE",
4420 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004421 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004422 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004423
4424# no C99 // comments
4425 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07004426 if (ERROR("C99_COMMENTS",
4427 "do not use C99 // comments\n" . $herecurr) &&
4428 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004429 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07004430 if ($line =~ /\/\/(.*)$/) {
4431 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07004432 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07004433 }
4434 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004435 }
4436 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004437 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004438 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004439
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004440# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4441# the whole statement.
4442#print "APW <$lines[$realline_next - 1]>\n";
4443 if (defined $realline_next &&
4444 exists $lines[$realline_next - 1] &&
4445 !defined $suppress_export{$realline_next} &&
Christoph Hellwig36794822021-02-02 13:13:34 +01004446 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004447 # Handle definitions which produce identifiers with
4448 # a prefix:
4449 # XXX(foo);
4450 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004451 my $name = $1;
Joe Perches70a11652021-11-08 18:33:31 -08004452 $name =~ s/^\s*($Ident).*/$1/;
Andy Whitcroft87a53872012-01-10 15:10:04 -08004453 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004454 $name =~ /^${Ident}_$2/) {
4455#print "FOO C name<$name>\n";
4456 $suppress_export{$realline_next} = 1;
4457
4458 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004459 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07004460 ^.DEFINE_$Ident\(\Q$name\E\)|
4461 ^.DECLARE_$Ident\(\Q$name\E\)|
4462 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004463 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4464 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07004465 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004466#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4467 $suppress_export{$realline_next} = 2;
4468 } else {
4469 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004470 }
4471 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004472 if (!defined $suppress_export{$linenr} &&
4473 $prevline =~ /^.\s*$/ &&
Christoph Hellwig36794822021-02-02 13:13:34 +01004474 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004475#print "FOO B <$lines[$linenr - 1]>\n";
4476 $suppress_export{$linenr} = 2;
4477 }
4478 if (defined $suppress_export{$linenr} &&
4479 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004480 WARN("EXPORT_SYMBOL",
4481 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004482 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004483
Joe Eloff5150bda2010-08-09 17:21:00 -07004484# check for global initialisers.
Song Liu5b8f82e2021-02-25 17:22:08 -08004485 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
4486 !exclude_global_initialisers($realfile)) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004487 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004488 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004489 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004490 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004491 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004492 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004493# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004494 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004495 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004496 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004497 $herecurr) &&
4498 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004499 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004500 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004501 }
4502
Joe Perches18130872014-08-06 16:11:22 -07004503# check for misordered declarations of char/short/int/long with signed/unsigned
4504 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4505 my $tmp = trim($1);
4506 WARN("MISORDERED_TYPE",
4507 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4508 }
4509
Joe Perches809e0822018-08-21 21:58:12 -07004510# check for unnecessary <signed> int declarations of short/long/long long
4511 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4512 my $type = trim($1);
4513 next if ($type !~ /\bint\b/);
4514 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4515 my $new_type = $type;
4516 $new_type =~ s/\b\s*int\s*\b/ /;
4517 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4518 $new_type =~ s/^const\s+//;
4519 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4520 $new_type = "const $new_type" if ($type =~ /^const\b/);
4521 $new_type =~ s/\s+/ /g;
4522 $new_type = trim($new_type);
4523 if (WARN("UNNECESSARY_INT",
4524 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4525 $fix) {
4526 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4527 }
4528 }
4529
Joe Perchescb710ec2010-10-26 14:23:20 -07004530# check for static const char * arrays.
4531 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004532 WARN("STATIC_CONST_CHAR_ARRAY",
4533 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004534 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004535 }
4536
4537# check for initialized const char arrays that should be static const
4538 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4539 if (WARN("STATIC_CONST_CHAR_ARRAY",
4540 "const array should probably be static const\n" . $herecurr) &&
4541 $fix) {
4542 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4543 }
4544 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004545
4546# check for static char foo[] = "bar" declarations.
4547 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004548 WARN("STATIC_CONST_CHAR_ARRAY",
4549 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004550 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004551 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004552
Joe Perchesab7e23f2015-04-16 12:44:22 -07004553# check for const <foo> const where <foo> is not a pointer or array type
4554 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4555 my $found = $1;
4556 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4557 WARN("CONST_CONST",
4558 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4559 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4560 WARN("CONST_CONST",
4561 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4562 }
4563 }
4564
Joe Perches73169762020-12-15 20:44:30 -08004565# check for const static or static <non ptr type> const declarations
4566# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
4567 if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
4568 $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
4569 if (WARN("STATIC_CONST",
4570 "Move const after static - use 'static const $1'\n" . $herecurr) &&
4571 $fix) {
4572 $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
4573 $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
4574 }
4575 }
4576
Joe Perches9b0fa602014-04-03 14:49:18 -07004577# check for non-global char *foo[] = {"bar", ...} declarations.
4578 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4579 WARN("STATIC_CONST_CHAR_ARRAY",
4580 "char * array declaration might be better as static const\n" .
4581 $herecurr);
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08004582 }
Joe Perches9b0fa602014-04-03 14:49:18 -07004583
Joe Perchesb598b672015-04-16 12:44:36 -07004584# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4585 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4586 my $array = $1;
4587 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4588 my $array_div = $1;
4589 if (WARN("ARRAY_SIZE",
4590 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4591 $fix) {
4592 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4593 }
4594 }
4595 }
4596
Joe Perchesb36190c2014-01-27 17:07:18 -08004597# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004598 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004599 if (ERROR("FUNCTION_WITHOUT_ARGS",
4600 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4601 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004602 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004603 }
4604 }
4605
Andy Whitcroft653d4872007-06-23 17:16:34 -07004606# check for new typedefs, only function parameters and sparse annotations
4607# make sense.
4608 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004609 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004610 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004611 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004612 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004613 WARN("NEW_TYPEDEFS",
4614 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004615 }
4616
4617# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004618 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004619 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4620 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004621 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004622
Andy Whitcroft65863862009-01-06 14:41:21 -08004623 # Should start with a space.
4624 $to =~ s/^(\S)/ $1/;
4625 # Should not end with a space.
4626 $to =~ s/\s+$//;
4627 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004628 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004629 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004630
Joe Perches3705ce52013-07-03 15:05:31 -07004631## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004632 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004633 if (ERROR("POINTER_LOCATION",
4634 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4635 $fix) {
4636 my $sub_from = $ident;
4637 my $sub_to = $ident;
4638 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004639 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004640 s@\Q$sub_from\E@$sub_to@;
4641 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004642 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004643 }
4644 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4645 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004646 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004647
Andy Whitcroft65863862009-01-06 14:41:21 -08004648 # Should start with a space.
4649 $to =~ s/^(\S)/ $1/;
4650 # Should not end with a space.
4651 $to =~ s/\s+$//;
4652 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004653 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004654 }
4655 # Modifiers should have spaces.
4656 $to =~ s/(\b$Modifier$)/$1 /;
4657
Joe Perches3705ce52013-07-03 15:05:31 -07004658## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004659 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004660 if (ERROR("POINTER_LOCATION",
4661 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4662 $fix) {
4663
4664 my $sub_from = $match;
4665 my $sub_to = $match;
4666 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004667 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004668 s@\Q$sub_from\E@$sub_to@;
4669 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004670 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004671 }
4672
Joe Perches9d3e3c72015-09-09 15:37:27 -07004673# avoid BUG() or BUG_ON()
4674 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004675 my $msg_level = \&WARN;
4676 $msg_level = \&CHK if ($file);
4677 &{$msg_level}("AVOID_BUG",
4678 "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 -07004679 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004680
Joe Perches9d3e3c72015-09-09 15:37:27 -07004681# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004682 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004683 WARN("LINUX_VERSION_CODE",
4684 "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 -08004685 }
4686
Joe Perches17441222011-06-15 15:08:17 -07004687# check for uses of printk_ratelimit
4688 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004689 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004690 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004691 }
4692
Joe Percheseeef5732017-11-17 15:28:41 -08004693# printk should use KERN_* levels
4694 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4695 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4696 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004697 }
4698
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004699# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4700 if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4701 my $printk = $1;
4702 my $modifier = $2;
4703 my $orig = $3;
4704 $modifier = "" if (!defined($modifier));
Joe Perches243f3802012-05-31 16:26:09 -07004705 my $level = lc($orig);
4706 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004707 my $level2 = $level;
4708 $level2 = "dbg" if ($level eq "debug");
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004709 $level .= $modifier;
4710 $level2 .= $modifier;
Joe Perches243f3802012-05-31 16:26:09 -07004711 WARN("PREFER_PR_LEVEL",
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004712 "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 -07004713 }
4714
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004715# prefer dev_<level> to dev_printk(KERN_<LEVEL>
Joe Perchesdc139312013-02-21 16:44:13 -08004716 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4717 my $orig = $1;
4718 my $level = lc($orig);
4719 $level = "warn" if ($level eq "warning");
4720 $level = "dbg" if ($level eq "debug");
4721 WARN("PREFER_DEV_LEVEL",
4722 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4723 }
4724
Nicolas Boichat8020b252020-10-15 20:12:02 -07004725# trace_printk should not be used in production code.
4726 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4727 WARN("TRACE_PRINTK",
4728 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4729 }
4730
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004731# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4732# number of false positives, but assembly files are not checked, so at
4733# least the arch entry code will not trigger this warning.
4734 if ($line =~ /\bENOSYS\b/) {
4735 WARN("ENOSYS",
4736 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4737 }
4738
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004739# ENOTSUPP is not a standard error code and should be avoided in new patches.
4740# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4741# Similarly to ENOSYS warning a small number of false positives is expected.
4742 if (!$file && $line =~ /\bENOTSUPP\b/) {
4743 if (WARN("ENOTSUPP",
4744 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4745 $fix) {
4746 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4747 }
4748 }
4749
Andy Whitcroft653d4872007-06-23 17:16:34 -07004750# function brace can't be on same line, except for #defines of do while,
4751# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004752 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004753 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4754 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4755 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004756 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004757 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004758 $fix) {
4759 fix_delete_line($fixlinenr, $rawline);
4760 my $fixed_line = $rawline;
Dwaipayan Ray03f49352020-12-15 20:45:02 -08004761 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
Joe Perches8d182472014-08-06 16:11:12 -07004762 my $line1 = $1;
4763 my $line2 = $2;
4764 fix_insert_line($fixlinenr, ltrim($line1));
4765 fix_insert_line($fixlinenr, "\+{");
4766 if ($line2 !~ /^\s*$/) {
4767 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4768 }
4769 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004770 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004771
Andy Whitcroft8905a672007-11-28 16:21:06 -08004772# open braces for enum, union and struct go on the same line.
4773 if ($line =~ /^.\s*{/ &&
4774 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004775 if (ERROR("OPEN_BRACE",
4776 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4777 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4778 fix_delete_line($fixlinenr - 1, $prevrawline);
4779 fix_delete_line($fixlinenr, $rawline);
4780 my $fixedline = rtrim($prevrawline) . " {";
4781 fix_insert_line($fixlinenr, $fixedline);
4782 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004783 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004784 if ($fixedline !~ /^\+\s*$/) {
4785 fix_insert_line($fixlinenr, $fixedline);
4786 }
4787 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004788 }
4789
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004790# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004791 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4792 if (WARN("SPACING",
4793 "missing space after $1 definition\n" . $herecurr) &&
4794 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004795 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004796 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4797 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004798 }
4799
Joe Perches31070b52014-01-23 15:54:49 -08004800# Function pointer declarations
4801# check spacing between type, funcptr, and args
4802# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004803 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004804 my $declare = $1;
4805 my $pre_pointer_space = $2;
4806 my $post_pointer_space = $3;
4807 my $funcname = $4;
4808 my $post_funcname_space = $5;
4809 my $pre_args_space = $6;
4810
Joe Perches91f72e92014-04-03 14:49:12 -07004811# the $Declare variable will capture all spaces after the type
4812# so check it for a missing trailing missing space but pointer return types
4813# don't need a space so don't warn for those.
4814 my $post_declare_space = "";
4815 if ($declare =~ /(\s+)$/) {
4816 $post_declare_space = $1;
4817 $declare = rtrim($declare);
4818 }
4819 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004820 WARN("SPACING",
4821 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004822 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004823 }
4824
4825# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004826# This test is not currently implemented because these declarations are
4827# equivalent to
4828# int foo(int bar, ...)
4829# and this is form shouldn't/doesn't generate a checkpatch warning.
4830#
4831# elsif ($declare =~ /\s{2,}$/) {
4832# WARN("SPACING",
4833# "Multiple spaces after return type\n" . $herecurr);
4834# }
Joe Perches31070b52014-01-23 15:54:49 -08004835
4836# unnecessary space "type ( *funcptr)(args...)"
4837 if (defined $pre_pointer_space &&
4838 $pre_pointer_space =~ /^\s/) {
4839 WARN("SPACING",
4840 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4841 }
4842
4843# unnecessary space "type (* funcptr)(args...)"
4844 if (defined $post_pointer_space &&
4845 $post_pointer_space =~ /^\s/) {
4846 WARN("SPACING",
4847 "Unnecessary space before function pointer name\n" . $herecurr);
4848 }
4849
4850# unnecessary space "type (*funcptr )(args...)"
4851 if (defined $post_funcname_space &&
4852 $post_funcname_space =~ /^\s/) {
4853 WARN("SPACING",
4854 "Unnecessary space after function pointer name\n" . $herecurr);
4855 }
4856
4857# unnecessary space "type (*funcptr) (args...)"
4858 if (defined $pre_args_space &&
4859 $pre_args_space =~ /^\s/) {
4860 WARN("SPACING",
4861 "Unnecessary space before function pointer arguments\n" . $herecurr);
4862 }
4863
4864 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004865 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004866 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004867 }
4868 }
4869
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004870# check for spacing round square brackets; allowed:
4871# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004872# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4873# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004874 while ($line =~ /(.*?\s)\[/g) {
4875 my ($where, $prefix) = ($-[1], $1);
4876 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004877 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004878 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004879 if (ERROR("BRACKET_SPACE",
4880 "space prohibited before open square bracket '['\n" . $herecurr) &&
4881 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004882 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004883 s/^(\+.*?)\s+\[/$1\[/;
4884 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004885 }
4886 }
4887
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004888# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004889 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004890 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004891 my $ctx_before = substr($line, 0, $-[1]);
4892 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004893
4894 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004895 if ($name =~ /^(?:
4896 if|for|while|switch|return|case|
4897 volatile|__volatile__|
4898 __attribute__|format|__extension__|
4899 asm|__asm__)$/x)
4900 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004901 # cpp #define statements have non-optional spaces, ie
4902 # if there is a space between the name and the open
4903 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004904 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004905
4906 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004907 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004908
4909 # If this whole things ends with a type its most
4910 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004911 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004912
4913 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004914 if (WARN("SPACING",
4915 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4916 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004917 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004918 s/\b$name\s+\(/$name\(/;
4919 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004920 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004921 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004922
Andy Whitcroft653d4872007-06-23 17:16:34 -07004923# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004924 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004925 my $fixed_line = "";
4926 my $line_fixed = 0;
4927
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004928 my $ops = qr{
4929 <<=|>>=|<=|>=|==|!=|
4930 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4931 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004932 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004933 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004934 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004935 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004936
4937## print("element count: <" . $#elements . ">\n");
4938## foreach my $el (@elements) {
4939## print("el: <$el>\n");
4940## }
4941
4942 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004943 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004944
Joe Perches3705ce52013-07-03 15:05:31 -07004945 foreach my $el (@elements) {
4946 push(@fix_elements, substr($rawline, $off, length($el)));
4947 $off += length($el);
4948 }
4949
4950 $off = 0;
4951
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004952 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004953 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004954
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004955 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004956
4957 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4958
4959## print("n: <$n> good: <$good>\n");
4960
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004961 $off += length($elements[$n]);
4962
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004963 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004964 my $ca = substr($opline, 0, $off);
4965 my $cc = '';
4966 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4967 $cc = substr($opline, $off + length($elements[$n + 1]));
4968 }
4969 my $cb = "$ca$;$cc";
4970
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004971 my $a = '';
4972 $a = 'V' if ($elements[$n] ne '');
4973 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004974 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004975 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4976 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004977 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004978
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004979 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004980
4981 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004982 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004983 $c = 'V' if ($elements[$n + 2] ne '');
4984 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004985 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004986 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4987 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004988 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004989 } else {
4990 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004991 }
4992
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004993 my $ctx = "${a}x${c}";
4994
4995 my $at = "(ctx:$ctx)";
4996
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004997 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004998 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004999
Andy Whitcroft74048ed2008-07-23 21:29:10 -07005000 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005001 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005002
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005003 # Get the full operator variant.
5004 my $opv = $op . substr($curr_vars, $off, 1);
5005
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005006 # Ignore operators passed as parameters.
5007 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07005008 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005009
Andy Whitcroftcf655042008-03-04 14:28:20 -08005010# # Ignore comments
5011# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005012
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005013 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005014 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005015 if ($ctx !~ /.x[WEBC]/ &&
5016 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005017 if (ERROR("SPACING",
5018 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005019 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07005020 $line_fixed = 1;
5021 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005022 }
5023
5024 # // is a comment
5025 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005026
Joe Perchesb00e4812014-04-03 14:49:33 -07005027 # : when part of a bitfield
5028 } elsif ($opv eq ':B') {
5029 # skip the bitfield test for now
5030
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005031 # No spaces for:
5032 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07005033 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005034 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005035 if (ERROR("SPACING",
5036 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005037 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005038 if (defined $fix_elements[$n + 2]) {
5039 $fix_elements[$n + 2] =~ s/^\s+//;
5040 }
Joe Perchesb34c6482013-09-11 14:24:01 -07005041 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07005042 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005043 }
5044
Joe Perches23810972014-12-10 15:51:32 -08005045 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005046 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08005047 my $rtrim_before = 0;
5048 my $space_after = 0;
5049 if ($ctx =~ /Wx./) {
5050 if (ERROR("SPACING",
5051 "space prohibited before that '$op' $at\n" . $hereptr)) {
5052 $line_fixed = 1;
5053 $rtrim_before = 1;
5054 }
5055 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005056 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005057 if (ERROR("SPACING",
5058 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07005059 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07005060 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08005061 $space_after = 1;
5062 }
5063 }
5064 if ($rtrim_before || $space_after) {
5065 if ($rtrim_before) {
5066 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
5067 } else {
5068 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
5069 }
5070 if ($space_after) {
5071 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07005072 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005073 }
5074
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005075 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07005076 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005077 #warn "'*' is part of type\n";
5078
5079 # unary operators should have a space before and
5080 # none after. May be left adjacent to another
5081 # unary operator, or a cast
5082 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07005083 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07005084 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005085 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005086 if (ERROR("SPACING",
5087 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005088 if ($n != $last_after + 2) {
5089 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
5090 $line_fixed = 1;
5091 }
Joe Perches3705ce52013-07-03 15:05:31 -07005092 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005093 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08005094 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005095 # A unary '*' may be const
5096
5097 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005098 if (ERROR("SPACING",
5099 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005100 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005101 if (defined $fix_elements[$n + 2]) {
5102 $fix_elements[$n + 2] =~ s/^\s+//;
5103 }
Joe Perchesb34c6482013-09-11 14:24:01 -07005104 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07005105 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005106 }
5107
5108 # unary ++ and unary -- are allowed no space on one side.
5109 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005110 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005111 if (ERROR("SPACING",
5112 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005113 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07005114 $line_fixed = 1;
5115 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005116 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005117 if ($ctx =~ /Wx[BE]/ ||
5118 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07005119 if (ERROR("SPACING",
5120 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005121 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005122 $line_fixed = 1;
5123 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005124 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005125 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005126 if (ERROR("SPACING",
5127 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005128 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005129 if (defined $fix_elements[$n + 2]) {
5130 $fix_elements[$n + 2] =~ s/^\s+//;
5131 }
Joe Perchesb34c6482013-09-11 14:24:01 -07005132 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07005133 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005134 }
5135
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005136 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005137 } elsif ($op eq '<<' or $op eq '>>' or
5138 $op eq '&' or $op eq '^' or $op eq '|' or
5139 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005140 $op eq '*' or $op eq '/' or
5141 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005142 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08005143 if ($check) {
5144 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
5145 if (CHK("SPACING",
5146 "spaces preferred around that '$op' $at\n" . $hereptr)) {
5147 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5148 $fix_elements[$n + 2] =~ s/^\s+//;
5149 $line_fixed = 1;
5150 }
5151 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
5152 if (CHK("SPACING",
5153 "space preferred before that '$op' $at\n" . $hereptr)) {
5154 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
5155 $line_fixed = 1;
5156 }
5157 }
5158 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005159 if (ERROR("SPACING",
5160 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005161 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5162 if (defined $fix_elements[$n + 2]) {
5163 $fix_elements[$n + 2] =~ s/^\s+//;
5164 }
Joe Perches3705ce52013-07-03 15:05:31 -07005165 $line_fixed = 1;
5166 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005167 }
5168
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005169 # A colon needs no spaces before when it is
5170 # terminating a case value or a label.
5171 } elsif ($opv eq ':C' || $opv eq ':L') {
Chris Down263afd32021-02-25 17:22:04 -08005172 if ($ctx =~ /Wx./ and $realfile !~ m@.*\.lds\.h$@) {
Joe Perches3705ce52013-07-03 15:05:31 -07005173 if (ERROR("SPACING",
5174 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005175 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005176 $line_fixed = 1;
5177 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005178 }
5179
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005180 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08005181 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005182 my $ok = 0;
5183
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005184 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005185 if (($op eq '<' &&
5186 $cc =~ /^\S+\@\S+>/) ||
5187 ($op eq '>' &&
5188 $ca =~ /<\S+\@\S+$/))
5189 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07005190 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005191 }
5192
Joe Perchese0df7e12015-04-16 12:44:53 -07005193 # for asm volatile statements
5194 # ignore a colon with another
5195 # colon immediately before or after
5196 if (($op eq ':') &&
5197 ($ca =~ /:$/ || $cc =~ /^:/)) {
5198 $ok = 1;
5199 }
5200
Joe Perches84731622013-11-12 15:10:05 -08005201 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005202 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07005203 my $msg_level = \&ERROR;
5204 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08005205
Jean Delvare0675a8f2017-09-08 16:16:07 -07005206 if (&{$msg_level}("SPACING",
5207 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005208 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5209 if (defined $fix_elements[$n + 2]) {
5210 $fix_elements[$n + 2] =~ s/^\s+//;
5211 }
Joe Perches3705ce52013-07-03 15:05:31 -07005212 $line_fixed = 1;
5213 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005214 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005215 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005216 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005217
5218## print("n: <$n> GOOD: <$good>\n");
5219
5220 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005221 }
Joe Perches3705ce52013-07-03 15:05:31 -07005222
5223 if (($#elements % 2) == 0) {
5224 $fixed_line = $fixed_line . $fix_elements[$#elements];
5225 }
5226
Joe Perches194f66f2014-08-06 16:11:03 -07005227 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5228 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07005229 }
5230
5231
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005232 }
5233
Joe Perches786b6322013-07-03 15:05:32 -07005234# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08005235 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07005236 if (WARN("SPACING",
5237 "space prohibited before semicolon\n" . $herecurr) &&
5238 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005239 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07005240 s/^(\+.*\S)\s+;/$1;/;
5241 }
5242 }
5243
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005244# check for multiple assignments
5245 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005246 CHK("MULTIPLE_ASSIGNMENTS",
5247 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005248 }
5249
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005250## # check for multiple declarations, allowing for a function declaration
5251## # continuation.
5252## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
5253## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
5254##
5255## # Remove any bracketed sections to ensure we do not
Dwaipayan Raye73d2712020-12-15 20:44:56 -08005256## # falsely report the parameters of functions.
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005257## my $ln = $line;
5258## while ($ln =~ s/\([^\(\)]*\)//g) {
5259## }
5260## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005261## WARN("MULTIPLE_DECLARATION",
5262## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005263## }
5264## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005265
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005266#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07005267 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07005268 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005269 if (ERROR("SPACING",
5270 "space required before the open brace '{'\n" . $herecurr) &&
5271 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07005272 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07005273 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005274 }
5275
Joe Perchesc4a62ef2013-07-03 15:05:28 -07005276## # check for blank lines before declarations
5277## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5278## $prevrawline =~ /^.\s*$/) {
5279## WARN("SPACING",
5280## "No blank lines before declarations\n" . $hereprev);
5281## }
5282##
5283
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005284# closing brace should have a space following it when it has anything
5285# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07005286 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005287 if (ERROR("SPACING",
5288 "space required after that close brace '}'\n" . $herecurr) &&
5289 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005290 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07005291 s/}((?!(?:,|;|\)))\S)/} $1/;
5292 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005293 }
5294
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005295# check spacing on square brackets
5296 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005297 if (ERROR("SPACING",
5298 "space prohibited after that open square bracket '['\n" . $herecurr) &&
5299 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005300 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005301 s/\[\s+/\[/;
5302 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005303 }
5304 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005305 if (ERROR("SPACING",
5306 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
5307 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005308 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005309 s/\s+\]/\]/;
5310 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005311 }
5312
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005313# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005314 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
5315 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005316 if (ERROR("SPACING",
5317 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
5318 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005319 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005320 s/\(\s+/\(/;
5321 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005322 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005323 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005324 $line !~ /for\s*\(.*;\s+\)/ &&
5325 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005326 if (ERROR("SPACING",
5327 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
5328 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005329 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005330 s/\s+\)/\)/;
5331 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005332 }
5333
Joe Perchese2826fd2014-08-06 16:10:48 -07005334# check unnecessary parentheses around addressof/dereference single $Lvals
5335# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5336
5337 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08005338 my $var = $1;
5339 if (CHK("UNNECESSARY_PARENTHESES",
5340 "Unnecessary parentheses around $var\n" . $herecurr) &&
5341 $fix) {
5342 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5343 }
5344 }
5345
5346# check for unnecessary parentheses around function pointer uses
5347# ie: (foo->bar)(); should be foo->bar();
5348# but not "if (foo->bar) (" to avoid some false positives
5349 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5350 my $var = $2;
5351 if (CHK("UNNECESSARY_PARENTHESES",
5352 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5353 $fix) {
5354 my $var2 = deparenthesize($var);
5355 $var2 =~ s/\s//g;
5356 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5357 }
5358 }
Joe Perchese2826fd2014-08-06 16:10:48 -07005359
Joe Perches63b7c732017-09-08 16:16:01 -07005360# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08005361# when !drivers/staging or command-line uses --strict
5362 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07005363 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07005364 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
5365 my $if_stat = $1;
5366 my $test = substr($2, 1, -1);
5367 my $herectx;
5368 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
5369 my $match = $1;
5370 # avoid parentheses around potential macro args
5371 next if ($match =~ /^\s*\w+\s*$/);
5372 if (!defined($herectx)) {
5373 $herectx = $here . "\n";
5374 my $cnt = statement_rawlines($if_stat);
5375 for (my $n = 0; $n < $cnt; $n++) {
5376 my $rl = raw_line($linenr, $n);
5377 $herectx .= $rl . "\n";
5378 last if $rl =~ /^[ \+].*\{/;
5379 }
5380 }
5381 CHK("UNNECESSARY_PARENTHESES",
5382 "Unnecessary parentheses around '$match'\n" . $herectx);
5383 }
5384 }
5385
Joe Perches69078652021-06-30 18:56:22 -07005386# check that goto labels aren't indented (allow a single space indentation)
5387# and ignore bitfield definitions like foo:1
5388# Strictly, labels can have whitespace after the identifier and before the :
5389# but this is not allowed here as many ?: uses would appear to be labels
5390 if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ &&
5391 $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ &&
5392 $sline !~ /^.\s+default:/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005393 if (WARN("INDENTED_LABEL",
5394 "labels should not be indented\n" . $herecurr) &&
5395 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005396 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005397 s/^(.)\s+/$1/;
5398 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005399 }
5400
Joe Perches40873ab2020-10-15 20:11:56 -07005401# check if a statement with a comma should be two statements like:
5402# foo = bar(), /* comma should be semicolon */
5403# bar = baz();
5404 if (defined($stat) &&
5405 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5406 my $cnt = statement_rawlines($stat);
5407 my $herectx = get_stat_here($linenr, $cnt, $here);
5408 WARN("SUSPECT_COMMA_SEMICOLON",
5409 "Possible comma where semicolon could be used\n" . $herectx);
5410 }
5411
Joe Perches5b9553a2014-04-03 14:49:21 -07005412# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08005413 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005414 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07005415 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07005416 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5417 my $value = $1;
5418 $value = deparenthesize($value);
5419 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5420 ERROR("RETURN_PARENTHESES",
5421 "return is not a function, parentheses are not required\n" . $herecurr);
5422 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005423 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005424 ERROR("SPACING",
5425 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005426 }
5427 }
Joe Perches507e5142013-11-12 15:10:13 -08005428
Joe Perchesb43ae212014-06-23 13:22:07 -07005429# unnecessary return in a void function
5430# at end-of-function, with the previous line a single leading tab, then return;
5431# and the line before that not a goto label target like "out:"
5432 if ($sline =~ /^[ \+]}\s*$/ &&
5433 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5434 $linenr >= 3 &&
5435 $lines[$linenr - 3] =~ /^[ +]/ &&
5436 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07005437 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07005438 "void function return statements are not generally useful\n" . $hereprev);
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08005439 }
Joe Perches9819cf22014-06-04 16:12:09 -07005440
Joe Perches189248d2014-01-23 15:54:47 -08005441# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07005442 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08005443 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5444 my $openparens = $1;
5445 my $count = $openparens =~ tr@\(@\(@;
5446 my $msg = "";
5447 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5448 my $comp = $4; #Not $1 because of $LvalOrFunc
5449 $msg = " - maybe == should be = ?" if ($comp eq "==");
5450 WARN("UNNECESSARY_PARENTHESES",
5451 "Unnecessary parentheses$msg\n" . $herecurr);
5452 }
5453 }
5454
Joe Perchesc5595fa2015-09-09 15:37:58 -07005455# comparisons with a constant or upper case identifier on the left
5456# avoid cases like "foo + BAR < baz"
5457# only fix matches surrounded by parentheses to avoid incorrect
5458# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07005459 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005460 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5461 my $lead = $1;
5462 my $const = $2;
5463 my $comp = $3;
5464 my $to = $4;
5465 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07005466 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005467 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5468 WARN("CONSTANT_COMPARISON",
5469 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5470 $fix) {
5471 if ($comp eq "<") {
5472 $newcomp = ">";
5473 } elsif ($comp eq "<=") {
5474 $newcomp = ">=";
5475 } elsif ($comp eq ">") {
5476 $newcomp = "<";
5477 } elsif ($comp eq ">=") {
5478 $newcomp = "<=";
5479 }
5480 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5481 }
5482 }
5483
Joe Perchesf34e4a42015-04-16 12:44:19 -07005484# Return of what appears to be an errno should normally be negative
5485 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005486 my $name = $1;
Guenter Roeck46b85bf2021-06-30 18:56:25 -07005487 if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005488 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07005489 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005490 }
5491 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005492
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005493# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07005494 if ($line =~ /\b(if|while|for|switch)\(/) {
5495 if (ERROR("SPACING",
5496 "space required before the open parenthesis '('\n" . $herecurr) &&
5497 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005498 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005499 s/\b(if|while|for|switch)\(/$1 \(/;
5500 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005501 }
5502
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07005503# Check for illegal assignment in if conditional -- and check for trailing
5504# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005505 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08005506 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5507 ctx_statement_block($linenr, $realcnt, 0)
5508 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005509 my ($stat_next) = ctx_statement_block($line_nr_next,
5510 $remain_next, $off_next);
5511 $stat_next =~ s/\n./\n /g;
5512 ##print "stat<$stat> stat_next<$stat_next>\n";
5513
5514 if ($stat_next =~ /^\s*while\b/) {
5515 # If the statement carries leading newlines,
5516 # then count those as offsets.
5517 my ($whitespace) =
5518 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5519 my $offset =
5520 statement_rawlines($whitespace) - 1;
5521
5522 $suppress_whiletrailers{$line_nr_next +
5523 $offset} = 1;
5524 }
5525 }
5526 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005527 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005528 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005529 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005530
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005531 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches65b64b32020-08-11 18:35:10 -07005532 if (ERROR("ASSIGN_IN_IF",
5533 "do not use assignment in if condition\n" . $herecurr) &&
5534 $fix && $perl_version_ok) {
5535 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5536 my $space = $1;
5537 my $not = $2;
5538 my $statement = $3;
5539 my $assigned = $4;
5540 my $test = $8;
5541 my $against = $9;
5542 my $brace = $15;
5543 fix_delete_line($fixlinenr, $rawline);
5544 fix_insert_line($fixlinenr, "$space$statement;");
5545 my $newline = "${space}if (";
5546 $newline .= '!' if defined($not);
5547 $newline .= '(' if (defined $not && defined($test) && defined($against));
5548 $newline .= "$assigned";
5549 $newline .= " $test $against" if (defined($test) && defined($against));
5550 $newline .= ')' if (defined $not && defined($test) && defined($against));
5551 $newline .= ')';
5552 $newline .= " {" if (defined($brace));
5553 fix_insert_line($fixlinenr + 1, $newline);
5554 }
5555 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005556 }
5557
5558 # Find out what is on the end of the line after the
5559 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005560 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005561 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005562 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005563 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5564 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005565 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005566 # Find out how long the conditional actually is.
5567 my @newlines = ($c =~ /\n/gs);
5568 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005569 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005570
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005571 $stat_real = raw_line($linenr, $cond_lines)
5572 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005573 if (defined($stat_real) && $cond_lines > 1) {
5574 $stat_real = "[...]\n$stat_real";
5575 }
5576
Joe Perches000d1cc12011-07-25 17:13:25 -07005577 ERROR("TRAILING_STATEMENTS",
5578 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005579 }
5580 }
5581
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005582# Check for bitwise tests written as boolean
5583 if ($line =~ /
5584 (?:
5585 (?:\[|\(|\&\&|\|\|)
5586 \s*0[xX][0-9]+\s*
5587 (?:\&\&|\|\|)
5588 |
5589 (?:\&\&|\|\|)
5590 \s*0[xX][0-9]+\s*
5591 (?:\&\&|\|\||\)|\])
5592 )/x)
5593 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005594 WARN("HEXADECIMAL_BOOLEAN_TEST",
5595 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005596 }
5597
Andy Whitcroft8905a672007-11-28 16:21:06 -08005598# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005599 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5600 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005601 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005602 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005603 ERROR("TRAILING_STATEMENTS",
5604 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005605 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005606 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005607# if should not continue a brace
5608 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005609 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005610 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005611 $herecurr);
5612 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005613# case and default should not have general statements after them
5614 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5615 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005616 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005617 \s*return\s+
5618 )/xg)
5619 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005620 ERROR("TRAILING_STATEMENTS",
5621 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005622 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005623
5624 # Check for }<nl>else {, these must be at the same
5625 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005626 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5627 $previndent == $indent) {
5628 if (ERROR("ELSE_AFTER_BRACE",
5629 "else should follow close brace '}'\n" . $hereprev) &&
5630 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5631 fix_delete_line($fixlinenr - 1, $prevrawline);
5632 fix_delete_line($fixlinenr, $rawline);
5633 my $fixedline = $prevrawline;
5634 $fixedline =~ s/}\s*$//;
5635 if ($fixedline !~ /^\+\s*$/) {
5636 fix_insert_line($fixlinenr, $fixedline);
5637 }
5638 $fixedline = $rawline;
5639 $fixedline =~ s/^(.\s*)else/$1} else/;
5640 fix_insert_line($fixlinenr, $fixedline);
5641 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005642 }
5643
Joe Perches8b8856f2014-08-06 16:11:14 -07005644 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5645 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005646 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5647
5648 # Find out what is on the end of the line after the
5649 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005650 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005651 $s =~ s/\n.*//g;
5652
5653 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005654 if (ERROR("WHILE_AFTER_BRACE",
5655 "while should follow close brace '}'\n" . $hereprev) &&
5656 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5657 fix_delete_line($fixlinenr - 1, $prevrawline);
5658 fix_delete_line($fixlinenr, $rawline);
5659 my $fixedline = $prevrawline;
5660 my $trailing = $rawline;
5661 $trailing =~ s/^\+//;
5662 $trailing = trim($trailing);
5663 $fixedline =~ s/}\s*$/} $trailing/;
5664 fix_insert_line($fixlinenr, $fixedline);
5665 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005666 }
5667 }
5668
Joe Perches95e2c602013-07-03 15:05:20 -07005669#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005670 while ($line =~ m{($Constant|$Lval)}g) {
5671 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005672
Joe Perches95e2c602013-07-03 15:05:20 -07005673#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005674 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005675 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Łukasz Stelmach4104a202020-12-15 20:44:27 -08005676#Ignore some autogenerated defines and enum values
5677 $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005678#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005679 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005680#Ignore SI style variants like nS, mV and dB
5681#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5682 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005683#Ignore some three character SI units explicitly, like MiB and KHz
5684 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005685 while ($var =~ m{($Ident)}g) {
5686 my $word = $1;
5687 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005688 if ($check) {
5689 seed_camelcase_includes();
5690 if (!$file && !$camelcase_file_seeded) {
5691 seed_camelcase_file($realfile);
5692 $camelcase_file_seeded = 1;
5693 }
5694 }
Joe Perches7e781f62013-09-11 14:23:55 -07005695 if (!defined $camelcase{$word}) {
5696 $camelcase{$word} = 1;
5697 CHK("CAMELCASE",
5698 "Avoid CamelCase: <$word>\n" . $herecurr);
5699 }
Joe Perches34456862013-07-03 15:05:34 -07005700 }
Joe Perches323c1262012-12-17 16:02:07 -08005701 }
5702 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005703
5704#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005705 if ($line =~ /\#\s*define.*\\\s+$/) {
5706 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5707 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5708 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005709 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005710 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005711 }
5712
Fabian Frederick0e212e02015-04-16 12:44:25 -07005713# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5714# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005715 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005716 my $file = "$1.h";
5717 my $checkfile = "include/linux/$file";
5718 if (-f "$root/$checkfile" &&
5719 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005720 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005721 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005722 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5723 if ($asminclude > 0) {
5724 if ($realfile =~ m{^arch/}) {
5725 CHK("ARCH_INCLUDE_LINUX",
5726 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5727 } else {
5728 WARN("INCLUDE_LINUX",
5729 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5730 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005731 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005732 }
5733 }
5734
Andy Whitcroft653d4872007-06-23 17:16:34 -07005735# multi-statement macros should be enclosed in a do while loop, grab the
5736# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005737# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07005738 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5739 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005740 my $ln = $linenr;
5741 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005742 my ($off, $dstat, $dcond, $rest);
5743 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005744 my $has_flow_statement = 0;
5745 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005746 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005747 ctx_statement_block($linenr, $realcnt, 0);
5748 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005749 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005750 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005751
Joe Perches08a28432014-10-13 15:51:55 -07005752 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005753 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005754
Joe Perchesf59b64b2016-10-11 13:52:08 -07005755 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5756 my $define_args = $1;
5757 my $define_stmt = $dstat;
5758 my @def_args = ();
5759
5760 if (defined $define_args && $define_args ne "") {
5761 $define_args = substr($define_args, 1, length($define_args) - 2);
5762 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005763 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005764 @def_args = split(",", $define_args);
5765 }
5766
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005767 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005768 $dstat =~ s/\\\n.//g;
5769 $dstat =~ s/^\s*//s;
5770 $dstat =~ s/\s*$//s;
5771
5772 # Flatten any parentheses and braces
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005773 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5774 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5775 $dstat =~ s/.\[[^\[\]]*\]/1u/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005776 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005777 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005778
Antonio Borneo342d3d22020-04-06 20:11:01 -07005779 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005780 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5781 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005782 {
5783 }
5784
Joe Perches42e15292016-03-15 14:58:01 -07005785 # Make asm volatile uses seem like a generic function
5786 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5787
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005788 my $exceptions = qr{
5789 $Declare|
5790 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005791 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005792 DECLARE_PER_CPU|
5793 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005794 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005795 union|
5796 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005797 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005798 ^\"|\"$|
5799 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005800 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005801 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005802
5803 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005804 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005805 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005806
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005807 if ($dstat ne '' &&
5808 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5809 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005810 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005811 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005812 $dstat !~ /$exceptions/ &&
5813 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005814 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005815 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005816 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005817 $dstat !~ /^for\s*$Constant$/ && # for (...)
5818 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5819 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005820 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005821 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005822 {
Joe Perchese7955562017-05-08 15:55:48 -07005823 if ($dstat =~ /^\s*if\b/) {
5824 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5825 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5826 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005827 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5828 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5829 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005830 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005831 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005832 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005833
5834 }
Joe Perches52076492016-10-11 13:52:14 -07005835
5836 # Make $define_stmt single line, comment-free, etc
5837 my @stmt_array = split('\n', $define_stmt);
5838 my $first = 1;
5839 $define_stmt = "";
5840 foreach my $l (@stmt_array) {
5841 $l =~ s/\\$//;
5842 if ($first) {
5843 $define_stmt = $l;
5844 $first = 0;
5845 } elsif ($l =~ /^[\+ ]/) {
5846 $define_stmt .= substr($l, 1);
5847 }
5848 }
5849 $define_stmt =~ s/$;//g;
5850 $define_stmt =~ s/\s+/ /g;
5851 $define_stmt = trim($define_stmt);
5852
Joe Perchesf59b64b2016-10-11 13:52:08 -07005853# check if any macro arguments are reused (ignore '...' and 'type')
5854 foreach my $arg (@def_args) {
5855 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005856 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005857 my $tmp_stmt = $define_stmt;
Vincent Mailhol7b844342021-05-06 18:03:58 -07005858 $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 -07005859 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5860 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005861 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005862 if ($use_cnt > 1) {
5863 CHK("MACRO_ARG_REUSE",
5864 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005865 }
5866# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005867 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005868 ((defined($1) && $1 ne ',') ||
5869 (defined($2) && $2 ne ','))) {
5870 CHK("MACRO_ARG_PRECEDENCE",
5871 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005872 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005873 }
Joe Perches5023d342012-12-17 16:01:47 -08005874
Joe Perches08a28432014-10-13 15:51:55 -07005875# check for macros with flow control, but without ## concatenation
5876# ## concatenation is commonly a macro that defines a function so ignore those
5877 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005878 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005879 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005880
Joe Perches08a28432014-10-13 15:51:55 -07005881 WARN("MACRO_WITH_FLOW_CONTROL",
5882 "Macros with flow control statements should be avoided\n" . "$herectx");
5883 }
5884
Joe Perches481eb482012-12-17 16:01:56 -08005885# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005886
5887 } else {
5888 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005889 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5890 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005891 $line =~ /^\+.*\\$/) {
5892 WARN("LINE_CONTINUATIONS",
5893 "Avoid unnecessary line continuations\n" . $herecurr);
5894 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005895 }
5896
Joe Perchesb13edf72012-07-30 14:41:24 -07005897# do {} while (0) macro tests:
5898# single-statement macros do not need to be enclosed in do while (0) loop,
5899# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005900 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005901 $realfile !~ m@/vmlinux.lds.h$@ &&
5902 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5903 my $ln = $linenr;
5904 my $cnt = $realcnt;
5905 my ($off, $dstat, $dcond, $rest);
5906 my $ctx = '';
5907 ($dstat, $dcond, $ln, $cnt, $off) =
5908 ctx_statement_block($linenr, $realcnt, 0);
5909 $ctx = $dstat;
5910
5911 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005912 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005913
5914 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5915 my $stmts = $2;
5916 my $semis = $3;
5917
5918 $ctx =~ s/\n*$//;
5919 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005920 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005921
Joe Perchesac8e97f2012-08-21 16:15:53 -07005922 if (($stmts =~ tr/;/;/) == 1 &&
5923 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005924 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5925 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5926 }
5927 if (defined $semis && $semis ne "") {
5928 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5929 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5930 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005931 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5932 $ctx =~ s/\n*$//;
5933 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005934 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005935
5936 WARN("TRAILING_SEMICOLON",
5937 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005938 }
5939 }
5940
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005941# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005942 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5943 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005944 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005945 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005946 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5947 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005948 my @allowed = ();
5949 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005950 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005951 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005952 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005953 for my $chunk (@chunks) {
5954 my ($cond, $block) = @{$chunk};
5955
Andy Whitcroft773647a2008-03-28 14:15:58 -07005956 # If the condition carries leading newlines, then count those as offsets.
5957 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5958 my $offset = statement_rawlines($whitespace) - 1;
5959
Joe Perchesaad4f612012-03-23 15:02:19 -07005960 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005961 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5962
5963 # We have looked at and allowed this specific line.
5964 $suppress_ifbraces{$ln + $offset} = 1;
5965
5966 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005967 $ln += statement_rawlines($block) - 1;
5968
Andy Whitcroft773647a2008-03-28 14:15:58 -07005969 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005970
5971 $seen++ if ($block =~ /^\s*{/);
5972
Joe Perchesaad4f612012-03-23 15:02:19 -07005973 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005974 if (statement_lines($cond) > 1) {
5975 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005976 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005977 }
5978 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005979 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005980 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005981 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005982 if (statement_block_size($block) > 1) {
5983 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005984 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005985 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005986 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005987 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005988 if ($seen) {
5989 my $sum_allowed = 0;
5990 foreach (@allowed) {
5991 $sum_allowed += $_;
5992 }
5993 if ($sum_allowed == 0) {
5994 WARN("BRACES",
5995 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5996 } elsif ($sum_allowed != $allow &&
5997 $seen != $allow) {
5998 CHK("BRACES",
5999 "braces {} should be used on all arms of this statement\n" . $herectx);
6000 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006001 }
6002 }
6003 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006004 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006005 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08006006 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006007
Andy Whitcroftcf655042008-03-04 14:28:20 -08006008 # Check the pre-context.
6009 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
6010 #print "APW: ALLOWED: pre<$1>\n";
6011 $allowed = 1;
6012 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006013
6014 my ($level, $endln, @chunks) =
6015 ctx_statement_full($linenr, $realcnt, $-[0]);
6016
Andy Whitcroftcf655042008-03-04 14:28:20 -08006017 # Check the condition.
6018 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07006019 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08006020 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07006021 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08006022 }
6023 if (statement_lines($cond) > 1) {
6024 #print "APW: ALLOWED: cond<$cond>\n";
6025 $allowed = 1;
6026 }
6027 if ($block =~/\b(?:if|for|while)\b/) {
6028 #print "APW: ALLOWED: block<$block>\n";
6029 $allowed = 1;
6030 }
6031 if (statement_block_size($block) > 1) {
6032 #print "APW: ALLOWED: lines block<$block>\n";
6033 $allowed = 1;
6034 }
6035 # Check the post-context.
6036 if (defined $chunks[1]) {
6037 my ($cond, $block) = @{$chunks[1]};
6038 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07006039 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006040 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08006041 if ($block =~ /^\s*\{/) {
6042 #print "APW: ALLOWED: chunk-1 block<$block>\n";
6043 $allowed = 1;
6044 }
6045 }
6046 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07006047 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006048 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08006049
Joe Perches000d1cc12011-07-25 17:13:25 -07006050 WARN("BRACES",
6051 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006052 }
6053 }
6054
Joe Perchese4c5bab2017-02-24 15:01:41 -08006055# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08006056 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
6057 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08006058 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
6059 }
6060
Joe Perches0979ae62012-12-17 16:01:59 -08006061# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07006062 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08006063 if (CHK("BRACES",
6064 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
6065 $fix && $prevrawline =~ /^\+/) {
6066 fix_delete_line($fixlinenr - 1, $prevrawline);
6067 }
Joe Perches0979ae62012-12-17 16:01:59 -08006068 }
Joe Perches77b9a532013-07-03 15:05:29 -07006069 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08006070 if (CHK("BRACES",
6071 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
6072 $fix) {
6073 fix_delete_line($fixlinenr, $rawline);
6074 }
Joe Perches0979ae62012-12-17 16:01:59 -08006075 }
6076
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006077# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07006078 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
6079 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006080 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006081 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006082 }
6083
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006084# Check for user-visible strings broken across lines, which breaks the ability
6085# to grep for the string. Make exceptions when the previous string ends in a
6086# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
6087# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07006088 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006089 $prevline =~ /"\s*$/ &&
6090 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
6091 if (WARN("SPLIT_STRING",
6092 "quoted string split across lines\n" . $hereprev) &&
6093 $fix &&
6094 $prevrawline =~ /^\+.*"\s*$/ &&
6095 $last_coalesced_string_linenr != $linenr - 1) {
6096 my $extracted_string = get_quoted_string($line, $rawline);
6097 my $comma_close = "";
6098 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
6099 $comma_close = $1;
6100 }
6101
6102 fix_delete_line($fixlinenr - 1, $prevrawline);
6103 fix_delete_line($fixlinenr, $rawline);
6104 my $fixedline = $prevrawline;
6105 $fixedline =~ s/"\s*$//;
6106 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
6107 fix_insert_line($fixlinenr - 1, $fixedline);
6108 $fixedline = $rawline;
6109 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
6110 if ($fixedline !~ /\+\s*$/) {
6111 fix_insert_line($fixlinenr, $fixedline);
6112 }
6113 $last_coalesced_string_linenr = $linenr;
6114 }
6115 }
6116
6117# check for missing a space in a string concatenation
6118 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
6119 WARN('MISSING_SPACE',
6120 "break quoted strings at a space character\n" . $hereprev);
6121 }
6122
Joe Perchese4b7d302017-05-08 15:55:51 -07006123# check for an embedded function name in a string when the function is known
6124# This does not work very well for -f --file checking as it depends on patch
6125# context providing the function name or a single line form for in-file
6126# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08006127 if ($line =~ /^\+.*$String/ &&
6128 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07006129 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
6130 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08006131 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07006132 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08006133 }
6134
Joe Perchesadb2da82021-02-25 17:21:50 -08006135# check for unnecessary function tracing like uses
6136# This does not use $logFunctions because there are many instances like
6137# 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions
6138 if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) {
6139 if (WARN("TRACING_LOGGING",
6140 "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) &&
6141 $fix) {
6142 fix_delete_line($fixlinenr, $rawline);
6143 }
6144 }
6145
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006146# check for spaces before a quoted newline
6147 if ($rawline =~ /^.*\".*\s\\n/) {
6148 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
6149 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
6150 $fix) {
6151 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
6152 }
6153
6154 }
6155
Joe Perchesf17dba42014-10-13 15:51:51 -07006156# concatenated string without spaces between elements
Joe Perchesd2af5aa2021-09-07 19:59:51 -07006157 if ($line =~ /$String[A-Z_]/ ||
6158 ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) {
Joe Perches79682c02018-08-21 21:57:29 -07006159 if (CHK("CONCATENATED_STRING",
6160 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
6161 $fix) {
6162 while ($line =~ /($String)/g) {
6163 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6164 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
6165 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
6166 }
6167 }
Joe Perchesf17dba42014-10-13 15:51:51 -07006168 }
6169
Joe Perches90ad30e2014-12-10 15:51:59 -08006170# uncoalesced string fragments
Joe Perchesd2af5aa2021-09-07 19:59:51 -07006171 if ($line =~ /$String\s*[Lu]?"/) {
Joe Perches79682c02018-08-21 21:57:29 -07006172 if (WARN("STRING_FRAGMENTS",
6173 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
6174 $fix) {
6175 while ($line =~ /($String)(?=\s*")/g) {
6176 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6177 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
6178 }
6179 }
Joe Perches90ad30e2014-12-10 15:51:59 -08006180 }
6181
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006182# check for non-standard and hex prefixed decimal printf formats
6183 my $show_L = 1; #don't show the same defect twice
6184 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006185 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006186 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006187 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006188 # check for %L
6189 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006190 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006191 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6192 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006193 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006194 # check for %Z
6195 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6196 WARN("PRINTF_Z",
6197 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6198 $show_Z = 0;
6199 }
6200 # check for 0x<decimal>
6201 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6202 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07006203 "Prefixing 0x with decimal output is defective\n" . $herecurr);
6204 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006205 }
6206
6207# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08006208 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006209 WARN("LINE_CONTINUATIONS",
6210 "Avoid line continuations in quoted strings\n" . $herecurr);
6211 }
6212
Andy Whitcroft00df3442007-06-08 13:47:06 -07006213# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006214 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07006215 WARN("IF_0",
6216 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
6217 }
6218
6219# warn about #if 1
6220 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
6221 WARN("IF_1",
6222 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006223 }
6224
Andy Whitcroft03df4b52012-12-17 16:01:52 -08006225# check for needless "if (<foo>) fn(<foo>)" uses
6226 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07006227 my $tested = quotemeta($1);
6228 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6229 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6230 my $func = $1;
6231 if (WARN('NEEDLESS_IF',
6232 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6233 $fix) {
6234 my $do_fix = 1;
6235 my $leading_tabs = "";
6236 my $new_leading_tabs = "";
6237 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6238 $leading_tabs = $1;
6239 } else {
6240 $do_fix = 0;
6241 }
6242 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6243 $new_leading_tabs = $1;
6244 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6245 $do_fix = 0;
6246 }
6247 } else {
6248 $do_fix = 0;
6249 }
6250 if ($do_fix) {
6251 fix_delete_line($fixlinenr - 1, $prevrawline);
6252 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6253 }
6254 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07006255 }
6256 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006257
Joe Perchesebfdc402014-08-06 16:10:27 -07006258# check for unnecessary "Out of Memory" messages
6259 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6260 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6261 (defined $1 || defined $3) &&
6262 $linenr > 3) {
6263 my $testval = $2;
6264 my $testline = $lines[$linenr - 3];
6265
6266 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6267# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6268
Joe Perchese29a70f2019-03-07 16:28:35 -08006269 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6270 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07006271 WARN("OOM_MESSAGE",
6272 "Possible unnecessary 'out of memory' message\n" . $hereprev);
6273 }
6274 }
6275
Joe Perchesf78d98f2014-10-13 15:52:01 -07006276# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08006277 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07006278 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6279 my $level = $1;
6280 if (WARN("UNNECESSARY_KERN_LEVEL",
6281 "Possible unnecessary $level\n" . $herecurr) &&
6282 $fix) {
6283 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
6284 }
6285 }
6286
Joe Perches45c55e92017-02-24 15:01:31 -08006287# check for logging continuations
6288 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
6289 WARN("LOGGING_CONTINUATION",
6290 "Avoid logging continuation uses where feasible\n" . $herecurr);
6291 }
6292
Dwaipayan Ray70eb2272020-12-15 20:45:15 -08006293# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
6294 if (defined $stat &&
6295 $line =~ /\b$logFunctions\s*\(/ &&
6296 index($stat, '"') >= 0) {
6297 my $lc = $stat =~ tr@\n@@;
6298 $lc = $lc + $linenr;
6299 my $stat_real = get_stat_real($linenr, $lc);
6300 pos($stat_real) = index($stat_real, '"');
6301 while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
6302 my $pspec = $1;
6303 my $h = $2;
6304 my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
6305 if (WARN("UNNECESSARY_MODIFIER",
6306 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
6307 $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
6308 my $nspec = $pspec;
6309 $nspec =~ s/h//g;
6310 $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
6311 }
6312 }
6313 }
6314
Joe Perchesabb08a52014-12-10 15:51:46 -08006315# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07006316 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08006317 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6318 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6319 WARN("MASK_THEN_SHIFT",
6320 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6321 }
6322
Joe Perchesb75ac612014-12-10 15:52:02 -08006323# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07006324 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08006325 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6326 my $val = $1;
6327 my $equal = "!";
6328 $equal = "" if ($4 eq "!=");
6329 if (CHK("COMPARISON_TO_NULL",
6330 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6331 $fix) {
6332 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6333 }
6334 }
6335 }
6336
Joe Perches8716de32013-09-11 14:24:05 -07006337# check for bad placement of section $InitAttribute (e.g.: __initdata)
6338 if ($line =~ /(\b$InitAttribute\b)/) {
6339 my $attr = $1;
6340 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
6341 my $ptr = $1;
6342 my $var = $2;
6343 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
6344 ERROR("MISPLACED_INIT",
6345 "$attr should be placed after $var\n" . $herecurr)) ||
6346 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
6347 WARN("MISPLACED_INIT",
6348 "$attr should be placed after $var\n" . $herecurr))) &&
6349 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006350 $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 -07006351 }
6352 }
6353 }
6354
Joe Perchese970b8842013-11-12 15:10:10 -08006355# check for $InitAttributeData (ie: __initdata) with const
6356 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6357 my $attr = $1;
6358 $attr =~ /($InitAttributePrefix)(.*)/;
6359 my $attr_prefix = $1;
6360 my $attr_type = $2;
6361 if (ERROR("INIT_ATTRIBUTE",
6362 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6363 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006364 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006365 s/$InitAttributeData/${attr_prefix}initconst/;
6366 }
6367 }
6368
6369# check for $InitAttributeConst (ie: __initconst) without const
6370 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6371 my $attr = $1;
6372 if (ERROR("INIT_ATTRIBUTE",
6373 "Use of $attr requires a separate use of const\n" . $herecurr) &&
6374 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006375 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006376 /(^\+\s*(?:static\s+))/;
6377 $lead = rtrim($1);
6378 $lead = "$lead " if ($lead !~ /^\+$/);
6379 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07006380 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08006381 }
6382 }
6383
Joe Perchesc17893c2015-04-16 12:44:42 -07006384# check for __read_mostly with const non-pointer (should just be const)
6385 if ($line =~ /\b__read_mostly\b/ &&
6386 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6387 if (ERROR("CONST_READ_MOSTLY",
6388 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6389 $fix) {
6390 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6391 }
6392 }
6393
Joe Perchesfbdb8132014-04-03 14:49:14 -07006394# don't use __constant_<foo> functions outside of include/uapi/
6395 if ($realfile !~ m@^include/uapi/@ &&
6396 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6397 my $constant_func = $1;
6398 my $func = $constant_func;
6399 $func =~ s/^__constant_//;
6400 if (WARN("CONSTANT_CONVERSION",
6401 "$constant_func should be $func\n" . $herecurr) &&
6402 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006403 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07006404 }
6405 }
6406
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006407# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08006408 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07006409 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006410 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07006411 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006412 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006413 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07006414 }
6415 if ($delay > 2000) {
6416 WARN("LONG_UDELAY",
6417 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006418 }
6419 }
6420
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006421# warn about unexpectedly long msleep's
6422 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6423 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006424 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006425 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006426 }
6427 }
6428
Joe Perches36ec1932013-07-03 15:05:25 -07006429# check for comparisons of jiffies
6430 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6431 WARN("JIFFIES_COMPARISON",
6432 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6433 }
6434
Joe Perches9d7a34a2013-07-03 15:05:26 -07006435# check for comparisons of get_jiffies_64()
6436 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6437 WARN("JIFFIES_COMPARISON",
6438 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6439 }
6440
Andy Whitcroft00df3442007-06-08 13:47:06 -07006441# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006442# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07006443# print "#ifdef in C files should be avoided\n";
6444# print "$herecurr";
6445# $clean = 0;
6446# }
6447
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006448# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006449 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07006450 if (ERROR("SPACING",
6451 "exactly one space required after that #$1\n" . $herecurr) &&
6452 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006453 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07006454 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6455 }
6456
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006457 }
6458
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006459# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006460 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6461 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006462 my $which = $1;
6463 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006464 CHK("UNCOMMENTED_DEFINITION",
6465 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006466 }
6467 }
6468# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006469
6470 my $barriers = qr{
6471 mb|
6472 rmb|
Will Deaconad83ec62019-11-07 14:49:00 +00006473 wmb
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006474 }x;
6475 my $barrier_stems = qr{
6476 mb__before_atomic|
6477 mb__after_atomic|
6478 store_release|
6479 load_acquire|
6480 store_mb|
6481 (?:$barriers)
6482 }x;
6483 my $all_barriers = qr{
6484 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02006485 smp_(?:$barrier_stems)|
6486 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006487 }x;
6488
6489 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006490 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08006491 WARN("MEMORY_BARRIER",
6492 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006493 }
6494 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006495
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02006496 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6497
6498 if ($realfile !~ m@^include/asm-generic/@ &&
6499 $realfile !~ m@/barrier\.h$@ &&
6500 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6501 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6502 WARN("MEMORY_BARRIER",
6503 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6504 }
6505
Joe Perchescb426e92015-06-25 15:02:46 -07006506# check for waitqueue_active without a comment.
6507 if ($line =~ /\bwaitqueue_active\s*\(/) {
6508 if (!ctx_has_comment($first_line, $linenr)) {
6509 WARN("WAITQUEUE_ACTIVE",
6510 "waitqueue_active without comment\n" . $herecurr);
6511 }
6512 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006513
Marco Elver5099a722020-04-01 12:17:14 +02006514# check for data_race without a comment.
6515 if ($line =~ /\bdata_race\s*\(/) {
6516 if (!ctx_has_comment($first_line, $linenr)) {
6517 WARN("DATA_RACE",
6518 "data_race without comment\n" . $herecurr);
6519 }
6520 }
6521
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006522# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006523 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006524 CHK("ARCH_DEFINES",
6525 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006526 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006527
Joe Perches596ed452017-07-12 14:37:02 -07006528# check that the storage class is not after a type
6529 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006530 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07006531 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6532 }
6533# Check that the storage class is at the beginning of a declaration
6534 if ($line =~ /\b$Storage\b/ &&
6535 $line !~ /^.\s*$Storage/ &&
6536 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6537 $1 !~ /[\,\)]\s*$/) {
6538 WARN("STORAGE_CLASS",
6539 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07006540 }
6541
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006542# check the location of the inline attribute, that it is between
6543# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006544 if ($line =~ /\b$Type\s+$Inline\b/ ||
6545 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006546 ERROR("INLINE_LOCATION",
6547 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006548 }
6549
Andy Whitcroft8905a672007-11-28 16:21:06 -08006550# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08006551 if ($realfile !~ m@\binclude/uapi/@ &&
6552 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006553 if (WARN("INLINE",
6554 "plain inline is preferred over $1\n" . $herecurr) &&
6555 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006556 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006557
6558 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006559 }
6560
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006561# Check for compiler attributes
Joe Perches2b7ab452013-11-12 15:10:14 -08006562 if ($realfile !~ m@\binclude/uapi/@ &&
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006563 $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
6564 my $attr = $1;
6565 $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
Joe Perches3d130fd2011-01-12 17:00:00 -08006566
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006567 my %attr_list = (
Joe Perches0830aab2020-12-15 20:44:50 -08006568 "alias" => "__alias",
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006569 "aligned" => "__aligned",
6570 "always_inline" => "__always_inline",
6571 "assume_aligned" => "__assume_aligned",
6572 "cold" => "__cold",
6573 "const" => "__attribute_const__",
6574 "copy" => "__copy",
6575 "designated_init" => "__designated_init",
6576 "externally_visible" => "__visible",
6577 "format" => "printf|scanf",
6578 "gnu_inline" => "__gnu_inline",
6579 "malloc" => "__malloc",
6580 "mode" => "__mode",
6581 "no_caller_saved_registers" => "__no_caller_saved_registers",
6582 "noclone" => "__noclone",
6583 "noinline" => "noinline",
6584 "nonstring" => "__nonstring",
6585 "noreturn" => "__noreturn",
6586 "packed" => "__packed",
6587 "pure" => "__pure",
Joe Perches339f29d2020-12-15 20:44:43 -08006588 "section" => "__section",
Joe Perches0830aab2020-12-15 20:44:50 -08006589 "used" => "__used",
6590 "weak" => "__weak"
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006591 );
Joe Perches39b7e282011-07-25 17:13:24 -07006592
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006593 while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
Joe Perches339f29d2020-12-15 20:44:43 -08006594 my $orig_attr = $1;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006595 my $params = '';
6596 $params = $2 if defined($2);
Joe Perches339f29d2020-12-15 20:44:43 -08006597 my $curr_attr = $orig_attr;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006598 $curr_attr =~ s/^[\s_]+|[\s_]+$//g;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006599 if (exists($attr_list{$curr_attr})) {
Joe Perches339f29d2020-12-15 20:44:43 -08006600 my $new = $attr_list{$curr_attr};
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006601 if ($curr_attr eq "format" && $params) {
6602 $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
Joe Perches339f29d2020-12-15 20:44:43 -08006603 $new = "__$1\($2";
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006604 } else {
Joe Perches339f29d2020-12-15 20:44:43 -08006605 $new = "$new$params";
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006606 }
Joe Perches339f29d2020-12-15 20:44:43 -08006607 if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6608 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
6609 $fix) {
6610 my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6611 $fixed[$fixlinenr] =~ s/$remove//;
6612 $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6613 $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6614 $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
6615 }
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006616 }
6617 }
6618
6619 # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
6620 if ($attr =~ /^_*unused/) {
6621 WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6622 "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
Joe Perchesd5e616f2013-09-11 14:23:54 -07006623 }
Joe Perches6061d942012-03-23 15:02:16 -07006624 }
6625
Joe Perches619a9082014-12-10 15:51:35 -08006626# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006627 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006628 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6629 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6630 $line =~ /\b__weak\b/)) {
6631 ERROR("WEAK_DECLARATION",
6632 "Using weak declarations can have unintended link defects\n" . $herecurr);
6633 }
6634
Tomas Winklerfd39f902016-12-12 16:46:34 -08006635# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006636 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006637 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006638 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6639 my $type = $1;
6640 if ($type =~ /\b($typeC99Typedefs)\b/) {
6641 $type = $1;
6642 my $kernel_type = 'u';
6643 $kernel_type = 's' if ($type =~ /^_*[si]/);
6644 $type =~ /(\d+)/;
6645 $kernel_type .= $1;
6646 if (CHK("PREFER_KERNEL_TYPES",
6647 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6648 $fix) {
6649 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6650 }
6651 }
6652 }
6653
Joe Perches938224b2016-01-20 14:59:15 -08006654# check for cast of C90 native int or longer types constants
6655 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6656 my $cast = $1;
6657 my $const = $2;
Joe Perches0972b8b2021-02-25 17:21:54 -08006658 my $suffix = "";
6659 my $newconst = $const;
6660 $newconst =~ s/${Int_type}$//;
6661 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6662 if ($cast =~ /\blong\s+long\b/) {
6663 $suffix .= 'LL';
6664 } elsif ($cast =~ /\blong\b/) {
6665 $suffix .= 'L';
6666 }
Joe Perches938224b2016-01-20 14:59:15 -08006667 if (WARN("TYPECAST_INT_CONSTANT",
Joe Perches0972b8b2021-02-25 17:21:54 -08006668 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
Joe Perches938224b2016-01-20 14:59:15 -08006669 $fix) {
Joe Perches938224b2016-01-20 14:59:15 -08006670 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6671 }
6672 }
6673
Joe Perches8f53a9b2010-03-05 13:43:48 -08006674# check for sizeof(&)
6675 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006676 WARN("SIZEOF_ADDRESS",
6677 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006678 }
6679
Joe Perches66c80b62012-07-30 14:41:22 -07006680# check for sizeof without parenthesis
6681 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006682 if (WARN("SIZEOF_PARENTHESIS",
6683 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6684 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006685 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006686 }
Joe Perches66c80b62012-07-30 14:41:22 -07006687 }
6688
Joe Perches88982fe2012-12-17 16:02:00 -08006689# check for struct spinlock declarations
6690 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6691 WARN("USE_SPINLOCK_T",
6692 "struct spinlock should be spinlock_t\n" . $herecurr);
6693 }
6694
Joe Perchesa6962d72013-04-29 16:18:13 -07006695# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006696 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006697 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006698 $fmt =~ s/%%//g;
6699 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006700 if (WARN("PREFER_SEQ_PUTS",
6701 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6702 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006703 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006704 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006705 }
6706 }
6707
Joe Perches478b1792018-04-10 16:33:34 -07006708# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006709 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006710 defined $stat &&
6711 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6712 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006713 my $stat_real;
6714
Joe Perches0b523762017-05-08 15:55:36 -07006715 my $lc = $stat =~ tr@\n@@;
6716 $lc = $lc + $linenr;
6717 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006718 my $specifier;
6719 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006720 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006721 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006722 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6723 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006724
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006725 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006726 $specifier = $1;
6727 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006728 $qualifier = $3;
Sakari Ailusaf612e42021-02-16 17:57:20 +02006729 if ($extension !~ /[4SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006730 ($extension eq "f" &&
Sakari Ailusaf612e42021-02-16 17:57:20 +02006731 defined $qualifier && $qualifier !~ /^w/) ||
6732 ($extension eq "4" &&
6733 defined $qualifier && $qualifier !~ /^cc/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006734 $bad_specifier = $specifier;
6735 last;
6736 }
6737 if ($extension eq "x" && !defined($stat_real)) {
6738 if (!defined($stat_real)) {
6739 $stat_real = get_stat_real($linenr, $lc);
6740 }
6741 WARN("VSPRINTF_SPECIFIER_PX",
6742 "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");
6743 }
6744 }
6745 if ($bad_specifier ne "") {
6746 my $stat_real = get_stat_real($linenr, $lc);
6747 my $ext_type = "Invalid";
6748 my $use = "";
6749 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006750 $use = " - use %pS instead";
6751 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6752 }
6753
6754 WARN("VSPRINTF_POINTER_EXTENSION",
6755 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6756 }
Joe Perches0b523762017-05-08 15:55:36 -07006757 }
6758 }
6759
Andy Whitcroft554e1652012-01-10 15:09:57 -08006760# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006761 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006762 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006763 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006764
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006765 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006766 my $ms_val = $7;
6767 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006768
Andy Whitcroft554e1652012-01-10 15:09:57 -08006769 if ($ms_size =~ /^(0x|)0$/i) {
6770 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006771 "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 -08006772 } elsif ($ms_size =~ /^(0x|)1$/i) {
6773 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006774 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6775 }
6776 }
6777
Joe Perches98a9bba2014-01-23 15:54:52 -08006778# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006779# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006780# defined $stat &&
6781# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6782# if (WARN("PREFER_ETHER_ADDR_COPY",
6783# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6784# $fix) {
6785# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6786# }
6787# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006788
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006789# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006790# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006791# defined $stat &&
6792# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6793# WARN("PREFER_ETHER_ADDR_EQUAL",
6794# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6795# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006796
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006797# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6798# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006799# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006800# defined $stat &&
6801# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6802#
6803# my $ms_val = $7;
6804#
6805# if ($ms_val =~ /^(?:0x|)0+$/i) {
6806# if (WARN("PREFER_ETH_ZERO_ADDR",
6807# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6808# $fix) {
6809# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6810# }
6811# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6812# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6813# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6814# $fix) {
6815# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6816# }
6817# }
6818# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006819
Joe Perches5dbdb2d2020-12-29 15:14:31 -08006820# strlcpy uses that should likely be strscpy
6821 if ($line =~ /\bstrlcpy\s*\(/) {
6822 WARN("STRLCPY",
6823 "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr);
6824 }
6825
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006826# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006827 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006828 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006829 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006830 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006831 my $call = $1;
6832 my $cast1 = deparenthesize($2);
6833 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006834 my $cast2 = deparenthesize($7);
6835 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006836 my $cast;
6837
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006838 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006839 $cast = "$cast1 or $cast2";
6840 } elsif ($cast1 ne "") {
6841 $cast = $cast1;
6842 } else {
6843 $cast = $cast2;
6844 }
6845 WARN("MINMAX",
6846 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006847 }
6848 }
6849
Joe Perches4a273192012-07-30 14:41:20 -07006850# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006851 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006852 defined $stat &&
6853 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6854 my $min = $1;
6855 my $max = $7;
6856 if ($min eq $max) {
6857 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006858 "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 -07006859 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6860 $min > $max) {
6861 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006862 "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 -07006863 }
6864 }
6865
Joe Perches823b7942013-11-12 15:10:15 -08006866# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006867 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006868 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006869 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006870 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6871 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6872 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6873 my $lc = $stat =~ tr@\n@@;
6874 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006875 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006876 WARN("NAKED_SSCANF",
6877 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6878 }
6879
Joe Perchesafc819a2014-06-04 16:12:08 -07006880# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006881 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006882 defined $stat &&
6883 $line =~ /\bsscanf\b/) {
6884 my $lc = $stat =~ tr@\n@@;
6885 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006886 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006887 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6888 my $format = $6;
6889 my $count = $format =~ tr@%@%@;
6890 if ($count == 1 &&
6891 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6892 WARN("SSCANF_TO_KSTRTO",
6893 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6894 }
6895 }
6896 }
6897
Joe Perches70dc8a42013-09-11 14:23:58 -07006898# check for new externs in .h files.
6899 if ($realfile =~ /\.h$/ &&
6900 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006901 if (CHK("AVOID_EXTERNS",
6902 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006903 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006904 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006905 }
6906 }
6907
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006908# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006909 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006910 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006911 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006912 my $function_name = $1;
6913 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006914
6915 my $s = $stat;
6916 if (defined $cond) {
6917 substr($s, 0, length($cond), '');
6918 }
Kees Cookd8b44b52020-06-03 14:18:09 -07006919 if ($s =~ /^\s*;/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006920 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006921 WARN("AVOID_EXTERNS",
6922 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006923 }
6924
6925 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006926 WARN("FUNCTION_ARGUMENTS",
6927 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006928 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006929
6930 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6931 $stat =~ /^.\s*extern\s+/)
6932 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006933 WARN("AVOID_EXTERNS",
6934 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006935 }
6936
Joe Perchesa0ad7592017-07-10 15:52:19 -07006937# check for function declarations that have arguments without identifier names
6938 if (defined $stat &&
Kees Cookd8b44b52020-06-03 14:18:09 -07006939 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6940 $1 ne "void") {
6941 my $args = trim($1);
Joe Perchesca0d8922016-10-11 13:52:16 -07006942 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6943 my $arg = trim($1);
Kees Cookd8b44b52020-06-03 14:18:09 -07006944 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006945 WARN("FUNCTION_ARGUMENTS",
6946 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6947 }
6948 }
6949 }
6950
Joe Perchesa0ad7592017-07-10 15:52:19 -07006951# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006952 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006953 defined $stat &&
6954 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6955 $context_function = $1;
6956
6957# check for multiline function definition with misplaced open brace
6958 my $ok = 0;
6959 my $cnt = statement_rawlines($stat);
6960 my $herectx = $here . "\n";
6961 for (my $n = 0; $n < $cnt; $n++) {
6962 my $rl = raw_line($linenr, $n);
6963 $herectx .= $rl . "\n";
6964 $ok = 1 if ($rl =~ /^[ \+]\{/);
6965 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6966 last if $rl =~ /^[ \+].*\{/;
6967 }
6968 if (!$ok) {
6969 ERROR("OPEN_BRACE",
6970 "open brace '{' following function definitions go on the next line\n" . $herectx);
6971 }
6972 }
6973
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006974# checks for new __setup's
6975 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6976 my $name = $1;
6977
6978 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006979 CHK("UNDOCUMENTED_SETUP",
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07006980 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006981 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006982 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006983
Joe Perchese29a70f2019-03-07 16:28:35 -08006984# check for pointless casting of alloc functions
6985 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006986 WARN("UNNECESSARY_CASTS",
6987 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006988 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006989
Joe Perchesa640d252013-07-03 15:05:21 -07006990# alloc style
6991# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006992 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006993 $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 -07006994 CHK("ALLOC_SIZEOF_STRUCT",
6995 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6996 }
6997
Joe Perches60a55362014-06-04 16:12:07 -07006998# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006999 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07007000 defined $stat &&
7001 $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 -07007002 my $oldfunc = $3;
7003 my $a1 = $4;
7004 my $a2 = $10;
7005 my $newfunc = "kmalloc_array";
7006 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07007007 my $r1 = $a1;
7008 my $r2 = $a2;
7009 if ($a1 =~ /^sizeof\s*\S/) {
7010 $r1 = $a2;
7011 $r2 = $a1;
7012 }
7013 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
7014 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07007015 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07007016 my $herectx = get_stat_here($linenr, $cnt, $here);
7017
Joe Perches60a55362014-06-04 16:12:07 -07007018 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07007019 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
7020 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07007021 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07007022 $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 -07007023 }
7024 }
7025 }
7026
Joe Perches972fdea2013-04-29 16:18:12 -07007027# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07007028 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07007029 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
7030 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07007031 WARN("KREALLOC_ARG_REUSE",
7032 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
7033 }
7034
Joe Perches5ce59ae2013-02-21 16:44:18 -08007035# check for alloc argument mismatch
Christophe JAILLET7e6cdd72021-05-06 18:04:01 -07007036 if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
Joe Perches5ce59ae2013-02-21 16:44:18 -08007037 WARN("ALLOC_ARRAY_ARGS",
7038 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
7039 }
7040
Joe Perchescaf2a542011-01-12 16:59:56 -08007041# check for multiple semicolons
7042 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07007043 if (WARN("ONE_SEMICOLON",
7044 "Statements terminations use 1 semicolon\n" . $herecurr) &&
7045 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07007046 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07007047 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007048 }
7049
Tomas Winklercec3aaa2016-08-02 14:04:39 -07007050# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
7051 if ($realfile !~ m@^include/uapi/@ &&
7052 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08007053 my $ull = "";
7054 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
7055 if (CHK("BIT_MACRO",
7056 "Prefer using the BIT$ull macro\n" . $herecurr) &&
7057 $fix) {
7058 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
7059 }
7060 }
7061
Joe Perches50161262020-08-11 18:35:07 -07007062# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
Jerome Forissier3e89ad82020-10-15 20:11:49 -07007063 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
Joe Perches50161262020-08-11 18:35:07 -07007064 WARN("IS_ENABLED_CONFIG",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07007065 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
Joe Perches50161262020-08-11 18:35:07 -07007066 }
7067
Joe Perches2d632742016-05-20 17:04:00 -07007068# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
Jerome Forissier3e89ad82020-10-15 20:11:49 -07007069 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 -07007070 my $config = $1;
7071 if (WARN("PREFER_IS_ENABLED",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07007072 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
Joe Perches2d632742016-05-20 17:04:00 -07007073 $fix) {
7074 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
7075 }
7076 }
7077
Joe Perchesf36d3eb2020-04-06 20:10:58 -07007078# check for /* fallthrough */ like comment, prefer fallthrough;
7079 my @fallthroughs = (
7080 'fallthrough',
7081 '@fallthrough@',
7082 'lint -fallthrough[ \t]*',
7083 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
7084 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
7085 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7086 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7087 );
7088 if ($raw_comment ne '') {
7089 foreach my $ft (@fallthroughs) {
7090 if ($raw_comment =~ /$ft/) {
7091 my $msg_level = \&WARN;
7092 $msg_level = \&CHK if ($file);
7093 &{$msg_level}("PREFER_FALLTHROUGH",
7094 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
7095 last;
7096 }
7097 }
7098 }
7099
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007100# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07007101 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007102 defined $stat &&
7103 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007104 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07007105 my $herectx = get_stat_here($linenr, $cnt, $here);
7106
Joe Perchesd1e2ad02012-12-17 16:02:01 -08007107 WARN("DEFAULT_NO_BREAK",
7108 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08007109 }
7110
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007111# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07007112 if ($line =~ /\b__FUNCTION__\b/) {
7113 if (WARN("USE_FUNC",
7114 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
7115 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07007116 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07007117 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007118 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07007119
Joe Perches62ec8182015-02-13 14:38:18 -08007120# check for uses of __DATE__, __TIME__, __TIMESTAMP__
7121 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
7122 ERROR("DATE_TIME",
7123 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
7124 }
7125
Joe Perches2c924882012-03-23 15:02:20 -07007126# check for use of yield()
7127 if ($line =~ /\byield\s*\(\s*\)/) {
7128 WARN("YIELD",
7129 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
7130 }
7131
Joe Perches179f8f42013-07-03 15:05:30 -07007132# check for comparisons against true and false
7133 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
7134 my $lead = $1;
7135 my $arg = $2;
7136 my $test = $3;
7137 my $otype = $4;
7138 my $trail = $5;
7139 my $op = "!";
7140
7141 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
7142
7143 my $type = lc($otype);
7144 if ($type =~ /^(?:true|false)$/) {
7145 if (("$test" eq "==" && "$type" eq "true") ||
7146 ("$test" eq "!=" && "$type" eq "false")) {
7147 $op = "";
7148 }
7149
7150 CHK("BOOL_COMPARISON",
7151 "Using comparison to $otype is error prone\n" . $herecurr);
7152
7153## maybe suggesting a correct construct would better
7154## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
7155
7156 }
7157 }
7158
Thomas Gleixner4882720b2010-09-07 14:34:01 +00007159# check for semaphores initialized locked
7160 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007161 WARN("CONSIDER_COMPLETION",
7162 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07007163 }
Joe Perches6712d852012-03-23 15:02:20 -07007164
Joe Perches67d0a072011-10-31 17:13:10 -07007165# recommend kstrto* over simple_strto* and strict_strto*
7166 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007167 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07007168 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07007169 }
Joe Perches6712d852012-03-23 15:02:20 -07007170
Fabian Frederickae3ccc42014-06-04 16:12:10 -07007171# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07007172 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007173 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07007174 "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 -07007175 }
Joe Perches6712d852012-03-23 15:02:20 -07007176
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08007177# check for spin_is_locked(), suggest lockdep instead
7178 if ($line =~ /\bspin_is_locked\(/) {
7179 WARN("USE_LOCKDEP",
7180 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
7181 }
7182
Joe Perches9189c7e2018-09-07 15:26:18 -07007183# check for deprecated apis
7184 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
7185 my $deprecated_api = $1;
7186 my $new_api = $deprecated_apis{$deprecated_api};
7187 WARN("DEPRECATED_API",
7188 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
7189 }
7190
Joe Perches0f3c5aa2015-02-13 14:39:05 -08007191# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07007192# and avoid what seem like struct definitions 'struct foo {'
Quentin Monnetced69da12020-08-11 18:35:13 -07007193 if (defined($const_structs) &&
7194 $line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07007195 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007196 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07007197 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08007198 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07007199
7200# use of NR_CPUS is usually wrong
7201# ignore definitions of NR_CPUS and usage to define arrays as likely right
Peng Wang35cdcbf2021-02-25 17:21:44 -08007202# ignore designated initializers using NR_CPUS
Andy Whitcroft773647a2008-03-28 14:15:58 -07007203 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07007204 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7205 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07007206 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7207 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
Peng Wang35cdcbf2021-02-25 17:21:44 -08007208 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
7209 $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07007210 {
Joe Perches000d1cc12011-07-25 17:13:25 -07007211 WARN("NR_CPUS",
7212 "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 -07007213 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07007214
Joe Perches52ea8502013-11-12 15:10:09 -08007215# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
7216 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
7217 ERROR("DEFINE_ARCH_HAS",
7218 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
7219 }
7220
Joe Perchesacd93622015-02-13 14:38:38 -08007221# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07007222 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08007223 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7224 WARN("LIKELY_MISUSE",
7225 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7226 }
7227
Joe Perchesfbe74542021-05-06 18:03:55 -07007228# return sysfs_emit(foo, fmt, ...) fmt without newline
7229 if ($line =~ /\breturn\s+sysfs_emit\s*\(\s*$FuncArg\s*,\s*($String)/ &&
7230 substr($rawline, $-[6], $+[6] - $-[6]) !~ /\\n"$/) {
7231 my $offset = $+[6] - 1;
7232 if (WARN("SYSFS_EMIT",
7233 "return sysfs_emit(...) formats should include a terminating newline\n" . $herecurr) &&
7234 $fix) {
7235 substr($fixed[$fixlinenr], $offset, 0) = '\\n';
7236 }
7237 }
7238
Denis Efremovde3f1862019-09-25 16:49:25 -07007239# nested likely/unlikely calls
7240 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7241 WARN("LIKELY_MISUSE",
7242 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7243 }
7244
Andy Whitcroft691d77b2009-01-06 14:41:16 -08007245# whine mightly about in_atomic
7246 if ($line =~ /\bin_atomic\s*\(/) {
7247 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007248 ERROR("IN_ATOMIC",
7249 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08007250 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007251 WARN("IN_ATOMIC",
7252 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08007253 }
7254 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01007255
7256# check for lockdep_set_novalidate_class
7257 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
7258 $line =~ /__lockdep_no_validate__\s*\)/ ) {
7259 if ($realfile !~ m@^kernel/lockdep@ &&
7260 $realfile !~ m@^include/linux/lockdep@ &&
7261 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007262 ERROR("LOCKDEP",
7263 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01007264 }
7265 }
Dave Jones88f88312011-01-12 16:59:59 -08007266
Joe Perchesb392c642015-04-16 12:44:16 -07007267 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7268 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007269 WARN("EXPORTED_WORLD_WRITABLE",
7270 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08007271 }
Joe Perches24358802014-04-03 14:49:13 -07007272
Joe Perches00180462018-02-06 15:38:55 -08007273# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
7274# and whether or not function naming is typical and if
7275# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07007276 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08007277 defined $stat &&
7278 $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*\)/) {
7279 my $var = $1;
7280 my $perms = $2;
7281 my $show = $3;
7282 my $store = $4;
7283 my $octal_perms = perms_to_octal($perms);
7284 if ($show =~ /^${var}_show$/ &&
7285 $store =~ /^${var}_store$/ &&
7286 $octal_perms eq "0644") {
7287 if (WARN("DEVICE_ATTR_RW",
7288 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
7289 $fix) {
7290 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
7291 }
7292 } elsif ($show =~ /^${var}_show$/ &&
7293 $store =~ /^NULL$/ &&
7294 $octal_perms eq "0444") {
7295 if (WARN("DEVICE_ATTR_RO",
7296 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
7297 $fix) {
7298 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
7299 }
7300 } elsif ($show =~ /^NULL$/ &&
7301 $store =~ /^${var}_store$/ &&
7302 $octal_perms eq "0200") {
7303 if (WARN("DEVICE_ATTR_WO",
7304 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
7305 $fix) {
7306 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
7307 }
7308 } elsif ($octal_perms eq "0644" ||
7309 $octal_perms eq "0444" ||
7310 $octal_perms eq "0200") {
7311 my $newshow = "$show";
7312 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
7313 my $newstore = $store;
7314 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
7315 my $rename = "";
7316 if ($show ne $newshow) {
7317 $rename .= " '$show' to '$newshow'";
7318 }
7319 if ($store ne $newstore) {
7320 $rename .= " '$store' to '$newstore'";
7321 }
7322 WARN("DEVICE_ATTR_FUNCTIONS",
7323 "Consider renaming function(s)$rename\n" . $herecurr);
7324 } else {
7325 WARN("DEVICE_ATTR_PERMS",
7326 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
7327 }
7328 }
7329
Joe Perches515a2352014-04-03 14:49:24 -07007330# Mode permission misuses where it seems decimal should be octal
7331# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08007332# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
7333# specific definition of not visible in sysfs.
7334# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
7335# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07007336 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07007337 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07007338 $line =~ /$mode_perms_search/) {
7339 foreach my $entry (@mode_permission_funcs) {
7340 my $func = $entry->[0];
7341 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07007342
Joe Perches459cf0a2016-10-11 13:52:19 -07007343 my $lc = $stat =~ tr@\n@@;
7344 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07007345 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07007346
Joe Perches515a2352014-04-03 14:49:24 -07007347 my $skip_args = "";
7348 if ($arg_pos > 1) {
7349 $arg_pos--;
7350 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
7351 }
Joe Perchesf90774e2016-10-11 13:51:47 -07007352 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07007353 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07007354 my $val = $1;
7355 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08007356 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
7357 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
7358 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07007359 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07007360 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07007361 }
7362 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08007363 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07007364 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07007365 }
Joe Perches24358802014-04-03 14:49:13 -07007366 }
7367 }
7368 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007369
Joe Perches459cf0a2016-10-11 13:52:19 -07007370# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07007371 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08007372 my $oval = $1;
7373 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07007374 if (WARN("SYMBOLIC_PERMS",
7375 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7376 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08007377 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07007378 }
7379 }
7380
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007381# validate content of MODULE_LICENSE against list from include/linux/module.h
7382 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
7383 my $extracted_string = get_quoted_string($line, $rawline);
7384 my $valid_licenses = qr{
7385 GPL|
7386 GPL\ v2|
7387 GPL\ and\ additional\ rights|
7388 Dual\ BSD/GPL|
7389 Dual\ MIT/GPL|
7390 Dual\ MPL/GPL|
7391 Proprietary
7392 }x;
7393 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
7394 WARN("MODULE_LICENSE",
7395 "unknown module license " . $extracted_string . "\n" . $herecurr);
7396 }
7397 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07007398
7399# check for sysctl duplicate constants
7400 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
7401 WARN("DUPLICATED_SYSCTL_CONST",
7402 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
7403 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007404 }
7405
7406 # If we have no input at all, then there is nothing to report on
7407 # so just keep quiet.
7408 if ($#rawlines == -1) {
7409 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007410 }
7411
Andy Whitcroft8905a672007-11-28 16:21:06 -08007412 # In mailback mode only produce a report in the negative, for
7413 # things that appear to be patches.
7414 if ($mailback && ($clean == 1 || !$is_patch)) {
7415 exit(0);
7416 }
7417
Dwaipayan Raye73d2712020-12-15 20:44:56 -08007418 # This is not a patch, and we are in 'no-patch' mode so
Andy Whitcroft8905a672007-11-28 16:21:06 -08007419 # just keep quiet.
7420 if (!$chk_patch && !$is_patch) {
7421 exit(0);
7422 }
7423
Stafford Hornea08ffbe2017-10-03 16:16:51 -07007424 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007425 ERROR("NOT_UNIFIED_DIFF",
7426 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007427 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007428 if ($is_patch && $has_commit_log && $chk_signoff) {
7429 if ($signoff == 0) {
7430 ERROR("MISSING_SIGN_OFF",
7431 "Missing Signed-off-by: line(s)\n");
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07007432 } elsif ($authorsignoff != 1) {
7433 # authorsignoff values:
7434 # 0 -> missing sign off
7435 # 1 -> sign off identical
7436 # 2 -> names and addresses match, comments mismatch
7437 # 3 -> addresses match, names different
7438 # 4 -> names match, addresses different
7439 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
7440
7441 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
7442
7443 if ($authorsignoff == 0) {
7444 ERROR("NO_AUTHOR_SIGN_OFF",
7445 "Missing Signed-off-by: line by nominal patch author '$author'\n");
7446 } elsif ($authorsignoff == 2) {
7447 CHK("FROM_SIGN_OFF_MISMATCH",
7448 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7449 } elsif ($authorsignoff == 3) {
7450 WARN("FROM_SIGN_OFF_MISMATCH",
7451 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7452 } elsif ($authorsignoff == 4) {
7453 WARN("FROM_SIGN_OFF_MISMATCH",
7454 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7455 } elsif ($authorsignoff == 5) {
7456 WARN("FROM_SIGN_OFF_MISMATCH",
7457 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7458 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007459 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007460 }
7461
Andy Whitcroft8905a672007-11-28 16:21:06 -08007462 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007463 if ($summary && !($clean == 1 && $quiet == 1)) {
7464 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08007465 print "total: $cnt_error errors, $cnt_warn warnings, " .
7466 (($check)? "$cnt_chk checks, " : "") .
7467 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07007468 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08007469
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007470 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07007471 # If there were any defects found and not already fixing them
7472 if (!$clean and !$fix) {
7473 print << "EOM"
7474
7475NOTE: For some of the reported defects, checkpatch may be able to
7476 mechanically convert to the typical style using --fix or --fix-inplace.
7477EOM
7478 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007479 # If there were whitespace errors which cleanpatch can fix
7480 # then suggest that.
7481 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07007482 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07007483 print << "EOM"
7484
7485NOTE: Whitespace errors detected.
7486 You may wish to use scripts/cleanpatch or scripts/cleanfile
7487EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007488 }
7489 }
7490
Joe Perchesd752fcc2014-08-06 16:11:05 -07007491 if ($clean == 0 && $fix &&
7492 ("@rawlines" ne "@fixed" ||
7493 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08007494 my $newfile = $filename;
7495 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07007496 my $linecount = 0;
7497 my $f;
7498
Joe Perchesd752fcc2014-08-06 16:11:05 -07007499 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7500
Joe Perches3705ce52013-07-03 15:05:31 -07007501 open($f, '>', $newfile)
7502 or die "$P: Can't open $newfile for write\n";
7503 foreach my $fixed_line (@fixed) {
7504 $linecount++;
7505 if ($file) {
7506 if ($linecount > 3) {
7507 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07007508 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07007509 }
7510 } else {
7511 print $f $fixed_line . "\n";
7512 }
7513 }
7514 close($f);
7515
7516 if (!$quiet) {
7517 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07007518
Joe Perches3705ce52013-07-03 15:05:31 -07007519Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
7520
7521Do _NOT_ trust the results written to this file.
7522Do _NOT_ submit these changes without inspecting them for correctness.
7523
7524This EXPERIMENTAL file is simply a convenience to help rewrite patches.
7525No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07007526EOM
7527 }
7528 }
7529
Joe Perchesd8469f12015-06-25 15:03:00 -07007530 if ($quiet == 0) {
7531 print "\n";
7532 if ($clean == 1) {
7533 print "$vname has no obvious style problems and is ready for submission.\n";
7534 } else {
7535 print "$vname has style problems, please review.\n";
7536 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007537 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007538 return $clean;
7539}