blob: d8793bdbc49294cdd10a2b5c2feea4de934938c5 [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;
26my $tree = 1;
27my $chk_signoff = 1;
28my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070029my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080031my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070032my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070034my $git = 0;
Joe Perches0dea9f1e2016-05-20 17:04:19 -070035my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070037my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080038my $summary = 1;
39my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080040my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070041my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070042my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070043my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080044my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070045my $root;
Joe Perches0f7f6352020-10-24 16:59:04 -070046my $gitroot = $ENV{'GIT_DIR'};
47$gitroot = ".git" if !defined($gitroot);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080048my %debug;
Joe Perches34456862013-07-03 15:05:34 -070049my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070050my %use_type = ();
51my @use = ();
52my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070053my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070054my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070055my $configuration_file = ".checkpatch.conf";
Joe Perchesbdc48fa2020-05-29 16:12:21 -070056my $max_line_length = 100;
Dave Hansend62a2012013-09-11 14:23:56 -070057my $ignore_perl_version = 0;
58my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070059my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070060my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070061my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070062my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070063my $conststructsfile = "$D/const_structs.checkpatch";
Quentin Monnetced69da12020-08-11 18:35:13 -070064my $typedefsfile;
John Brooks737c0762017-07-10 15:52:24 -070065my $color = "auto";
Vadim Bendebury98005e82019-03-07 16:28:38 -080066my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
Joe Perchesdbbf8692019-09-25 16:46:52 -070067# git output parsing needs US English output, so first set backtick child process LANGUAGE
68my $git_command ='export LANGUAGE=en_US.UTF-8; git';
Antonio Borneo713a09d2020-04-06 20:11:07 -070069my $tabsize = 8;
Jerome Forissier3e89ad82020-10-15 20:11:49 -070070my ${CONFIG_} = "CONFIG_";
Hannes Eder77f5b102009-09-21 17:04:37 -070071
72sub help {
73 my ($exitcode) = @_;
74
75 print << "EOM";
76Usage: $P [OPTION]... [FILE]...
77Version: $V
78
79Options:
80 -q, --quiet quiet
81 --no-tree run without a kernel tree
82 --no-signoff do not check for 'Signed-off-by' line
83 --patch treat FILE as patchfile (default)
84 --emacs emacs compile window format
85 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070086 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070087 -g, --git treat FILE as a single commit or git revision range
88 single git commit with:
89 <rev>
90 <rev>^
91 <rev>~n
92 multiple git commits with:
93 <rev1>..<rev2>
94 <rev1>...<rev2>
95 <rev>-<count>
96 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070097 -f, --file treat FILE as regular source file
98 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070099 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -0700100 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -0700101 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -0700102 --show-types show the specific message type in the output
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700103 --max-line-length=n set the maximum line length, (default $max_line_length)
104 if exceeded, warn on patches
105 requires --strict for use with --file
Vadim Bendebury56193272014-10-13 15:51:48 -0700106 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700107 --tab-size=n set the number of spaces for tab (default $tabsize)
Hannes Eder77f5b102009-09-21 17:04:37 -0700108 --root=PATH PATH to the kernel tree root
109 --no-summary suppress the per-file summary
110 --mailback only produce a report in case of warnings/errors
111 --summary-file include the filename in summary
112 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
113 'values', 'possible', 'type', and 'attr' (default
114 is all off)
115 --test-only=WORD report only warnings/errors containing WORD
116 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700117 --fix EXPERIMENTAL - may create horrible results
118 If correctable single-line errors exist, create
119 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
120 with potential errors corrected to the preferred
121 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800122 --fix-inplace EXPERIMENTAL - may create horrible results
123 Is the same as --fix, but overwrites the input
124 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700125 --ignore-perl-version override checking of perl version. expect
126 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700127 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700128 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700129 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700130 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700131 --color[=WHEN] Use colors 'always', 'never', or only when output
132 is a terminal ('auto'). Default is 'auto'.
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700133 --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default
134 ${CONFIG_})
Hannes Eder77f5b102009-09-21 17:04:37 -0700135 -h, --help, --version display this help and exit
136
137When FILE is - read standard input.
138EOM
139
140 exit($exitcode);
141}
142
Joe Perches3beb42e2016-05-20 17:04:14 -0700143sub uniq {
144 my %seen;
145 return grep { !$seen{$_}++ } @_;
146}
147
148sub list_types {
149 my ($exitcode) = @_;
150
151 my $count = 0;
152
153 local $/ = undef;
154
155 open(my $script, '<', abs_path($P)) or
156 die "$P: Can't read '$P' $!\n";
157
158 my $text = <$script>;
159 close($script);
160
161 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700162 # Also catch when type or level is passed through a variable
163 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700164 push (@types, $_);
165 }
166 @types = sort(uniq(@types));
167 print("#\tMessage type\n\n");
168 foreach my $type (@types) {
169 print(++$count . "\t" . $type . "\n");
170 }
171
172 exit($exitcode);
173}
174
Joe Perches000d1cc12011-07-25 17:13:25 -0700175my $conf = which_conf($configuration_file);
176if (-f $conf) {
177 my @conf_args;
178 open(my $conffile, '<', "$conf")
179 or warn "$P: Can't find a readable $configuration_file file $!\n";
180
181 while (<$conffile>) {
182 my $line = $_;
183
184 $line =~ s/\s*\n?$//g;
185 $line =~ s/^\s*//g;
186 $line =~ s/\s+/ /g;
187
188 next if ($line =~ m/^\s*#/);
189 next if ($line =~ m/^\s*$/);
190
191 my @words = split(" ", $line);
192 foreach my $word (@words) {
193 last if ($word =~ m/^#/);
194 push (@conf_args, $word);
195 }
196 }
197 close($conffile);
198 unshift(@ARGV, @conf_args) if @conf_args;
199}
200
John Brooks737c0762017-07-10 15:52:24 -0700201# Perl's Getopt::Long allows options to take optional arguments after a space.
202# Prevent --color by itself from consuming other arguments
203foreach (@ARGV) {
204 if ($_ eq "--color" || $_ eq "-color") {
205 $_ = "--color=$color";
206 }
207}
208
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700209GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700210 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700211 'tree!' => \$tree,
212 'signoff!' => \$chk_signoff,
213 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700214 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800215 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700216 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700217 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700218 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700219 'subjective!' => \$check,
220 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700221 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700222 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700223 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700224 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800225 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700226 'min-conf-desc-length=i' => \$min_conf_desc_length,
Antonio Borneo713a09d2020-04-06 20:11:07 -0700227 'tab-size=i' => \$tabsize,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700228 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800229 'summary!' => \$summary,
230 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800231 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700232 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800233 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700234 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800235 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700236 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700237 'codespell!' => \$codespell,
238 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700239 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700240 'color=s' => \$color,
241 'no-color' => \$color, #keep old behaviors of -nocolor
242 'nocolor' => \$color, #keep old behaviors of -nocolor
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700243 'kconfig-prefix=s' => \${CONFIG_},
Hannes Eder77f5b102009-09-21 17:04:37 -0700244 'h|help' => \$help,
245 'version' => \$help
246) or help(1);
247
248help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700249
Joe Perches3beb42e2016-05-20 17:04:14 -0700250list_types(0) if ($list_types);
251
Joe Perches9624b8d2014-01-23 15:54:44 -0800252$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700253$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800254
Joe Perches32f30ca2020-06-04 16:50:40 -0700255die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
256
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700257my $exit = 0;
258
Joe Perches5b579802018-08-21 21:57:33 -0700259my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700260if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700261 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700262 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700263 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700264}
265
Allen Hubbe45107ff2016-08-02 14:04:47 -0700266#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700267if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700268 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700269}
270
John Brooks737c0762017-07-10 15:52:24 -0700271if ($color =~ /^[01]$/) {
272 $color = !$color;
273} elsif ($color =~ /^always$/i) {
274 $color = 1;
275} elsif ($color =~ /^never$/i) {
276 $color = 0;
277} elsif ($color =~ /^auto$/i) {
278 $color = (-t STDOUT);
279} else {
Joe Perches32f30ca2020-06-04 16:50:40 -0700280 die "$P: Invalid color mode: $color\n";
John Brooks737c0762017-07-10 15:52:24 -0700281}
282
Antonio Borneo713a09d2020-04-06 20:11:07 -0700283# skip TAB size 1 to avoid additional checks on $tabsize - 1
Joe Perches32f30ca2020-06-04 16:50:40 -0700284die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
Antonio Borneo713a09d2020-04-06 20:11:07 -0700285
Joe Perches91bfe482013-09-11 14:23:59 -0700286sub hash_save_array_words {
287 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700288
Joe Perches91bfe482013-09-11 14:23:59 -0700289 my @array = split(/,/, join(',', @$arrayRef));
290 foreach my $word (@array) {
291 $word =~ s/\s*\n?$//g;
292 $word =~ s/^\s*//g;
293 $word =~ s/\s+/ /g;
294 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700295
Joe Perches91bfe482013-09-11 14:23:59 -0700296 next if ($word =~ m/^\s*#/);
297 next if ($word =~ m/^\s*$/);
298
299 $hashRef->{$word}++;
300 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700301}
302
Joe Perches91bfe482013-09-11 14:23:59 -0700303sub hash_show_words {
304 my ($hashRef, $prefix) = @_;
305
Joe Perches3c816e42015-06-25 15:03:29 -0700306 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700307 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700308 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700309 print " $word";
310 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700311 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700312 }
313}
314
315hash_save_array_words(\%ignore_type, \@ignore);
316hash_save_array_words(\%use_type, \@use);
317
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800318my $dbg_values = 0;
319my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700320my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700321my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800322for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800323 ## no critic
324 eval "\${dbg_$key} = '$debug{$key}';";
325 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800326}
327
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700328my $rpt_cleaners = 0;
329
Andy Whitcroft8905a672007-11-28 16:21:06 -0800330if ($terse) {
331 $emacs = 1;
332 $quiet++;
333}
334
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700335if ($tree) {
336 if (defined $root) {
337 if (!top_of_kernel_tree($root)) {
338 die "$P: $root: --root does not point at a valid tree\n";
339 }
340 } else {
341 if (top_of_kernel_tree('.')) {
342 $root = '.';
343 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
344 top_of_kernel_tree($1)) {
345 $root = $1;
346 }
347 }
348
349 if (!defined $root) {
350 print "Must be run from the top-level dir. of a kernel tree\n";
351 exit(2);
352 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700353}
354
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700355my $emitted_corrupt = 0;
356
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700357our $Ident = qr{
358 [A-Za-z_][A-Za-z\d_]*
359 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
360 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700361our $Storage = qr{extern|static|asmlinkage};
362our $Sparse = qr{
363 __user|
364 __kernel|
365 __force|
366 __iomem|
367 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800368 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700369 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700370 __refconst|
371 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800372 __rcu|
373 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700374 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800375our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
376our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
377our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
378our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
379our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700380
Wolfram Sang52131292010-03-05 13:43:51 -0800381# Notes to $Attribute:
382# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700383our $Attribute = qr{
384 const|
Joe Perchesb5e87362021-02-25 17:21:40 -0800385 volatile|
Joe Perches03f1df72010-10-26 14:23:16 -0700386 __percpu|
387 __nocast|
388 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200389 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700390 __packed__|
391 __packed2__|
392 __naked|
393 __maybe_unused|
394 __always_unused|
395 __noreturn|
396 __used|
397 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800398 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700399 __noclone|
400 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700401 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700402 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700403 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700404 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700405 ____cacheline_aligned|
406 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800407 ____cacheline_internodealigned_in_smp|
408 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700409 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700410our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700411our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700412our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
413our $Lval = qr{$Ident(?:$Member)*};
414
Joe Perches95e2c602013-07-03 15:05:20 -0700415our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
416our $Binary = qr{(?i)0b[01]+$Int_type?};
417our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
418our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700419our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800420our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800421our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
422our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
423our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800424our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700425our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800426our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700427our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700428our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700429our $Operators = qr{
430 <=|>=|==|!=|
431 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700432 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700433 }x;
434
Joe Perches91cb5192014-04-03 14:49:32 -0700435our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
436
Joe Perchesab7e23f2015-04-16 12:44:22 -0700437our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800438our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700439our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700440our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800441our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700442our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800443our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700444our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800445
Joe Perches15662b32011-10-31 17:13:12 -0700446our $NON_ASCII_UTF8 = qr{
447 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700448 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
449 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
450 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
451 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
452 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
453 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
454}x;
455
Joe Perches15662b32011-10-31 17:13:12 -0700456our $UTF8 = qr{
457 [\x09\x0A\x0D\x20-\x7E] # ASCII
458 | $NON_ASCII_UTF8
459}x;
460
Joe Perchese6176fa2015-06-25 15:02:49 -0700461our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800462our $typeOtherOSTypedefs = qr{(?x:
463 u_(?:char|short|int|long) | # bsd
464 u(?:nchar|short|int|long) # sysv
465)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700466our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700467 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700468 atomic_t
469)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700470our $typeTypedefs = qr{(?x:
471 $typeC99Typedefs\b|
472 $typeOtherOSTypedefs\b|
473 $typeKernelTypedefs\b
474)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700475
Joe Perches6d32f7a2015-11-06 16:31:37 -0800476our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
477
Joe Perches691e6692010-03-05 13:43:51 -0800478our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800479 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700480 (?:[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 -0800481 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700482 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700483 panic|
Joe Perches06668722013-11-12 15:10:07 -0800484 MODULE_[A-Z_]+|
485 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800486)};
487
Joe Perchese29a70f2019-03-07 16:28:35 -0800488our $allocFunctions = qr{(?x:
489 (?:(?:devm_)?
490 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
491 kstrdup(?:_const)? |
492 kmemdup(?:_nul)?) |
Christophe JAILLET461e1562020-04-20 18:13:58 -0700493 (?:\w+)?alloc_skb(?:_ip_align)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800494 # dev_alloc_skb/netdev_alloc_skb, et al
495 dma_alloc_coherent
496)};
497
Joe Perches20112472011-07-25 17:13:23 -0700498our $signature_tags = qr{(?xi:
499 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800500 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700501 Acked-by:|
502 Tested-by:|
503 Reviewed-by:|
504 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700505 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700506 To:|
507 Cc:
508)};
509
Joe Perchesadb2da82021-02-25 17:21:50 -0800510our $tracing_logging_tags = qr{(?xi:
511 [=-]*> |
512 <[=-]* |
513 \[ |
514 \] |
515 start |
516 called |
517 entered |
518 entry |
519 enter |
520 in |
521 inside |
522 here |
523 begin |
524 exit |
525 end |
526 done |
527 leave |
528 completed |
529 out |
530 return |
531 [\.\!:\s]*
532)};
533
Aditya Srivastava831242a2020-12-15 20:45:12 -0800534sub edit_distance_min {
535 my (@arr) = @_;
536 my $len = scalar @arr;
537 if ((scalar @arr) < 1) {
538 # if underflow, return
539 return;
540 }
541 my $min = $arr[0];
542 for my $i (0 .. ($len-1)) {
543 if ($arr[$i] < $min) {
544 $min = $arr[$i];
545 }
546 }
547 return $min;
548}
549
550sub get_edit_distance {
551 my ($str1, $str2) = @_;
552 $str1 = lc($str1);
553 $str2 = lc($str2);
554 $str1 =~ s/-//g;
555 $str2 =~ s/-//g;
556 my $len1 = length($str1);
557 my $len2 = length($str2);
558 # two dimensional array storing minimum edit distance
559 my @distance;
560 for my $i (0 .. $len1) {
561 for my $j (0 .. $len2) {
562 if ($i == 0) {
563 $distance[$i][$j] = $j;
564 } elsif ($j == 0) {
565 $distance[$i][$j] = $i;
566 } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
567 $distance[$i][$j] = $distance[$i - 1][$j - 1];
568 } else {
569 my $dist1 = $distance[$i][$j - 1]; #insert distance
570 my $dist2 = $distance[$i - 1][$j]; # remove
571 my $dist3 = $distance[$i - 1][$j - 1]; #replace
572 $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
573 }
574 }
575 }
576 return $distance[$len1][$len2];
577}
578
579sub find_standard_signature {
580 my ($sign_off) = @_;
581 my @standard_signature_tags = (
582 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
583 'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
584 );
585 foreach my $signature (@standard_signature_tags) {
586 return $signature if (get_edit_distance($sign_off, $signature) <= 2);
587 }
588
589 return "";
590}
591
Joe Perches18130872014-08-06 16:11:22 -0700592our @typeListMisordered = (
593 qr{char\s+(?:un)?signed},
594 qr{int\s+(?:(?:un)?signed\s+)?short\s},
595 qr{int\s+short(?:\s+(?:un)?signed)},
596 qr{short\s+int(?:\s+(?:un)?signed)},
597 qr{(?:un)?signed\s+int\s+short},
598 qr{short\s+(?:un)?signed},
599 qr{long\s+int\s+(?:un)?signed},
600 qr{int\s+long\s+(?:un)?signed},
601 qr{long\s+(?:un)?signed\s+int},
602 qr{int\s+(?:un)?signed\s+long},
603 qr{int\s+(?:un)?signed},
604 qr{int\s+long\s+long\s+(?:un)?signed},
605 qr{long\s+long\s+int\s+(?:un)?signed},
606 qr{long\s+long\s+(?:un)?signed\s+int},
607 qr{long\s+long\s+(?:un)?signed},
608 qr{long\s+(?:un)?signed},
609);
610
Andy Whitcroft8905a672007-11-28 16:21:06 -0800611our @typeList = (
612 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700613 qr{(?:(?:un)?signed\s+)?char},
614 qr{(?:(?:un)?signed\s+)?short\s+int},
615 qr{(?:(?:un)?signed\s+)?short},
616 qr{(?:(?:un)?signed\s+)?int},
617 qr{(?:(?:un)?signed\s+)?long\s+int},
618 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
619 qr{(?:(?:un)?signed\s+)?long\s+long},
620 qr{(?:(?:un)?signed\s+)?long},
621 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800622 qr{float},
623 qr{double},
624 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800625 qr{struct\s+$Ident},
626 qr{union\s+$Ident},
627 qr{enum\s+$Ident},
628 qr{${Ident}_t},
629 qr{${Ident}_handler},
630 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700631 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800632);
Joe Perches938224b2016-01-20 14:59:15 -0800633
634our $C90_int_types = qr{(?x:
635 long\s+long\s+int\s+(?:un)?signed|
636 long\s+long\s+(?:un)?signed\s+int|
637 long\s+long\s+(?:un)?signed|
638 (?:(?:un)?signed\s+)?long\s+long\s+int|
639 (?:(?:un)?signed\s+)?long\s+long|
640 int\s+long\s+long\s+(?:un)?signed|
641 int\s+(?:(?:un)?signed\s+)?long\s+long|
642
643 long\s+int\s+(?:un)?signed|
644 long\s+(?:un)?signed\s+int|
645 long\s+(?:un)?signed|
646 (?:(?:un)?signed\s+)?long\s+int|
647 (?:(?:un)?signed\s+)?long|
648 int\s+long\s+(?:un)?signed|
649 int\s+(?:(?:un)?signed\s+)?long|
650
651 int\s+(?:un)?signed|
652 (?:(?:un)?signed\s+)?int
653)};
654
Alex Dowad485ff232015-06-25 15:02:52 -0700655our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700656our @typeListWithAttr = (
657 @typeList,
658 qr{struct\s+$InitAttribute\s+$Ident},
659 qr{union\s+$InitAttribute\s+$Ident},
660);
661
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700662our @modifierList = (
663 qr{fastcall},
664);
Alex Dowad485ff232015-06-25 15:02:52 -0700665our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800666
Joe Perches24358802014-04-03 14:49:13 -0700667our @mode_permission_funcs = (
668 ["module_param", 3],
669 ["module_param_(?:array|named|string)", 4],
670 ["module_param_array_named", 5],
671 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
672 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700673 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
674 ["IIO_DEV_ATTR_[A-Z_]+", 1],
675 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
676 ["SENSOR_TEMPLATE(?:_2|)", 3],
677 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700678);
679
Joe Perches1a3dcf22020-08-11 18:35:16 -0700680my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
681
Joe Perches515a2352014-04-03 14:49:24 -0700682#Create a search pattern for all these functions to speed up a loop below
683our $mode_perms_search = "";
684foreach my $entry (@mode_permission_funcs) {
685 $mode_perms_search .= '|' if ($mode_perms_search ne "");
686 $mode_perms_search .= $entry->[0];
687}
Joe Perches00180462018-02-06 15:38:55 -0800688$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700689
Joe Perches9189c7e2018-09-07 15:26:18 -0700690our %deprecated_apis = (
691 "synchronize_rcu_bh" => "synchronize_rcu",
692 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
693 "call_rcu_bh" => "call_rcu",
694 "rcu_barrier_bh" => "rcu_barrier",
695 "synchronize_sched" => "synchronize_rcu",
696 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
697 "call_rcu_sched" => "call_rcu",
698 "rcu_barrier_sched" => "rcu_barrier",
699 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
700 "cond_synchronize_sched" => "cond_synchronize_rcu",
701);
702
703#Create a search pattern for all these strings to speed up a loop below
704our $deprecated_apis_search = "";
705foreach my $entry (keys %deprecated_apis) {
706 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
707 $deprecated_apis_search .= $entry;
708}
709$deprecated_apis_search = "(?:${deprecated_apis_search})";
710
Joe Perchesb392c642015-04-16 12:44:16 -0700711our $mode_perms_world_writable = qr{
712 S_IWUGO |
713 S_IWOTH |
714 S_IRWXUGO |
715 S_IALLUGO |
716 0[0-7][0-7][2367]
717}x;
718
Joe Perchesf90774e2016-10-11 13:51:47 -0700719our %mode_permission_string_types = (
720 "S_IRWXU" => 0700,
721 "S_IRUSR" => 0400,
722 "S_IWUSR" => 0200,
723 "S_IXUSR" => 0100,
724 "S_IRWXG" => 0070,
725 "S_IRGRP" => 0040,
726 "S_IWGRP" => 0020,
727 "S_IXGRP" => 0010,
728 "S_IRWXO" => 0007,
729 "S_IROTH" => 0004,
730 "S_IWOTH" => 0002,
731 "S_IXOTH" => 0001,
732 "S_IRWXUGO" => 0777,
733 "S_IRUGO" => 0444,
734 "S_IWUGO" => 0222,
735 "S_IXUGO" => 0111,
736);
737
738#Create a search pattern for all these strings to speed up a loop below
739our $mode_perms_string_search = "";
740foreach my $entry (keys %mode_permission_string_types) {
741 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
742 $mode_perms_string_search .= $entry;
743}
Joe Perches00180462018-02-06 15:38:55 -0800744our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
745our $multi_mode_perms_string_search = qr{
746 ${single_mode_perms_string_search}
747 (?:\s*\|\s*${single_mode_perms_string_search})*
748}x;
749
750sub perms_to_octal {
751 my ($string) = @_;
752
753 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
754
755 my $val = "";
756 my $oval = "";
757 my $to = 0;
758 my $curpos = 0;
759 my $lastpos = 0;
760 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
761 $curpos = pos($string);
762 my $match = $2;
763 my $omatch = $1;
764 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
765 $lastpos = $curpos;
766 $to |= $mode_permission_string_types{$match};
767 $val .= '\s*\|\s*' if ($val ne "");
768 $val .= $match;
769 $oval .= $omatch;
770 }
771 $oval =~ s/^\s*\|\s*//;
772 $oval =~ s/\s*\|\s*$//;
773 return sprintf("%04o", $to);
774}
Joe Perchesf90774e2016-10-11 13:51:47 -0700775
Wolfram Sang7840a942010-08-09 17:20:57 -0700776our $allowed_asm_includes = qr{(?x:
777 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700778 memory|
779 time|
780 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700781)};
782# memory.h: ARM has a custom one
783
Kees Cook66b47b42014-10-13 15:51:57 -0700784# Load common spelling mistakes and build regular expression list.
785my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700786my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700787
Joe Perches36061e32014-12-10 15:51:43 -0800788if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800789 while (<$spelling>) {
790 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700791
Joe Perches36061e32014-12-10 15:51:43 -0800792 $line =~ s/\s*\n?$//g;
793 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700794
Joe Perches36061e32014-12-10 15:51:43 -0800795 next if ($line =~ m/^\s*#/);
796 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700797
Joe Perches36061e32014-12-10 15:51:43 -0800798 my ($suspect, $fix) = split(/\|\|/, $line);
799
Joe Perches36061e32014-12-10 15:51:43 -0800800 $spelling_fix{$suspect} = $fix;
801 }
802 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800803} else {
804 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700805}
Kees Cook66b47b42014-10-13 15:51:57 -0700806
Joe Perchesebfd7d62015-04-16 12:44:14 -0700807if ($codespell) {
808 if (open(my $spelling, '<', $codespellfile)) {
809 while (<$spelling>) {
810 my $line = $_;
811
812 $line =~ s/\s*\n?$//g;
813 $line =~ s/^\s*//g;
814
815 next if ($line =~ m/^\s*#/);
816 next if ($line =~ m/^\s*$/);
817 next if ($line =~ m/, disabled/i);
818
819 $line =~ s/,.*$//;
820
821 my ($suspect, $fix) = split(/->/, $line);
822
823 $spelling_fix{$suspect} = $fix;
824 }
825 close($spelling);
826 } else {
827 warn "No codespell typos will be found - file '$codespellfile': $!\n";
828 }
829}
830
831$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
832
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700833sub read_words {
834 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700835
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700836 if (open(my $words, '<', $file)) {
837 while (<$words>) {
838 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700839
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700840 $line =~ s/\s*\n?$//g;
841 $line =~ s/^\s*//g;
842
843 next if ($line =~ m/^\s*#/);
844 next if ($line =~ m/^\s*$/);
845 if ($line =~ /\s/) {
846 print("$file: '$line' invalid - ignored\n");
847 next;
848 }
849
Quentin Monnetced69da12020-08-11 18:35:13 -0700850 $$wordsRef .= '|' if (defined $$wordsRef);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700851 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700852 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700853 close($file);
854 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700855 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700856
857 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700858}
859
Quentin Monnetced69da12020-08-11 18:35:13 -0700860my $const_structs;
861if (show_type("CONST_STRUCT")) {
862 read_words(\$const_structs, $conststructsfile)
863 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
864}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700865
Quentin Monnetced69da12020-08-11 18:35:13 -0700866if (defined($typedefsfile)) {
867 my $typeOtherTypedefs;
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700868 read_words(\$typeOtherTypedefs, $typedefsfile)
869 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
Quentin Monnetced69da12020-08-11 18:35:13 -0700870 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700871}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700872
Andy Whitcroft8905a672007-11-28 16:21:06 -0800873sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700874 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
875 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700876 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700877 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700878 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700879 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700880 (?:$typeTypedefs\b)|
881 (?:${all}\b)
882 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800883 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700884 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800885 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800886 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700887 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700888 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800889 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700890 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800891 }x;
Joe Perches18130872014-08-06 16:11:22 -0700892 $NonptrTypeMisordered = qr{
893 (?:$Modifier\s+|const\s+)*
894 (?:
895 (?:${Misordered}\b)
896 )
897 (?:\s+$Modifier|\s+const)*
898 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700899 $NonptrTypeWithAttr = qr{
900 (?:$Modifier\s+|const\s+)*
901 (?:
902 (?:typeof|__typeof__)\s*\([^\)]*\)|
903 (?:$typeTypedefs\b)|
904 (?:${allWithAttr}\b)
905 )
906 (?:\s+$Modifier|\s+const)*
907 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800908 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700909 $NonptrType
Antonio Borneo7b184962020-04-06 20:11:04 -0700910 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700911 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800912 }x;
Joe Perches18130872014-08-06 16:11:22 -0700913 $TypeMisordered = qr{
914 $NonptrTypeMisordered
Antonio Borneo7b184962020-04-06 20:11:04 -0700915 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Perches18130872014-08-06 16:11:22 -0700916 (?:\s+$Inline|\s+$Modifier)*
917 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700918 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700919 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800920}
921build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700922
Joe Perches7d2367a2011-07-25 17:13:22 -0700923our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700924
925# Using $balanced_parens, $LvalOrFunc, or $FuncArg
926# requires at least perl version v5.10.0
927# Any use must be runtime checked with $^V
928
929our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700930our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800931our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700932
Joe Perchesf8422302014-08-06 16:11:31 -0700933our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700934 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700935 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700936 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700937)};
938
Aditya Srivastava8d0325c2020-12-15 20:44:24 -0800939our %allow_repeated_words = (
940 add => '',
941 added => '',
942 bad => '',
943 be => '',
944);
945
Joe Perches7d2367a2011-07-25 17:13:22 -0700946sub deparenthesize {
947 my ($string) = @_;
948 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700949
950 while ($string =~ /^\s*\(.*\)\s*$/) {
951 $string =~ s@^\s*\(\s*@@;
952 $string =~ s@\s*\)\s*$@@;
953 }
954
Joe Perches7d2367a2011-07-25 17:13:22 -0700955 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700956
Joe Perches7d2367a2011-07-25 17:13:22 -0700957 return $string;
958}
959
Joe Perches34456862013-07-03 15:05:34 -0700960sub seed_camelcase_file {
961 my ($file) = @_;
962
963 return if (!(-f $file));
964
965 local $/;
966
967 open(my $include_file, '<', "$file")
968 or warn "$P: Can't read '$file' $!\n";
969 my $text = <$include_file>;
970 close($include_file);
971
972 my @lines = split('\n', $text);
973
974 foreach my $line (@lines) {
975 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
976 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
977 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800978 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
979 $camelcase{$1} = 1;
980 } 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 -0700981 $camelcase{$1} = 1;
982 }
983 }
984}
985
Joe Perchescd28b112019-12-04 16:52:09 -0800986our %maintained_status = ();
987
Joe Perches85b0ee12016-10-11 13:51:44 -0700988sub is_maintained_obsolete {
989 my ($filename) = @_;
990
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800991 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700992
Joe Perchescd28b112019-12-04 16:52:09 -0800993 if (!exists($maintained_status{$filename})) {
994 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
995 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700996
Joe Perchescd28b112019-12-04 16:52:09 -0800997 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700998}
999
Joe Perches3b6e8ac2018-08-21 21:57:47 -07001000sub is_SPDX_License_valid {
1001 my ($license) = @_;
1002
Joe Perches0f7f6352020-10-24 16:59:04 -07001003 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -07001004
Joe Perches56294112018-08-21 21:58:04 -07001005 my $root_path = abs_path($root);
1006 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -07001007 return 0 if ($status ne "");
1008 return 1;
1009}
1010
Joe Perches34456862013-07-03 15:05:34 -07001011my $camelcase_seeded = 0;
1012sub seed_camelcase_includes {
1013 return if ($camelcase_seeded);
1014
1015 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -07001016 my $camelcase_cache = "";
1017 my @include_files = ();
1018
1019 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -07001020
Joe Perches0f7f6352020-10-24 16:59:04 -07001021 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -07001022 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -07001023 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -07001024 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -07001025 } else {
Joe Perchesc707a812013-07-08 16:00:43 -07001026 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -07001027 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -07001028 @include_files = split('\n', $files);
1029 foreach my $file (@include_files) {
1030 my $date = POSIX::strftime("%Y%m%d%H%M",
1031 localtime((stat $file)[9]));
1032 $last_mod_date = $date if ($last_mod_date < $date);
1033 }
1034 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -07001035 }
Joe Perchesc707a812013-07-08 16:00:43 -07001036
1037 if ($camelcase_cache ne "" && -f $camelcase_cache) {
1038 open(my $camelcase_file, '<', "$camelcase_cache")
1039 or warn "$P: Can't read '$camelcase_cache' $!\n";
1040 while (<$camelcase_file>) {
1041 chomp;
1042 $camelcase{$_} = 1;
1043 }
1044 close($camelcase_file);
1045
1046 return;
1047 }
1048
Joe Perches0f7f6352020-10-24 16:59:04 -07001049 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -07001050 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -07001051 @include_files = split('\n', $files);
1052 }
1053
Joe Perches34456862013-07-03 15:05:34 -07001054 foreach my $file (@include_files) {
1055 seed_camelcase_file($file);
1056 }
Joe Perches351b2a12013-07-03 15:05:36 -07001057
Joe Perchesc707a812013-07-08 16:00:43 -07001058 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -07001059 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -07001060 open(my $camelcase_file, '>', "$camelcase_cache")
1061 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -07001062 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1063 print $camelcase_file ("$_\n");
1064 }
1065 close($camelcase_file);
1066 }
Joe Perches34456862013-07-03 15:05:34 -07001067}
1068
Joe Perchesf5f61322020-10-15 20:12:12 -07001069sub git_is_single_file {
1070 my ($filename) = @_;
1071
1072 return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1073
1074 my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1075 my $count = $output =~ tr/\n//;
1076 return $count eq 1 && $output =~ m{^${filename}$};
1077}
1078
Joe Perchesd311cd42014-08-06 16:10:57 -07001079sub git_commit_info {
1080 my ($commit, $id, $desc) = @_;
1081
Joe Perches0f7f6352020-10-24 16:59:04 -07001082 return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
Joe Perchesd311cd42014-08-06 16:10:57 -07001083
Joe Perchesdbbf8692019-09-25 16:46:52 -07001084 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -07001085 $output =~ s/^\s*//gm;
1086 my @lines = split("\n", $output);
1087
Joe Perches0d7835f2015-02-13 14:38:35 -08001088 return ($id, $desc) if ($#lines < 0);
1089
Sean Christopherson5a7f4452019-09-25 16:46:49 -07001090 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -07001091# Maybe one day convert this block of bash into something that returns
1092# all matching commit ids, but it's very slow...
1093#
1094# echo "checking commits $1..."
1095# git rev-list --remotes | grep -i "^$1" |
1096# while read line ; do
1097# git log --format='%H %s' -1 $line |
1098# echo "commit $(cut -c 1-12,41-)"
1099# done
1100 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07001101 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -07001102 } else {
1103 $id = substr($lines[0], 0, 12);
1104 $desc = substr($lines[0], 41);
1105 }
1106
1107 return ($id, $desc);
1108}
1109
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001110$chk_signoff = 0 if ($file);
1111
Andy Whitcroft00df3442007-06-08 13:47:06 -07001112my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001113my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001114my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001115my @fixed_inserted = ();
1116my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001117my $fixlinenr = -1;
1118
Du, Changbin4a593c32016-05-20 17:04:16 -07001119# If input is git commits, extract all commits from the commit expressions.
1120# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
Joe Perches0f7f6352020-10-24 16:59:04 -07001121die "$P: No git repository found\n" if ($git && !-e "$gitroot");
Du, Changbin4a593c32016-05-20 17:04:16 -07001122
1123if ($git) {
1124 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001125 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001126 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001127 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1128 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001129 } elsif ($commit_expr =~ m/\.\./) {
1130 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001131 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001132 $git_range = "-1 $commit_expr";
1133 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001134 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001135 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001136 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1137 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001138 my $sha1 = $1;
1139 my $subject = $2;
1140 unshift(@commits, $sha1);
1141 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001142 }
1143 }
1144 die "$P: no git commits after extraction!\n" if (@commits == 0);
1145 @ARGV = @commits;
1146}
1147
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001148my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001149$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001150for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001151 my $FILE;
Joe Perchesf5f61322020-10-15 20:12:12 -07001152 my $is_git_file = git_is_single_file($filename);
1153 my $oldfile = $file;
1154 $file = 1 if ($is_git_file);
Du, Changbin4a593c32016-05-20 17:04:16 -07001155 if ($git) {
1156 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1157 die "$P: $filename: git format-patch failed - $!\n";
1158 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001159 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001160 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001161 } elsif ($filename eq '-') {
1162 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001163 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001164 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001165 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001166 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001167 if ($filename eq '-') {
1168 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001169 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001170 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001171 } else {
1172 $vname = $filename;
1173 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001174 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001175 chomp;
1176 push(@rawlines, $_);
Geert Uytterhoevenc7f574d2020-06-04 16:50:43 -07001177 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001178 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001179 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001180
1181 if ($#ARGV > 0 && $quiet == 0) {
1182 print '-' x length($vname) . "\n";
1183 print "$vname\n";
1184 print '-' x length($vname) . "\n";
1185 }
1186
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001187 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001188 $exit = 1;
1189 }
1190 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001191 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001192 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001193 @fixed_inserted = ();
1194 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001195 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001196 @modifierListFile = ();
1197 @typeListFile = ();
1198 build_types();
Joe Perchesf5f61322020-10-15 20:12:12 -07001199 $file = $oldfile if ($is_git_file);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001200}
1201
Joe Perchesd8469f12015-06-25 15:03:00 -07001202if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001203 hash_show_words(\%use_type, "Used");
1204 hash_show_words(\%ignore_type, "Ignored");
1205
Joe Perches5b579802018-08-21 21:57:33 -07001206 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001207 print << "EOM"
1208
1209NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001210 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001211EOM
1212 }
1213 if ($exit) {
1214 print << "EOM"
1215
1216NOTE: If any of the errors are false positives, please report
1217 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1218EOM
1219 }
1220}
1221
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001222exit($exit);
1223
1224sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001225 my ($root) = @_;
1226
1227 my @tree_check = (
1228 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1229 "README", "Documentation", "arch", "include", "drivers",
1230 "fs", "init", "ipc", "kernel", "lib", "scripts",
1231 );
1232
1233 foreach my $check (@tree_check) {
1234 if (! -e $root . '/' . $check) {
1235 return 0;
1236 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001237 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001238 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001239}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001240
Joe Perches20112472011-07-25 17:13:23 -07001241sub parse_email {
1242 my ($formatted_email) = @_;
1243
1244 my $name = "";
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001245 my $quoted = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001246 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001247 my $address = "";
1248 my $comment = "";
1249
1250 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1251 $name = $1;
1252 $address = $2;
1253 $comment = $3 if defined $3;
1254 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1255 $address = $1;
1256 $comment = $2 if defined $2;
1257 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1258 $address = $1;
1259 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001260 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001261 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001262 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001263 $name =~ s/^\"|\"$//g;
1264 # If there's a name left after stripping spaces and
1265 # leading quotes, and the address doesn't have both
1266 # leading and trailing angle brackets, the address
1267 # is invalid. ie:
1268 # "joe smith joe@smith.com" bad
1269 # "joe smith <joe@smith.com" bad
1270 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1271 $name = "";
1272 $address = "";
1273 $comment = "";
1274 }
1275 }
1276
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001277 # Extract comments from names excluding quoted parts
1278 # "John D. (Doe)" - Do not extract
1279 if ($name =~ s/\"(.+)\"//) {
1280 $quoted = $1;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001281 }
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001282 while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1283 $name_comment .= trim($1);
1284 }
1285 $name =~ s/^[ \"]+|[ \"]+$//g;
1286 $name = trim("$quoted $name");
1287
Joe Perches3705ce52013-07-03 15:05:31 -07001288 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001289 $address =~ s/^\<|\>$//g;
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001290 $comment = trim($comment);
Joe Perches20112472011-07-25 17:13:23 -07001291
1292 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1293 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1294 $name = "\"$name\"";
1295 }
1296
Joe Perchesdfa05c22020-04-06 20:10:48 -07001297 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001298}
1299
1300sub format_email {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001301 my ($name, $name_comment, $address, $comment) = @_;
Joe Perches20112472011-07-25 17:13:23 -07001302
1303 my $formatted_email;
1304
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001305 $name =~ s/^[ \"]+|[ \"]+$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001306 $address = trim($address);
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001307 $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
Joe Perches20112472011-07-25 17:13:23 -07001308
1309 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1310 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1311 $name = "\"$name\"";
1312 }
1313
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001314 $name_comment = trim($name_comment);
1315 $name_comment = " $name_comment" if ($name_comment ne "");
1316 $comment = trim($comment);
1317 $comment = " $comment" if ($comment ne "");
1318
Joe Perches20112472011-07-25 17:13:23 -07001319 if ("$name" eq "") {
1320 $formatted_email = "$address";
1321 } else {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001322 $formatted_email = "$name$name_comment <$address>";
Joe Perches20112472011-07-25 17:13:23 -07001323 }
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001324 $formatted_email .= "$comment";
Joe Perches20112472011-07-25 17:13:23 -07001325 return $formatted_email;
1326}
1327
Joe Perchesdfa05c22020-04-06 20:10:48 -07001328sub reformat_email {
1329 my ($email) = @_;
1330
1331 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001332 return format_email($email_name, $name_comment, $email_address, $comment);
Joe Perchesdfa05c22020-04-06 20:10:48 -07001333}
1334
1335sub same_email_addresses {
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001336 my ($email1, $email2) = @_;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001337
1338 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1339 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1340
1341 return $email1_name eq $email2_name &&
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001342 $email1_address eq $email2_address &&
1343 $name1_comment eq $name2_comment &&
1344 $comment1 eq $comment2;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001345}
1346
Joe Perchesd311cd42014-08-06 16:10:57 -07001347sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001348 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001349
Joe Perchesbd474ca2014-08-06 16:11:10 -07001350 foreach my $path (split(/:/, $ENV{PATH})) {
1351 if (-e "$path/$bin") {
1352 return "$path/$bin";
1353 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001354 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001355
Joe Perchesbd474ca2014-08-06 16:11:10 -07001356 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001357}
1358
Joe Perches000d1cc12011-07-25 17:13:25 -07001359sub which_conf {
1360 my ($conf) = @_;
1361
1362 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1363 if (-e "$path/$conf") {
1364 return "$path/$conf";
1365 }
1366 }
1367
1368 return "";
1369}
1370
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001371sub expand_tabs {
1372 my ($str) = @_;
1373
1374 my $res = '';
1375 my $n = 0;
1376 for my $c (split(//, $str)) {
1377 if ($c eq "\t") {
1378 $res .= ' ';
1379 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001380 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001381 $res .= ' ';
1382 }
1383 next;
1384 }
1385 $res .= $c;
1386 $n++;
1387 }
1388
1389 return $res;
1390}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001391sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001392 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001393 return $res;
1394}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001395
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001396sub line_stats {
1397 my ($line) = @_;
1398
1399 # Drop the diff line leader and expand tabs
1400 $line =~ s/^.//;
1401 $line = expand_tabs($line);
1402
1403 # Pick the indent from the front of the line.
1404 my ($white) = ($line =~ /^(\s*)/);
1405
1406 return (length($line), length($white));
1407}
1408
Andy Whitcroft773647a2008-03-28 14:15:58 -07001409my $sanitise_quote = '';
1410
1411sub sanitise_line_reset {
1412 my ($in_comment) = @_;
1413
1414 if ($in_comment) {
1415 $sanitise_quote = '*/';
1416 } else {
1417 $sanitise_quote = '';
1418 }
1419}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001420sub sanitise_line {
1421 my ($line) = @_;
1422
1423 my $res = '';
1424 my $l = '';
1425
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001426 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001427 my $off = 0;
1428 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001429
Andy Whitcroft773647a2008-03-28 14:15:58 -07001430 # Always copy over the diff marker.
1431 $res = substr($line, 0, 1);
1432
1433 for ($off = 1; $off < length($line); $off++) {
1434 $c = substr($line, $off, 1);
1435
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001436 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001437 # and end, all to $;.
1438 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1439 $sanitise_quote = '*/';
1440
1441 substr($res, $off, 2, "$;$;");
1442 $off++;
1443 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001444 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001445 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001446 $sanitise_quote = '';
1447 substr($res, $off, 2, "$;$;");
1448 $off++;
1449 next;
1450 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001451 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1452 $sanitise_quote = '//';
1453
1454 substr($res, $off, 2, $sanitise_quote);
1455 $off++;
1456 next;
1457 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001458
1459 # A \ in a string means ignore the next character.
1460 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1461 $c eq "\\") {
1462 substr($res, $off, 2, 'XX');
1463 $off++;
1464 next;
1465 }
1466 # Regular quotes.
1467 if ($c eq "'" || $c eq '"') {
1468 if ($sanitise_quote eq '') {
1469 $sanitise_quote = $c;
1470
1471 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001472 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001473 } elsif ($sanitise_quote eq $c) {
1474 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001475 }
1476 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001477
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001478 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001479 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1480 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001481 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1482 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001483 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1484 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001485 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001486 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001487 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001488 }
1489
Daniel Walker113f04a2009-09-21 17:04:35 -07001490 if ($sanitise_quote eq '//') {
1491 $sanitise_quote = '';
1492 }
1493
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001494 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001495 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001496 my $clean = 'X' x length($1);
1497 $res =~ s@\<.*\>@<$clean>@;
1498
1499 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001500 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001501 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001502 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001503 }
1504
Joe Perchesdadf6802016-08-02 14:04:33 -07001505 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1506 my $match = $1;
1507 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1508 }
1509
Andy Whitcroft00df3442007-06-08 13:47:06 -07001510 return $res;
1511}
1512
Joe Perchesa6962d72013-04-29 16:18:13 -07001513sub get_quoted_string {
1514 my ($line, $rawline) = @_;
1515
Joe Perches478b1792018-04-10 16:33:34 -07001516 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001517 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001518 return substr($rawline, $-[0], $+[0] - $-[0]);
1519}
1520
Andy Whitcroft8905a672007-11-28 16:21:06 -08001521sub ctx_statement_block {
1522 my ($linenr, $remain, $off) = @_;
1523 my $line = $linenr - 1;
1524 my $blk = '';
1525 my $soff = $off;
1526 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001527 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001528
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001529 my $loff = 0;
1530
Andy Whitcroft8905a672007-11-28 16:21:06 -08001531 my $type = '';
1532 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001533 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001534 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001535 my $c;
1536 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001537
1538 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001539 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001540 @stack = (['', 0]) if ($#stack == -1);
1541
Andy Whitcroft773647a2008-03-28 14:15:58 -07001542 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001543 # If we are about to drop off the end, pull in more
1544 # context.
1545 if ($off >= $len) {
1546 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001547 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001548 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001549 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001550 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001551 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001552 $len = length($blk);
1553 $line++;
1554 last;
1555 }
1556 # Bail if there is no further context.
1557 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001558 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001559 last;
1560 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001561 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1562 $level++;
1563 $type = '#';
1564 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001565 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001566 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001567 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001568 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001569
Andy Whitcroft773647a2008-03-28 14:15:58 -07001570 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001571
1572 # Handle nested #if/#else.
1573 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1574 push(@stack, [ $type, $level ]);
1575 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1576 ($type, $level) = @{$stack[$#stack - 1]};
1577 } elsif ($remainder =~ /^#\s*endif\b/) {
1578 ($type, $level) = @{pop(@stack)};
1579 }
1580
Andy Whitcroft8905a672007-11-28 16:21:06 -08001581 # Statement ends at the ';' or a close '}' at the
1582 # outermost level.
1583 if ($level == 0 && $c eq ';') {
1584 last;
1585 }
1586
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001587 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001588 if ($level == 0 && $coff_set == 0 &&
1589 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1590 $remainder =~ /^(else)(?:\s|{)/ &&
1591 $remainder !~ /^else\s+if\b/) {
1592 $coff = $off + length($1) - 1;
1593 $coff_set = 1;
1594 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1595 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001596 }
1597
Andy Whitcroft8905a672007-11-28 16:21:06 -08001598 if (($type eq '' || $type eq '(') && $c eq '(') {
1599 $level++;
1600 $type = '(';
1601 }
1602 if ($type eq '(' && $c eq ')') {
1603 $level--;
1604 $type = ($level != 0)? '(' : '';
1605
1606 if ($level == 0 && $coff < $soff) {
1607 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001608 $coff_set = 1;
1609 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001610 }
1611 }
1612 if (($type eq '' || $type eq '{') && $c eq '{') {
1613 $level++;
1614 $type = '{';
1615 }
1616 if ($type eq '{' && $c eq '}') {
1617 $level--;
1618 $type = ($level != 0)? '{' : '';
1619
1620 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001621 if (substr($blk, $off + 1, 1) eq ';') {
1622 $off++;
1623 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001624 last;
1625 }
1626 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001627 # Preprocessor commands end at the newline unless escaped.
1628 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1629 $level--;
1630 $type = '';
1631 $off++;
1632 last;
1633 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001634 $off++;
1635 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001636 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001637 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001638 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001639 $line++;
1640 $remain--;
1641 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001642
1643 my $statement = substr($blk, $soff, $off - $soff + 1);
1644 my $condition = substr($blk, $soff, $coff - $soff + 1);
1645
1646 #warn "STATEMENT<$statement>\n";
1647 #warn "CONDITION<$condition>\n";
1648
Andy Whitcroft773647a2008-03-28 14:15:58 -07001649 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001650
1651 return ($statement, $condition,
1652 $line, $remain + 1, $off - $loff + 1, $level);
1653}
1654
Andy Whitcroftcf655042008-03-04 14:28:20 -08001655sub statement_lines {
1656 my ($stmt) = @_;
1657
1658 # Strip the diff line prefixes and rip blank lines at start and end.
1659 $stmt =~ s/(^|\n)./$1/g;
1660 $stmt =~ s/^\s*//;
1661 $stmt =~ s/\s*$//;
1662
1663 my @stmt_lines = ($stmt =~ /\n/g);
1664
1665 return $#stmt_lines + 2;
1666}
1667
1668sub statement_rawlines {
1669 my ($stmt) = @_;
1670
1671 my @stmt_lines = ($stmt =~ /\n/g);
1672
1673 return $#stmt_lines + 2;
1674}
1675
1676sub statement_block_size {
1677 my ($stmt) = @_;
1678
1679 $stmt =~ s/(^|\n)./$1/g;
1680 $stmt =~ s/^\s*{//;
1681 $stmt =~ s/}\s*$//;
1682 $stmt =~ s/^\s*//;
1683 $stmt =~ s/\s*$//;
1684
1685 my @stmt_lines = ($stmt =~ /\n/g);
1686 my @stmt_statements = ($stmt =~ /;/g);
1687
1688 my $stmt_lines = $#stmt_lines + 2;
1689 my $stmt_statements = $#stmt_statements + 1;
1690
1691 if ($stmt_lines > $stmt_statements) {
1692 return $stmt_lines;
1693 } else {
1694 return $stmt_statements;
1695 }
1696}
1697
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001698sub ctx_statement_full {
1699 my ($linenr, $remain, $off) = @_;
1700 my ($statement, $condition, $level);
1701
1702 my (@chunks);
1703
Andy Whitcroftcf655042008-03-04 14:28:20 -08001704 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001705 ($statement, $condition, $linenr, $remain, $off, $level) =
1706 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001707 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001708 push(@chunks, [ $condition, $statement ]);
1709 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1710 return ($level, $linenr, @chunks);
1711 }
1712
1713 # Pull in the following conditional/block pairs and see if they
1714 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001715 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001716 ($statement, $condition, $linenr, $remain, $off, $level) =
1717 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001718 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001719 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001720 #print "C: push\n";
1721 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001722 }
1723
1724 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001725}
1726
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001727sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001728 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001729 my $line;
1730 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001731 my $blk = '';
1732 my @o;
1733 my @c;
1734 my @res = ();
1735
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001736 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001737 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001738 for ($line = $start; $remain > 0; $line++) {
1739 next if ($rawlines[$line] =~ /^-/);
1740 $remain--;
1741
1742 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001743
1744 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001745 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001746 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001747 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001748 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001749 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001750 $level = pop(@stack);
1751 }
1752
Andy Whitcroft01464f32010-10-26 14:23:19 -07001753 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001754 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1755 if ($off > 0) {
1756 $off--;
1757 next;
1758 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001759
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001760 if ($c eq $close && $level > 0) {
1761 $level--;
1762 last if ($level == 0);
1763 } elsif ($c eq $open) {
1764 $level++;
1765 }
1766 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001767
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001768 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001769 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001770 }
1771
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001772 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001773 }
1774
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001775 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001776}
1777sub ctx_block_outer {
1778 my ($linenr, $remain) = @_;
1779
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001780 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1781 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001782}
1783sub ctx_block {
1784 my ($linenr, $remain) = @_;
1785
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001786 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1787 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001788}
1789sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001790 my ($linenr, $remain, $off) = @_;
1791
1792 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1793 return @r;
1794}
1795sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001796 my ($linenr, $remain) = @_;
1797
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001798 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001799}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001800sub ctx_statement_level {
1801 my ($linenr, $remain, $off) = @_;
1802
1803 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1804}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001805
1806sub ctx_locate_comment {
1807 my ($first_line, $end_line) = @_;
1808
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001809 # If c99 comment on the current line, or the line before or after
1810 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1811 return $current_comment if (defined $current_comment);
1812 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1813 return $current_comment if (defined $current_comment);
1814 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1815 return $current_comment if (defined $current_comment);
1816
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001817 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001818 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001819 return $current_comment if (defined $current_comment);
1820
1821 # Look through the context and try and figure out if there is a
1822 # comment.
1823 my $in_comment = 0;
1824 $current_comment = '';
1825 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001826 my $line = $rawlines[$linenr - 1];
1827 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001828 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1829 $in_comment = 1;
1830 }
1831 if ($line =~ m@/\*@) {
1832 $in_comment = 1;
1833 }
1834 if (!$in_comment && $current_comment ne '') {
1835 $current_comment = '';
1836 }
1837 $current_comment .= $line . "\n" if ($in_comment);
1838 if ($line =~ m@\*/@) {
1839 $in_comment = 0;
1840 }
1841 }
1842
1843 chomp($current_comment);
1844 return($current_comment);
1845}
1846sub ctx_has_comment {
1847 my ($first_line, $end_line) = @_;
1848 my $cmt = ctx_locate_comment($first_line, $end_line);
1849
Andy Whitcroft00df3442007-06-08 13:47:06 -07001850 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001851 ##print "CMMT: $cmt\n";
1852
1853 return ($cmt ne '');
1854}
1855
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001856sub raw_line {
1857 my ($linenr, $cnt) = @_;
1858
1859 my $offset = $linenr - 1;
1860 $cnt++;
1861
1862 my $line;
1863 while ($cnt) {
1864 $line = $rawlines[$offset++];
1865 next if (defined($line) && $line =~ /^-/);
1866 $cnt--;
1867 }
1868
1869 return $line;
1870}
1871
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001872sub get_stat_real {
1873 my ($linenr, $lc) = @_;
1874
1875 my $stat_real = raw_line($linenr, 0);
1876 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1877 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1878 }
1879
1880 return $stat_real;
1881}
1882
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001883sub get_stat_here {
1884 my ($linenr, $cnt, $here) = @_;
1885
1886 my $herectx = $here . "\n";
1887 for (my $n = 0; $n < $cnt; $n++) {
1888 $herectx .= raw_line($linenr, $n) . "\n";
1889 }
1890
1891 return $herectx;
1892}
1893
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001894sub cat_vet {
1895 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001896 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001897
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001898 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001899 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1900 $res .= $1;
1901 if ($2 ne '') {
1902 $coded = sprintf("^%c", unpack('C', $2) + 64);
1903 $res .= $coded;
1904 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001905 }
1906 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001907
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001908 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001909}
1910
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001911my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001912my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001913my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001914my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001915
1916sub annotate_reset {
1917 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001918 $av_pending = '_';
1919 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001920 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001921}
1922
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001923sub annotate_values {
1924 my ($stream, $type) = @_;
1925
1926 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001927 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001928 my $cur = $stream;
1929
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001930 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001931
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001932 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001933 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001934 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001935 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001936 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001937 print "WS($1)\n" if ($dbg_values > 1);
1938 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001939 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001940 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001941 }
1942
Florian Micklerc023e4732011-01-12 16:59:58 -08001943 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001944 print "CAST($1)\n" if ($dbg_values > 1);
1945 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001946 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001947
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001948 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001949 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001950 $type = 'T';
1951
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001952 } elsif ($cur =~ /^($Modifier)\s*/) {
1953 print "MODIFIER($1)\n" if ($dbg_values > 1);
1954 $type = 'T';
1955
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001956 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001957 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001958 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001959 push(@av_paren_type, $type);
1960 if ($2 ne '') {
1961 $av_pending = 'N';
1962 }
1963 $type = 'E';
1964
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001965 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001966 print "UNDEF($1)\n" if ($dbg_values > 1);
1967 $av_preprocessor = 1;
1968 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001969
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001970 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001971 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001972 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001973
1974 push(@av_paren_type, $type);
1975 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001976 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001977
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001978 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001979 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1980 $av_preprocessor = 1;
1981
1982 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1983
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001984 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001985
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001986 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001987 print "PRE_END($1)\n" if ($dbg_values > 1);
1988
1989 $av_preprocessor = 1;
1990
1991 # Assume all arms of the conditional end as this
1992 # one does, and continue as if the #endif was not here.
1993 pop(@av_paren_type);
1994 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001995 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001996
1997 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001998 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001999
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002000 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
2001 print "ATTR($1)\n" if ($dbg_values > 1);
2002 $av_pending = $type;
2003 $type = 'N';
2004
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002005 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002006 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002007 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002008 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002009 }
2010 $type = 'N';
2011
Andy Whitcroft14b111c2008-10-15 22:02:16 -07002012 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002013 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07002014 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002015 $type = 'N';
2016
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002017 } elsif ($cur =~/^(case)/o) {
2018 print "CASE($1)\n" if ($dbg_values > 1);
2019 $av_pend_colon = 'C';
2020 $type = 'N';
2021
Andy Whitcroft14b111c2008-10-15 22:02:16 -07002022 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002023 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002024 $type = 'N';
2025
2026 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002027 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002028 push(@av_paren_type, $av_pending);
2029 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002030 $type = 'N';
2031
2032 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002033 my $new_type = pop(@av_paren_type);
2034 if ($new_type ne '_') {
2035 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002036 print "PAREN('$1') -> $type\n"
2037 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002038 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002039 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002040 }
2041
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07002042 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002043 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07002044 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08002045 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002046
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002047 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
2048 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002049 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002050 } elsif ($type eq 'E') {
2051 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002052 }
2053 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
2054 $type = 'V';
2055
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002056 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002057 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002058 $type = 'V';
2059
2060 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002061 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002062 $type = 'N';
2063
Andy Whitcroftcf655042008-03-04 14:28:20 -08002064 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002065 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002066 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002067 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002068
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002069 } elsif ($cur =~/^(,)/) {
2070 print "COMMA($1)\n" if ($dbg_values > 1);
2071 $type = 'C';
2072
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002073 } elsif ($cur =~ /^(\?)/o) {
2074 print "QUESTION($1)\n" if ($dbg_values > 1);
2075 $type = 'N';
2076
2077 } elsif ($cur =~ /^(:)/o) {
2078 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
2079
2080 substr($var, length($res), 1, $av_pend_colon);
2081 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
2082 $type = 'E';
2083 } else {
2084 $type = 'N';
2085 }
2086 $av_pend_colon = 'O';
2087
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002088 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002089 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002090 $type = 'N';
2091
Andy Whitcroft0d413862008-10-15 22:02:16 -07002092 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002093 my $variant;
2094
2095 print "OPV($1)\n" if ($dbg_values > 1);
2096 if ($type eq 'V') {
2097 $variant = 'B';
2098 } else {
2099 $variant = 'U';
2100 }
2101
2102 substr($var, length($res), 1, $variant);
2103 $type = 'N';
2104
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002105 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002106 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002107 if ($1 ne '++' && $1 ne '--') {
2108 $type = 'N';
2109 }
2110
2111 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002112 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002113 }
2114 if (defined $1) {
2115 $cur = substr($cur, length($1));
2116 $res .= $type x length($1);
2117 }
2118 }
2119
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002120 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002121}
2122
Andy Whitcroft8905a672007-11-28 16:21:06 -08002123sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002124 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002125 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002126 ^(?:
2127 $Modifier|
2128 $Storage|
2129 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002130 DEFINE_\S+
2131 )$|
2132 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002133 goto|
2134 return|
2135 case|
2136 else|
2137 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002138 do|
2139 \#|
2140 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002141 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002142 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002143 )}x;
2144 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2145 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002146 # Check for modifiers.
2147 $possible =~ s/\s*$Storage\s*//g;
2148 $possible =~ s/\s*$Sparse\s*//g;
2149 if ($possible =~ /^\s*$/) {
2150
2151 } elsif ($possible =~ /\s/) {
2152 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002153 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002154 if ($modifier !~ $notPermitted) {
2155 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002156 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002157 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002158 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002159
2160 } else {
2161 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002162 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002163 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002164 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002165 } else {
2166 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002167 }
2168}
2169
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002170my $prefix = '';
2171
Joe Perches000d1cc12011-07-25 17:13:25 -07002172sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002173 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002174
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002175 $type =~ tr/[a-z]/[A-Z]/;
2176
Joe Perchescbec18a2014-04-03 14:49:19 -07002177 return defined $use_type{$type} if (scalar keys %use_type > 0);
2178
2179 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002180}
2181
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002182sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002183 my ($level, $type, $msg) = @_;
2184
2185 if (!show_type($type) ||
2186 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002187 return 0;
2188 }
Joe Perches57230292015-06-25 15:03:03 -07002189 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002190 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002191 if ($level eq 'ERROR') {
2192 $output .= RED;
2193 } elsif ($level eq 'WARNING') {
2194 $output .= YELLOW;
2195 } else {
2196 $output .= GREEN;
2197 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002198 }
Joe Perches57230292015-06-25 15:03:03 -07002199 $output .= $prefix . $level . ':';
2200 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002201 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002202 $output .= "$type:";
2203 }
John Brooks737c0762017-07-10 15:52:24 -07002204 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002205 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002206
2207 if ($showfile) {
2208 my @lines = split("\n", $output, -1);
2209 splice(@lines, 1, 1);
2210 $output = join("\n", @lines);
2211 }
Joe Perches57230292015-06-25 15:03:03 -07002212 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002213
Joe Perches57230292015-06-25 15:03:03 -07002214 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002215
2216 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002217}
Joe Perchescbec18a2014-04-03 14:49:19 -07002218
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002219sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002220 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002221}
Joe Perches000d1cc12011-07-25 17:13:25 -07002222
Joe Perchesd752fcc2014-08-06 16:11:05 -07002223sub fixup_current_range {
2224 my ($lineRef, $offset, $length) = @_;
2225
2226 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2227 my $o = $1;
2228 my $l = $2;
2229 my $no = $o + $offset;
2230 my $nl = $l + $length;
2231 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2232 }
2233}
2234
2235sub fix_inserted_deleted_lines {
2236 my ($linesRef, $insertedRef, $deletedRef) = @_;
2237
2238 my $range_last_linenr = 0;
2239 my $delta_offset = 0;
2240
2241 my $old_linenr = 0;
2242 my $new_linenr = 0;
2243
2244 my $next_insert = 0;
2245 my $next_delete = 0;
2246
2247 my @lines = ();
2248
2249 my $inserted = @{$insertedRef}[$next_insert++];
2250 my $deleted = @{$deletedRef}[$next_delete++];
2251
2252 foreach my $old_line (@{$linesRef}) {
2253 my $save_line = 1;
2254 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002255 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002256 $delta_offset = 0;
2257 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2258 $range_last_linenr = $new_linenr;
2259 fixup_current_range(\$line, $delta_offset, 0);
2260 }
2261
2262 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2263 $deleted = @{$deletedRef}[$next_delete++];
2264 $save_line = 0;
2265 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2266 }
2267
2268 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2269 push(@lines, ${$inserted}{'LINE'});
2270 $inserted = @{$insertedRef}[$next_insert++];
2271 $new_linenr++;
2272 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2273 }
2274
2275 if ($save_line) {
2276 push(@lines, $line);
2277 $new_linenr++;
2278 }
2279
2280 $old_linenr++;
2281 }
2282
2283 return @lines;
2284}
2285
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002286sub fix_insert_line {
2287 my ($linenr, $line) = @_;
2288
2289 my $inserted = {
2290 LINENR => $linenr,
2291 LINE => $line,
2292 };
2293 push(@fixed_inserted, $inserted);
2294}
2295
2296sub fix_delete_line {
2297 my ($linenr, $line) = @_;
2298
2299 my $deleted = {
2300 LINENR => $linenr,
2301 LINE => $line,
2302 };
2303
2304 push(@fixed_deleted, $deleted);
2305}
2306
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002307sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002308 my ($type, $msg) = @_;
2309
2310 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002311 our $clean = 0;
2312 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002313 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002314 }
Joe Perches3705ce52013-07-03 15:05:31 -07002315 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002316}
2317sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002318 my ($type, $msg) = @_;
2319
2320 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002321 our $clean = 0;
2322 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002323 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002324 }
Joe Perches3705ce52013-07-03 15:05:31 -07002325 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002326}
2327sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002328 my ($type, $msg) = @_;
2329
2330 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002331 our $clean = 0;
2332 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002333 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002334 }
Joe Perches3705ce52013-07-03 15:05:31 -07002335 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002336}
2337
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002338sub check_absolute_file {
2339 my ($absolute, $herecurr) = @_;
2340 my $file = $absolute;
2341
2342 ##print "absolute<$absolute>\n";
2343
2344 # See if any suffix of this path is a path within the tree.
2345 while ($file =~ s@^[^/]*/@@) {
2346 if (-f "$root/$file") {
2347 ##print "file<$file>\n";
2348 last;
2349 }
2350 }
2351 if (! -f _) {
2352 return 0;
2353 }
2354
2355 # It is, so see if the prefix is acceptable.
2356 my $prefix = $absolute;
2357 substr($prefix, -length($file)) = '';
2358
2359 ##print "prefix<$prefix>\n";
2360 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002361 WARN("USE_RELATIVE_PATH",
2362 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002363 }
2364}
2365
Joe Perches3705ce52013-07-03 15:05:31 -07002366sub trim {
2367 my ($string) = @_;
2368
Joe Perchesb34c6482013-09-11 14:24:01 -07002369 $string =~ s/^\s+|\s+$//g;
2370
2371 return $string;
2372}
2373
2374sub ltrim {
2375 my ($string) = @_;
2376
2377 $string =~ s/^\s+//;
2378
2379 return $string;
2380}
2381
2382sub rtrim {
2383 my ($string) = @_;
2384
2385 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002386
2387 return $string;
2388}
2389
Joe Perches52ea8502013-11-12 15:10:09 -08002390sub string_find_replace {
2391 my ($string, $find, $replace) = @_;
2392
2393 $string =~ s/$find/$replace/g;
2394
2395 return $string;
2396}
2397
Joe Perches3705ce52013-07-03 15:05:31 -07002398sub tabify {
2399 my ($leading) = @_;
2400
Antonio Borneo713a09d2020-04-06 20:11:07 -07002401 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002402 my $max_spaces_before_tab = $source_indent - 1;
2403 my $spaces_to_tab = " " x $source_indent;
2404
2405 #convert leading spaces to tabs
2406 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2407 #Remove spaces before a tab
2408 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2409
2410 return "$leading";
2411}
2412
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002413sub pos_last_openparen {
2414 my ($line) = @_;
2415
2416 my $pos = 0;
2417
2418 my $opens = $line =~ tr/\(/\(/;
2419 my $closes = $line =~ tr/\)/\)/;
2420
2421 my $last_openparen = 0;
2422
2423 if (($opens == 0) || ($closes >= $opens)) {
2424 return -1;
2425 }
2426
2427 my $len = length($line);
2428
2429 for ($pos = 0; $pos < $len; $pos++) {
2430 my $string = substr($line, $pos);
2431 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2432 $pos += length($1) - 1;
2433 } elsif (substr($line, $pos, 1) eq '(') {
2434 $last_openparen = $pos;
2435 } elsif (index($string, '(') == -1) {
2436 last;
2437 }
2438 }
2439
Joe Perches91cb5192014-04-03 14:49:32 -07002440 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002441}
2442
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002443sub get_raw_comment {
2444 my ($line, $rawline) = @_;
2445 my $comment = '';
2446
2447 for my $i (0 .. (length($line) - 1)) {
2448 if (substr($line, $i, 1) eq "$;") {
2449 $comment .= substr($rawline, $i, 1);
2450 }
2451 }
2452
2453 return $comment;
2454}
2455
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002456sub process {
2457 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002458
2459 my $linenr=0;
2460 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002461 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002462 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002463 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002464
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002465 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002466 my $indent;
2467 my $previndent=0;
2468 my $stashindent=0;
2469
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002470 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002471 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002472 my $author = '';
2473 my $authorsignoff = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002474 my $author_sob = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002475 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002476 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002477 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002478 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002479 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002480 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002481 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002482 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002483 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002484 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002485 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002486 my $non_utf8_charset = 0;
2487
Joe Perches365dd4e2014-08-06 16:10:42 -07002488 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002489 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002490
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002491 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002492 our $cnt_lines = 0;
2493 our $cnt_error = 0;
2494 our $cnt_warn = 0;
2495 our $cnt_chk = 0;
2496
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002497 # Trace the real file/line as we go.
2498 my $realfile = '';
2499 my $realline = 0;
2500 my $realcnt = 0;
2501 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002502 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002503 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002504 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002505 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002506 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002507
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002508 my $prev_values = 'E';
2509
2510 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002511 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002512 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002513 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002514 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002515
Joe Perches7e51f192013-09-11 14:23:57 -07002516 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002517
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002518 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002519 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002520 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002521 my @setup_docs = ();
2522 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002523
Joe Perchesd8b07712013-11-12 15:10:06 -08002524 my $camelcase_file_seeded = 0;
2525
Rob Herring9f3a8992018-04-10 16:33:13 -07002526 my $checklicenseline = 1;
2527
Andy Whitcroft773647a2008-03-28 14:15:58 -07002528 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002529 my $line;
2530 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002531 $linenr++;
2532 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002533
Joe Perches3705ce52013-07-03 15:05:31 -07002534 push(@fixed, $rawline) if ($fix);
2535
Andy Whitcroft773647a2008-03-28 14:15:58 -07002536 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002537 $setup_docs = 0;
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07002538 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002539 $setup_docs = 1;
2540 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002541 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002542 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002543 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002544 $realline=$1-1;
2545 if (defined $2) {
2546 $realcnt=$3+1;
2547 } else {
2548 $realcnt=1+1;
2549 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002550 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002551
2552 # Guestimate if this is a continuing comment. Run
2553 # the context looking for a comment "edge". If this
2554 # edge is a close comment then we must be in a comment
2555 # at context start.
2556 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002557 my $cnt = $realcnt;
2558 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2559 next if (defined $rawlines[$ln - 1] &&
2560 $rawlines[$ln - 1] =~ /^-/);
2561 $cnt--;
2562 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002563 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002564 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2565 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2566 ($edge) = $1;
2567 last;
2568 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002569 }
2570 if (defined $edge && $edge eq '*/') {
2571 $in_comment = 1;
2572 }
2573
2574 # Guestimate if this is a continuing comment. If this
2575 # is the start of a diff block and this line starts
2576 # ' *' then it is very likely a comment.
2577 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002578 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002579 {
2580 $in_comment = 1;
2581 }
2582
2583 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2584 sanitise_line_reset($in_comment);
2585
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002586 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002587 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002588 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002589 $line = sanitise_line($rawline);
2590 }
2591 push(@lines, $line);
2592
2593 if ($realcnt > 1) {
2594 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2595 } else {
2596 $realcnt = 0;
2597 }
2598
2599 #print "==>$rawline\n";
2600 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002601
2602 if ($setup_docs && $line =~ /^\+/) {
2603 push(@setup_docs, $line);
2604 }
2605 }
2606
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002607 $prefix = '';
2608
Andy Whitcroft773647a2008-03-28 14:15:58 -07002609 $realcnt = 0;
2610 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002611 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002612 foreach my $line (@lines) {
2613 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002614 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002615 my $sline = $line; #copy of $line
2616 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002617
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002618 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002619 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002620
Joe Perches12c253a2018-06-07 17:10:58 -07002621# check if it's a mode change, rename or start of a patch
2622 if (!$in_commit_log &&
2623 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2624 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2625 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2626 $is_patch = 1;
2627 }
2628
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002629#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002630 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002631 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2632 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002633 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002634 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002635 $realline=$1-1;
2636 if (defined $2) {
2637 $realcnt=$3+1;
2638 } else {
2639 $realcnt=1+1;
2640 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002641 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002642 $prev_values = 'E';
2643
Andy Whitcroft773647a2008-03-28 14:15:58 -07002644 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002645 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002646 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002647 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002648 if ($context =~ /\b(\w+)\s*\(/) {
2649 $context_function = $1;
2650 } else {
2651 undef $context_function;
2652 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002653 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002654
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002655# track the line number as we move through the hunk, note that
2656# new versions of GNU diff omit the leading space on completely
2657# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002658 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002659 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002660 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002661
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002662 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002663 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002664
2665 # Track the previous line.
2666 ($prevline, $stashline) = ($stashline, $line);
2667 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002668 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2669
Andy Whitcroft773647a2008-03-28 14:15:58 -07002670 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002671
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002672 } elsif ($realcnt == 1) {
2673 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002674 }
2675
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002676 my $hunk_line = ($realcnt != 0);
2677
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002678 $here = "#$linenr: " if (!$file);
2679 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002680
Joe Perches2ac73b42014-06-04 16:12:05 -07002681 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002682 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002683 if ($line =~ /^diff --git.*?(\S+)$/) {
2684 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002685 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002686 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002687 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002688 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002689 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002690 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002691 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002692
2693 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002694 if (!$file && $tree && $p1_prefix ne '' &&
2695 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002696 WARN("PATCH_PREFIX",
2697 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002698 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002699
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002700 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002701 ERROR("MODIFIED_INCLUDE_ASM",
2702 "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 -07002703 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002704 $found_file = 1;
2705 }
2706
Joe Perches34d88152015-06-25 15:03:05 -07002707#make up the handle for any error we report on this line
2708 if ($showfile) {
2709 $prefix = "$realfile:$realline: "
2710 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002711 if ($file) {
2712 $prefix = "$filename:$realline: ";
2713 } else {
2714 $prefix = "$filename:$linenr: ";
2715 }
Joe Perches34d88152015-06-25 15:03:05 -07002716 }
2717
Joe Perches2ac73b42014-06-04 16:12:05 -07002718 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002719 if (is_maintained_obsolete($realfile)) {
2720 WARN("OBSOLETE",
2721 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2722 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002723 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002724 $check = 1;
2725 } else {
2726 $check = $check_orig;
2727 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002728 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002729
2730 if ($realfile !~ /^MAINTAINERS/) {
2731 my $last_binding_patch = $is_binding_patch;
2732
2733 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2734
2735 if (($last_binding_patch != -1) &&
2736 ($last_binding_patch ^ $is_binding_patch)) {
2737 WARN("DT_SPLIT_BINDING_PATCH",
Mauro Carvalho Chehab858e6842020-04-15 16:45:25 +02002738 "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 -07002739 }
2740 }
2741
Andy Whitcroft773647a2008-03-28 14:15:58 -07002742 next;
2743 }
2744
Randy Dunlap389834b2007-06-08 13:47:03 -07002745 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002746
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002747 my $hereline = "$here\n$rawline\n";
2748 my $herecurr = "$here\n$rawline\n";
2749 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002750
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002751 $cnt_lines++ if ($realcnt != 0);
2752
Joe Perches490b2922018-08-21 21:58:01 -07002753# Verify the existence of a commit log if appropriate
2754# 2 is used because a $signature is counted in $commit_log_lines
2755 if ($in_commit_log) {
2756 if ($line !~ /^\s*$/) {
2757 $commit_log_lines++; #could be a $signature
2758 }
2759 } elsif ($has_commit_log && $commit_log_lines < 2) {
2760 WARN("COMMIT_MESSAGE",
2761 "Missing commit description - Add an appropriate one\n");
2762 $commit_log_lines = 2; #warn only once
2763 }
2764
Joe Perchese518e9a2015-06-25 15:03:27 -07002765# Check if the commit log has what seems like a diff which can confuse patch
2766 if ($in_commit_log && !$commit_log_has_diff &&
Mrinal Pandey13e45412020-09-04 16:35:52 -07002767 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2768 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
Joe Perchese518e9a2015-06-25 15:03:27 -07002769 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2770 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2771 ERROR("DIFF_IN_COMMIT_MSG",
2772 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2773 $commit_log_has_diff = 1;
2774 }
2775
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002776# Check for incorrect file permissions
2777 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2778 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002779 if ($realfile !~ m@scripts/@ &&
2780 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002781 ERROR("EXECUTE_PERMISSIONS",
2782 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002783 }
2784 }
2785
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002786# Check the patch for a From:
2787 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2788 $author = $1;
Dwaipayan Raye7f929f2020-10-15 20:12:15 -07002789 my $curline = $linenr;
2790 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2791 $author .= $1;
2792 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002793 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2794 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002795 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002796 }
2797
Joe Perches20112472011-07-25 17:13:23 -07002798# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002799 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002800 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002801 $in_commit_log = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002802 if ($author ne '' && $authorsignoff != 1) {
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002803 if (same_email_addresses($1, $author)) {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002804 $authorsignoff = 1;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002805 } else {
2806 my $ctx = $1;
2807 my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
2808 my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
2809
2810 if ($email_address eq $author_address && $email_name eq $author_name) {
2811 $author_sob = $ctx;
2812 $authorsignoff = 2;
2813 } elsif ($email_address eq $author_address) {
2814 $author_sob = $ctx;
2815 $authorsignoff = 3;
2816 } elsif ($email_name eq $author_name) {
2817 $author_sob = $ctx;
2818 $authorsignoff = 4;
2819
2820 my $address1 = $email_address;
2821 my $address2 = $author_address;
2822
2823 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
2824 $address1 = "$1$2";
2825 }
2826 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
2827 $address2 = "$1$2";
2828 }
2829 if ($address1 eq $address2) {
2830 $authorsignoff = 5;
2831 }
2832 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002833 }
2834 }
Joe Perches20112472011-07-25 17:13:23 -07002835 }
2836
Joe Perches44d303e2020-04-06 20:11:10 -07002837# Check for patch separator
2838 if ($line =~ /^---$/) {
2839 $has_patch_separator = 1;
2840 $in_commit_log = 0;
2841 }
2842
Joe Perchese0d975b2014-12-10 15:51:49 -08002843# Check if MAINTAINERS is being updated. If so, there's probably no need to
2844# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2845 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2846 $reported_maintainer_file = 1;
2847 }
2848
Joe Perches20112472011-07-25 17:13:23 -07002849# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002850 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002851 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002852 my $space_before = $1;
2853 my $sign_off = $2;
2854 my $space_after = $3;
2855 my $email = $4;
2856 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2857
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002858 if ($sign_off !~ /$signature_tags/) {
Aditya Srivastava831242a2020-12-15 20:45:12 -08002859 my $suggested_signature = find_standard_signature($sign_off);
2860 if ($suggested_signature eq "") {
2861 WARN("BAD_SIGN_OFF",
2862 "Non-standard signature: $sign_off\n" . $herecurr);
2863 } else {
2864 if (WARN("BAD_SIGN_OFF",
2865 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
2866 $fix) {
2867 $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
2868 }
2869 }
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002870 }
Joe Perches20112472011-07-25 17:13:23 -07002871 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002872 if (WARN("BAD_SIGN_OFF",
2873 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2874 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002875 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002876 "$ucfirst_sign_off $email";
2877 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002878 }
Joe Perches20112472011-07-25 17:13:23 -07002879 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002880 if (WARN("BAD_SIGN_OFF",
2881 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2882 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002883 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002884 "$ucfirst_sign_off $email";
2885 }
2886
Joe Perches20112472011-07-25 17:13:23 -07002887 }
2888 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002889 if (WARN("BAD_SIGN_OFF",
2890 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2891 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002892 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002893 "$ucfirst_sign_off $email";
2894 }
Joe Perches20112472011-07-25 17:13:23 -07002895 }
2896
Joe Perchesdfa05c22020-04-06 20:10:48 -07002897 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002898 my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
Joe Perches20112472011-07-25 17:13:23 -07002899 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002900 ERROR("BAD_SIGN_OFF",
2901 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002902 } else {
2903 my $dequoted = $suggested_email;
2904 $dequoted =~ s/^"//;
2905 $dequoted =~ s/" </ </;
2906 # Don't force email to have quotes
2907 # Allow just an angle bracketed address
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002908 if (!same_email_addresses($email, $suggested_email)) {
2909 if (WARN("BAD_SIGN_OFF",
2910 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
2911 $fix) {
2912 $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
2913 }
2914 }
2915
2916 # Address part shouldn't have comments
2917 my $stripped_address = $email_address;
2918 $stripped_address =~ s/\([^\(\)]*\)//g;
2919 if ($email_address ne $stripped_address) {
2920 if (WARN("BAD_SIGN_OFF",
2921 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
2922 $fix) {
2923 $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
2924 }
2925 }
2926
2927 # Only one name comment should be allowed
2928 my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
2929 if ($comment_count > 1) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002930 WARN("BAD_SIGN_OFF",
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002931 "Use a single name comment in email: '$email'\n" . $herecurr);
2932 }
2933
2934
2935 # stable@vger.kernel.org or stable@kernel.org shouldn't
Dwaipayan Raye73d2712020-12-15 20:44:56 -08002936 # have an email name. In addition comments should strictly
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002937 # begin with a #
2938 if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
2939 if (($comment ne "" && $comment !~ /^#.+/) ||
2940 ($email_name ne "")) {
2941 my $cur_name = $email_name;
2942 my $new_comment = $comment;
2943 $cur_name =~ s/[a-zA-Z\s\-\"]+//g;
2944
2945 # Remove brackets enclosing comment text
2946 # and # from start of comments to get comment text
2947 $new_comment =~ s/^\((.*)\)$/$1/;
2948 $new_comment =~ s/^\[(.*)\]$/$1/;
2949 $new_comment =~ s/^[\s\#]+|\s+$//g;
2950
2951 $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
2952 $new_comment = " # $new_comment" if ($new_comment ne "");
2953 my $new_email = "$email_address$new_comment";
2954
2955 if (WARN("BAD_STABLE_ADDRESS_STYLE",
2956 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
2957 $fix) {
2958 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2959 }
2960 }
2961 } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
2962 my $new_comment = $comment;
2963
2964 # Extract comment text from within brackets or
2965 # c89 style /*...*/ comments
2966 $new_comment =~ s/^\[(.*)\]$/$1/;
2967 $new_comment =~ s/^\/\*(.*)\*\/$/$1/;
2968
2969 $new_comment = trim($new_comment);
2970 $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
2971 $new_comment = "($new_comment)" if ($new_comment ne "");
2972 my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
2973
2974 if (WARN("BAD_SIGN_OFF",
2975 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
2976 $fix) {
2977 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2978 }
Joe Perches20112472011-07-25 17:13:23 -07002979 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002980 }
Joe Perches7e51f192013-09-11 14:23:57 -07002981
2982# Check for duplicate signatures
2983 my $sig_nospace = $line;
2984 $sig_nospace =~ s/\s//g;
2985 $sig_nospace = lc($sig_nospace);
2986 if (defined $signatures{$sig_nospace}) {
2987 WARN("BAD_SIGN_OFF",
2988 "Duplicate signature\n" . $herecurr);
2989 } else {
2990 $signatures{$sig_nospace} = 1;
2991 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002992
2993# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2994 if ($sign_off =~ /^co-developed-by:$/i) {
2995 if ($email eq $author) {
2996 WARN("BAD_SIGN_OFF",
2997 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2998 }
2999 if (!defined $lines[$linenr]) {
3000 WARN("BAD_SIGN_OFF",
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08003001 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07003002 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
3003 WARN("BAD_SIGN_OFF",
3004 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
3005 } elsif ($1 ne $email) {
3006 WARN("BAD_SIGN_OFF",
3007 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
3008 }
3009 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003010 }
3011
Joe Perchesa2fe16b2015-02-13 14:39:02 -08003012# Check email subject for common tools that don't need to be mentioned
3013 if ($in_header_lines &&
3014 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
3015 WARN("EMAIL_SUBJECT",
3016 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
3017 }
3018
Joe Perches44d303e2020-04-06 20:11:10 -07003019# Check for Gerrit Change-Ids not in any patch context
3020 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Aditya Srivastava7580c5b2020-12-15 20:44:47 -08003021 if (ERROR("GERRIT_CHANGE_ID",
3022 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
3023 $fix) {
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08003024 fix_delete_line($fixlinenr, $rawline);
3025 }
Christopher Covington7ebd05e2014-04-03 14:49:31 -07003026 }
3027
Joe Perches369c8dd2015-11-06 16:31:34 -08003028# Check if the commit log is in a possible stack dump
3029 if ($in_commit_log && !$commit_log_possible_stack_dump &&
3030 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3031 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3032 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07003033 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3034 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3035 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3036 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08003037 $commit_log_possible_stack_dump = 1;
3038 }
3039
Joe Perches2a076f42015-04-16 12:44:28 -07003040# Check for line lengths > 75 in commit log, warn once
3041 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08003042 length($line) > 75 &&
3043 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3044 # file delta changes
3045 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
3046 # filename then :
Aditya Srivastava27b379a2020-12-15 20:44:59 -08003047 $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
3048 # A Fixes: or Link: line or signature tag line
Joe Perches369c8dd2015-11-06 16:31:34 -08003049 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07003050 WARN("COMMIT_LOG_LONG_LINE",
3051 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
3052 $commit_log_long_line = 1;
3053 }
3054
Joe Perchesbf4daf12015-09-09 15:37:50 -07003055# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08003056 if ($in_commit_log && $commit_log_possible_stack_dump &&
3057 $line =~ /^\s*$/) {
3058 $commit_log_possible_stack_dump = 0;
3059 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07003060
Dwaipayan Ray084a6172020-12-15 20:45:18 -08003061# Check for lines starting with a #
3062 if ($in_commit_log && $line =~ /^#/) {
3063 if (WARN("COMMIT_COMMENT_SYMBOL",
3064 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3065 $fix) {
3066 $fixed[$fixlinenr] =~ s/^/ /;
3067 }
3068 }
3069
Joe Perches0d7835f2015-02-13 14:38:35 -08003070# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08003071 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07003072 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07003073 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07003074 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07003075 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08003076 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3077 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07003078 my $init_char = "c";
3079 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08003080 my $short = 1;
3081 my $long = 0;
3082 my $case = 1;
3083 my $space = 1;
3084 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08003085 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08003086 my $id = '0123456789ab';
3087 my $orig_desc = "commit description";
3088 my $description = "";
3089
Joe Perchesfe043ea2015-09-09 15:37:25 -07003090 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3091 $init_char = $1;
3092 $orig_commit = lc($2);
3093 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
3094 $orig_commit = lc($1);
3095 }
3096
Joe Perches0d7835f2015-02-13 14:38:35 -08003097 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
3098 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
3099 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
3100 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
3101 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
3102 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08003103 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08003104 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
3105 defined $rawlines[$linenr] &&
3106 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
3107 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08003108 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08003109 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
3110 defined $rawlines[$linenr] &&
3111 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
3112 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
3113 $orig_desc = $1;
3114 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
3115 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08003116 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08003117 }
3118
3119 ($id, $description) = git_commit_info($orig_commit,
3120 $id, $orig_desc);
3121
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07003122 if (defined($id) &&
3123 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08003124 ERROR("GIT_COMMIT_ID",
3125 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
3126 }
Joe Perchesd311cd42014-08-06 16:10:57 -07003127 }
3128
Joe Perches13f19372014-08-06 16:10:59 -07003129# Check for added, moved or deleted files
3130 if (!$reported_maintainer_file && !$in_commit_log &&
3131 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
3132 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
3133 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
3134 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08003135 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07003136 $reported_maintainer_file = 1;
3137 WARN("FILE_PATH_CHANGES",
3138 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
3139 }
3140
Rob Herringe400edb2019-09-12 15:18:20 +01003141# Check for adding new DT bindings not in schema format
3142 if (!$in_commit_log &&
3143 ($line =~ /^new file mode\s*\d+\s*$/) &&
3144 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3145 WARN("DT_SCHEMA_BINDING_PATCH",
3146 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
3147 }
3148
Andy Whitcroft00df3442007-06-08 13:47:06 -07003149# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08003150 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003151 ERROR("CORRUPTED_PATCH",
3152 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003153 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003154 }
3155
3156# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3157 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003158 $rawline !~ m/^$UTF8*$/) {
3159 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3160
3161 my $blank = copy_spacing($rawline);
3162 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3163 my $hereptr = "$hereline$ptr\n";
3164
Joe Perches34d99212011-07-25 17:13:26 -07003165 CHK("INVALID_UTF8",
3166 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003167 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003168
Joe Perches15662b32011-10-31 17:13:12 -07003169# Check if it's the start of a commit log
3170# (not a header line and we haven't seen the patch filename)
3171 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07003172 !($rawline =~ /^\s+(?:\S|$)/ ||
3173 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07003174 $in_header_lines = 0;
3175 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07003176 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07003177 }
3178
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003179# Check if there is UTF-8 in a commit log when a mail header has explicitly
3180# declined it, i.e defined some charset where it is missing.
3181 if ($in_header_lines &&
3182 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3183 $1 !~ /utf-8/i) {
3184 $non_utf8_charset = 1;
3185 }
3186
3187 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07003188 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003189 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07003190 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3191 }
3192
Joe Perchesd6430f72016-12-12 16:46:28 -08003193# Check for absolute kernel paths in commit message
3194 if ($tree && $in_commit_log) {
3195 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3196 my $file = $1;
3197
3198 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3199 check_absolute_file($1, $herecurr)) {
3200 #
3201 } else {
3202 check_absolute_file($file, $herecurr);
3203 }
3204 }
3205 }
3206
Kees Cook66b47b42014-10-13 15:51:57 -07003207# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07003208 if (defined($misspellings) &&
3209 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003210 while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07003211 my $typo = $1;
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003212 my $blank = copy_spacing($rawline);
3213 my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
3214 my $hereptr = "$hereline$ptr\n";
Kees Cook66b47b42014-10-13 15:51:57 -07003215 my $typo_fix = $spelling_fix{lc($typo)};
3216 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3217 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07003218 my $msg_level = \&WARN;
3219 $msg_level = \&CHK if ($file);
3220 if (&{$msg_level}("TYPO_SPELLING",
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003221 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07003222 $fix) {
3223 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3224 }
3225 }
3226 }
3227
Matteo Crocea8dd86b2019-09-25 16:46:38 -07003228# check for invalid commit id
3229 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3230 my $id;
3231 my $description;
3232 ($id, $description) = git_commit_info($2, undef, undef);
3233 if (!defined($id)) {
3234 WARN("UNKNOWN_COMMIT_ID",
3235 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3236 }
3237 }
3238
Joe Perches310cd062020-10-15 20:11:52 -07003239# check for repeated words separated by a single space
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003240# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3241 if (($rawline =~ /^\+/ || $in_commit_log) &&
3242 $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003243 pos($rawline) = 1 if (!$in_commit_log);
Joe Perches310cd062020-10-15 20:11:52 -07003244 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3245
3246 my $first = $1;
3247 my $second = $2;
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003248 my $start_pos = $-[1];
3249 my $end_pos = $+[2];
Joe Perches310cd062020-10-15 20:11:52 -07003250 if ($first =~ /(?:struct|union|enum)/) {
3251 pos($rawline) += length($first) + length($second) + 1;
3252 next;
3253 }
3254
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003255 next if (lc($first) ne lc($second));
Joe Perches310cd062020-10-15 20:11:52 -07003256 next if ($first eq 'long');
3257
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003258 # check for character before and after the word matches
3259 my $start_char = '';
3260 my $end_char = '';
3261 $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
3262 $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3263
3264 next if ($start_char =~ /^\S$/);
3265 next if (index(" \t.,;?!", $end_char) == -1);
3266
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08003267 # avoid repeating hex occurrences like 'ff ff fe 09 ...'
3268 if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3269 next if (!exists($allow_repeated_words{lc($first)}));
3270 }
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003271
Joe Perches310cd062020-10-15 20:11:52 -07003272 if (WARN("REPEATED_WORD",
3273 "Possible repeated word: '$first'\n" . $herecurr) &&
3274 $fix) {
3275 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3276 }
3277 }
3278
3279 # if it's a repeated word on consecutive lines in a comment block
3280 if ($prevline =~ /$;+\s*$/ &&
3281 $prevrawline =~ /($word_pattern)\s*$/) {
3282 my $last_word = $1;
3283 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3284 if (WARN("REPEATED_WORD",
3285 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3286 $fix) {
3287 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3288 }
3289 }
3290 }
3291 }
3292
Andy Whitcroft306708542008-10-15 22:02:28 -07003293# ignore non-hunk lines and lines being removed
3294 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003295
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003296#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003297 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003298 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07003299 if (ERROR("DOS_LINE_ENDINGS",
3300 "DOS line endings\n" . $herevet) &&
3301 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003302 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003303 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003304 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3305 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003306 if (ERROR("TRAILING_WHITESPACE",
3307 "trailing whitespace\n" . $herevet) &&
3308 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003309 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003310 }
3311
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003312 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003313 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07003314
Josh Triplett4783f892013-11-12 15:10:12 -08003315# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003316 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003317 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003318 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3319 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003320 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003321 my $msg_level = \&ERROR;
3322 $msg_level = \&CHK if ($file);
3323 &{$msg_level}("FSF_MAILING_ADDRESS",
3324 "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 -08003325 }
3326
Andi Kleen33549572010-05-24 14:33:29 -07003327# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003328# Only applies when adding the entry originally, after that we do not have
3329# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003330 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003331 # 'choice' is usually the last thing on the line (though
3332 # Kconfig supports named choices), so use a word boundary
3333 # (\b) rather than a whitespace character (\s)
3334 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003335 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003336 my $cnt = $realcnt;
3337 my $ln = $linenr + 1;
3338 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003339 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003340 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003341 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003342 $f = $lines[$ln - 1];
3343 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3344 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003345
3346 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003347 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003348
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003349 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003350 $is_start = 1;
Masahiro Yamada22a4ac02020-06-17 12:02:20 +09003351 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003352 $length = -1;
3353 }
3354
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003355 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003356 $f =~ s/#.*//;
3357 $f =~ s/^\s+//;
3358 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003359
3360 # This only checks context lines in the patch
3361 # and so hopefully shouldn't trigger false
3362 # positives, even though some of these are
3363 # common words in help texts
3364 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3365 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003366 $is_end = 1;
3367 last;
3368 }
Andi Kleen33549572010-05-24 14:33:29 -07003369 $length++;
3370 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003371 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3372 WARN("CONFIG_DESCRIPTION",
3373 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3374 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003375 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003376 }
3377
Joe Perches7ccf41a2020-06-04 16:50:33 -07003378# check MAINTAINERS entries
3379 if ($realfile =~ /^MAINTAINERS$/) {
3380# check MAINTAINERS entries for the right form
3381 if ($rawline =~ /^\+[A-Z]:/ &&
3382 $rawline !~ /^\+[A-Z]:\t\S/) {
3383 if (WARN("MAINTAINERS_STYLE",
3384 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3385 $fix) {
3386 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3387 }
3388 }
3389# check MAINTAINERS entries for the right ordering too
3390 my $preferred_order = 'MRLSWQBCPTFXNK';
3391 if ($rawline =~ /^\+[A-Z]:/ &&
3392 $prevrawline =~ /^[\+ ][A-Z]:/) {
3393 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3394 my $cur = $1;
3395 my $curval = $2;
3396 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3397 my $prev = $1;
3398 my $prevval = $2;
3399 my $curindex = index($preferred_order, $cur);
3400 my $previndex = index($preferred_order, $prev);
3401 if ($curindex < 0) {
3402 WARN("MAINTAINERS_STYLE",
3403 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3404 } else {
3405 if ($previndex >= 0 && $curindex < $previndex) {
3406 WARN("MAINTAINERS_STYLE",
3407 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3408 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3409 ($prev eq 'X' && $cur eq 'X')) &&
3410 ($prevval cmp $curval) > 0) {
3411 WARN("MAINTAINERS_STYLE",
3412 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3413 }
3414 }
Joe Perches628f91a2017-07-10 15:52:07 -07003415 }
3416 }
3417
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003418 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3419 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3420 my $flag = $1;
3421 my $replacement = {
3422 'EXTRA_AFLAGS' => 'asflags-y',
3423 'EXTRA_CFLAGS' => 'ccflags-y',
3424 'EXTRA_CPPFLAGS' => 'cppflags-y',
3425 'EXTRA_LDFLAGS' => 'ldflags-y',
3426 };
3427
3428 WARN("DEPRECATED_VARIABLE",
3429 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3430 }
3431
Rob Herringbff5da42014-01-23 15:54:51 -08003432# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003433 if (defined $root &&
3434 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3435 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3436
Rob Herringbff5da42014-01-23 15:54:51 -08003437 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3438
Florian Vaussardcc933192014-04-03 14:49:27 -07003439 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003440 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003441
Rob Herringbff5da42014-01-23 15:54:51 -08003442 foreach my $compat (@compats) {
3443 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003444 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3445 my $compat3 = $compat;
3446 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3447 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003448 if ( $? >> 8 ) {
3449 WARN("UNDOCUMENTED_DT_STRING",
3450 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3451 }
3452
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003453 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3454 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003455 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003456 if ( $? >> 8 ) {
3457 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003458 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003459 }
3460 }
3461 }
3462
Rob Herring9f3a8992018-04-10 16:33:13 -07003463# check for using SPDX license tag at beginning of files
3464 if ($realline == $checklicenseline) {
3465 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3466 $checklicenseline = 2;
3467 } elsif ($rawline =~ /^\+/) {
3468 my $comment = "";
3469 if ($realfile =~ /\.(h|s|S)$/) {
3470 $comment = '/*';
3471 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3472 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003473 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003474 $comment = '#';
3475 } elsif ($realfile =~ /\.rst$/) {
3476 $comment = '..';
3477 }
3478
Joe Perchesfdf13692019-03-07 16:28:32 -08003479# check SPDX comment style for .[chsS] files
3480 if ($realfile =~ /\.[chsS]$/ &&
3481 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003482 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003483 WARN("SPDX_LICENSE_TAG",
3484 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3485 }
3486
Rob Herring9f3a8992018-04-10 16:33:13 -07003487 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003488 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3489 WARN("SPDX_LICENSE_TAG",
3490 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003491 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003492 my $spdx_license = $1;
3493 if (!is_SPDX_License_valid($spdx_license)) {
3494 WARN("SPDX_LICENSE_TAG",
3495 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3496 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003497 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3498 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3499 my $msg_level = \&WARN;
3500 $msg_level = \&CHK if ($file);
3501 if (&{$msg_level}("SPDX_LICENSE_TAG",
3502
3503 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3504 $fix) {
3505 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3506 }
3507 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003508 }
3509 }
3510 }
3511
Joe Perchesa0154cd2020-10-15 20:12:19 -07003512# check for embedded filenames
3513 if ($rawline =~ /^\+.*\Q$realfile\E/) {
3514 WARN("EMBEDDED_FILENAME",
3515 "It's generally not useful to have the filename in the file\n" . $herecurr);
3516 }
3517
Andy Whitcroft5368df202008-10-15 22:02:27 -07003518# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003519 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003520
Joe Perchesa8da38a2019-03-07 16:28:42 -08003521# check for using SPDX-License-Identifier on the wrong line number
3522 if ($realline != $checklicenseline &&
3523 $rawline =~ /\bSPDX-License-Identifier:/ &&
3524 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3525 WARN("SPDX_LICENSE_TAG",
3526 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3527 }
3528
Joe Perches47e0c882015-06-25 15:02:57 -07003529# line length limit (with some exclusions)
3530#
3531# There are a few types of lines that may extend beyond $max_line_length:
3532# logging functions like pr_info that end in a string
3533# lines with a single string
3534# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003535# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003536#
3537# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003538# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003539# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3540# LONG_LINE all other lines longer than $max_line_length
3541#
3542# if LONG_LINE is ignored, the other 2 types are also ignored
3543#
3544
Joe Perchesb4749e92015-07-17 16:24:01 -07003545 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003546 my $msg_type = "LONG_LINE";
3547
3548 # Check the allowed long line types first
3549
3550 # logging functions that end in a string that starts
3551 # before $max_line_length
3552 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3553 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3554 $msg_type = "";
3555
3556 # lines with only strings (w/ possible termination)
3557 # #defines with only strings
3558 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3559 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3560 $msg_type = "";
3561
Joe Perchescc147502017-11-17 15:28:44 -08003562 # More special cases
3563 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3564 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003565 $msg_type = "";
3566
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003567 # URL ($rawline is used in case the URL is in a comment)
3568 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3569 $msg_type = "";
3570
Joe Perches47e0c882015-06-25 15:02:57 -07003571 # Otherwise set the alternate message types
3572
3573 # a comment starts before $max_line_length
3574 } elsif ($line =~ /($;[\s$;]*)$/ &&
3575 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3576 $msg_type = "LONG_LINE_COMMENT"
3577
3578 # a quoted string starts before $max_line_length
3579 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3580 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3581 $msg_type = "LONG_LINE_STRING"
3582 }
3583
3584 if ($msg_type ne "" &&
3585 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003586 my $msg_level = \&WARN;
3587 $msg_level = \&CHK if ($file);
3588 &{$msg_level}($msg_type,
3589 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003590 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003591 }
3592
Andy Whitcroft8905a672007-11-28 16:21:06 -08003593# check for adding lines without a newline.
3594 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Tom Rix47ca69b2020-12-15 20:44:40 -08003595 if (WARN("MISSING_EOF_NEWLINE",
3596 "adding a line without newline at end of file\n" . $herecurr) &&
3597 $fix) {
3598 fix_delete_line($fixlinenr+1, "No newline at end of file");
3599 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003600 }
3601
Aditya Srivastavade932452021-02-25 17:21:57 -08003602# check for .L prefix local symbols in .S files
3603 if ($realfile =~ /\.S$/ &&
3604 $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) {
3605 WARN("AVOID_L_PREFIX",
3606 "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);
3607 }
3608
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003609# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003610 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003611
3612# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003613# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003614 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3615 $rawline =~ /^\+\s* \s*/) {
3616 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003617 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003618 if (ERROR("CODE_INDENT",
3619 "code indent should use tabs where possible\n" . $herevet) &&
3620 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003621 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003622 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003623 }
3624
Alberto Panizzo08e44362010-03-05 13:43:54 -08003625# check for space before tabs.
3626 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3627 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003628 if (WARN("SPACE_BEFORE_TAB",
3629 "please, no space before tabs\n" . $herevet) &&
3630 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003631 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003632 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003633 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003634 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003635 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003636 }
3637
Joe Perches6a487212018-04-10 16:34:04 -07003638# check for assignments on the start of a line
3639 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
Aditya Srivastavada7355a2020-12-15 20:45:06 -08003640 my $operator = $1;
3641 if (CHK("ASSIGNMENT_CONTINUATIONS",
3642 "Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3643 $fix && $prevrawline =~ /^\+/) {
3644 # add assignment operator to the previous line, remove from current line
3645 $fixed[$fixlinenr - 1] .= " $operator";
3646 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3647 }
Joe Perches6a487212018-04-10 16:34:04 -07003648 }
3649
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003650# check for && or || at the start of a line
3651 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
Aditya Srivastava8e08f072020-12-15 20:45:09 -08003652 my $operator = $1;
3653 if (CHK("LOGICAL_CONTINUATIONS",
3654 "Logical continuations should be on the previous line\n" . $hereprev) &&
3655 $fix && $prevrawline =~ /^\+/) {
3656 # insert logical operator at last non-comment, non-whitepsace char on previous line
3657 $prevline =~ /[\s$;]*$/;
3658 my $line_end = substr($prevrawline, $-[0]);
3659 $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
3660 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3661 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003662 }
3663
Joe Perchesa91e8992016-05-20 17:04:05 -07003664# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003665 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003666 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003667 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003668 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003669 if (WARN("TABSTOP",
3670 "Statements should start on a tabstop\n" . $herecurr) &&
3671 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003672 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003673 }
3674 }
3675 }
3676
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003677# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003678 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003679 $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 -07003680 $prevline =~ /^\+(\t*)(.*)$/;
3681 my $oldindent = $1;
3682 my $rest = $2;
3683
3684 my $pos = pos_last_openparen($rest);
3685 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003686 $line =~ /^(\+| )([ \t]*)/;
3687 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003688
3689 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003690 "\t" x ($pos / $tabsize) .
3691 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003692 my $goodspaceindent = $oldindent . " " x $pos;
3693
3694 if ($newindent ne $goodtabindent &&
3695 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003696
3697 if (CHK("PARENTHESIS_ALIGNMENT",
3698 "Alignment should match open parenthesis\n" . $hereprev) &&
3699 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003700 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003701 s/^\+[ \t]*/\+$goodtabindent/;
3702 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003703 }
3704 }
3705 }
3706
Joe Perches6ab3a972015-04-16 12:44:05 -07003707# check for space after cast like "(int) foo" or "(struct foo) bar"
3708# avoid checking a few false positives:
3709# "sizeof(<type>)" or "__alignof__(<type>)"
3710# function pointer declarations like "(*foo)(int) = bar;"
3711# structure definitions like "(struct foo) { 0 };"
3712# multiline macros that define functions
3713# known attributes or the __attribute__ keyword
3714 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3715 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003716 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003717 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003718 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003719 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003720 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003721 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003722 }
3723
Joe Perches86406b12015-09-09 15:37:41 -07003724# Block comment styles
3725# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003726 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003727 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003728 $rawline =~ /^\+[ \t]*\*/ &&
Łukasz Stelmachc70735c2020-10-15 20:12:25 -07003729 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
Joe Perches05880602012-10-04 17:13:35 -07003730 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3731 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3732 }
3733
Joe Perches86406b12015-09-09 15:37:41 -07003734# Block comments use * on subsequent lines
3735 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3736 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003737 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003738 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003739 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003740 WARN("BLOCK_COMMENT_STYLE",
3741 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003742 }
3743
Joe Perches86406b12015-09-09 15:37:41 -07003744# Block comments use */ on trailing lines
3745 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003746 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3747 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3748 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003749 WARN("BLOCK_COMMENT_STYLE",
3750 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003751 }
3752
Joe Perches08eb9b82016-10-11 13:51:50 -07003753# Block comment * alignment
3754 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003755 $line =~ /^\+[ \t]*$;/ && #leading comment
3756 $rawline =~ /^\+[ \t]*\*/ && #leading *
3757 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003758 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003759 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3760 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003761 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003762 if (defined($1)) {
3763 $oldindent = expand_tabs($1);
3764 } else {
3765 $prevrawline =~ m@^\+(.*/?)\*@;
3766 $oldindent = expand_tabs($1);
3767 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003768 $rawline =~ m@^\+([ \t]*)\*@;
3769 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003770 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003771 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003772 WARN("BLOCK_COMMENT_STYLE",
3773 "Block comments should align the * on each line\n" . $hereprev);
3774 }
3775 }
3776
Joe Perches7f619192014-08-06 16:10:39 -07003777# check for missing blank lines after struct/union declarations
3778# with exceptions for various attributes and macros
3779 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3780 $line =~ /^\+/ &&
3781 !($line =~ /^\+\s*$/ ||
3782 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3783 $line =~ /^\+\s*MODULE_/i ||
3784 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3785 $line =~ /^\+[a-z_]*init/ ||
3786 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3787 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003788 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003789 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003790 if (CHK("LINE_SPACING",
3791 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3792 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003793 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003794 }
Joe Perches7f619192014-08-06 16:10:39 -07003795 }
3796
Joe Perches365dd4e2014-08-06 16:10:42 -07003797# check for multiple consecutive blank lines
3798 if ($prevline =~ /^[\+ ]\s*$/ &&
3799 $line =~ /^\+\s*$/ &&
3800 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003801 if (CHK("LINE_SPACING",
3802 "Please don't use multiple blank lines\n" . $hereprev) &&
3803 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003804 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003805 }
3806
Joe Perches365dd4e2014-08-06 16:10:42 -07003807 $last_blank_line = $linenr;
3808 }
3809
Joe Perches3b617e32014-04-03 14:49:28 -07003810# check for missing blank lines after declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08003811# (declarations must have the same indentation and not be at the start of line)
3812 if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
3813 # use temporaries
3814 my $sl = $sline;
3815 my $pl = $prevline;
3816 # remove $Attribute/$Sparse uses to simplify comparisons
3817 $sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3818 $pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3819 if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003820 # function pointer declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08003821 $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003822 # foo bar; where foo is some local typedef or #define
Joe Perchesb5e87362021-02-25 17:21:40 -08003823 $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003824 # known declaration macros
Joe Perchesb5e87362021-02-25 17:21:40 -08003825 $pl =~ /^\+\s+$declaration_macros/) &&
Joe Perches3f7bac02014-06-04 16:12:04 -07003826 # for "else if" which can look like "$Ident $Ident"
Joe Perchesb5e87362021-02-25 17:21:40 -08003827 !($pl =~ /^\+\s+$c90_Keywords\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003828 # other possible extensions of declaration lines
Joe Perchesb5e87362021-02-25 17:21:40 -08003829 $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003830 # not starting a section or a macro "\" extended line
Joe Perchesb5e87362021-02-25 17:21:40 -08003831 $pl =~ /(?:\{\s*|\\)$/) &&
Joe Perches3f7bac02014-06-04 16:12:04 -07003832 # looks like a declaration
Joe Perchesb5e87362021-02-25 17:21:40 -08003833 !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003834 # function pointer declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08003835 $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003836 # foo bar; where foo is some local typedef or #define
Joe Perchesb5e87362021-02-25 17:21:40 -08003837 $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003838 # known declaration macros
Joe Perchesb5e87362021-02-25 17:21:40 -08003839 $sl =~ /^\+\s+$declaration_macros/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003840 # start of struct or union or enum
Joe Perchesb5e87362021-02-25 17:21:40 -08003841 $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003842 # start or end of block or continuation of declaration
Joe Perchesb5e87362021-02-25 17:21:40 -08003843 $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003844 # bitfield continuation
Joe Perchesb5e87362021-02-25 17:21:40 -08003845 $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003846 # other possible extensions of declaration lines
Joe Perchesb5e87362021-02-25 17:21:40 -08003847 $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
3848 if (WARN("LINE_SPACING",
3849 "Missing a blank line after declarations\n" . $hereprev) &&
3850 $fix) {
3851 fix_insert_line($fixlinenr, "\+");
3852 }
Joe Perchesd752fcc2014-08-06 16:11:05 -07003853 }
Joe Perches3b617e32014-04-03 14:49:28 -07003854 }
3855
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003856# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003857# Exceptions:
3858# 1) within comments
3859# 2) indented preprocessor commands
3860# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003861 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003862 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003863 if (WARN("LEADING_SPACE",
3864 "please, no spaces at the start of a line\n" . $herevet) &&
3865 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003866 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003867 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003868 }
3869
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003870# check we are in a valid C source file if not then ignore this hunk
3871 next if ($realfile !~ /\.(h|c)$/);
3872
Joe Perches5751a242017-11-17 15:28:52 -08003873# check for unusual line ending [ or (
3874 if ($line =~ /^\+.*([\[\(])\s*$/) {
3875 CHK("OPEN_ENDED_LINE",
3876 "Lines should not end with a '$1'\n" . $herecurr);
3877 }
3878
Joe Perches4dbed762017-05-08 15:55:39 -07003879# check if this appears to be the start function declaration, save the name
3880 if ($sline =~ /^\+\{\s*$/ &&
3881 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3882 $context_function = $1;
3883 }
3884
3885# check if this appears to be the end of function declaration
3886 if ($sline =~ /^\+\}\s*$/) {
3887 undef $context_function;
3888 }
3889
Joe Perches032a4c02014-08-06 16:10:29 -07003890# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003891# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003892# if the previous line is a break or return and is indented 1 tab more...
3893 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3894 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003895 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3896 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3897 defined $lines[$linenr] &&
3898 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003899 WARN("UNNECESSARY_ELSE",
3900 "else is not generally useful after a break or return\n" . $hereprev);
3901 }
3902 }
3903
Joe Perchesc00df192014-08-06 16:11:01 -07003904# check indentation of a line with a break;
Joe Perchesdc58bc52020-12-15 20:44:33 -08003905# if the previous line is a goto, return or break
3906# and is indented the same # of tabs
Joe Perchesc00df192014-08-06 16:11:01 -07003907 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3908 my $tabs = $1;
Joe Perchesdc58bc52020-12-15 20:44:33 -08003909 if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
3910 if (WARN("UNNECESSARY_BREAK",
3911 "break is not useful after a $1\n" . $hereprev) &&
3912 $fix) {
3913 fix_delete_line($fixlinenr, $rawline);
3914 }
Joe Perchesc00df192014-08-06 16:11:01 -07003915 }
3916 }
3917
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003918# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003919 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003920 WARN("CVS_KEYWORD",
3921 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003922 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003923
Joe Perches56e77d72013-02-21 16:44:14 -08003924# check for old HOTPLUG __dev<foo> section markings
3925 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3926 WARN("HOTPLUG_SECTION",
3927 "Using $1 is unnecessary\n" . $herecurr);
3928 }
3929
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003930# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003931 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3932 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003933#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003934 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003935 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003936 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003937 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003938 $stat =~ s/\n./\n /g;
3939 $cond =~ s/\n./\n /g;
3940
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003941#print "linenr<$linenr> <$stat>\n";
3942 # If this statement has no statement boundaries within
3943 # it there is no point in retrying a statement scan
3944 # until we hit end of it.
3945 my $frag = $stat; $frag =~ s/;+\s*$//;
3946 if ($frag !~ /(?:{|;)/) {
3947#print "skip<$line_nr_next>\n";
3948 $suppress_statement = $line_nr_next;
3949 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003950
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003951 # Find the real next line.
3952 $realline_next = $line_nr_next;
3953 if (defined $realline_next &&
3954 (!defined $lines[$realline_next - 1] ||
3955 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3956 $realline_next++;
3957 }
3958
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003959 my $s = $stat;
3960 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003961
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003962 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003963 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003964
3965 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003966 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003967
Andy Whitcroft463f2862009-09-21 17:04:34 -07003968 } elsif ($s =~ /^.\s*else\b/s) {
3969
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003970 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003971 } 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 -07003972 my $type = $1;
3973 $type =~ s/\s+/ /g;
3974 possible($type, "A:" . $s);
3975
Andy Whitcroft8905a672007-11-28 16:21:06 -08003976 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003977 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003978 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003979 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003980
3981 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003982 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003983 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003984 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003985
3986 # Check for any sort of function declaration.
3987 # int foo(something bar, other baz);
3988 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003989 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 -08003990 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003991
Andy Whitcroftcf655042008-03-04 14:28:20 -08003992 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003993 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003994 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003995
Andy Whitcroft8905a672007-11-28 16:21:06 -08003996 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003997 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003998
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003999 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004000 }
4001 }
4002 }
4003
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004004 }
4005
Andy Whitcroft653d4872007-06-23 17:16:34 -07004006#
4007# Checks which may be anchored in the context.
4008#
4009
4010# Check for switch () and associated case and default
4011# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07004012 if ($line=~/\bswitch\s*\(.*\)/) {
4013 my $err = '';
4014 my $sep = '';
4015 my @ctx = ctx_block_outer($linenr, $realcnt);
4016 shift(@ctx);
4017 for my $ctx (@ctx) {
4018 my ($clen, $cindent) = line_stats($ctx);
4019 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
4020 $indent != $cindent) {
4021 $err .= "$sep$ctx\n";
4022 $sep = '';
4023 } else {
4024 $sep = "[...]\n";
4025 }
4026 }
4027 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004028 ERROR("SWITCH_CASE_INDENT_LEVEL",
4029 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004030 }
4031 }
4032
4033# if/while/etc brace do not go on next line, unless defining a do while loop,
4034# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07004035 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 -07004036 my $pre_ctx = "$1$2";
4037
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004038 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08004039
4040 if ($line =~ /^\+\t{6,}/) {
4041 WARN("DEEP_INDENTATION",
4042 "Too many leading tabs - consider code refactoring\n" . $herecurr);
4043 }
4044
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004045 my $ctx_cnt = $realcnt - $#ctx - 1;
4046 my $ctx = join("\n", @ctx);
4047
Andy Whitcroft548596d2008-07-23 21:29:01 -07004048 my $ctx_ln = $linenr;
4049 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004050
Andy Whitcroft548596d2008-07-23 21:29:01 -07004051 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4052 defined $lines[$ctx_ln - 1] &&
4053 $lines[$ctx_ln - 1] =~ /^-/)) {
4054 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4055 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004056 $ctx_ln++;
4057 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07004058
Andy Whitcroft53210162008-07-23 21:29:03 -07004059 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
4060 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07004061
Joe Perchesd752fcc2014-08-06 16:11:05 -07004062 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004063 ERROR("OPEN_BRACE",
4064 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07004065 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07004066 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004067 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4068 $ctx =~ /\)\s*\;\s*$/ &&
4069 defined $lines[$ctx_ln - 1])
4070 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004071 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
4072 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004073 WARN("TRAILING_SEMICOLON",
4074 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07004075 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004076 }
4077 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004078 }
4079
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004080# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07004081 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 -08004082 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4083 ctx_statement_block($linenr, $realcnt, 0)
4084 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004085 my ($s, $c) = ($stat, $cond);
4086
4087 substr($s, 0, length($c), '');
4088
Joe Perches9f5af482015-09-09 15:37:30 -07004089 # remove inline comments
4090 $s =~ s/$;/ /g;
4091 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004092
4093 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07004094 my @newlines = ($c =~ /\n/gs);
4095 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004096
Joe Perches9f5af482015-09-09 15:37:30 -07004097 # Make sure we remove the line prefixes as we have
4098 # none on the first line, and are going to readd them
4099 # where necessary.
4100 $s =~ s/\n./\n/gs;
4101 while ($s =~ /\n\s+\\\n/) {
4102 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
4103 }
4104
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004105 # We want to check the first line inside the block
4106 # starting at the end of the conditional, so remove:
4107 # 1) any blank line termination
4108 # 2) any opening brace { on end of the line
4109 # 3) any do (...) {
4110 my $continuation = 0;
4111 my $check = 0;
4112 $s =~ s/^.*\bdo\b//;
4113 $s =~ s/^\s*{//;
4114 if ($s =~ s/^\s*\\//) {
4115 $continuation = 1;
4116 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004117 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004118 $check = 1;
4119 $cond_lines++;
4120 }
4121
4122 # Also ignore a loop construct at the end of a
4123 # preprocessor statement.
4124 if (($prevline =~ /^.\s*#\s*define\s/ ||
4125 $prevline =~ /\\\s*$/) && $continuation == 0) {
4126 $check = 0;
4127 }
4128
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004129 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07004130 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004131 while ($cond_ptr != $cond_lines) {
4132 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004133
Andy Whitcroftf16fa282008-10-15 22:02:32 -07004134 # If we see an #else/#elif then the code
4135 # is not linear.
4136 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4137 $check = 0;
4138 }
4139
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004140 # Ignore:
4141 # 1) blank lines, they should be at 0,
4142 # 2) preprocessor lines, and
4143 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07004144 if ($continuation ||
4145 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004146 $s =~ /^\s*#\s*?/ ||
4147 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07004148 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07004149 if ($s =~ s/^.*?\n//) {
4150 $cond_lines++;
4151 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004152 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004153 }
4154
4155 my (undef, $sindent) = line_stats("+" . $s);
4156 my $stat_real = raw_line($linenr, $cond_lines);
4157
4158 # Check if either of these lines are modified, else
4159 # this is not this patch's fault.
4160 if (!defined($stat_real) ||
4161 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
4162 $check = 0;
4163 }
4164 if (defined($stat_real) && $cond_lines > 1) {
4165 $stat_real = "[...]\n$stat_real";
4166 }
4167
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004168 #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 -07004169
Joe Perches9f5af482015-09-09 15:37:30 -07004170 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07004171 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07004172 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07004173 ($sindent == $indent &&
4174 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07004175 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004176 WARN("SUSPECT_CODE_INDENT",
4177 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004178 }
4179 }
4180
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004181 # Track the 'values' across context and added lines.
4182 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004183 my ($curr_values, $curr_vars) =
4184 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004185 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004186 if ($dbg_values) {
4187 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004188 print "$linenr > .$outline\n";
4189 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004190 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004191 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004192 $prev_values = substr($curr_values, -1);
4193
Andy Whitcroft00df3442007-06-08 13:47:06 -07004194#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07004195 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07004196
Joe Perches99ca38c2020-10-15 20:12:09 -07004197# check for self assignments used to avoid compiler warnings
4198# e.g.: int foo = foo, *bar = NULL;
4199# struct foo bar = *(&(bar));
4200 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
4201 my $var = $1;
4202 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
4203 WARN("SELF_ASSIGNMENT",
4204 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
4205 }
4206 }
4207
Joe Perches11ca40a2016-12-12 16:46:31 -08004208# check for dereferences that span multiple lines
4209 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
4210 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
4211 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
4212 my $ref = $1;
4213 $line =~ /^.\s*($Lval)/;
4214 $ref .= $1;
4215 $ref =~ s/\s//g;
4216 WARN("MULTILINE_DEREFERENCE",
4217 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
4218 }
4219
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004220# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07004221 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 -07004222 my $type = $1;
4223 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07004224 $var = "" if (!defined $var);
4225 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004226 my $sign = $1;
4227 my $pointer = $2;
4228
4229 $pointer = "" if (!defined $pointer);
4230
4231 if (WARN("UNSPECIFIED_INT",
4232 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4233 $fix) {
4234 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07004235 my $comp_pointer = $pointer;
4236 $comp_pointer =~ s/\s//g;
4237 $decl .= $comp_pointer;
4238 $decl = rtrim($decl) if ($var eq "");
4239 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004240 }
4241 }
4242 }
4243
Andy Whitcroft653d4872007-06-23 17:16:34 -07004244# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07004245 if ($dbg_type) {
4246 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004247 ERROR("TEST_TYPE",
4248 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004249 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004250 ERROR("TEST_NOT_TYPE",
4251 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004252 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004253 next;
4254 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004255# TEST: allow direct testing of the attribute matcher.
4256 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004257 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004258 ERROR("TEST_ATTR",
4259 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004260 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004261 ERROR("TEST_NOT_ATTR",
4262 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004263 }
4264 next;
4265 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004266
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004267# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07004268 if ($line =~ /^.\s*{/ &&
4269 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07004270 if (ERROR("OPEN_BRACE",
4271 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004272 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4273 fix_delete_line($fixlinenr - 1, $prevrawline);
4274 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004275 my $fixedline = $prevrawline;
4276 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004277 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004278 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004279 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004280 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004281 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004282 }
4283
Andy Whitcroft653d4872007-06-23 17:16:34 -07004284#
4285# Checks which are anchored on the added line.
4286#
4287
4288# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004289 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07004290 my $path = $1;
4291 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004292 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08004293 "malformed #include filename\n" . $herecurr);
4294 }
4295 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4296 ERROR("UAPI_INCLUDE",
4297 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004298 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004299 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004300
4301# no C99 // comments
4302 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07004303 if (ERROR("C99_COMMENTS",
4304 "do not use C99 // comments\n" . $herecurr) &&
4305 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004306 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07004307 if ($line =~ /\/\/(.*)$/) {
4308 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07004309 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07004310 }
4311 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004312 }
4313 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004314 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004315 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004316
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004317# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4318# the whole statement.
4319#print "APW <$lines[$realline_next - 1]>\n";
4320 if (defined $realline_next &&
4321 exists $lines[$realline_next - 1] &&
4322 !defined $suppress_export{$realline_next} &&
Christoph Hellwig36794822021-02-02 13:13:34 +01004323 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004324 # Handle definitions which produce identifiers with
4325 # a prefix:
4326 # XXX(foo);
4327 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004328 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08004329 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004330 $name =~ /^${Ident}_$2/) {
4331#print "FOO C name<$name>\n";
4332 $suppress_export{$realline_next} = 1;
4333
4334 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004335 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07004336 ^.DEFINE_$Ident\(\Q$name\E\)|
4337 ^.DECLARE_$Ident\(\Q$name\E\)|
4338 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004339 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4340 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07004341 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004342#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4343 $suppress_export{$realline_next} = 2;
4344 } else {
4345 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004346 }
4347 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004348 if (!defined $suppress_export{$linenr} &&
4349 $prevline =~ /^.\s*$/ &&
Christoph Hellwig36794822021-02-02 13:13:34 +01004350 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004351#print "FOO B <$lines[$linenr - 1]>\n";
4352 $suppress_export{$linenr} = 2;
4353 }
4354 if (defined $suppress_export{$linenr} &&
4355 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004356 WARN("EXPORT_SYMBOL",
4357 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004358 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004359
Joe Eloff5150bda2010-08-09 17:21:00 -07004360# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004361 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004362 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004363 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004364 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004365 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004366 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004367 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004368# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004369 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004370 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004371 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004372 $herecurr) &&
4373 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004374 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004375 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004376 }
4377
Joe Perches18130872014-08-06 16:11:22 -07004378# check for misordered declarations of char/short/int/long with signed/unsigned
4379 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4380 my $tmp = trim($1);
4381 WARN("MISORDERED_TYPE",
4382 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4383 }
4384
Joe Perches809e0822018-08-21 21:58:12 -07004385# check for unnecessary <signed> int declarations of short/long/long long
4386 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4387 my $type = trim($1);
4388 next if ($type !~ /\bint\b/);
4389 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4390 my $new_type = $type;
4391 $new_type =~ s/\b\s*int\s*\b/ /;
4392 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4393 $new_type =~ s/^const\s+//;
4394 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4395 $new_type = "const $new_type" if ($type =~ /^const\b/);
4396 $new_type =~ s/\s+/ /g;
4397 $new_type = trim($new_type);
4398 if (WARN("UNNECESSARY_INT",
4399 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4400 $fix) {
4401 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4402 }
4403 }
4404
Joe Perchescb710ec2010-10-26 14:23:20 -07004405# check for static const char * arrays.
4406 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004407 WARN("STATIC_CONST_CHAR_ARRAY",
4408 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004409 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004410 }
4411
4412# check for initialized const char arrays that should be static const
4413 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4414 if (WARN("STATIC_CONST_CHAR_ARRAY",
4415 "const array should probably be static const\n" . $herecurr) &&
4416 $fix) {
4417 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4418 }
4419 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004420
4421# check for static char foo[] = "bar" declarations.
4422 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004423 WARN("STATIC_CONST_CHAR_ARRAY",
4424 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004425 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004426 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004427
Joe Perchesab7e23f2015-04-16 12:44:22 -07004428# check for const <foo> const where <foo> is not a pointer or array type
4429 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4430 my $found = $1;
4431 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4432 WARN("CONST_CONST",
4433 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4434 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4435 WARN("CONST_CONST",
4436 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4437 }
4438 }
4439
Joe Perches73169762020-12-15 20:44:30 -08004440# check for const static or static <non ptr type> const declarations
4441# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
4442 if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
4443 $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
4444 if (WARN("STATIC_CONST",
4445 "Move const after static - use 'static const $1'\n" . $herecurr) &&
4446 $fix) {
4447 $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
4448 $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
4449 }
4450 }
4451
Joe Perches9b0fa602014-04-03 14:49:18 -07004452# check for non-global char *foo[] = {"bar", ...} declarations.
4453 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4454 WARN("STATIC_CONST_CHAR_ARRAY",
4455 "char * array declaration might be better as static const\n" .
4456 $herecurr);
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08004457 }
Joe Perches9b0fa602014-04-03 14:49:18 -07004458
Joe Perchesb598b672015-04-16 12:44:36 -07004459# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4460 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4461 my $array = $1;
4462 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4463 my $array_div = $1;
4464 if (WARN("ARRAY_SIZE",
4465 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4466 $fix) {
4467 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4468 }
4469 }
4470 }
4471
Joe Perchesb36190c2014-01-27 17:07:18 -08004472# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004473 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004474 if (ERROR("FUNCTION_WITHOUT_ARGS",
4475 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4476 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004477 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004478 }
4479 }
4480
Andy Whitcroft653d4872007-06-23 17:16:34 -07004481# check for new typedefs, only function parameters and sparse annotations
4482# make sense.
4483 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004484 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004485 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004486 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004487 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004488 WARN("NEW_TYPEDEFS",
4489 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004490 }
4491
4492# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004493 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004494 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4495 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004496 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004497
Andy Whitcroft65863862009-01-06 14:41:21 -08004498 # Should start with a space.
4499 $to =~ s/^(\S)/ $1/;
4500 # Should not end with a space.
4501 $to =~ s/\s+$//;
4502 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004503 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004504 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004505
Joe Perches3705ce52013-07-03 15:05:31 -07004506## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004507 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004508 if (ERROR("POINTER_LOCATION",
4509 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4510 $fix) {
4511 my $sub_from = $ident;
4512 my $sub_to = $ident;
4513 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004514 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004515 s@\Q$sub_from\E@$sub_to@;
4516 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004517 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004518 }
4519 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4520 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004521 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004522
Andy Whitcroft65863862009-01-06 14:41:21 -08004523 # Should start with a space.
4524 $to =~ s/^(\S)/ $1/;
4525 # Should not end with a space.
4526 $to =~ s/\s+$//;
4527 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004528 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004529 }
4530 # Modifiers should have spaces.
4531 $to =~ s/(\b$Modifier$)/$1 /;
4532
Joe Perches3705ce52013-07-03 15:05:31 -07004533## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004534 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004535 if (ERROR("POINTER_LOCATION",
4536 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4537 $fix) {
4538
4539 my $sub_from = $match;
4540 my $sub_to = $match;
4541 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004542 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004543 s@\Q$sub_from\E@$sub_to@;
4544 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004545 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004546 }
4547
Joe Perches9d3e3c72015-09-09 15:37:27 -07004548# avoid BUG() or BUG_ON()
4549 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004550 my $msg_level = \&WARN;
4551 $msg_level = \&CHK if ($file);
4552 &{$msg_level}("AVOID_BUG",
4553 "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 -07004554 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004555
Joe Perches9d3e3c72015-09-09 15:37:27 -07004556# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004557 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004558 WARN("LINUX_VERSION_CODE",
4559 "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 -08004560 }
4561
Joe Perches17441222011-06-15 15:08:17 -07004562# check for uses of printk_ratelimit
4563 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004564 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004565 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004566 }
4567
Joe Percheseeef5732017-11-17 15:28:41 -08004568# printk should use KERN_* levels
4569 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4570 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4571 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004572 }
4573
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004574# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4575 if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4576 my $printk = $1;
4577 my $modifier = $2;
4578 my $orig = $3;
4579 $modifier = "" if (!defined($modifier));
Joe Perches243f3802012-05-31 16:26:09 -07004580 my $level = lc($orig);
4581 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004582 my $level2 = $level;
4583 $level2 = "dbg" if ($level eq "debug");
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004584 $level .= $modifier;
4585 $level2 .= $modifier;
Joe Perches243f3802012-05-31 16:26:09 -07004586 WARN("PREFER_PR_LEVEL",
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004587 "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 -07004588 }
4589
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004590# prefer dev_<level> to dev_printk(KERN_<LEVEL>
Joe Perchesdc139312013-02-21 16:44:13 -08004591 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4592 my $orig = $1;
4593 my $level = lc($orig);
4594 $level = "warn" if ($level eq "warning");
4595 $level = "dbg" if ($level eq "debug");
4596 WARN("PREFER_DEV_LEVEL",
4597 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4598 }
4599
Nicolas Boichat8020b252020-10-15 20:12:02 -07004600# trace_printk should not be used in production code.
4601 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4602 WARN("TRACE_PRINTK",
4603 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4604 }
4605
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004606# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4607# number of false positives, but assembly files are not checked, so at
4608# least the arch entry code will not trigger this warning.
4609 if ($line =~ /\bENOSYS\b/) {
4610 WARN("ENOSYS",
4611 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4612 }
4613
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004614# ENOTSUPP is not a standard error code and should be avoided in new patches.
4615# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4616# Similarly to ENOSYS warning a small number of false positives is expected.
4617 if (!$file && $line =~ /\bENOTSUPP\b/) {
4618 if (WARN("ENOTSUPP",
4619 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4620 $fix) {
4621 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4622 }
4623 }
4624
Andy Whitcroft653d4872007-06-23 17:16:34 -07004625# function brace can't be on same line, except for #defines of do while,
4626# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004627 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004628 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4629 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4630 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004631 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004632 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004633 $fix) {
4634 fix_delete_line($fixlinenr, $rawline);
4635 my $fixed_line = $rawline;
Dwaipayan Ray03f49352020-12-15 20:45:02 -08004636 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
Joe Perches8d182472014-08-06 16:11:12 -07004637 my $line1 = $1;
4638 my $line2 = $2;
4639 fix_insert_line($fixlinenr, ltrim($line1));
4640 fix_insert_line($fixlinenr, "\+{");
4641 if ($line2 !~ /^\s*$/) {
4642 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4643 }
4644 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004645 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004646
Andy Whitcroft8905a672007-11-28 16:21:06 -08004647# open braces for enum, union and struct go on the same line.
4648 if ($line =~ /^.\s*{/ &&
4649 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004650 if (ERROR("OPEN_BRACE",
4651 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4652 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4653 fix_delete_line($fixlinenr - 1, $prevrawline);
4654 fix_delete_line($fixlinenr, $rawline);
4655 my $fixedline = rtrim($prevrawline) . " {";
4656 fix_insert_line($fixlinenr, $fixedline);
4657 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004658 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004659 if ($fixedline !~ /^\+\s*$/) {
4660 fix_insert_line($fixlinenr, $fixedline);
4661 }
4662 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004663 }
4664
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004665# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004666 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4667 if (WARN("SPACING",
4668 "missing space after $1 definition\n" . $herecurr) &&
4669 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004670 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004671 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4672 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004673 }
4674
Joe Perches31070b52014-01-23 15:54:49 -08004675# Function pointer declarations
4676# check spacing between type, funcptr, and args
4677# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004678 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004679 my $declare = $1;
4680 my $pre_pointer_space = $2;
4681 my $post_pointer_space = $3;
4682 my $funcname = $4;
4683 my $post_funcname_space = $5;
4684 my $pre_args_space = $6;
4685
Joe Perches91f72e92014-04-03 14:49:12 -07004686# the $Declare variable will capture all spaces after the type
4687# so check it for a missing trailing missing space but pointer return types
4688# don't need a space so don't warn for those.
4689 my $post_declare_space = "";
4690 if ($declare =~ /(\s+)$/) {
4691 $post_declare_space = $1;
4692 $declare = rtrim($declare);
4693 }
4694 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004695 WARN("SPACING",
4696 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004697 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004698 }
4699
4700# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004701# This test is not currently implemented because these declarations are
4702# equivalent to
4703# int foo(int bar, ...)
4704# and this is form shouldn't/doesn't generate a checkpatch warning.
4705#
4706# elsif ($declare =~ /\s{2,}$/) {
4707# WARN("SPACING",
4708# "Multiple spaces after return type\n" . $herecurr);
4709# }
Joe Perches31070b52014-01-23 15:54:49 -08004710
4711# unnecessary space "type ( *funcptr)(args...)"
4712 if (defined $pre_pointer_space &&
4713 $pre_pointer_space =~ /^\s/) {
4714 WARN("SPACING",
4715 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4716 }
4717
4718# unnecessary space "type (* funcptr)(args...)"
4719 if (defined $post_pointer_space &&
4720 $post_pointer_space =~ /^\s/) {
4721 WARN("SPACING",
4722 "Unnecessary space before function pointer name\n" . $herecurr);
4723 }
4724
4725# unnecessary space "type (*funcptr )(args...)"
4726 if (defined $post_funcname_space &&
4727 $post_funcname_space =~ /^\s/) {
4728 WARN("SPACING",
4729 "Unnecessary space after function pointer name\n" . $herecurr);
4730 }
4731
4732# unnecessary space "type (*funcptr) (args...)"
4733 if (defined $pre_args_space &&
4734 $pre_args_space =~ /^\s/) {
4735 WARN("SPACING",
4736 "Unnecessary space before function pointer arguments\n" . $herecurr);
4737 }
4738
4739 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004740 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004741 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004742 }
4743 }
4744
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004745# check for spacing round square brackets; allowed:
4746# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004747# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4748# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004749 while ($line =~ /(.*?\s)\[/g) {
4750 my ($where, $prefix) = ($-[1], $1);
4751 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004752 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004753 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004754 if (ERROR("BRACKET_SPACE",
4755 "space prohibited before open square bracket '['\n" . $herecurr) &&
4756 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004757 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004758 s/^(\+.*?)\s+\[/$1\[/;
4759 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004760 }
4761 }
4762
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004763# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004764 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004765 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004766 my $ctx_before = substr($line, 0, $-[1]);
4767 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004768
4769 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004770 if ($name =~ /^(?:
4771 if|for|while|switch|return|case|
4772 volatile|__volatile__|
4773 __attribute__|format|__extension__|
4774 asm|__asm__)$/x)
4775 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004776 # cpp #define statements have non-optional spaces, ie
4777 # if there is a space between the name and the open
4778 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004779 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004780
4781 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004782 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004783
4784 # If this whole things ends with a type its most
4785 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004786 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004787
4788 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004789 if (WARN("SPACING",
4790 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4791 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004792 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004793 s/\b$name\s+\(/$name\(/;
4794 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004795 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004796 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004797
Andy Whitcroft653d4872007-06-23 17:16:34 -07004798# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004799 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004800 my $fixed_line = "";
4801 my $line_fixed = 0;
4802
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004803 my $ops = qr{
4804 <<=|>>=|<=|>=|==|!=|
4805 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4806 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004807 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004808 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004809 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004810 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004811
4812## print("element count: <" . $#elements . ">\n");
4813## foreach my $el (@elements) {
4814## print("el: <$el>\n");
4815## }
4816
4817 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004818 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004819
Joe Perches3705ce52013-07-03 15:05:31 -07004820 foreach my $el (@elements) {
4821 push(@fix_elements, substr($rawline, $off, length($el)));
4822 $off += length($el);
4823 }
4824
4825 $off = 0;
4826
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004827 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004828 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004829
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004830 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004831
4832 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4833
4834## print("n: <$n> good: <$good>\n");
4835
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004836 $off += length($elements[$n]);
4837
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004838 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004839 my $ca = substr($opline, 0, $off);
4840 my $cc = '';
4841 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4842 $cc = substr($opline, $off + length($elements[$n + 1]));
4843 }
4844 my $cb = "$ca$;$cc";
4845
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004846 my $a = '';
4847 $a = 'V' if ($elements[$n] ne '');
4848 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004849 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004850 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4851 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004852 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004853
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004854 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004855
4856 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004857 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004858 $c = 'V' if ($elements[$n + 2] ne '');
4859 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004860 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004861 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4862 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004863 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004864 } else {
4865 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004866 }
4867
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004868 my $ctx = "${a}x${c}";
4869
4870 my $at = "(ctx:$ctx)";
4871
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004872 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004873 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004874
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004875 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004876 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004877
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004878 # Get the full operator variant.
4879 my $opv = $op . substr($curr_vars, $off, 1);
4880
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004881 # Ignore operators passed as parameters.
4882 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004883 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004884
Andy Whitcroftcf655042008-03-04 14:28:20 -08004885# # Ignore comments
4886# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004887
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004888 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004889 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004890 if ($ctx !~ /.x[WEBC]/ &&
4891 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004892 if (ERROR("SPACING",
4893 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004894 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004895 $line_fixed = 1;
4896 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004897 }
4898
4899 # // is a comment
4900 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004901
Joe Perchesb00e4812014-04-03 14:49:33 -07004902 # : when part of a bitfield
4903 } elsif ($opv eq ':B') {
4904 # skip the bitfield test for now
4905
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004906 # No spaces for:
4907 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004908 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004909 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004910 if (ERROR("SPACING",
4911 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004912 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004913 if (defined $fix_elements[$n + 2]) {
4914 $fix_elements[$n + 2] =~ s/^\s+//;
4915 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004916 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004917 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004918 }
4919
Joe Perches23810972014-12-10 15:51:32 -08004920 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004921 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004922 my $rtrim_before = 0;
4923 my $space_after = 0;
4924 if ($ctx =~ /Wx./) {
4925 if (ERROR("SPACING",
4926 "space prohibited before that '$op' $at\n" . $hereptr)) {
4927 $line_fixed = 1;
4928 $rtrim_before = 1;
4929 }
4930 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004931 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004932 if (ERROR("SPACING",
4933 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004934 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004935 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004936 $space_after = 1;
4937 }
4938 }
4939 if ($rtrim_before || $space_after) {
4940 if ($rtrim_before) {
4941 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4942 } else {
4943 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4944 }
4945 if ($space_after) {
4946 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004947 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004948 }
4949
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004950 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004951 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004952 #warn "'*' is part of type\n";
4953
4954 # unary operators should have a space before and
4955 # none after. May be left adjacent to another
4956 # unary operator, or a cast
4957 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004958 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004959 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004960 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004961 if (ERROR("SPACING",
4962 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004963 if ($n != $last_after + 2) {
4964 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4965 $line_fixed = 1;
4966 }
Joe Perches3705ce52013-07-03 15:05:31 -07004967 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004968 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004969 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004970 # A unary '*' may be const
4971
4972 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004973 if (ERROR("SPACING",
4974 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004975 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004976 if (defined $fix_elements[$n + 2]) {
4977 $fix_elements[$n + 2] =~ s/^\s+//;
4978 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004979 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004980 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004981 }
4982
4983 # unary ++ and unary -- are allowed no space on one side.
4984 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004985 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004986 if (ERROR("SPACING",
4987 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004988 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004989 $line_fixed = 1;
4990 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004991 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004992 if ($ctx =~ /Wx[BE]/ ||
4993 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004994 if (ERROR("SPACING",
4995 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004996 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004997 $line_fixed = 1;
4998 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004999 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005000 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005001 if (ERROR("SPACING",
5002 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005003 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005004 if (defined $fix_elements[$n + 2]) {
5005 $fix_elements[$n + 2] =~ s/^\s+//;
5006 }
Joe Perchesb34c6482013-09-11 14:24:01 -07005007 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07005008 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005009 }
5010
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005011 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005012 } elsif ($op eq '<<' or $op eq '>>' or
5013 $op eq '&' or $op eq '^' or $op eq '|' or
5014 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005015 $op eq '*' or $op eq '/' or
5016 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005017 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08005018 if ($check) {
5019 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
5020 if (CHK("SPACING",
5021 "spaces preferred around that '$op' $at\n" . $hereptr)) {
5022 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5023 $fix_elements[$n + 2] =~ s/^\s+//;
5024 $line_fixed = 1;
5025 }
5026 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
5027 if (CHK("SPACING",
5028 "space preferred before that '$op' $at\n" . $hereptr)) {
5029 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
5030 $line_fixed = 1;
5031 }
5032 }
5033 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005034 if (ERROR("SPACING",
5035 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005036 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5037 if (defined $fix_elements[$n + 2]) {
5038 $fix_elements[$n + 2] =~ s/^\s+//;
5039 }
Joe Perches3705ce52013-07-03 15:05:31 -07005040 $line_fixed = 1;
5041 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005042 }
5043
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005044 # A colon needs no spaces before when it is
5045 # terminating a case value or a label.
5046 } elsif ($opv eq ':C' || $opv eq ':L') {
5047 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07005048 if (ERROR("SPACING",
5049 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005050 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005051 $line_fixed = 1;
5052 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005053 }
5054
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005055 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08005056 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005057 my $ok = 0;
5058
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005059 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005060 if (($op eq '<' &&
5061 $cc =~ /^\S+\@\S+>/) ||
5062 ($op eq '>' &&
5063 $ca =~ /<\S+\@\S+$/))
5064 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07005065 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005066 }
5067
Joe Perchese0df7e12015-04-16 12:44:53 -07005068 # for asm volatile statements
5069 # ignore a colon with another
5070 # colon immediately before or after
5071 if (($op eq ':') &&
5072 ($ca =~ /:$/ || $cc =~ /^:/)) {
5073 $ok = 1;
5074 }
5075
Joe Perches84731622013-11-12 15:10:05 -08005076 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005077 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07005078 my $msg_level = \&ERROR;
5079 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08005080
Jean Delvare0675a8f2017-09-08 16:16:07 -07005081 if (&{$msg_level}("SPACING",
5082 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005083 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5084 if (defined $fix_elements[$n + 2]) {
5085 $fix_elements[$n + 2] =~ s/^\s+//;
5086 }
Joe Perches3705ce52013-07-03 15:05:31 -07005087 $line_fixed = 1;
5088 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005089 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005090 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005091 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005092
5093## print("n: <$n> GOOD: <$good>\n");
5094
5095 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005096 }
Joe Perches3705ce52013-07-03 15:05:31 -07005097
5098 if (($#elements % 2) == 0) {
5099 $fixed_line = $fixed_line . $fix_elements[$#elements];
5100 }
5101
Joe Perches194f66f2014-08-06 16:11:03 -07005102 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5103 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07005104 }
5105
5106
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005107 }
5108
Joe Perches786b6322013-07-03 15:05:32 -07005109# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08005110 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07005111 if (WARN("SPACING",
5112 "space prohibited before semicolon\n" . $herecurr) &&
5113 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005114 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07005115 s/^(\+.*\S)\s+;/$1;/;
5116 }
5117 }
5118
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005119# check for multiple assignments
5120 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005121 CHK("MULTIPLE_ASSIGNMENTS",
5122 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005123 }
5124
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005125## # check for multiple declarations, allowing for a function declaration
5126## # continuation.
5127## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
5128## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
5129##
5130## # Remove any bracketed sections to ensure we do not
Dwaipayan Raye73d2712020-12-15 20:44:56 -08005131## # falsely report the parameters of functions.
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005132## my $ln = $line;
5133## while ($ln =~ s/\([^\(\)]*\)//g) {
5134## }
5135## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005136## WARN("MULTIPLE_DECLARATION",
5137## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005138## }
5139## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005140
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005141#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07005142 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07005143 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005144 if (ERROR("SPACING",
5145 "space required before the open brace '{'\n" . $herecurr) &&
5146 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07005147 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07005148 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005149 }
5150
Joe Perchesc4a62ef2013-07-03 15:05:28 -07005151## # check for blank lines before declarations
5152## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5153## $prevrawline =~ /^.\s*$/) {
5154## WARN("SPACING",
5155## "No blank lines before declarations\n" . $hereprev);
5156## }
5157##
5158
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005159# closing brace should have a space following it when it has anything
5160# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07005161 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005162 if (ERROR("SPACING",
5163 "space required after that close brace '}'\n" . $herecurr) &&
5164 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005165 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07005166 s/}((?!(?:,|;|\)))\S)/} $1/;
5167 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005168 }
5169
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005170# check spacing on square brackets
5171 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005172 if (ERROR("SPACING",
5173 "space prohibited after that open square bracket '['\n" . $herecurr) &&
5174 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005175 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005176 s/\[\s+/\[/;
5177 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005178 }
5179 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005180 if (ERROR("SPACING",
5181 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
5182 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005183 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005184 s/\s+\]/\]/;
5185 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005186 }
5187
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005188# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005189 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
5190 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005191 if (ERROR("SPACING",
5192 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
5193 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005194 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005195 s/\(\s+/\(/;
5196 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005197 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005198 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005199 $line !~ /for\s*\(.*;\s+\)/ &&
5200 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005201 if (ERROR("SPACING",
5202 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
5203 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005204 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005205 s/\s+\)/\)/;
5206 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005207 }
5208
Joe Perchese2826fd2014-08-06 16:10:48 -07005209# check unnecessary parentheses around addressof/dereference single $Lvals
5210# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5211
5212 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08005213 my $var = $1;
5214 if (CHK("UNNECESSARY_PARENTHESES",
5215 "Unnecessary parentheses around $var\n" . $herecurr) &&
5216 $fix) {
5217 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5218 }
5219 }
5220
5221# check for unnecessary parentheses around function pointer uses
5222# ie: (foo->bar)(); should be foo->bar();
5223# but not "if (foo->bar) (" to avoid some false positives
5224 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5225 my $var = $2;
5226 if (CHK("UNNECESSARY_PARENTHESES",
5227 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5228 $fix) {
5229 my $var2 = deparenthesize($var);
5230 $var2 =~ s/\s//g;
5231 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5232 }
5233 }
Joe Perchese2826fd2014-08-06 16:10:48 -07005234
Joe Perches63b7c732017-09-08 16:16:01 -07005235# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08005236# when !drivers/staging or command-line uses --strict
5237 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07005238 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07005239 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
5240 my $if_stat = $1;
5241 my $test = substr($2, 1, -1);
5242 my $herectx;
5243 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
5244 my $match = $1;
5245 # avoid parentheses around potential macro args
5246 next if ($match =~ /^\s*\w+\s*$/);
5247 if (!defined($herectx)) {
5248 $herectx = $here . "\n";
5249 my $cnt = statement_rawlines($if_stat);
5250 for (my $n = 0; $n < $cnt; $n++) {
5251 my $rl = raw_line($linenr, $n);
5252 $herectx .= $rl . "\n";
5253 last if $rl =~ /^[ \+].*\{/;
5254 }
5255 }
5256 CHK("UNNECESSARY_PARENTHESES",
5257 "Unnecessary parentheses around '$match'\n" . $herectx);
5258 }
5259 }
5260
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005261#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005262 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005263 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07005264 if (WARN("INDENTED_LABEL",
5265 "labels should not be indented\n" . $herecurr) &&
5266 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005267 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005268 s/^(.)\s+/$1/;
5269 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005270 }
5271
Joe Perches40873ab2020-10-15 20:11:56 -07005272# check if a statement with a comma should be two statements like:
5273# foo = bar(), /* comma should be semicolon */
5274# bar = baz();
5275 if (defined($stat) &&
5276 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5277 my $cnt = statement_rawlines($stat);
5278 my $herectx = get_stat_here($linenr, $cnt, $here);
5279 WARN("SUSPECT_COMMA_SEMICOLON",
5280 "Possible comma where semicolon could be used\n" . $herectx);
5281 }
5282
Joe Perches5b9553a2014-04-03 14:49:21 -07005283# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08005284 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005285 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07005286 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07005287 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5288 my $value = $1;
5289 $value = deparenthesize($value);
5290 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5291 ERROR("RETURN_PARENTHESES",
5292 "return is not a function, parentheses are not required\n" . $herecurr);
5293 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005294 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005295 ERROR("SPACING",
5296 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005297 }
5298 }
Joe Perches507e5142013-11-12 15:10:13 -08005299
Joe Perchesb43ae212014-06-23 13:22:07 -07005300# unnecessary return in a void function
5301# at end-of-function, with the previous line a single leading tab, then return;
5302# and the line before that not a goto label target like "out:"
5303 if ($sline =~ /^[ \+]}\s*$/ &&
5304 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5305 $linenr >= 3 &&
5306 $lines[$linenr - 3] =~ /^[ +]/ &&
5307 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07005308 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07005309 "void function return statements are not generally useful\n" . $hereprev);
Dwaipayan Rayea7dbab2021-02-25 17:21:47 -08005310 }
Joe Perches9819cf22014-06-04 16:12:09 -07005311
Joe Perches189248d2014-01-23 15:54:47 -08005312# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07005313 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08005314 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5315 my $openparens = $1;
5316 my $count = $openparens =~ tr@\(@\(@;
5317 my $msg = "";
5318 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5319 my $comp = $4; #Not $1 because of $LvalOrFunc
5320 $msg = " - maybe == should be = ?" if ($comp eq "==");
5321 WARN("UNNECESSARY_PARENTHESES",
5322 "Unnecessary parentheses$msg\n" . $herecurr);
5323 }
5324 }
5325
Joe Perchesc5595fa2015-09-09 15:37:58 -07005326# comparisons with a constant or upper case identifier on the left
5327# avoid cases like "foo + BAR < baz"
5328# only fix matches surrounded by parentheses to avoid incorrect
5329# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07005330 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005331 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5332 my $lead = $1;
5333 my $const = $2;
5334 my $comp = $3;
5335 my $to = $4;
5336 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07005337 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005338 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5339 WARN("CONSTANT_COMPARISON",
5340 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5341 $fix) {
5342 if ($comp eq "<") {
5343 $newcomp = ">";
5344 } elsif ($comp eq "<=") {
5345 $newcomp = ">=";
5346 } elsif ($comp eq ">") {
5347 $newcomp = "<";
5348 } elsif ($comp eq ">=") {
5349 $newcomp = "<=";
5350 }
5351 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5352 }
5353 }
5354
Joe Perchesf34e4a42015-04-16 12:44:19 -07005355# Return of what appears to be an errno should normally be negative
5356 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005357 my $name = $1;
5358 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07005359 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07005360 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005361 }
5362 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005363
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005364# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07005365 if ($line =~ /\b(if|while|for|switch)\(/) {
5366 if (ERROR("SPACING",
5367 "space required before the open parenthesis '('\n" . $herecurr) &&
5368 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005369 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005370 s/\b(if|while|for|switch)\(/$1 \(/;
5371 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005372 }
5373
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07005374# Check for illegal assignment in if conditional -- and check for trailing
5375# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005376 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08005377 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5378 ctx_statement_block($linenr, $realcnt, 0)
5379 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005380 my ($stat_next) = ctx_statement_block($line_nr_next,
5381 $remain_next, $off_next);
5382 $stat_next =~ s/\n./\n /g;
5383 ##print "stat<$stat> stat_next<$stat_next>\n";
5384
5385 if ($stat_next =~ /^\s*while\b/) {
5386 # If the statement carries leading newlines,
5387 # then count those as offsets.
5388 my ($whitespace) =
5389 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5390 my $offset =
5391 statement_rawlines($whitespace) - 1;
5392
5393 $suppress_whiletrailers{$line_nr_next +
5394 $offset} = 1;
5395 }
5396 }
5397 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005398 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005399 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005400 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005401
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005402 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches65b64b32020-08-11 18:35:10 -07005403 if (ERROR("ASSIGN_IN_IF",
5404 "do not use assignment in if condition\n" . $herecurr) &&
5405 $fix && $perl_version_ok) {
5406 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5407 my $space = $1;
5408 my $not = $2;
5409 my $statement = $3;
5410 my $assigned = $4;
5411 my $test = $8;
5412 my $against = $9;
5413 my $brace = $15;
5414 fix_delete_line($fixlinenr, $rawline);
5415 fix_insert_line($fixlinenr, "$space$statement;");
5416 my $newline = "${space}if (";
5417 $newline .= '!' if defined($not);
5418 $newline .= '(' if (defined $not && defined($test) && defined($against));
5419 $newline .= "$assigned";
5420 $newline .= " $test $against" if (defined($test) && defined($against));
5421 $newline .= ')' if (defined $not && defined($test) && defined($against));
5422 $newline .= ')';
5423 $newline .= " {" if (defined($brace));
5424 fix_insert_line($fixlinenr + 1, $newline);
5425 }
5426 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005427 }
5428
5429 # Find out what is on the end of the line after the
5430 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005431 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005432 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005433 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005434 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5435 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005436 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005437 # Find out how long the conditional actually is.
5438 my @newlines = ($c =~ /\n/gs);
5439 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005440 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005441
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005442 $stat_real = raw_line($linenr, $cond_lines)
5443 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005444 if (defined($stat_real) && $cond_lines > 1) {
5445 $stat_real = "[...]\n$stat_real";
5446 }
5447
Joe Perches000d1cc12011-07-25 17:13:25 -07005448 ERROR("TRAILING_STATEMENTS",
5449 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005450 }
5451 }
5452
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005453# Check for bitwise tests written as boolean
5454 if ($line =~ /
5455 (?:
5456 (?:\[|\(|\&\&|\|\|)
5457 \s*0[xX][0-9]+\s*
5458 (?:\&\&|\|\|)
5459 |
5460 (?:\&\&|\|\|)
5461 \s*0[xX][0-9]+\s*
5462 (?:\&\&|\|\||\)|\])
5463 )/x)
5464 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005465 WARN("HEXADECIMAL_BOOLEAN_TEST",
5466 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005467 }
5468
Andy Whitcroft8905a672007-11-28 16:21:06 -08005469# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005470 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5471 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005472 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005473 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005474 ERROR("TRAILING_STATEMENTS",
5475 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005476 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005477 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005478# if should not continue a brace
5479 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005480 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005481 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005482 $herecurr);
5483 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005484# case and default should not have general statements after them
5485 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5486 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005487 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005488 \s*return\s+
5489 )/xg)
5490 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005491 ERROR("TRAILING_STATEMENTS",
5492 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005493 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005494
5495 # Check for }<nl>else {, these must be at the same
5496 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005497 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5498 $previndent == $indent) {
5499 if (ERROR("ELSE_AFTER_BRACE",
5500 "else should follow close brace '}'\n" . $hereprev) &&
5501 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5502 fix_delete_line($fixlinenr - 1, $prevrawline);
5503 fix_delete_line($fixlinenr, $rawline);
5504 my $fixedline = $prevrawline;
5505 $fixedline =~ s/}\s*$//;
5506 if ($fixedline !~ /^\+\s*$/) {
5507 fix_insert_line($fixlinenr, $fixedline);
5508 }
5509 $fixedline = $rawline;
5510 $fixedline =~ s/^(.\s*)else/$1} else/;
5511 fix_insert_line($fixlinenr, $fixedline);
5512 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005513 }
5514
Joe Perches8b8856f2014-08-06 16:11:14 -07005515 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5516 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005517 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5518
5519 # Find out what is on the end of the line after the
5520 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005521 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005522 $s =~ s/\n.*//g;
5523
5524 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005525 if (ERROR("WHILE_AFTER_BRACE",
5526 "while should follow close brace '}'\n" . $hereprev) &&
5527 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5528 fix_delete_line($fixlinenr - 1, $prevrawline);
5529 fix_delete_line($fixlinenr, $rawline);
5530 my $fixedline = $prevrawline;
5531 my $trailing = $rawline;
5532 $trailing =~ s/^\+//;
5533 $trailing = trim($trailing);
5534 $fixedline =~ s/}\s*$/} $trailing/;
5535 fix_insert_line($fixlinenr, $fixedline);
5536 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005537 }
5538 }
5539
Joe Perches95e2c602013-07-03 15:05:20 -07005540#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005541 while ($line =~ m{($Constant|$Lval)}g) {
5542 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005543
Joe Perches95e2c602013-07-03 15:05:20 -07005544#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005545 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005546 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Łukasz Stelmach4104a202020-12-15 20:44:27 -08005547#Ignore some autogenerated defines and enum values
5548 $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005549#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005550 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005551#Ignore SI style variants like nS, mV and dB
5552#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5553 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005554#Ignore some three character SI units explicitly, like MiB and KHz
5555 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005556 while ($var =~ m{($Ident)}g) {
5557 my $word = $1;
5558 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005559 if ($check) {
5560 seed_camelcase_includes();
5561 if (!$file && !$camelcase_file_seeded) {
5562 seed_camelcase_file($realfile);
5563 $camelcase_file_seeded = 1;
5564 }
5565 }
Joe Perches7e781f62013-09-11 14:23:55 -07005566 if (!defined $camelcase{$word}) {
5567 $camelcase{$word} = 1;
5568 CHK("CAMELCASE",
5569 "Avoid CamelCase: <$word>\n" . $herecurr);
5570 }
Joe Perches34456862013-07-03 15:05:34 -07005571 }
Joe Perches323c1262012-12-17 16:02:07 -08005572 }
5573 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005574
5575#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005576 if ($line =~ /\#\s*define.*\\\s+$/) {
5577 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5578 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5579 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005580 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005581 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005582 }
5583
Fabian Frederick0e212e02015-04-16 12:44:25 -07005584# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5585# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005586 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005587 my $file = "$1.h";
5588 my $checkfile = "include/linux/$file";
5589 if (-f "$root/$checkfile" &&
5590 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005591 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005592 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005593 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5594 if ($asminclude > 0) {
5595 if ($realfile =~ m{^arch/}) {
5596 CHK("ARCH_INCLUDE_LINUX",
5597 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5598 } else {
5599 WARN("INCLUDE_LINUX",
5600 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5601 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005602 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005603 }
5604 }
5605
Andy Whitcroft653d4872007-06-23 17:16:34 -07005606# multi-statement macros should be enclosed in a do while loop, grab the
5607# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005608# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005609 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5610 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005611 my $ln = $linenr;
5612 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005613 my ($off, $dstat, $dcond, $rest);
5614 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005615 my $has_flow_statement = 0;
5616 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005617 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005618 ctx_statement_block($linenr, $realcnt, 0);
5619 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005620 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005621 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005622
Joe Perches08a28432014-10-13 15:51:55 -07005623 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005624 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005625
Joe Perchesf59b64b2016-10-11 13:52:08 -07005626 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5627 my $define_args = $1;
5628 my $define_stmt = $dstat;
5629 my @def_args = ();
5630
5631 if (defined $define_args && $define_args ne "") {
5632 $define_args = substr($define_args, 1, length($define_args) - 2);
5633 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005634 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005635 @def_args = split(",", $define_args);
5636 }
5637
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005638 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005639 $dstat =~ s/\\\n.//g;
5640 $dstat =~ s/^\s*//s;
5641 $dstat =~ s/\s*$//s;
5642
5643 # Flatten any parentheses and braces
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005644 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5645 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5646 $dstat =~ s/.\[[^\[\]]*\]/1u/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005647 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005648 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005649
Antonio Borneo342d3d22020-04-06 20:11:01 -07005650 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005651 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5652 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005653 {
5654 }
5655
Joe Perches42e15292016-03-15 14:58:01 -07005656 # Make asm volatile uses seem like a generic function
5657 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5658
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005659 my $exceptions = qr{
5660 $Declare|
5661 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005662 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005663 DECLARE_PER_CPU|
5664 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005665 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005666 union|
5667 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005668 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005669 ^\"|\"$|
5670 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005671 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005672 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005673
5674 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005675 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005676 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005677
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005678 if ($dstat ne '' &&
5679 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5680 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005681 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005682 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005683 $dstat !~ /$exceptions/ &&
5684 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005685 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005686 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005687 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005688 $dstat !~ /^for\s*$Constant$/ && # for (...)
5689 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5690 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005691 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005692 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005693 {
Joe Perchese7955562017-05-08 15:55:48 -07005694 if ($dstat =~ /^\s*if\b/) {
5695 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5696 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5697 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005698 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5699 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5700 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005701 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005702 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005703 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005704
5705 }
Joe Perches52076492016-10-11 13:52:14 -07005706
5707 # Make $define_stmt single line, comment-free, etc
5708 my @stmt_array = split('\n', $define_stmt);
5709 my $first = 1;
5710 $define_stmt = "";
5711 foreach my $l (@stmt_array) {
5712 $l =~ s/\\$//;
5713 if ($first) {
5714 $define_stmt = $l;
5715 $first = 0;
5716 } elsif ($l =~ /^[\+ ]/) {
5717 $define_stmt .= substr($l, 1);
5718 }
5719 }
5720 $define_stmt =~ s/$;//g;
5721 $define_stmt =~ s/\s+/ /g;
5722 $define_stmt = trim($define_stmt);
5723
Joe Perchesf59b64b2016-10-11 13:52:08 -07005724# check if any macro arguments are reused (ignore '...' and 'type')
5725 foreach my $arg (@def_args) {
5726 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005727 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005728 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005729 $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
Joe Perches7fe528a22017-07-10 15:52:27 -07005730 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5731 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005732 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005733 if ($use_cnt > 1) {
5734 CHK("MACRO_ARG_REUSE",
5735 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005736 }
5737# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005738 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005739 ((defined($1) && $1 ne ',') ||
5740 (defined($2) && $2 ne ','))) {
5741 CHK("MACRO_ARG_PRECEDENCE",
5742 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005743 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005744 }
Joe Perches5023d342012-12-17 16:01:47 -08005745
Joe Perches08a28432014-10-13 15:51:55 -07005746# check for macros with flow control, but without ## concatenation
5747# ## concatenation is commonly a macro that defines a function so ignore those
5748 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005749 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005750 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005751
Joe Perches08a28432014-10-13 15:51:55 -07005752 WARN("MACRO_WITH_FLOW_CONTROL",
5753 "Macros with flow control statements should be avoided\n" . "$herectx");
5754 }
5755
Joe Perches481eb482012-12-17 16:01:56 -08005756# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005757
5758 } else {
5759 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005760 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5761 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005762 $line =~ /^\+.*\\$/) {
5763 WARN("LINE_CONTINUATIONS",
5764 "Avoid unnecessary line continuations\n" . $herecurr);
5765 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005766 }
5767
Joe Perchesb13edf72012-07-30 14:41:24 -07005768# do {} while (0) macro tests:
5769# single-statement macros do not need to be enclosed in do while (0) loop,
5770# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005771 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005772 $realfile !~ m@/vmlinux.lds.h$@ &&
5773 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5774 my $ln = $linenr;
5775 my $cnt = $realcnt;
5776 my ($off, $dstat, $dcond, $rest);
5777 my $ctx = '';
5778 ($dstat, $dcond, $ln, $cnt, $off) =
5779 ctx_statement_block($linenr, $realcnt, 0);
5780 $ctx = $dstat;
5781
5782 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005783 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005784
5785 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5786 my $stmts = $2;
5787 my $semis = $3;
5788
5789 $ctx =~ s/\n*$//;
5790 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005791 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005792
Joe Perchesac8e97f2012-08-21 16:15:53 -07005793 if (($stmts =~ tr/;/;/) == 1 &&
5794 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005795 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5796 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5797 }
5798 if (defined $semis && $semis ne "") {
5799 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5800 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5801 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005802 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5803 $ctx =~ s/\n*$//;
5804 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005805 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005806
5807 WARN("TRAILING_SEMICOLON",
5808 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005809 }
5810 }
5811
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005812# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005813 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5814 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005815 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005816 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005817 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5818 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005819 my @allowed = ();
5820 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005821 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005822 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005823 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005824 for my $chunk (@chunks) {
5825 my ($cond, $block) = @{$chunk};
5826
Andy Whitcroft773647a2008-03-28 14:15:58 -07005827 # If the condition carries leading newlines, then count those as offsets.
5828 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5829 my $offset = statement_rawlines($whitespace) - 1;
5830
Joe Perchesaad4f612012-03-23 15:02:19 -07005831 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005832 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5833
5834 # We have looked at and allowed this specific line.
5835 $suppress_ifbraces{$ln + $offset} = 1;
5836
5837 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005838 $ln += statement_rawlines($block) - 1;
5839
Andy Whitcroft773647a2008-03-28 14:15:58 -07005840 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005841
5842 $seen++ if ($block =~ /^\s*{/);
5843
Joe Perchesaad4f612012-03-23 15:02:19 -07005844 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005845 if (statement_lines($cond) > 1) {
5846 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005847 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005848 }
5849 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005850 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005851 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005852 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005853 if (statement_block_size($block) > 1) {
5854 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005855 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005856 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005857 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005858 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005859 if ($seen) {
5860 my $sum_allowed = 0;
5861 foreach (@allowed) {
5862 $sum_allowed += $_;
5863 }
5864 if ($sum_allowed == 0) {
5865 WARN("BRACES",
5866 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5867 } elsif ($sum_allowed != $allow &&
5868 $seen != $allow) {
5869 CHK("BRACES",
5870 "braces {} should be used on all arms of this statement\n" . $herectx);
5871 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005872 }
5873 }
5874 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005875 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005876 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005877 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005878
Andy Whitcroftcf655042008-03-04 14:28:20 -08005879 # Check the pre-context.
5880 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5881 #print "APW: ALLOWED: pre<$1>\n";
5882 $allowed = 1;
5883 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005884
5885 my ($level, $endln, @chunks) =
5886 ctx_statement_full($linenr, $realcnt, $-[0]);
5887
Andy Whitcroftcf655042008-03-04 14:28:20 -08005888 # Check the condition.
5889 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005890 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005891 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005892 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005893 }
5894 if (statement_lines($cond) > 1) {
5895 #print "APW: ALLOWED: cond<$cond>\n";
5896 $allowed = 1;
5897 }
5898 if ($block =~/\b(?:if|for|while)\b/) {
5899 #print "APW: ALLOWED: block<$block>\n";
5900 $allowed = 1;
5901 }
5902 if (statement_block_size($block) > 1) {
5903 #print "APW: ALLOWED: lines block<$block>\n";
5904 $allowed = 1;
5905 }
5906 # Check the post-context.
5907 if (defined $chunks[1]) {
5908 my ($cond, $block) = @{$chunks[1]};
5909 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005910 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005911 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005912 if ($block =~ /^\s*\{/) {
5913 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5914 $allowed = 1;
5915 }
5916 }
5917 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005918 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005919 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005920
Joe Perches000d1cc12011-07-25 17:13:25 -07005921 WARN("BRACES",
5922 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005923 }
5924 }
5925
Joe Perchese4c5bab2017-02-24 15:01:41 -08005926# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005927 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5928 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005929 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5930 }
5931
Joe Perches0979ae62012-12-17 16:01:59 -08005932# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005933 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005934 if (CHK("BRACES",
5935 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5936 $fix && $prevrawline =~ /^\+/) {
5937 fix_delete_line($fixlinenr - 1, $prevrawline);
5938 }
Joe Perches0979ae62012-12-17 16:01:59 -08005939 }
Joe Perches77b9a532013-07-03 15:05:29 -07005940 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005941 if (CHK("BRACES",
5942 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5943 $fix) {
5944 fix_delete_line($fixlinenr, $rawline);
5945 }
Joe Perches0979ae62012-12-17 16:01:59 -08005946 }
5947
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005948# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005949 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5950 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005951 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005952 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005953 }
5954
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005955# Check for user-visible strings broken across lines, which breaks the ability
5956# to grep for the string. Make exceptions when the previous string ends in a
5957# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5958# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005959 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005960 $prevline =~ /"\s*$/ &&
5961 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5962 if (WARN("SPLIT_STRING",
5963 "quoted string split across lines\n" . $hereprev) &&
5964 $fix &&
5965 $prevrawline =~ /^\+.*"\s*$/ &&
5966 $last_coalesced_string_linenr != $linenr - 1) {
5967 my $extracted_string = get_quoted_string($line, $rawline);
5968 my $comma_close = "";
5969 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5970 $comma_close = $1;
5971 }
5972
5973 fix_delete_line($fixlinenr - 1, $prevrawline);
5974 fix_delete_line($fixlinenr, $rawline);
5975 my $fixedline = $prevrawline;
5976 $fixedline =~ s/"\s*$//;
5977 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5978 fix_insert_line($fixlinenr - 1, $fixedline);
5979 $fixedline = $rawline;
5980 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5981 if ($fixedline !~ /\+\s*$/) {
5982 fix_insert_line($fixlinenr, $fixedline);
5983 }
5984 $last_coalesced_string_linenr = $linenr;
5985 }
5986 }
5987
5988# check for missing a space in a string concatenation
5989 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5990 WARN('MISSING_SPACE',
5991 "break quoted strings at a space character\n" . $hereprev);
5992 }
5993
Joe Perchese4b7d302017-05-08 15:55:51 -07005994# check for an embedded function name in a string when the function is known
5995# This does not work very well for -f --file checking as it depends on patch
5996# context providing the function name or a single line form for in-file
5997# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005998 if ($line =~ /^\+.*$String/ &&
5999 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07006000 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
6001 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08006002 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07006003 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08006004 }
6005
Joe Perchesadb2da82021-02-25 17:21:50 -08006006# check for unnecessary function tracing like uses
6007# This does not use $logFunctions because there are many instances like
6008# 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions
6009 if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) {
6010 if (WARN("TRACING_LOGGING",
6011 "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) &&
6012 $fix) {
6013 fix_delete_line($fixlinenr, $rawline);
6014 }
6015 }
6016
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006017# check for spaces before a quoted newline
6018 if ($rawline =~ /^.*\".*\s\\n/) {
6019 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
6020 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
6021 $fix) {
6022 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
6023 }
6024
6025 }
6026
Joe Perchesf17dba42014-10-13 15:51:51 -07006027# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07006028 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
6029 if (CHK("CONCATENATED_STRING",
6030 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
6031 $fix) {
6032 while ($line =~ /($String)/g) {
6033 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6034 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
6035 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
6036 }
6037 }
Joe Perchesf17dba42014-10-13 15:51:51 -07006038 }
6039
Joe Perches90ad30e2014-12-10 15:51:59 -08006040# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07006041 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07006042 if (WARN("STRING_FRAGMENTS",
6043 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
6044 $fix) {
6045 while ($line =~ /($String)(?=\s*")/g) {
6046 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6047 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
6048 }
6049 }
Joe Perches90ad30e2014-12-10 15:51:59 -08006050 }
6051
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006052# check for non-standard and hex prefixed decimal printf formats
6053 my $show_L = 1; #don't show the same defect twice
6054 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006055 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006056 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006057 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006058 # check for %L
6059 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006060 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006061 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6062 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006063 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006064 # check for %Z
6065 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6066 WARN("PRINTF_Z",
6067 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6068 $show_Z = 0;
6069 }
6070 # check for 0x<decimal>
6071 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6072 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07006073 "Prefixing 0x with decimal output is defective\n" . $herecurr);
6074 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006075 }
6076
6077# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08006078 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006079 WARN("LINE_CONTINUATIONS",
6080 "Avoid line continuations in quoted strings\n" . $herecurr);
6081 }
6082
Andy Whitcroft00df3442007-06-08 13:47:06 -07006083# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006084 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07006085 WARN("IF_0",
6086 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
6087 }
6088
6089# warn about #if 1
6090 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
6091 WARN("IF_1",
6092 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006093 }
6094
Andy Whitcroft03df4b52012-12-17 16:01:52 -08006095# check for needless "if (<foo>) fn(<foo>)" uses
6096 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07006097 my $tested = quotemeta($1);
6098 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6099 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6100 my $func = $1;
6101 if (WARN('NEEDLESS_IF',
6102 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6103 $fix) {
6104 my $do_fix = 1;
6105 my $leading_tabs = "";
6106 my $new_leading_tabs = "";
6107 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6108 $leading_tabs = $1;
6109 } else {
6110 $do_fix = 0;
6111 }
6112 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6113 $new_leading_tabs = $1;
6114 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6115 $do_fix = 0;
6116 }
6117 } else {
6118 $do_fix = 0;
6119 }
6120 if ($do_fix) {
6121 fix_delete_line($fixlinenr - 1, $prevrawline);
6122 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6123 }
6124 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07006125 }
6126 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006127
Joe Perchesebfdc402014-08-06 16:10:27 -07006128# check for unnecessary "Out of Memory" messages
6129 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6130 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6131 (defined $1 || defined $3) &&
6132 $linenr > 3) {
6133 my $testval = $2;
6134 my $testline = $lines[$linenr - 3];
6135
6136 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6137# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6138
Joe Perchese29a70f2019-03-07 16:28:35 -08006139 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6140 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07006141 WARN("OOM_MESSAGE",
6142 "Possible unnecessary 'out of memory' message\n" . $hereprev);
6143 }
6144 }
6145
Joe Perchesf78d98f2014-10-13 15:52:01 -07006146# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08006147 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07006148 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6149 my $level = $1;
6150 if (WARN("UNNECESSARY_KERN_LEVEL",
6151 "Possible unnecessary $level\n" . $herecurr) &&
6152 $fix) {
6153 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
6154 }
6155 }
6156
Joe Perches45c55e92017-02-24 15:01:31 -08006157# check for logging continuations
6158 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
6159 WARN("LOGGING_CONTINUATION",
6160 "Avoid logging continuation uses where feasible\n" . $herecurr);
6161 }
6162
Dwaipayan Ray70eb2272020-12-15 20:45:15 -08006163# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
6164 if (defined $stat &&
6165 $line =~ /\b$logFunctions\s*\(/ &&
6166 index($stat, '"') >= 0) {
6167 my $lc = $stat =~ tr@\n@@;
6168 $lc = $lc + $linenr;
6169 my $stat_real = get_stat_real($linenr, $lc);
6170 pos($stat_real) = index($stat_real, '"');
6171 while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
6172 my $pspec = $1;
6173 my $h = $2;
6174 my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
6175 if (WARN("UNNECESSARY_MODIFIER",
6176 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
6177 $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
6178 my $nspec = $pspec;
6179 $nspec =~ s/h//g;
6180 $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
6181 }
6182 }
6183 }
6184
Joe Perchesabb08a52014-12-10 15:51:46 -08006185# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07006186 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08006187 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6188 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6189 WARN("MASK_THEN_SHIFT",
6190 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6191 }
6192
Joe Perchesb75ac612014-12-10 15:52:02 -08006193# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07006194 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08006195 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6196 my $val = $1;
6197 my $equal = "!";
6198 $equal = "" if ($4 eq "!=");
6199 if (CHK("COMPARISON_TO_NULL",
6200 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6201 $fix) {
6202 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6203 }
6204 }
6205 }
6206
Joe Perches8716de32013-09-11 14:24:05 -07006207# check for bad placement of section $InitAttribute (e.g.: __initdata)
6208 if ($line =~ /(\b$InitAttribute\b)/) {
6209 my $attr = $1;
6210 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
6211 my $ptr = $1;
6212 my $var = $2;
6213 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
6214 ERROR("MISPLACED_INIT",
6215 "$attr should be placed after $var\n" . $herecurr)) ||
6216 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
6217 WARN("MISPLACED_INIT",
6218 "$attr should be placed after $var\n" . $herecurr))) &&
6219 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006220 $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 -07006221 }
6222 }
6223 }
6224
Joe Perchese970b8842013-11-12 15:10:10 -08006225# check for $InitAttributeData (ie: __initdata) with const
6226 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6227 my $attr = $1;
6228 $attr =~ /($InitAttributePrefix)(.*)/;
6229 my $attr_prefix = $1;
6230 my $attr_type = $2;
6231 if (ERROR("INIT_ATTRIBUTE",
6232 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6233 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006234 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006235 s/$InitAttributeData/${attr_prefix}initconst/;
6236 }
6237 }
6238
6239# check for $InitAttributeConst (ie: __initconst) without const
6240 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6241 my $attr = $1;
6242 if (ERROR("INIT_ATTRIBUTE",
6243 "Use of $attr requires a separate use of const\n" . $herecurr) &&
6244 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006245 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006246 /(^\+\s*(?:static\s+))/;
6247 $lead = rtrim($1);
6248 $lead = "$lead " if ($lead !~ /^\+$/);
6249 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07006250 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08006251 }
6252 }
6253
Joe Perchesc17893c2015-04-16 12:44:42 -07006254# check for __read_mostly with const non-pointer (should just be const)
6255 if ($line =~ /\b__read_mostly\b/ &&
6256 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6257 if (ERROR("CONST_READ_MOSTLY",
6258 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6259 $fix) {
6260 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6261 }
6262 }
6263
Joe Perchesfbdb8132014-04-03 14:49:14 -07006264# don't use __constant_<foo> functions outside of include/uapi/
6265 if ($realfile !~ m@^include/uapi/@ &&
6266 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6267 my $constant_func = $1;
6268 my $func = $constant_func;
6269 $func =~ s/^__constant_//;
6270 if (WARN("CONSTANT_CONVERSION",
6271 "$constant_func should be $func\n" . $herecurr) &&
6272 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006273 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07006274 }
6275 }
6276
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006277# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08006278 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07006279 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006280 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07006281 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006282 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006283 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07006284 }
6285 if ($delay > 2000) {
6286 WARN("LONG_UDELAY",
6287 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006288 }
6289 }
6290
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006291# warn about unexpectedly long msleep's
6292 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6293 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006294 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006295 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006296 }
6297 }
6298
Joe Perches36ec1932013-07-03 15:05:25 -07006299# check for comparisons of jiffies
6300 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6301 WARN("JIFFIES_COMPARISON",
6302 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6303 }
6304
Joe Perches9d7a34a2013-07-03 15:05:26 -07006305# check for comparisons of get_jiffies_64()
6306 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6307 WARN("JIFFIES_COMPARISON",
6308 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6309 }
6310
Andy Whitcroft00df3442007-06-08 13:47:06 -07006311# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006312# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07006313# print "#ifdef in C files should be avoided\n";
6314# print "$herecurr";
6315# $clean = 0;
6316# }
6317
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006318# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006319 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07006320 if (ERROR("SPACING",
6321 "exactly one space required after that #$1\n" . $herecurr) &&
6322 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006323 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07006324 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6325 }
6326
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006327 }
6328
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006329# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006330 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6331 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006332 my $which = $1;
6333 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006334 CHK("UNCOMMENTED_DEFINITION",
6335 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006336 }
6337 }
6338# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006339
6340 my $barriers = qr{
6341 mb|
6342 rmb|
Will Deaconad83ec62019-11-07 14:49:00 +00006343 wmb
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006344 }x;
6345 my $barrier_stems = qr{
6346 mb__before_atomic|
6347 mb__after_atomic|
6348 store_release|
6349 load_acquire|
6350 store_mb|
6351 (?:$barriers)
6352 }x;
6353 my $all_barriers = qr{
6354 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02006355 smp_(?:$barrier_stems)|
6356 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006357 }x;
6358
6359 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006360 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08006361 WARN("MEMORY_BARRIER",
6362 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006363 }
6364 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006365
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02006366 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6367
6368 if ($realfile !~ m@^include/asm-generic/@ &&
6369 $realfile !~ m@/barrier\.h$@ &&
6370 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6371 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6372 WARN("MEMORY_BARRIER",
6373 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6374 }
6375
Joe Perchescb426e92015-06-25 15:02:46 -07006376# check for waitqueue_active without a comment.
6377 if ($line =~ /\bwaitqueue_active\s*\(/) {
6378 if (!ctx_has_comment($first_line, $linenr)) {
6379 WARN("WAITQUEUE_ACTIVE",
6380 "waitqueue_active without comment\n" . $herecurr);
6381 }
6382 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006383
Marco Elver5099a722020-04-01 12:17:14 +02006384# check for data_race without a comment.
6385 if ($line =~ /\bdata_race\s*\(/) {
6386 if (!ctx_has_comment($first_line, $linenr)) {
6387 WARN("DATA_RACE",
6388 "data_race without comment\n" . $herecurr);
6389 }
6390 }
6391
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006392# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006393 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006394 CHK("ARCH_DEFINES",
6395 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006396 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006397
Joe Perches596ed452017-07-12 14:37:02 -07006398# check that the storage class is not after a type
6399 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006400 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07006401 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6402 }
6403# Check that the storage class is at the beginning of a declaration
6404 if ($line =~ /\b$Storage\b/ &&
6405 $line !~ /^.\s*$Storage/ &&
6406 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6407 $1 !~ /[\,\)]\s*$/) {
6408 WARN("STORAGE_CLASS",
6409 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07006410 }
6411
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006412# check the location of the inline attribute, that it is between
6413# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006414 if ($line =~ /\b$Type\s+$Inline\b/ ||
6415 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006416 ERROR("INLINE_LOCATION",
6417 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006418 }
6419
Andy Whitcroft8905a672007-11-28 16:21:06 -08006420# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08006421 if ($realfile !~ m@\binclude/uapi/@ &&
6422 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006423 if (WARN("INLINE",
6424 "plain inline is preferred over $1\n" . $herecurr) &&
6425 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006426 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006427
6428 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006429 }
6430
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006431# Check for compiler attributes
Joe Perches2b7ab452013-11-12 15:10:14 -08006432 if ($realfile !~ m@\binclude/uapi/@ &&
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006433 $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
6434 my $attr = $1;
6435 $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
Joe Perches3d130fd2011-01-12 17:00:00 -08006436
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006437 my %attr_list = (
Joe Perches0830aab2020-12-15 20:44:50 -08006438 "alias" => "__alias",
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006439 "aligned" => "__aligned",
6440 "always_inline" => "__always_inline",
6441 "assume_aligned" => "__assume_aligned",
6442 "cold" => "__cold",
6443 "const" => "__attribute_const__",
6444 "copy" => "__copy",
6445 "designated_init" => "__designated_init",
6446 "externally_visible" => "__visible",
6447 "format" => "printf|scanf",
6448 "gnu_inline" => "__gnu_inline",
6449 "malloc" => "__malloc",
6450 "mode" => "__mode",
6451 "no_caller_saved_registers" => "__no_caller_saved_registers",
6452 "noclone" => "__noclone",
6453 "noinline" => "noinline",
6454 "nonstring" => "__nonstring",
6455 "noreturn" => "__noreturn",
6456 "packed" => "__packed",
6457 "pure" => "__pure",
Joe Perches339f29d2020-12-15 20:44:43 -08006458 "section" => "__section",
Joe Perches0830aab2020-12-15 20:44:50 -08006459 "used" => "__used",
6460 "weak" => "__weak"
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006461 );
Joe Perches39b7e282011-07-25 17:13:24 -07006462
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006463 while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
Joe Perches339f29d2020-12-15 20:44:43 -08006464 my $orig_attr = $1;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006465 my $params = '';
6466 $params = $2 if defined($2);
Joe Perches339f29d2020-12-15 20:44:43 -08006467 my $curr_attr = $orig_attr;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006468 $curr_attr =~ s/^[\s_]+|[\s_]+$//g;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006469 if (exists($attr_list{$curr_attr})) {
Joe Perches339f29d2020-12-15 20:44:43 -08006470 my $new = $attr_list{$curr_attr};
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006471 if ($curr_attr eq "format" && $params) {
6472 $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
Joe Perches339f29d2020-12-15 20:44:43 -08006473 $new = "__$1\($2";
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006474 } else {
Joe Perches339f29d2020-12-15 20:44:43 -08006475 $new = "$new$params";
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006476 }
Joe Perches339f29d2020-12-15 20:44:43 -08006477 if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6478 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
6479 $fix) {
6480 my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6481 $fixed[$fixlinenr] =~ s/$remove//;
6482 $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6483 $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6484 $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
6485 }
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006486 }
6487 }
6488
6489 # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
6490 if ($attr =~ /^_*unused/) {
6491 WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6492 "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
Joe Perchesd5e616f2013-09-11 14:23:54 -07006493 }
Joe Perches6061d942012-03-23 15:02:16 -07006494 }
6495
Joe Perches619a9082014-12-10 15:51:35 -08006496# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006497 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006498 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6499 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6500 $line =~ /\b__weak\b/)) {
6501 ERROR("WEAK_DECLARATION",
6502 "Using weak declarations can have unintended link defects\n" . $herecurr);
6503 }
6504
Tomas Winklerfd39f902016-12-12 16:46:34 -08006505# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006506 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006507 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006508 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6509 my $type = $1;
6510 if ($type =~ /\b($typeC99Typedefs)\b/) {
6511 $type = $1;
6512 my $kernel_type = 'u';
6513 $kernel_type = 's' if ($type =~ /^_*[si]/);
6514 $type =~ /(\d+)/;
6515 $kernel_type .= $1;
6516 if (CHK("PREFER_KERNEL_TYPES",
6517 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6518 $fix) {
6519 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6520 }
6521 }
6522 }
6523
Joe Perches938224b2016-01-20 14:59:15 -08006524# check for cast of C90 native int or longer types constants
6525 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6526 my $cast = $1;
6527 my $const = $2;
Joe Perches0972b8b2021-02-25 17:21:54 -08006528 my $suffix = "";
6529 my $newconst = $const;
6530 $newconst =~ s/${Int_type}$//;
6531 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6532 if ($cast =~ /\blong\s+long\b/) {
6533 $suffix .= 'LL';
6534 } elsif ($cast =~ /\blong\b/) {
6535 $suffix .= 'L';
6536 }
Joe Perches938224b2016-01-20 14:59:15 -08006537 if (WARN("TYPECAST_INT_CONSTANT",
Joe Perches0972b8b2021-02-25 17:21:54 -08006538 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
Joe Perches938224b2016-01-20 14:59:15 -08006539 $fix) {
Joe Perches938224b2016-01-20 14:59:15 -08006540 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6541 }
6542 }
6543
Joe Perches8f53a9b2010-03-05 13:43:48 -08006544# check for sizeof(&)
6545 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006546 WARN("SIZEOF_ADDRESS",
6547 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006548 }
6549
Joe Perches66c80b62012-07-30 14:41:22 -07006550# check for sizeof without parenthesis
6551 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006552 if (WARN("SIZEOF_PARENTHESIS",
6553 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6554 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006555 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006556 }
Joe Perches66c80b62012-07-30 14:41:22 -07006557 }
6558
Joe Perches88982fe2012-12-17 16:02:00 -08006559# check for struct spinlock declarations
6560 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6561 WARN("USE_SPINLOCK_T",
6562 "struct spinlock should be spinlock_t\n" . $herecurr);
6563 }
6564
Joe Perchesa6962d72013-04-29 16:18:13 -07006565# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006566 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006567 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006568 $fmt =~ s/%%//g;
6569 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006570 if (WARN("PREFER_SEQ_PUTS",
6571 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6572 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006573 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006574 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006575 }
6576 }
6577
Joe Perches478b1792018-04-10 16:33:34 -07006578# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006579 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006580 defined $stat &&
6581 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6582 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006583 my $stat_real;
6584
Joe Perches0b523762017-05-08 15:55:36 -07006585 my $lc = $stat =~ tr@\n@@;
6586 $lc = $lc + $linenr;
6587 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006588 my $specifier;
6589 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006590 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006591 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006592 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6593 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006594
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006595 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006596 $specifier = $1;
6597 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006598 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006599 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006600 ($extension eq "f" &&
6601 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006602 $bad_specifier = $specifier;
6603 last;
6604 }
6605 if ($extension eq "x" && !defined($stat_real)) {
6606 if (!defined($stat_real)) {
6607 $stat_real = get_stat_real($linenr, $lc);
6608 }
6609 WARN("VSPRINTF_SPECIFIER_PX",
6610 "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");
6611 }
6612 }
6613 if ($bad_specifier ne "") {
6614 my $stat_real = get_stat_real($linenr, $lc);
6615 my $ext_type = "Invalid";
6616 my $use = "";
6617 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006618 $use = " - use %pS instead";
6619 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6620 }
6621
6622 WARN("VSPRINTF_POINTER_EXTENSION",
6623 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6624 }
Joe Perches0b523762017-05-08 15:55:36 -07006625 }
6626 }
6627
Andy Whitcroft554e1652012-01-10 15:09:57 -08006628# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006629 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006630 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006631 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006632
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006633 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006634 my $ms_val = $7;
6635 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006636
Andy Whitcroft554e1652012-01-10 15:09:57 -08006637 if ($ms_size =~ /^(0x|)0$/i) {
6638 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006639 "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 -08006640 } elsif ($ms_size =~ /^(0x|)1$/i) {
6641 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006642 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6643 }
6644 }
6645
Joe Perches98a9bba2014-01-23 15:54:52 -08006646# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006647# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006648# defined $stat &&
6649# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6650# if (WARN("PREFER_ETHER_ADDR_COPY",
6651# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6652# $fix) {
6653# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6654# }
6655# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006656
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006657# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006658# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006659# defined $stat &&
6660# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6661# WARN("PREFER_ETHER_ADDR_EQUAL",
6662# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6663# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006664
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006665# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6666# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006667# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006668# defined $stat &&
6669# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6670#
6671# my $ms_val = $7;
6672#
6673# if ($ms_val =~ /^(?:0x|)0+$/i) {
6674# if (WARN("PREFER_ETH_ZERO_ADDR",
6675# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6676# $fix) {
6677# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6678# }
6679# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6680# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6681# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6682# $fix) {
6683# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6684# }
6685# }
6686# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006687
Joe Perches5dbdb2d2020-12-29 15:14:31 -08006688# strlcpy uses that should likely be strscpy
6689 if ($line =~ /\bstrlcpy\s*\(/) {
6690 WARN("STRLCPY",
6691 "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr);
6692 }
6693
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006694# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006695 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006696 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006697 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006698 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006699 my $call = $1;
6700 my $cast1 = deparenthesize($2);
6701 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006702 my $cast2 = deparenthesize($7);
6703 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006704 my $cast;
6705
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006706 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006707 $cast = "$cast1 or $cast2";
6708 } elsif ($cast1 ne "") {
6709 $cast = $cast1;
6710 } else {
6711 $cast = $cast2;
6712 }
6713 WARN("MINMAX",
6714 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006715 }
6716 }
6717
Joe Perches4a273192012-07-30 14:41:20 -07006718# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006719 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006720 defined $stat &&
6721 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6722 my $min = $1;
6723 my $max = $7;
6724 if ($min eq $max) {
6725 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006726 "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 -07006727 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6728 $min > $max) {
6729 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006730 "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 -07006731 }
6732 }
6733
Joe Perches823b7942013-11-12 15:10:15 -08006734# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006735 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006736 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006737 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006738 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6739 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6740 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6741 my $lc = $stat =~ tr@\n@@;
6742 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006743 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006744 WARN("NAKED_SSCANF",
6745 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6746 }
6747
Joe Perchesafc819a2014-06-04 16:12:08 -07006748# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006749 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006750 defined $stat &&
6751 $line =~ /\bsscanf\b/) {
6752 my $lc = $stat =~ tr@\n@@;
6753 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006754 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006755 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6756 my $format = $6;
6757 my $count = $format =~ tr@%@%@;
6758 if ($count == 1 &&
6759 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6760 WARN("SSCANF_TO_KSTRTO",
6761 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6762 }
6763 }
6764 }
6765
Joe Perches70dc8a42013-09-11 14:23:58 -07006766# check for new externs in .h files.
6767 if ($realfile =~ /\.h$/ &&
6768 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006769 if (CHK("AVOID_EXTERNS",
6770 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006771 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006772 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006773 }
6774 }
6775
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006776# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006777 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006778 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006779 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006780 my $function_name = $1;
6781 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006782
6783 my $s = $stat;
6784 if (defined $cond) {
6785 substr($s, 0, length($cond), '');
6786 }
Kees Cookd8b44b52020-06-03 14:18:09 -07006787 if ($s =~ /^\s*;/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006788 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006789 WARN("AVOID_EXTERNS",
6790 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006791 }
6792
6793 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006794 WARN("FUNCTION_ARGUMENTS",
6795 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006796 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006797
6798 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6799 $stat =~ /^.\s*extern\s+/)
6800 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006801 WARN("AVOID_EXTERNS",
6802 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006803 }
6804
Joe Perchesa0ad7592017-07-10 15:52:19 -07006805# check for function declarations that have arguments without identifier names
6806 if (defined $stat &&
Kees Cookd8b44b52020-06-03 14:18:09 -07006807 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6808 $1 ne "void") {
6809 my $args = trim($1);
Joe Perchesca0d8922016-10-11 13:52:16 -07006810 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6811 my $arg = trim($1);
Kees Cookd8b44b52020-06-03 14:18:09 -07006812 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006813 WARN("FUNCTION_ARGUMENTS",
6814 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6815 }
6816 }
6817 }
6818
Joe Perchesa0ad7592017-07-10 15:52:19 -07006819# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006820 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006821 defined $stat &&
6822 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6823 $context_function = $1;
6824
6825# check for multiline function definition with misplaced open brace
6826 my $ok = 0;
6827 my $cnt = statement_rawlines($stat);
6828 my $herectx = $here . "\n";
6829 for (my $n = 0; $n < $cnt; $n++) {
6830 my $rl = raw_line($linenr, $n);
6831 $herectx .= $rl . "\n";
6832 $ok = 1 if ($rl =~ /^[ \+]\{/);
6833 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6834 last if $rl =~ /^[ \+].*\{/;
6835 }
6836 if (!$ok) {
6837 ERROR("OPEN_BRACE",
6838 "open brace '{' following function definitions go on the next line\n" . $herectx);
6839 }
6840 }
6841
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006842# checks for new __setup's
6843 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6844 my $name = $1;
6845
6846 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006847 CHK("UNDOCUMENTED_SETUP",
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07006848 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006849 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006850 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006851
Joe Perchese29a70f2019-03-07 16:28:35 -08006852# check for pointless casting of alloc functions
6853 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006854 WARN("UNNECESSARY_CASTS",
6855 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006856 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006857
Joe Perchesa640d252013-07-03 15:05:21 -07006858# alloc style
6859# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006860 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006861 $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 -07006862 CHK("ALLOC_SIZEOF_STRUCT",
6863 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6864 }
6865
Joe Perches60a55362014-06-04 16:12:07 -07006866# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006867 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006868 defined $stat &&
6869 $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 -07006870 my $oldfunc = $3;
6871 my $a1 = $4;
6872 my $a2 = $10;
6873 my $newfunc = "kmalloc_array";
6874 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006875 my $r1 = $a1;
6876 my $r2 = $a2;
6877 if ($a1 =~ /^sizeof\s*\S/) {
6878 $r1 = $a2;
6879 $r2 = $a1;
6880 }
6881 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6882 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006883 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006884 my $herectx = get_stat_here($linenr, $cnt, $here);
6885
Joe Perches60a55362014-06-04 16:12:07 -07006886 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006887 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6888 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006889 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006890 $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 -07006891 }
6892 }
6893 }
6894
Joe Perches972fdea2013-04-29 16:18:12 -07006895# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006896 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006897 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6898 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006899 WARN("KREALLOC_ARG_REUSE",
6900 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6901 }
6902
Joe Perches5ce59ae2013-02-21 16:44:18 -08006903# check for alloc argument mismatch
6904 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6905 WARN("ALLOC_ARRAY_ARGS",
6906 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6907 }
6908
Joe Perchescaf2a542011-01-12 16:59:56 -08006909# check for multiple semicolons
6910 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006911 if (WARN("ONE_SEMICOLON",
6912 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6913 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006914 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006915 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006916 }
6917
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006918# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6919 if ($realfile !~ m@^include/uapi/@ &&
6920 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006921 my $ull = "";
6922 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6923 if (CHK("BIT_MACRO",
6924 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6925 $fix) {
6926 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6927 }
6928 }
6929
Joe Perches50161262020-08-11 18:35:07 -07006930# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006931 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
Joe Perches50161262020-08-11 18:35:07 -07006932 WARN("IS_ENABLED_CONFIG",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006933 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
Joe Perches50161262020-08-11 18:35:07 -07006934 }
6935
Joe Perches2d632742016-05-20 17:04:00 -07006936# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006937 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 -07006938 my $config = $1;
6939 if (WARN("PREFER_IS_ENABLED",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006940 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
Joe Perches2d632742016-05-20 17:04:00 -07006941 $fix) {
6942 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6943 }
6944 }
6945
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006946# check for /* fallthrough */ like comment, prefer fallthrough;
6947 my @fallthroughs = (
6948 'fallthrough',
6949 '@fallthrough@',
6950 'lint -fallthrough[ \t]*',
6951 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6952 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6953 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6954 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6955 );
6956 if ($raw_comment ne '') {
6957 foreach my $ft (@fallthroughs) {
6958 if ($raw_comment =~ /$ft/) {
6959 my $msg_level = \&WARN;
6960 $msg_level = \&CHK if ($file);
6961 &{$msg_level}("PREFER_FALLTHROUGH",
6962 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6963 last;
6964 }
6965 }
6966 }
6967
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006968# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006969 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006970 defined $stat &&
6971 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006972 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006973 my $herectx = get_stat_here($linenr, $cnt, $here);
6974
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006975 WARN("DEFAULT_NO_BREAK",
6976 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006977 }
6978
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006979# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006980 if ($line =~ /\b__FUNCTION__\b/) {
6981 if (WARN("USE_FUNC",
6982 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6983 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006984 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006985 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006986 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006987
Joe Perches62ec8182015-02-13 14:38:18 -08006988# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6989 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6990 ERROR("DATE_TIME",
6991 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6992 }
6993
Joe Perches2c924882012-03-23 15:02:20 -07006994# check for use of yield()
6995 if ($line =~ /\byield\s*\(\s*\)/) {
6996 WARN("YIELD",
6997 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6998 }
6999
Joe Perches179f8f42013-07-03 15:05:30 -07007000# check for comparisons against true and false
7001 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
7002 my $lead = $1;
7003 my $arg = $2;
7004 my $test = $3;
7005 my $otype = $4;
7006 my $trail = $5;
7007 my $op = "!";
7008
7009 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
7010
7011 my $type = lc($otype);
7012 if ($type =~ /^(?:true|false)$/) {
7013 if (("$test" eq "==" && "$type" eq "true") ||
7014 ("$test" eq "!=" && "$type" eq "false")) {
7015 $op = "";
7016 }
7017
7018 CHK("BOOL_COMPARISON",
7019 "Using comparison to $otype is error prone\n" . $herecurr);
7020
7021## maybe suggesting a correct construct would better
7022## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
7023
7024 }
7025 }
7026
Thomas Gleixner4882720b2010-09-07 14:34:01 +00007027# check for semaphores initialized locked
7028 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007029 WARN("CONSIDER_COMPLETION",
7030 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07007031 }
Joe Perches6712d852012-03-23 15:02:20 -07007032
Joe Perches67d0a072011-10-31 17:13:10 -07007033# recommend kstrto* over simple_strto* and strict_strto*
7034 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007035 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07007036 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07007037 }
Joe Perches6712d852012-03-23 15:02:20 -07007038
Fabian Frederickae3ccc42014-06-04 16:12:10 -07007039# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07007040 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007041 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07007042 "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 -07007043 }
Joe Perches6712d852012-03-23 15:02:20 -07007044
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08007045# check for spin_is_locked(), suggest lockdep instead
7046 if ($line =~ /\bspin_is_locked\(/) {
7047 WARN("USE_LOCKDEP",
7048 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
7049 }
7050
Joe Perches9189c7e2018-09-07 15:26:18 -07007051# check for deprecated apis
7052 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
7053 my $deprecated_api = $1;
7054 my $new_api = $deprecated_apis{$deprecated_api};
7055 WARN("DEPRECATED_API",
7056 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
7057 }
7058
Joe Perches0f3c5aa2015-02-13 14:39:05 -08007059# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07007060# and avoid what seem like struct definitions 'struct foo {'
Quentin Monnetced69da12020-08-11 18:35:13 -07007061 if (defined($const_structs) &&
7062 $line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07007063 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007064 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07007065 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08007066 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07007067
7068# use of NR_CPUS is usually wrong
7069# ignore definitions of NR_CPUS and usage to define arrays as likely right
Peng Wang35cdcbf2021-02-25 17:21:44 -08007070# ignore designated initializers using NR_CPUS
Andy Whitcroft773647a2008-03-28 14:15:58 -07007071 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07007072 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7073 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07007074 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7075 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
Peng Wang35cdcbf2021-02-25 17:21:44 -08007076 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
7077 $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07007078 {
Joe Perches000d1cc12011-07-25 17:13:25 -07007079 WARN("NR_CPUS",
7080 "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 -07007081 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07007082
Joe Perches52ea8502013-11-12 15:10:09 -08007083# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
7084 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
7085 ERROR("DEFINE_ARCH_HAS",
7086 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
7087 }
7088
Joe Perchesacd93622015-02-13 14:38:38 -08007089# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07007090 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08007091 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7092 WARN("LIKELY_MISUSE",
7093 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7094 }
7095
Denis Efremovde3f1862019-09-25 16:49:25 -07007096# nested likely/unlikely calls
7097 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7098 WARN("LIKELY_MISUSE",
7099 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7100 }
7101
Andy Whitcroft691d77b2009-01-06 14:41:16 -08007102# whine mightly about in_atomic
7103 if ($line =~ /\bin_atomic\s*\(/) {
7104 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007105 ERROR("IN_ATOMIC",
7106 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08007107 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007108 WARN("IN_ATOMIC",
7109 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08007110 }
7111 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01007112
7113# check for lockdep_set_novalidate_class
7114 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
7115 $line =~ /__lockdep_no_validate__\s*\)/ ) {
7116 if ($realfile !~ m@^kernel/lockdep@ &&
7117 $realfile !~ m@^include/linux/lockdep@ &&
7118 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007119 ERROR("LOCKDEP",
7120 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01007121 }
7122 }
Dave Jones88f88312011-01-12 16:59:59 -08007123
Joe Perchesb392c642015-04-16 12:44:16 -07007124 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7125 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007126 WARN("EXPORTED_WORLD_WRITABLE",
7127 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08007128 }
Joe Perches24358802014-04-03 14:49:13 -07007129
Joe Perches00180462018-02-06 15:38:55 -08007130# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
7131# and whether or not function naming is typical and if
7132# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07007133 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08007134 defined $stat &&
7135 $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*\)/) {
7136 my $var = $1;
7137 my $perms = $2;
7138 my $show = $3;
7139 my $store = $4;
7140 my $octal_perms = perms_to_octal($perms);
7141 if ($show =~ /^${var}_show$/ &&
7142 $store =~ /^${var}_store$/ &&
7143 $octal_perms eq "0644") {
7144 if (WARN("DEVICE_ATTR_RW",
7145 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
7146 $fix) {
7147 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
7148 }
7149 } elsif ($show =~ /^${var}_show$/ &&
7150 $store =~ /^NULL$/ &&
7151 $octal_perms eq "0444") {
7152 if (WARN("DEVICE_ATTR_RO",
7153 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
7154 $fix) {
7155 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
7156 }
7157 } elsif ($show =~ /^NULL$/ &&
7158 $store =~ /^${var}_store$/ &&
7159 $octal_perms eq "0200") {
7160 if (WARN("DEVICE_ATTR_WO",
7161 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
7162 $fix) {
7163 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
7164 }
7165 } elsif ($octal_perms eq "0644" ||
7166 $octal_perms eq "0444" ||
7167 $octal_perms eq "0200") {
7168 my $newshow = "$show";
7169 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
7170 my $newstore = $store;
7171 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
7172 my $rename = "";
7173 if ($show ne $newshow) {
7174 $rename .= " '$show' to '$newshow'";
7175 }
7176 if ($store ne $newstore) {
7177 $rename .= " '$store' to '$newstore'";
7178 }
7179 WARN("DEVICE_ATTR_FUNCTIONS",
7180 "Consider renaming function(s)$rename\n" . $herecurr);
7181 } else {
7182 WARN("DEVICE_ATTR_PERMS",
7183 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
7184 }
7185 }
7186
Joe Perches515a2352014-04-03 14:49:24 -07007187# Mode permission misuses where it seems decimal should be octal
7188# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08007189# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
7190# specific definition of not visible in sysfs.
7191# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
7192# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07007193 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07007194 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07007195 $line =~ /$mode_perms_search/) {
7196 foreach my $entry (@mode_permission_funcs) {
7197 my $func = $entry->[0];
7198 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07007199
Joe Perches459cf0a2016-10-11 13:52:19 -07007200 my $lc = $stat =~ tr@\n@@;
7201 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07007202 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07007203
Joe Perches515a2352014-04-03 14:49:24 -07007204 my $skip_args = "";
7205 if ($arg_pos > 1) {
7206 $arg_pos--;
7207 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
7208 }
Joe Perchesf90774e2016-10-11 13:51:47 -07007209 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07007210 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07007211 my $val = $1;
7212 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08007213 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
7214 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
7215 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07007216 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07007217 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07007218 }
7219 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08007220 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07007221 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07007222 }
Joe Perches24358802014-04-03 14:49:13 -07007223 }
7224 }
7225 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007226
Joe Perches459cf0a2016-10-11 13:52:19 -07007227# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07007228 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08007229 my $oval = $1;
7230 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07007231 if (WARN("SYMBOLIC_PERMS",
7232 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7233 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08007234 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07007235 }
7236 }
7237
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007238# validate content of MODULE_LICENSE against list from include/linux/module.h
7239 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
7240 my $extracted_string = get_quoted_string($line, $rawline);
7241 my $valid_licenses = qr{
7242 GPL|
7243 GPL\ v2|
7244 GPL\ and\ additional\ rights|
7245 Dual\ BSD/GPL|
7246 Dual\ MIT/GPL|
7247 Dual\ MPL/GPL|
7248 Proprietary
7249 }x;
7250 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
7251 WARN("MODULE_LICENSE",
7252 "unknown module license " . $extracted_string . "\n" . $herecurr);
7253 }
7254 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07007255
7256# check for sysctl duplicate constants
7257 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
7258 WARN("DUPLICATED_SYSCTL_CONST",
7259 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
7260 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007261 }
7262
7263 # If we have no input at all, then there is nothing to report on
7264 # so just keep quiet.
7265 if ($#rawlines == -1) {
7266 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007267 }
7268
Andy Whitcroft8905a672007-11-28 16:21:06 -08007269 # In mailback mode only produce a report in the negative, for
7270 # things that appear to be patches.
7271 if ($mailback && ($clean == 1 || !$is_patch)) {
7272 exit(0);
7273 }
7274
Dwaipayan Raye73d2712020-12-15 20:44:56 -08007275 # This is not a patch, and we are in 'no-patch' mode so
Andy Whitcroft8905a672007-11-28 16:21:06 -08007276 # just keep quiet.
7277 if (!$chk_patch && !$is_patch) {
7278 exit(0);
7279 }
7280
Stafford Hornea08ffbe2017-10-03 16:16:51 -07007281 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007282 ERROR("NOT_UNIFIED_DIFF",
7283 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007284 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007285 if ($is_patch && $has_commit_log && $chk_signoff) {
7286 if ($signoff == 0) {
7287 ERROR("MISSING_SIGN_OFF",
7288 "Missing Signed-off-by: line(s)\n");
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07007289 } elsif ($authorsignoff != 1) {
7290 # authorsignoff values:
7291 # 0 -> missing sign off
7292 # 1 -> sign off identical
7293 # 2 -> names and addresses match, comments mismatch
7294 # 3 -> addresses match, names different
7295 # 4 -> names match, addresses different
7296 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
7297
7298 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
7299
7300 if ($authorsignoff == 0) {
7301 ERROR("NO_AUTHOR_SIGN_OFF",
7302 "Missing Signed-off-by: line by nominal patch author '$author'\n");
7303 } elsif ($authorsignoff == 2) {
7304 CHK("FROM_SIGN_OFF_MISMATCH",
7305 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7306 } elsif ($authorsignoff == 3) {
7307 WARN("FROM_SIGN_OFF_MISMATCH",
7308 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7309 } elsif ($authorsignoff == 4) {
7310 WARN("FROM_SIGN_OFF_MISMATCH",
7311 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7312 } elsif ($authorsignoff == 5) {
7313 WARN("FROM_SIGN_OFF_MISMATCH",
7314 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7315 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007316 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007317 }
7318
Andy Whitcroft8905a672007-11-28 16:21:06 -08007319 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007320 if ($summary && !($clean == 1 && $quiet == 1)) {
7321 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08007322 print "total: $cnt_error errors, $cnt_warn warnings, " .
7323 (($check)? "$cnt_chk checks, " : "") .
7324 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07007325 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08007326
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007327 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07007328 # If there were any defects found and not already fixing them
7329 if (!$clean and !$fix) {
7330 print << "EOM"
7331
7332NOTE: For some of the reported defects, checkpatch may be able to
7333 mechanically convert to the typical style using --fix or --fix-inplace.
7334EOM
7335 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007336 # If there were whitespace errors which cleanpatch can fix
7337 # then suggest that.
7338 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07007339 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07007340 print << "EOM"
7341
7342NOTE: Whitespace errors detected.
7343 You may wish to use scripts/cleanpatch or scripts/cleanfile
7344EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007345 }
7346 }
7347
Joe Perchesd752fcc2014-08-06 16:11:05 -07007348 if ($clean == 0 && $fix &&
7349 ("@rawlines" ne "@fixed" ||
7350 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08007351 my $newfile = $filename;
7352 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07007353 my $linecount = 0;
7354 my $f;
7355
Joe Perchesd752fcc2014-08-06 16:11:05 -07007356 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7357
Joe Perches3705ce52013-07-03 15:05:31 -07007358 open($f, '>', $newfile)
7359 or die "$P: Can't open $newfile for write\n";
7360 foreach my $fixed_line (@fixed) {
7361 $linecount++;
7362 if ($file) {
7363 if ($linecount > 3) {
7364 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07007365 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07007366 }
7367 } else {
7368 print $f $fixed_line . "\n";
7369 }
7370 }
7371 close($f);
7372
7373 if (!$quiet) {
7374 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07007375
Joe Perches3705ce52013-07-03 15:05:31 -07007376Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
7377
7378Do _NOT_ trust the results written to this file.
7379Do _NOT_ submit these changes without inspecting them for correctness.
7380
7381This EXPERIMENTAL file is simply a convenience to help rewrite patches.
7382No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07007383EOM
7384 }
7385 }
7386
Joe Perchesd8469f12015-06-25 15:03:00 -07007387 if ($quiet == 0) {
7388 print "\n";
7389 if ($clean == 1) {
7390 print "$vname has no obvious style problems and is ready for submission.\n";
7391 } else {
7392 print "$vname has style problems, please review.\n";
7393 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007394 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007395 return $clean;
7396}