blob: eabd2d5467b156af25fb9724fdbd09e054323129 [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 Perches03f1df72010-10-26 14:23:16 -0700385 __percpu|
386 __nocast|
387 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200388 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700389 __packed__|
390 __packed2__|
391 __naked|
392 __maybe_unused|
393 __always_unused|
394 __noreturn|
395 __used|
396 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800397 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700398 __noclone|
399 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700400 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700401 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700402 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700403 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700404 ____cacheline_aligned|
405 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800406 ____cacheline_internodealigned_in_smp|
407 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700408 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700409our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700410our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700411our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
412our $Lval = qr{$Ident(?:$Member)*};
413
Joe Perches95e2c602013-07-03 15:05:20 -0700414our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
415our $Binary = qr{(?i)0b[01]+$Int_type?};
416our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
417our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700418our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800419our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800420our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
421our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
422our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800423our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700424our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800425our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700426our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700427our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700428our $Operators = qr{
429 <=|>=|==|!=|
430 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700431 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700432 }x;
433
Joe Perches91cb5192014-04-03 14:49:32 -0700434our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
435
Joe Perchesab7e23f2015-04-16 12:44:22 -0700436our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800437our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700438our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700439our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800440our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700441our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800442our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700443our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800444
Joe Perches15662b32011-10-31 17:13:12 -0700445our $NON_ASCII_UTF8 = qr{
446 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700447 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
448 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
449 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
450 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
451 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
452 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
453}x;
454
Joe Perches15662b32011-10-31 17:13:12 -0700455our $UTF8 = qr{
456 [\x09\x0A\x0D\x20-\x7E] # ASCII
457 | $NON_ASCII_UTF8
458}x;
459
Joe Perchese6176fa2015-06-25 15:02:49 -0700460our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800461our $typeOtherOSTypedefs = qr{(?x:
462 u_(?:char|short|int|long) | # bsd
463 u(?:nchar|short|int|long) # sysv
464)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700465our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700466 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700467 atomic_t
468)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700469our $typeTypedefs = qr{(?x:
470 $typeC99Typedefs\b|
471 $typeOtherOSTypedefs\b|
472 $typeKernelTypedefs\b
473)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700474
Joe Perches6d32f7a2015-11-06 16:31:37 -0800475our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
476
Joe Perches691e6692010-03-05 13:43:51 -0800477our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800478 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700479 (?:[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 -0800480 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700481 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700482 panic|
Joe Perches06668722013-11-12 15:10:07 -0800483 MODULE_[A-Z_]+|
484 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800485)};
486
Joe Perchese29a70f2019-03-07 16:28:35 -0800487our $allocFunctions = qr{(?x:
488 (?:(?:devm_)?
489 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
490 kstrdup(?:_const)? |
491 kmemdup(?:_nul)?) |
Christophe JAILLET461e1562020-04-20 18:13:58 -0700492 (?:\w+)?alloc_skb(?:_ip_align)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800493 # dev_alloc_skb/netdev_alloc_skb, et al
494 dma_alloc_coherent
495)};
496
Joe Perches20112472011-07-25 17:13:23 -0700497our $signature_tags = qr{(?xi:
498 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800499 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700500 Acked-by:|
501 Tested-by:|
502 Reviewed-by:|
503 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700504 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700505 To:|
506 Cc:
507)};
508
Aditya Srivastava831242a2020-12-15 20:45:12 -0800509sub edit_distance_min {
510 my (@arr) = @_;
511 my $len = scalar @arr;
512 if ((scalar @arr) < 1) {
513 # if underflow, return
514 return;
515 }
516 my $min = $arr[0];
517 for my $i (0 .. ($len-1)) {
518 if ($arr[$i] < $min) {
519 $min = $arr[$i];
520 }
521 }
522 return $min;
523}
524
525sub get_edit_distance {
526 my ($str1, $str2) = @_;
527 $str1 = lc($str1);
528 $str2 = lc($str2);
529 $str1 =~ s/-//g;
530 $str2 =~ s/-//g;
531 my $len1 = length($str1);
532 my $len2 = length($str2);
533 # two dimensional array storing minimum edit distance
534 my @distance;
535 for my $i (0 .. $len1) {
536 for my $j (0 .. $len2) {
537 if ($i == 0) {
538 $distance[$i][$j] = $j;
539 } elsif ($j == 0) {
540 $distance[$i][$j] = $i;
541 } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
542 $distance[$i][$j] = $distance[$i - 1][$j - 1];
543 } else {
544 my $dist1 = $distance[$i][$j - 1]; #insert distance
545 my $dist2 = $distance[$i - 1][$j]; # remove
546 my $dist3 = $distance[$i - 1][$j - 1]; #replace
547 $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
548 }
549 }
550 }
551 return $distance[$len1][$len2];
552}
553
554sub find_standard_signature {
555 my ($sign_off) = @_;
556 my @standard_signature_tags = (
557 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
558 'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
559 );
560 foreach my $signature (@standard_signature_tags) {
561 return $signature if (get_edit_distance($sign_off, $signature) <= 2);
562 }
563
564 return "";
565}
566
Joe Perches18130872014-08-06 16:11:22 -0700567our @typeListMisordered = (
568 qr{char\s+(?:un)?signed},
569 qr{int\s+(?:(?:un)?signed\s+)?short\s},
570 qr{int\s+short(?:\s+(?:un)?signed)},
571 qr{short\s+int(?:\s+(?:un)?signed)},
572 qr{(?:un)?signed\s+int\s+short},
573 qr{short\s+(?:un)?signed},
574 qr{long\s+int\s+(?:un)?signed},
575 qr{int\s+long\s+(?:un)?signed},
576 qr{long\s+(?:un)?signed\s+int},
577 qr{int\s+(?:un)?signed\s+long},
578 qr{int\s+(?:un)?signed},
579 qr{int\s+long\s+long\s+(?:un)?signed},
580 qr{long\s+long\s+int\s+(?:un)?signed},
581 qr{long\s+long\s+(?:un)?signed\s+int},
582 qr{long\s+long\s+(?:un)?signed},
583 qr{long\s+(?:un)?signed},
584);
585
Andy Whitcroft8905a672007-11-28 16:21:06 -0800586our @typeList = (
587 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700588 qr{(?:(?:un)?signed\s+)?char},
589 qr{(?:(?:un)?signed\s+)?short\s+int},
590 qr{(?:(?:un)?signed\s+)?short},
591 qr{(?:(?:un)?signed\s+)?int},
592 qr{(?:(?:un)?signed\s+)?long\s+int},
593 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
594 qr{(?:(?:un)?signed\s+)?long\s+long},
595 qr{(?:(?:un)?signed\s+)?long},
596 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800597 qr{float},
598 qr{double},
599 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800600 qr{struct\s+$Ident},
601 qr{union\s+$Ident},
602 qr{enum\s+$Ident},
603 qr{${Ident}_t},
604 qr{${Ident}_handler},
605 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700606 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800607);
Joe Perches938224b2016-01-20 14:59:15 -0800608
609our $C90_int_types = qr{(?x:
610 long\s+long\s+int\s+(?:un)?signed|
611 long\s+long\s+(?:un)?signed\s+int|
612 long\s+long\s+(?:un)?signed|
613 (?:(?:un)?signed\s+)?long\s+long\s+int|
614 (?:(?:un)?signed\s+)?long\s+long|
615 int\s+long\s+long\s+(?:un)?signed|
616 int\s+(?:(?:un)?signed\s+)?long\s+long|
617
618 long\s+int\s+(?:un)?signed|
619 long\s+(?:un)?signed\s+int|
620 long\s+(?:un)?signed|
621 (?:(?:un)?signed\s+)?long\s+int|
622 (?:(?:un)?signed\s+)?long|
623 int\s+long\s+(?:un)?signed|
624 int\s+(?:(?:un)?signed\s+)?long|
625
626 int\s+(?:un)?signed|
627 (?:(?:un)?signed\s+)?int
628)};
629
Alex Dowad485ff232015-06-25 15:02:52 -0700630our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700631our @typeListWithAttr = (
632 @typeList,
633 qr{struct\s+$InitAttribute\s+$Ident},
634 qr{union\s+$InitAttribute\s+$Ident},
635);
636
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700637our @modifierList = (
638 qr{fastcall},
639);
Alex Dowad485ff232015-06-25 15:02:52 -0700640our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800641
Joe Perches24358802014-04-03 14:49:13 -0700642our @mode_permission_funcs = (
643 ["module_param", 3],
644 ["module_param_(?:array|named|string)", 4],
645 ["module_param_array_named", 5],
646 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
647 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700648 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
649 ["IIO_DEV_ATTR_[A-Z_]+", 1],
650 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
651 ["SENSOR_TEMPLATE(?:_2|)", 3],
652 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700653);
654
Joe Perches1a3dcf22020-08-11 18:35:16 -0700655my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
656
Joe Perches515a2352014-04-03 14:49:24 -0700657#Create a search pattern for all these functions to speed up a loop below
658our $mode_perms_search = "";
659foreach my $entry (@mode_permission_funcs) {
660 $mode_perms_search .= '|' if ($mode_perms_search ne "");
661 $mode_perms_search .= $entry->[0];
662}
Joe Perches00180462018-02-06 15:38:55 -0800663$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700664
Joe Perches9189c7e2018-09-07 15:26:18 -0700665our %deprecated_apis = (
666 "synchronize_rcu_bh" => "synchronize_rcu",
667 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
668 "call_rcu_bh" => "call_rcu",
669 "rcu_barrier_bh" => "rcu_barrier",
670 "synchronize_sched" => "synchronize_rcu",
671 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
672 "call_rcu_sched" => "call_rcu",
673 "rcu_barrier_sched" => "rcu_barrier",
674 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
675 "cond_synchronize_sched" => "cond_synchronize_rcu",
676);
677
678#Create a search pattern for all these strings to speed up a loop below
679our $deprecated_apis_search = "";
680foreach my $entry (keys %deprecated_apis) {
681 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
682 $deprecated_apis_search .= $entry;
683}
684$deprecated_apis_search = "(?:${deprecated_apis_search})";
685
Joe Perchesb392c642015-04-16 12:44:16 -0700686our $mode_perms_world_writable = qr{
687 S_IWUGO |
688 S_IWOTH |
689 S_IRWXUGO |
690 S_IALLUGO |
691 0[0-7][0-7][2367]
692}x;
693
Joe Perchesf90774e2016-10-11 13:51:47 -0700694our %mode_permission_string_types = (
695 "S_IRWXU" => 0700,
696 "S_IRUSR" => 0400,
697 "S_IWUSR" => 0200,
698 "S_IXUSR" => 0100,
699 "S_IRWXG" => 0070,
700 "S_IRGRP" => 0040,
701 "S_IWGRP" => 0020,
702 "S_IXGRP" => 0010,
703 "S_IRWXO" => 0007,
704 "S_IROTH" => 0004,
705 "S_IWOTH" => 0002,
706 "S_IXOTH" => 0001,
707 "S_IRWXUGO" => 0777,
708 "S_IRUGO" => 0444,
709 "S_IWUGO" => 0222,
710 "S_IXUGO" => 0111,
711);
712
713#Create a search pattern for all these strings to speed up a loop below
714our $mode_perms_string_search = "";
715foreach my $entry (keys %mode_permission_string_types) {
716 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
717 $mode_perms_string_search .= $entry;
718}
Joe Perches00180462018-02-06 15:38:55 -0800719our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
720our $multi_mode_perms_string_search = qr{
721 ${single_mode_perms_string_search}
722 (?:\s*\|\s*${single_mode_perms_string_search})*
723}x;
724
725sub perms_to_octal {
726 my ($string) = @_;
727
728 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
729
730 my $val = "";
731 my $oval = "";
732 my $to = 0;
733 my $curpos = 0;
734 my $lastpos = 0;
735 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
736 $curpos = pos($string);
737 my $match = $2;
738 my $omatch = $1;
739 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
740 $lastpos = $curpos;
741 $to |= $mode_permission_string_types{$match};
742 $val .= '\s*\|\s*' if ($val ne "");
743 $val .= $match;
744 $oval .= $omatch;
745 }
746 $oval =~ s/^\s*\|\s*//;
747 $oval =~ s/\s*\|\s*$//;
748 return sprintf("%04o", $to);
749}
Joe Perchesf90774e2016-10-11 13:51:47 -0700750
Wolfram Sang7840a942010-08-09 17:20:57 -0700751our $allowed_asm_includes = qr{(?x:
752 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700753 memory|
754 time|
755 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700756)};
757# memory.h: ARM has a custom one
758
Kees Cook66b47b42014-10-13 15:51:57 -0700759# Load common spelling mistakes and build regular expression list.
760my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700761my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700762
Joe Perches36061e32014-12-10 15:51:43 -0800763if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800764 while (<$spelling>) {
765 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700766
Joe Perches36061e32014-12-10 15:51:43 -0800767 $line =~ s/\s*\n?$//g;
768 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700769
Joe Perches36061e32014-12-10 15:51:43 -0800770 next if ($line =~ m/^\s*#/);
771 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700772
Joe Perches36061e32014-12-10 15:51:43 -0800773 my ($suspect, $fix) = split(/\|\|/, $line);
774
Joe Perches36061e32014-12-10 15:51:43 -0800775 $spelling_fix{$suspect} = $fix;
776 }
777 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800778} else {
779 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700780}
Kees Cook66b47b42014-10-13 15:51:57 -0700781
Joe Perchesebfd7d62015-04-16 12:44:14 -0700782if ($codespell) {
783 if (open(my $spelling, '<', $codespellfile)) {
784 while (<$spelling>) {
785 my $line = $_;
786
787 $line =~ s/\s*\n?$//g;
788 $line =~ s/^\s*//g;
789
790 next if ($line =~ m/^\s*#/);
791 next if ($line =~ m/^\s*$/);
792 next if ($line =~ m/, disabled/i);
793
794 $line =~ s/,.*$//;
795
796 my ($suspect, $fix) = split(/->/, $line);
797
798 $spelling_fix{$suspect} = $fix;
799 }
800 close($spelling);
801 } else {
802 warn "No codespell typos will be found - file '$codespellfile': $!\n";
803 }
804}
805
806$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
807
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700808sub read_words {
809 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700810
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700811 if (open(my $words, '<', $file)) {
812 while (<$words>) {
813 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700814
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700815 $line =~ s/\s*\n?$//g;
816 $line =~ s/^\s*//g;
817
818 next if ($line =~ m/^\s*#/);
819 next if ($line =~ m/^\s*$/);
820 if ($line =~ /\s/) {
821 print("$file: '$line' invalid - ignored\n");
822 next;
823 }
824
Quentin Monnetced69da12020-08-11 18:35:13 -0700825 $$wordsRef .= '|' if (defined $$wordsRef);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700826 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700827 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700828 close($file);
829 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700830 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700831
832 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700833}
834
Quentin Monnetced69da12020-08-11 18:35:13 -0700835my $const_structs;
836if (show_type("CONST_STRUCT")) {
837 read_words(\$const_structs, $conststructsfile)
838 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
839}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700840
Quentin Monnetced69da12020-08-11 18:35:13 -0700841if (defined($typedefsfile)) {
842 my $typeOtherTypedefs;
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700843 read_words(\$typeOtherTypedefs, $typedefsfile)
844 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
Quentin Monnetced69da12020-08-11 18:35:13 -0700845 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700846}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700847
Andy Whitcroft8905a672007-11-28 16:21:06 -0800848sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700849 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
850 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700851 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700852 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700853 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700854 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700855 (?:$typeTypedefs\b)|
856 (?:${all}\b)
857 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800858 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700859 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800860 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800861 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700862 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700863 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800864 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700865 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800866 }x;
Joe Perches18130872014-08-06 16:11:22 -0700867 $NonptrTypeMisordered = qr{
868 (?:$Modifier\s+|const\s+)*
869 (?:
870 (?:${Misordered}\b)
871 )
872 (?:\s+$Modifier|\s+const)*
873 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700874 $NonptrTypeWithAttr = qr{
875 (?:$Modifier\s+|const\s+)*
876 (?:
877 (?:typeof|__typeof__)\s*\([^\)]*\)|
878 (?:$typeTypedefs\b)|
879 (?:${allWithAttr}\b)
880 )
881 (?:\s+$Modifier|\s+const)*
882 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800883 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700884 $NonptrType
Antonio Borneo7b184962020-04-06 20:11:04 -0700885 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700886 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800887 }x;
Joe Perches18130872014-08-06 16:11:22 -0700888 $TypeMisordered = qr{
889 $NonptrTypeMisordered
Antonio Borneo7b184962020-04-06 20:11:04 -0700890 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Perches18130872014-08-06 16:11:22 -0700891 (?:\s+$Inline|\s+$Modifier)*
892 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700893 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700894 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800895}
896build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700897
Joe Perches7d2367a2011-07-25 17:13:22 -0700898our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700899
900# Using $balanced_parens, $LvalOrFunc, or $FuncArg
901# requires at least perl version v5.10.0
902# Any use must be runtime checked with $^V
903
904our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700905our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800906our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700907
Joe Perchesf8422302014-08-06 16:11:31 -0700908our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700909 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700910 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700911 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700912)};
913
Aditya Srivastava8d0325c2020-12-15 20:44:24 -0800914our %allow_repeated_words = (
915 add => '',
916 added => '',
917 bad => '',
918 be => '',
919);
920
Joe Perches7d2367a2011-07-25 17:13:22 -0700921sub deparenthesize {
922 my ($string) = @_;
923 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700924
925 while ($string =~ /^\s*\(.*\)\s*$/) {
926 $string =~ s@^\s*\(\s*@@;
927 $string =~ s@\s*\)\s*$@@;
928 }
929
Joe Perches7d2367a2011-07-25 17:13:22 -0700930 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700931
Joe Perches7d2367a2011-07-25 17:13:22 -0700932 return $string;
933}
934
Joe Perches34456862013-07-03 15:05:34 -0700935sub seed_camelcase_file {
936 my ($file) = @_;
937
938 return if (!(-f $file));
939
940 local $/;
941
942 open(my $include_file, '<', "$file")
943 or warn "$P: Can't read '$file' $!\n";
944 my $text = <$include_file>;
945 close($include_file);
946
947 my @lines = split('\n', $text);
948
949 foreach my $line (@lines) {
950 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
951 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
952 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800953 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
954 $camelcase{$1} = 1;
955 } 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 -0700956 $camelcase{$1} = 1;
957 }
958 }
959}
960
Joe Perchescd28b112019-12-04 16:52:09 -0800961our %maintained_status = ();
962
Joe Perches85b0ee12016-10-11 13:51:44 -0700963sub is_maintained_obsolete {
964 my ($filename) = @_;
965
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800966 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700967
Joe Perchescd28b112019-12-04 16:52:09 -0800968 if (!exists($maintained_status{$filename})) {
969 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
970 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700971
Joe Perchescd28b112019-12-04 16:52:09 -0800972 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700973}
974
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700975sub is_SPDX_License_valid {
976 my ($license) = @_;
977
Joe Perches0f7f6352020-10-24 16:59:04 -0700978 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700979
Joe Perches56294112018-08-21 21:58:04 -0700980 my $root_path = abs_path($root);
981 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700982 return 0 if ($status ne "");
983 return 1;
984}
985
Joe Perches34456862013-07-03 15:05:34 -0700986my $camelcase_seeded = 0;
987sub seed_camelcase_includes {
988 return if ($camelcase_seeded);
989
990 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700991 my $camelcase_cache = "";
992 my @include_files = ();
993
994 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700995
Joe Perches0f7f6352020-10-24 16:59:04 -0700996 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700997 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -0700998 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700999 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -07001000 } else {
Joe Perchesc707a812013-07-08 16:00:43 -07001001 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -07001002 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -07001003 @include_files = split('\n', $files);
1004 foreach my $file (@include_files) {
1005 my $date = POSIX::strftime("%Y%m%d%H%M",
1006 localtime((stat $file)[9]));
1007 $last_mod_date = $date if ($last_mod_date < $date);
1008 }
1009 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -07001010 }
Joe Perchesc707a812013-07-08 16:00:43 -07001011
1012 if ($camelcase_cache ne "" && -f $camelcase_cache) {
1013 open(my $camelcase_file, '<', "$camelcase_cache")
1014 or warn "$P: Can't read '$camelcase_cache' $!\n";
1015 while (<$camelcase_file>) {
1016 chomp;
1017 $camelcase{$_} = 1;
1018 }
1019 close($camelcase_file);
1020
1021 return;
1022 }
1023
Joe Perches0f7f6352020-10-24 16:59:04 -07001024 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -07001025 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -07001026 @include_files = split('\n', $files);
1027 }
1028
Joe Perches34456862013-07-03 15:05:34 -07001029 foreach my $file (@include_files) {
1030 seed_camelcase_file($file);
1031 }
Joe Perches351b2a12013-07-03 15:05:36 -07001032
Joe Perchesc707a812013-07-08 16:00:43 -07001033 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -07001034 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -07001035 open(my $camelcase_file, '>', "$camelcase_cache")
1036 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -07001037 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1038 print $camelcase_file ("$_\n");
1039 }
1040 close($camelcase_file);
1041 }
Joe Perches34456862013-07-03 15:05:34 -07001042}
1043
Joe Perchesf5f61322020-10-15 20:12:12 -07001044sub git_is_single_file {
1045 my ($filename) = @_;
1046
1047 return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1048
1049 my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1050 my $count = $output =~ tr/\n//;
1051 return $count eq 1 && $output =~ m{^${filename}$};
1052}
1053
Joe Perchesd311cd42014-08-06 16:10:57 -07001054sub git_commit_info {
1055 my ($commit, $id, $desc) = @_;
1056
Joe Perches0f7f6352020-10-24 16:59:04 -07001057 return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
Joe Perchesd311cd42014-08-06 16:10:57 -07001058
Joe Perchesdbbf8692019-09-25 16:46:52 -07001059 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -07001060 $output =~ s/^\s*//gm;
1061 my @lines = split("\n", $output);
1062
Joe Perches0d7835f2015-02-13 14:38:35 -08001063 return ($id, $desc) if ($#lines < 0);
1064
Sean Christopherson5a7f4452019-09-25 16:46:49 -07001065 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -07001066# Maybe one day convert this block of bash into something that returns
1067# all matching commit ids, but it's very slow...
1068#
1069# echo "checking commits $1..."
1070# git rev-list --remotes | grep -i "^$1" |
1071# while read line ; do
1072# git log --format='%H %s' -1 $line |
1073# echo "commit $(cut -c 1-12,41-)"
1074# done
1075 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07001076 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -07001077 } else {
1078 $id = substr($lines[0], 0, 12);
1079 $desc = substr($lines[0], 41);
1080 }
1081
1082 return ($id, $desc);
1083}
1084
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001085$chk_signoff = 0 if ($file);
1086
Andy Whitcroft00df3442007-06-08 13:47:06 -07001087my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001088my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001089my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001090my @fixed_inserted = ();
1091my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001092my $fixlinenr = -1;
1093
Du, Changbin4a593c32016-05-20 17:04:16 -07001094# If input is git commits, extract all commits from the commit expressions.
1095# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
Joe Perches0f7f6352020-10-24 16:59:04 -07001096die "$P: No git repository found\n" if ($git && !-e "$gitroot");
Du, Changbin4a593c32016-05-20 17:04:16 -07001097
1098if ($git) {
1099 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001100 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001101 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001102 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1103 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001104 } elsif ($commit_expr =~ m/\.\./) {
1105 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001106 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001107 $git_range = "-1 $commit_expr";
1108 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001109 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001110 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001111 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1112 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001113 my $sha1 = $1;
1114 my $subject = $2;
1115 unshift(@commits, $sha1);
1116 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001117 }
1118 }
1119 die "$P: no git commits after extraction!\n" if (@commits == 0);
1120 @ARGV = @commits;
1121}
1122
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001123my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001124$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001125for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001126 my $FILE;
Joe Perchesf5f61322020-10-15 20:12:12 -07001127 my $is_git_file = git_is_single_file($filename);
1128 my $oldfile = $file;
1129 $file = 1 if ($is_git_file);
Du, Changbin4a593c32016-05-20 17:04:16 -07001130 if ($git) {
1131 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1132 die "$P: $filename: git format-patch failed - $!\n";
1133 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001134 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001135 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001136 } elsif ($filename eq '-') {
1137 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001138 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001139 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001140 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001141 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001142 if ($filename eq '-') {
1143 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001144 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001145 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001146 } else {
1147 $vname = $filename;
1148 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001149 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001150 chomp;
1151 push(@rawlines, $_);
Geert Uytterhoevenc7f574d2020-06-04 16:50:43 -07001152 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001153 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001154 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001155
1156 if ($#ARGV > 0 && $quiet == 0) {
1157 print '-' x length($vname) . "\n";
1158 print "$vname\n";
1159 print '-' x length($vname) . "\n";
1160 }
1161
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001162 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001163 $exit = 1;
1164 }
1165 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001166 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001167 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001168 @fixed_inserted = ();
1169 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001170 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001171 @modifierListFile = ();
1172 @typeListFile = ();
1173 build_types();
Joe Perchesf5f61322020-10-15 20:12:12 -07001174 $file = $oldfile if ($is_git_file);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001175}
1176
Joe Perchesd8469f12015-06-25 15:03:00 -07001177if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001178 hash_show_words(\%use_type, "Used");
1179 hash_show_words(\%ignore_type, "Ignored");
1180
Joe Perches5b579802018-08-21 21:57:33 -07001181 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001182 print << "EOM"
1183
1184NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001185 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001186EOM
1187 }
1188 if ($exit) {
1189 print << "EOM"
1190
1191NOTE: If any of the errors are false positives, please report
1192 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1193EOM
1194 }
1195}
1196
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001197exit($exit);
1198
1199sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001200 my ($root) = @_;
1201
1202 my @tree_check = (
1203 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1204 "README", "Documentation", "arch", "include", "drivers",
1205 "fs", "init", "ipc", "kernel", "lib", "scripts",
1206 );
1207
1208 foreach my $check (@tree_check) {
1209 if (! -e $root . '/' . $check) {
1210 return 0;
1211 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001212 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001213 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001214}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001215
Joe Perches20112472011-07-25 17:13:23 -07001216sub parse_email {
1217 my ($formatted_email) = @_;
1218
1219 my $name = "";
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001220 my $quoted = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001221 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001222 my $address = "";
1223 my $comment = "";
1224
1225 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1226 $name = $1;
1227 $address = $2;
1228 $comment = $3 if defined $3;
1229 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1230 $address = $1;
1231 $comment = $2 if defined $2;
1232 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1233 $address = $1;
1234 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001235 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001236 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001237 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001238 $name =~ s/^\"|\"$//g;
1239 # If there's a name left after stripping spaces and
1240 # leading quotes, and the address doesn't have both
1241 # leading and trailing angle brackets, the address
1242 # is invalid. ie:
1243 # "joe smith joe@smith.com" bad
1244 # "joe smith <joe@smith.com" bad
1245 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1246 $name = "";
1247 $address = "";
1248 $comment = "";
1249 }
1250 }
1251
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001252 # Extract comments from names excluding quoted parts
1253 # "John D. (Doe)" - Do not extract
1254 if ($name =~ s/\"(.+)\"//) {
1255 $quoted = $1;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001256 }
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001257 while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1258 $name_comment .= trim($1);
1259 }
1260 $name =~ s/^[ \"]+|[ \"]+$//g;
1261 $name = trim("$quoted $name");
1262
Joe Perches3705ce52013-07-03 15:05:31 -07001263 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001264 $address =~ s/^\<|\>$//g;
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001265 $comment = trim($comment);
Joe Perches20112472011-07-25 17:13:23 -07001266
1267 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1268 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1269 $name = "\"$name\"";
1270 }
1271
Joe Perchesdfa05c22020-04-06 20:10:48 -07001272 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001273}
1274
1275sub format_email {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001276 my ($name, $name_comment, $address, $comment) = @_;
Joe Perches20112472011-07-25 17:13:23 -07001277
1278 my $formatted_email;
1279
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001280 $name =~ s/^[ \"]+|[ \"]+$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001281 $address = trim($address);
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001282 $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
Joe Perches20112472011-07-25 17:13:23 -07001283
1284 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1285 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1286 $name = "\"$name\"";
1287 }
1288
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001289 $name_comment = trim($name_comment);
1290 $name_comment = " $name_comment" if ($name_comment ne "");
1291 $comment = trim($comment);
1292 $comment = " $comment" if ($comment ne "");
1293
Joe Perches20112472011-07-25 17:13:23 -07001294 if ("$name" eq "") {
1295 $formatted_email = "$address";
1296 } else {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001297 $formatted_email = "$name$name_comment <$address>";
Joe Perches20112472011-07-25 17:13:23 -07001298 }
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001299 $formatted_email .= "$comment";
Joe Perches20112472011-07-25 17:13:23 -07001300 return $formatted_email;
1301}
1302
Joe Perchesdfa05c22020-04-06 20:10:48 -07001303sub reformat_email {
1304 my ($email) = @_;
1305
1306 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001307 return format_email($email_name, $name_comment, $email_address, $comment);
Joe Perchesdfa05c22020-04-06 20:10:48 -07001308}
1309
1310sub same_email_addresses {
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001311 my ($email1, $email2) = @_;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001312
1313 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1314 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1315
1316 return $email1_name eq $email2_name &&
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001317 $email1_address eq $email2_address &&
1318 $name1_comment eq $name2_comment &&
1319 $comment1 eq $comment2;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001320}
1321
Joe Perchesd311cd42014-08-06 16:10:57 -07001322sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001323 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001324
Joe Perchesbd474ca2014-08-06 16:11:10 -07001325 foreach my $path (split(/:/, $ENV{PATH})) {
1326 if (-e "$path/$bin") {
1327 return "$path/$bin";
1328 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001329 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001330
Joe Perchesbd474ca2014-08-06 16:11:10 -07001331 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001332}
1333
Joe Perches000d1cc12011-07-25 17:13:25 -07001334sub which_conf {
1335 my ($conf) = @_;
1336
1337 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1338 if (-e "$path/$conf") {
1339 return "$path/$conf";
1340 }
1341 }
1342
1343 return "";
1344}
1345
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001346sub expand_tabs {
1347 my ($str) = @_;
1348
1349 my $res = '';
1350 my $n = 0;
1351 for my $c (split(//, $str)) {
1352 if ($c eq "\t") {
1353 $res .= ' ';
1354 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001355 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001356 $res .= ' ';
1357 }
1358 next;
1359 }
1360 $res .= $c;
1361 $n++;
1362 }
1363
1364 return $res;
1365}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001366sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001367 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001368 return $res;
1369}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001370
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001371sub line_stats {
1372 my ($line) = @_;
1373
1374 # Drop the diff line leader and expand tabs
1375 $line =~ s/^.//;
1376 $line = expand_tabs($line);
1377
1378 # Pick the indent from the front of the line.
1379 my ($white) = ($line =~ /^(\s*)/);
1380
1381 return (length($line), length($white));
1382}
1383
Andy Whitcroft773647a2008-03-28 14:15:58 -07001384my $sanitise_quote = '';
1385
1386sub sanitise_line_reset {
1387 my ($in_comment) = @_;
1388
1389 if ($in_comment) {
1390 $sanitise_quote = '*/';
1391 } else {
1392 $sanitise_quote = '';
1393 }
1394}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001395sub sanitise_line {
1396 my ($line) = @_;
1397
1398 my $res = '';
1399 my $l = '';
1400
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001401 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001402 my $off = 0;
1403 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001404
Andy Whitcroft773647a2008-03-28 14:15:58 -07001405 # Always copy over the diff marker.
1406 $res = substr($line, 0, 1);
1407
1408 for ($off = 1; $off < length($line); $off++) {
1409 $c = substr($line, $off, 1);
1410
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001411 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001412 # and end, all to $;.
1413 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1414 $sanitise_quote = '*/';
1415
1416 substr($res, $off, 2, "$;$;");
1417 $off++;
1418 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001419 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001420 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001421 $sanitise_quote = '';
1422 substr($res, $off, 2, "$;$;");
1423 $off++;
1424 next;
1425 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001426 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1427 $sanitise_quote = '//';
1428
1429 substr($res, $off, 2, $sanitise_quote);
1430 $off++;
1431 next;
1432 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001433
1434 # A \ in a string means ignore the next character.
1435 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1436 $c eq "\\") {
1437 substr($res, $off, 2, 'XX');
1438 $off++;
1439 next;
1440 }
1441 # Regular quotes.
1442 if ($c eq "'" || $c eq '"') {
1443 if ($sanitise_quote eq '') {
1444 $sanitise_quote = $c;
1445
1446 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001447 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001448 } elsif ($sanitise_quote eq $c) {
1449 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001450 }
1451 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001452
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001453 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001454 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1455 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001456 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1457 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001458 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1459 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001460 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001461 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001462 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001463 }
1464
Daniel Walker113f04a2009-09-21 17:04:35 -07001465 if ($sanitise_quote eq '//') {
1466 $sanitise_quote = '';
1467 }
1468
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001469 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001470 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001471 my $clean = 'X' x length($1);
1472 $res =~ s@\<.*\>@<$clean>@;
1473
1474 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001475 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001476 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001477 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001478 }
1479
Joe Perchesdadf6802016-08-02 14:04:33 -07001480 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1481 my $match = $1;
1482 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1483 }
1484
Andy Whitcroft00df3442007-06-08 13:47:06 -07001485 return $res;
1486}
1487
Joe Perchesa6962d72013-04-29 16:18:13 -07001488sub get_quoted_string {
1489 my ($line, $rawline) = @_;
1490
Joe Perches478b1792018-04-10 16:33:34 -07001491 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001492 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001493 return substr($rawline, $-[0], $+[0] - $-[0]);
1494}
1495
Andy Whitcroft8905a672007-11-28 16:21:06 -08001496sub ctx_statement_block {
1497 my ($linenr, $remain, $off) = @_;
1498 my $line = $linenr - 1;
1499 my $blk = '';
1500 my $soff = $off;
1501 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001502 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001503
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001504 my $loff = 0;
1505
Andy Whitcroft8905a672007-11-28 16:21:06 -08001506 my $type = '';
1507 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001508 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001509 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001510 my $c;
1511 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001512
1513 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001514 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001515 @stack = (['', 0]) if ($#stack == -1);
1516
Andy Whitcroft773647a2008-03-28 14:15:58 -07001517 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001518 # If we are about to drop off the end, pull in more
1519 # context.
1520 if ($off >= $len) {
1521 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001522 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001523 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001524 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001525 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001526 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001527 $len = length($blk);
1528 $line++;
1529 last;
1530 }
1531 # Bail if there is no further context.
1532 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001533 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001534 last;
1535 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001536 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1537 $level++;
1538 $type = '#';
1539 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001540 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001541 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001542 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001543 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001544
Andy Whitcroft773647a2008-03-28 14:15:58 -07001545 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001546
1547 # Handle nested #if/#else.
1548 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1549 push(@stack, [ $type, $level ]);
1550 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1551 ($type, $level) = @{$stack[$#stack - 1]};
1552 } elsif ($remainder =~ /^#\s*endif\b/) {
1553 ($type, $level) = @{pop(@stack)};
1554 }
1555
Andy Whitcroft8905a672007-11-28 16:21:06 -08001556 # Statement ends at the ';' or a close '}' at the
1557 # outermost level.
1558 if ($level == 0 && $c eq ';') {
1559 last;
1560 }
1561
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001562 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001563 if ($level == 0 && $coff_set == 0 &&
1564 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1565 $remainder =~ /^(else)(?:\s|{)/ &&
1566 $remainder !~ /^else\s+if\b/) {
1567 $coff = $off + length($1) - 1;
1568 $coff_set = 1;
1569 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1570 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001571 }
1572
Andy Whitcroft8905a672007-11-28 16:21:06 -08001573 if (($type eq '' || $type eq '(') && $c eq '(') {
1574 $level++;
1575 $type = '(';
1576 }
1577 if ($type eq '(' && $c eq ')') {
1578 $level--;
1579 $type = ($level != 0)? '(' : '';
1580
1581 if ($level == 0 && $coff < $soff) {
1582 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001583 $coff_set = 1;
1584 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001585 }
1586 }
1587 if (($type eq '' || $type eq '{') && $c eq '{') {
1588 $level++;
1589 $type = '{';
1590 }
1591 if ($type eq '{' && $c eq '}') {
1592 $level--;
1593 $type = ($level != 0)? '{' : '';
1594
1595 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001596 if (substr($blk, $off + 1, 1) eq ';') {
1597 $off++;
1598 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001599 last;
1600 }
1601 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001602 # Preprocessor commands end at the newline unless escaped.
1603 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1604 $level--;
1605 $type = '';
1606 $off++;
1607 last;
1608 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001609 $off++;
1610 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001611 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001612 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001613 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001614 $line++;
1615 $remain--;
1616 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001617
1618 my $statement = substr($blk, $soff, $off - $soff + 1);
1619 my $condition = substr($blk, $soff, $coff - $soff + 1);
1620
1621 #warn "STATEMENT<$statement>\n";
1622 #warn "CONDITION<$condition>\n";
1623
Andy Whitcroft773647a2008-03-28 14:15:58 -07001624 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001625
1626 return ($statement, $condition,
1627 $line, $remain + 1, $off - $loff + 1, $level);
1628}
1629
Andy Whitcroftcf655042008-03-04 14:28:20 -08001630sub statement_lines {
1631 my ($stmt) = @_;
1632
1633 # Strip the diff line prefixes and rip blank lines at start and end.
1634 $stmt =~ s/(^|\n)./$1/g;
1635 $stmt =~ s/^\s*//;
1636 $stmt =~ s/\s*$//;
1637
1638 my @stmt_lines = ($stmt =~ /\n/g);
1639
1640 return $#stmt_lines + 2;
1641}
1642
1643sub statement_rawlines {
1644 my ($stmt) = @_;
1645
1646 my @stmt_lines = ($stmt =~ /\n/g);
1647
1648 return $#stmt_lines + 2;
1649}
1650
1651sub statement_block_size {
1652 my ($stmt) = @_;
1653
1654 $stmt =~ s/(^|\n)./$1/g;
1655 $stmt =~ s/^\s*{//;
1656 $stmt =~ s/}\s*$//;
1657 $stmt =~ s/^\s*//;
1658 $stmt =~ s/\s*$//;
1659
1660 my @stmt_lines = ($stmt =~ /\n/g);
1661 my @stmt_statements = ($stmt =~ /;/g);
1662
1663 my $stmt_lines = $#stmt_lines + 2;
1664 my $stmt_statements = $#stmt_statements + 1;
1665
1666 if ($stmt_lines > $stmt_statements) {
1667 return $stmt_lines;
1668 } else {
1669 return $stmt_statements;
1670 }
1671}
1672
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001673sub ctx_statement_full {
1674 my ($linenr, $remain, $off) = @_;
1675 my ($statement, $condition, $level);
1676
1677 my (@chunks);
1678
Andy Whitcroftcf655042008-03-04 14:28:20 -08001679 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001680 ($statement, $condition, $linenr, $remain, $off, $level) =
1681 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001682 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001683 push(@chunks, [ $condition, $statement ]);
1684 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1685 return ($level, $linenr, @chunks);
1686 }
1687
1688 # Pull in the following conditional/block pairs and see if they
1689 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001690 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001691 ($statement, $condition, $linenr, $remain, $off, $level) =
1692 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001693 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001694 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001695 #print "C: push\n";
1696 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001697 }
1698
1699 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001700}
1701
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001702sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001703 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001704 my $line;
1705 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001706 my $blk = '';
1707 my @o;
1708 my @c;
1709 my @res = ();
1710
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001711 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001712 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001713 for ($line = $start; $remain > 0; $line++) {
1714 next if ($rawlines[$line] =~ /^-/);
1715 $remain--;
1716
1717 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001718
1719 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001720 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001721 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001722 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001723 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001724 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001725 $level = pop(@stack);
1726 }
1727
Andy Whitcroft01464f32010-10-26 14:23:19 -07001728 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001729 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1730 if ($off > 0) {
1731 $off--;
1732 next;
1733 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001734
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001735 if ($c eq $close && $level > 0) {
1736 $level--;
1737 last if ($level == 0);
1738 } elsif ($c eq $open) {
1739 $level++;
1740 }
1741 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001742
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001743 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001744 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001745 }
1746
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001747 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001748 }
1749
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001750 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001751}
1752sub ctx_block_outer {
1753 my ($linenr, $remain) = @_;
1754
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001755 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1756 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001757}
1758sub ctx_block {
1759 my ($linenr, $remain) = @_;
1760
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001761 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1762 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001763}
1764sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001765 my ($linenr, $remain, $off) = @_;
1766
1767 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1768 return @r;
1769}
1770sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001771 my ($linenr, $remain) = @_;
1772
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001773 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001774}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001775sub ctx_statement_level {
1776 my ($linenr, $remain, $off) = @_;
1777
1778 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1779}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001780
1781sub ctx_locate_comment {
1782 my ($first_line, $end_line) = @_;
1783
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001784 # If c99 comment on the current line, or the line before or after
1785 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1786 return $current_comment if (defined $current_comment);
1787 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1788 return $current_comment if (defined $current_comment);
1789 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1790 return $current_comment if (defined $current_comment);
1791
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001792 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001793 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001794 return $current_comment if (defined $current_comment);
1795
1796 # Look through the context and try and figure out if there is a
1797 # comment.
1798 my $in_comment = 0;
1799 $current_comment = '';
1800 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001801 my $line = $rawlines[$linenr - 1];
1802 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001803 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1804 $in_comment = 1;
1805 }
1806 if ($line =~ m@/\*@) {
1807 $in_comment = 1;
1808 }
1809 if (!$in_comment && $current_comment ne '') {
1810 $current_comment = '';
1811 }
1812 $current_comment .= $line . "\n" if ($in_comment);
1813 if ($line =~ m@\*/@) {
1814 $in_comment = 0;
1815 }
1816 }
1817
1818 chomp($current_comment);
1819 return($current_comment);
1820}
1821sub ctx_has_comment {
1822 my ($first_line, $end_line) = @_;
1823 my $cmt = ctx_locate_comment($first_line, $end_line);
1824
Andy Whitcroft00df3442007-06-08 13:47:06 -07001825 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001826 ##print "CMMT: $cmt\n";
1827
1828 return ($cmt ne '');
1829}
1830
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001831sub raw_line {
1832 my ($linenr, $cnt) = @_;
1833
1834 my $offset = $linenr - 1;
1835 $cnt++;
1836
1837 my $line;
1838 while ($cnt) {
1839 $line = $rawlines[$offset++];
1840 next if (defined($line) && $line =~ /^-/);
1841 $cnt--;
1842 }
1843
1844 return $line;
1845}
1846
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001847sub get_stat_real {
1848 my ($linenr, $lc) = @_;
1849
1850 my $stat_real = raw_line($linenr, 0);
1851 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1852 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1853 }
1854
1855 return $stat_real;
1856}
1857
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001858sub get_stat_here {
1859 my ($linenr, $cnt, $here) = @_;
1860
1861 my $herectx = $here . "\n";
1862 for (my $n = 0; $n < $cnt; $n++) {
1863 $herectx .= raw_line($linenr, $n) . "\n";
1864 }
1865
1866 return $herectx;
1867}
1868
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001869sub cat_vet {
1870 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001871 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001872
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001873 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001874 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1875 $res .= $1;
1876 if ($2 ne '') {
1877 $coded = sprintf("^%c", unpack('C', $2) + 64);
1878 $res .= $coded;
1879 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001880 }
1881 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001882
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001883 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001884}
1885
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001886my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001887my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001888my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001889my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001890
1891sub annotate_reset {
1892 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001893 $av_pending = '_';
1894 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001895 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001896}
1897
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001898sub annotate_values {
1899 my ($stream, $type) = @_;
1900
1901 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001902 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001903 my $cur = $stream;
1904
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001905 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001906
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001907 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001908 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001909 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001910 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001911 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001912 print "WS($1)\n" if ($dbg_values > 1);
1913 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001914 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001915 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001916 }
1917
Florian Micklerc023e4732011-01-12 16:59:58 -08001918 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001919 print "CAST($1)\n" if ($dbg_values > 1);
1920 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001921 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001922
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001923 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001924 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001925 $type = 'T';
1926
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001927 } elsif ($cur =~ /^($Modifier)\s*/) {
1928 print "MODIFIER($1)\n" if ($dbg_values > 1);
1929 $type = 'T';
1930
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001931 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001932 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001933 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001934 push(@av_paren_type, $type);
1935 if ($2 ne '') {
1936 $av_pending = 'N';
1937 }
1938 $type = 'E';
1939
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001940 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001941 print "UNDEF($1)\n" if ($dbg_values > 1);
1942 $av_preprocessor = 1;
1943 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001944
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001945 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001946 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001947 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001948
1949 push(@av_paren_type, $type);
1950 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001951 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001952
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001953 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001954 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1955 $av_preprocessor = 1;
1956
1957 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1958
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001959 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001960
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001961 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001962 print "PRE_END($1)\n" if ($dbg_values > 1);
1963
1964 $av_preprocessor = 1;
1965
1966 # Assume all arms of the conditional end as this
1967 # one does, and continue as if the #endif was not here.
1968 pop(@av_paren_type);
1969 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001970 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001971
1972 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001973 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001974
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001975 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1976 print "ATTR($1)\n" if ($dbg_values > 1);
1977 $av_pending = $type;
1978 $type = 'N';
1979
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001980 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001981 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001982 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001983 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001984 }
1985 $type = 'N';
1986
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001987 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001988 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001989 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001990 $type = 'N';
1991
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001992 } elsif ($cur =~/^(case)/o) {
1993 print "CASE($1)\n" if ($dbg_values > 1);
1994 $av_pend_colon = 'C';
1995 $type = 'N';
1996
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001997 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001998 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001999 $type = 'N';
2000
2001 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002002 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002003 push(@av_paren_type, $av_pending);
2004 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002005 $type = 'N';
2006
2007 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002008 my $new_type = pop(@av_paren_type);
2009 if ($new_type ne '_') {
2010 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002011 print "PAREN('$1') -> $type\n"
2012 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002013 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002014 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002015 }
2016
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07002017 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002018 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07002019 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08002020 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002021
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002022 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
2023 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002024 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002025 } elsif ($type eq 'E') {
2026 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002027 }
2028 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
2029 $type = 'V';
2030
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002031 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002032 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002033 $type = 'V';
2034
2035 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002036 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002037 $type = 'N';
2038
Andy Whitcroftcf655042008-03-04 14:28:20 -08002039 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002040 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002041 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002042 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002043
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002044 } elsif ($cur =~/^(,)/) {
2045 print "COMMA($1)\n" if ($dbg_values > 1);
2046 $type = 'C';
2047
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002048 } elsif ($cur =~ /^(\?)/o) {
2049 print "QUESTION($1)\n" if ($dbg_values > 1);
2050 $type = 'N';
2051
2052 } elsif ($cur =~ /^(:)/o) {
2053 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
2054
2055 substr($var, length($res), 1, $av_pend_colon);
2056 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
2057 $type = 'E';
2058 } else {
2059 $type = 'N';
2060 }
2061 $av_pend_colon = 'O';
2062
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002063 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002064 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002065 $type = 'N';
2066
Andy Whitcroft0d413862008-10-15 22:02:16 -07002067 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002068 my $variant;
2069
2070 print "OPV($1)\n" if ($dbg_values > 1);
2071 if ($type eq 'V') {
2072 $variant = 'B';
2073 } else {
2074 $variant = 'U';
2075 }
2076
2077 substr($var, length($res), 1, $variant);
2078 $type = 'N';
2079
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002080 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002081 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002082 if ($1 ne '++' && $1 ne '--') {
2083 $type = 'N';
2084 }
2085
2086 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002087 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002088 }
2089 if (defined $1) {
2090 $cur = substr($cur, length($1));
2091 $res .= $type x length($1);
2092 }
2093 }
2094
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002095 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002096}
2097
Andy Whitcroft8905a672007-11-28 16:21:06 -08002098sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002099 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002100 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002101 ^(?:
2102 $Modifier|
2103 $Storage|
2104 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002105 DEFINE_\S+
2106 )$|
2107 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002108 goto|
2109 return|
2110 case|
2111 else|
2112 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002113 do|
2114 \#|
2115 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002116 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002117 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002118 )}x;
2119 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2120 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002121 # Check for modifiers.
2122 $possible =~ s/\s*$Storage\s*//g;
2123 $possible =~ s/\s*$Sparse\s*//g;
2124 if ($possible =~ /^\s*$/) {
2125
2126 } elsif ($possible =~ /\s/) {
2127 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002128 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002129 if ($modifier !~ $notPermitted) {
2130 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002131 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002132 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002133 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002134
2135 } else {
2136 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002137 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002138 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002139 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002140 } else {
2141 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002142 }
2143}
2144
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002145my $prefix = '';
2146
Joe Perches000d1cc12011-07-25 17:13:25 -07002147sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002148 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002149
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002150 $type =~ tr/[a-z]/[A-Z]/;
2151
Joe Perchescbec18a2014-04-03 14:49:19 -07002152 return defined $use_type{$type} if (scalar keys %use_type > 0);
2153
2154 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002155}
2156
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002157sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002158 my ($level, $type, $msg) = @_;
2159
2160 if (!show_type($type) ||
2161 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002162 return 0;
2163 }
Joe Perches57230292015-06-25 15:03:03 -07002164 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002165 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002166 if ($level eq 'ERROR') {
2167 $output .= RED;
2168 } elsif ($level eq 'WARNING') {
2169 $output .= YELLOW;
2170 } else {
2171 $output .= GREEN;
2172 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002173 }
Joe Perches57230292015-06-25 15:03:03 -07002174 $output .= $prefix . $level . ':';
2175 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002176 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002177 $output .= "$type:";
2178 }
John Brooks737c0762017-07-10 15:52:24 -07002179 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002180 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002181
2182 if ($showfile) {
2183 my @lines = split("\n", $output, -1);
2184 splice(@lines, 1, 1);
2185 $output = join("\n", @lines);
2186 }
Joe Perches57230292015-06-25 15:03:03 -07002187 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002188
Joe Perches57230292015-06-25 15:03:03 -07002189 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002190
2191 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002192}
Joe Perchescbec18a2014-04-03 14:49:19 -07002193
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002194sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002195 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002196}
Joe Perches000d1cc12011-07-25 17:13:25 -07002197
Joe Perchesd752fcc2014-08-06 16:11:05 -07002198sub fixup_current_range {
2199 my ($lineRef, $offset, $length) = @_;
2200
2201 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2202 my $o = $1;
2203 my $l = $2;
2204 my $no = $o + $offset;
2205 my $nl = $l + $length;
2206 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2207 }
2208}
2209
2210sub fix_inserted_deleted_lines {
2211 my ($linesRef, $insertedRef, $deletedRef) = @_;
2212
2213 my $range_last_linenr = 0;
2214 my $delta_offset = 0;
2215
2216 my $old_linenr = 0;
2217 my $new_linenr = 0;
2218
2219 my $next_insert = 0;
2220 my $next_delete = 0;
2221
2222 my @lines = ();
2223
2224 my $inserted = @{$insertedRef}[$next_insert++];
2225 my $deleted = @{$deletedRef}[$next_delete++];
2226
2227 foreach my $old_line (@{$linesRef}) {
2228 my $save_line = 1;
2229 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002230 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002231 $delta_offset = 0;
2232 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2233 $range_last_linenr = $new_linenr;
2234 fixup_current_range(\$line, $delta_offset, 0);
2235 }
2236
2237 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2238 $deleted = @{$deletedRef}[$next_delete++];
2239 $save_line = 0;
2240 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2241 }
2242
2243 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2244 push(@lines, ${$inserted}{'LINE'});
2245 $inserted = @{$insertedRef}[$next_insert++];
2246 $new_linenr++;
2247 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2248 }
2249
2250 if ($save_line) {
2251 push(@lines, $line);
2252 $new_linenr++;
2253 }
2254
2255 $old_linenr++;
2256 }
2257
2258 return @lines;
2259}
2260
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002261sub fix_insert_line {
2262 my ($linenr, $line) = @_;
2263
2264 my $inserted = {
2265 LINENR => $linenr,
2266 LINE => $line,
2267 };
2268 push(@fixed_inserted, $inserted);
2269}
2270
2271sub fix_delete_line {
2272 my ($linenr, $line) = @_;
2273
2274 my $deleted = {
2275 LINENR => $linenr,
2276 LINE => $line,
2277 };
2278
2279 push(@fixed_deleted, $deleted);
2280}
2281
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002282sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002283 my ($type, $msg) = @_;
2284
2285 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002286 our $clean = 0;
2287 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002288 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002289 }
Joe Perches3705ce52013-07-03 15:05:31 -07002290 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002291}
2292sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002293 my ($type, $msg) = @_;
2294
2295 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002296 our $clean = 0;
2297 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002298 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002299 }
Joe Perches3705ce52013-07-03 15:05:31 -07002300 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002301}
2302sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002303 my ($type, $msg) = @_;
2304
2305 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002306 our $clean = 0;
2307 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002308 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002309 }
Joe Perches3705ce52013-07-03 15:05:31 -07002310 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002311}
2312
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002313sub check_absolute_file {
2314 my ($absolute, $herecurr) = @_;
2315 my $file = $absolute;
2316
2317 ##print "absolute<$absolute>\n";
2318
2319 # See if any suffix of this path is a path within the tree.
2320 while ($file =~ s@^[^/]*/@@) {
2321 if (-f "$root/$file") {
2322 ##print "file<$file>\n";
2323 last;
2324 }
2325 }
2326 if (! -f _) {
2327 return 0;
2328 }
2329
2330 # It is, so see if the prefix is acceptable.
2331 my $prefix = $absolute;
2332 substr($prefix, -length($file)) = '';
2333
2334 ##print "prefix<$prefix>\n";
2335 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002336 WARN("USE_RELATIVE_PATH",
2337 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002338 }
2339}
2340
Joe Perches3705ce52013-07-03 15:05:31 -07002341sub trim {
2342 my ($string) = @_;
2343
Joe Perchesb34c6482013-09-11 14:24:01 -07002344 $string =~ s/^\s+|\s+$//g;
2345
2346 return $string;
2347}
2348
2349sub ltrim {
2350 my ($string) = @_;
2351
2352 $string =~ s/^\s+//;
2353
2354 return $string;
2355}
2356
2357sub rtrim {
2358 my ($string) = @_;
2359
2360 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002361
2362 return $string;
2363}
2364
Joe Perches52ea8502013-11-12 15:10:09 -08002365sub string_find_replace {
2366 my ($string, $find, $replace) = @_;
2367
2368 $string =~ s/$find/$replace/g;
2369
2370 return $string;
2371}
2372
Joe Perches3705ce52013-07-03 15:05:31 -07002373sub tabify {
2374 my ($leading) = @_;
2375
Antonio Borneo713a09d2020-04-06 20:11:07 -07002376 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002377 my $max_spaces_before_tab = $source_indent - 1;
2378 my $spaces_to_tab = " " x $source_indent;
2379
2380 #convert leading spaces to tabs
2381 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2382 #Remove spaces before a tab
2383 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2384
2385 return "$leading";
2386}
2387
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002388sub pos_last_openparen {
2389 my ($line) = @_;
2390
2391 my $pos = 0;
2392
2393 my $opens = $line =~ tr/\(/\(/;
2394 my $closes = $line =~ tr/\)/\)/;
2395
2396 my $last_openparen = 0;
2397
2398 if (($opens == 0) || ($closes >= $opens)) {
2399 return -1;
2400 }
2401
2402 my $len = length($line);
2403
2404 for ($pos = 0; $pos < $len; $pos++) {
2405 my $string = substr($line, $pos);
2406 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2407 $pos += length($1) - 1;
2408 } elsif (substr($line, $pos, 1) eq '(') {
2409 $last_openparen = $pos;
2410 } elsif (index($string, '(') == -1) {
2411 last;
2412 }
2413 }
2414
Joe Perches91cb5192014-04-03 14:49:32 -07002415 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002416}
2417
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002418sub get_raw_comment {
2419 my ($line, $rawline) = @_;
2420 my $comment = '';
2421
2422 for my $i (0 .. (length($line) - 1)) {
2423 if (substr($line, $i, 1) eq "$;") {
2424 $comment .= substr($rawline, $i, 1);
2425 }
2426 }
2427
2428 return $comment;
2429}
2430
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002431sub process {
2432 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002433
2434 my $linenr=0;
2435 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002436 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002437 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002438 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002439
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002440 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002441 my $indent;
2442 my $previndent=0;
2443 my $stashindent=0;
2444
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002445 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002446 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002447 my $author = '';
2448 my $authorsignoff = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002449 my $author_sob = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002450 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002451 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002452 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002453 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002454 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002455 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002456 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002457 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002458 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002459 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002460 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002461 my $non_utf8_charset = 0;
2462
Joe Perches365dd4e2014-08-06 16:10:42 -07002463 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002464 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002465
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002466 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002467 our $cnt_lines = 0;
2468 our $cnt_error = 0;
2469 our $cnt_warn = 0;
2470 our $cnt_chk = 0;
2471
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002472 # Trace the real file/line as we go.
2473 my $realfile = '';
2474 my $realline = 0;
2475 my $realcnt = 0;
2476 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002477 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002478 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002479 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002480 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002481 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002482
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002483 my $prev_values = 'E';
2484
2485 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002486 my %suppress_ifbraces;
Andy Whitcroft170d3a222008-10-15 22:02:30 -07002487 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002488 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002489 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002490
Joe Perches7e51f192013-09-11 14:23:57 -07002491 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002492
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002493 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002494 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002495 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002496 my @setup_docs = ();
2497 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002498
Joe Perchesd8b07712013-11-12 15:10:06 -08002499 my $camelcase_file_seeded = 0;
2500
Rob Herring9f3a8992018-04-10 16:33:13 -07002501 my $checklicenseline = 1;
2502
Andy Whitcroft773647a2008-03-28 14:15:58 -07002503 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002504 my $line;
2505 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002506 $linenr++;
2507 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002508
Joe Perches3705ce52013-07-03 15:05:31 -07002509 push(@fixed, $rawline) if ($fix);
2510
Andy Whitcroft773647a2008-03-28 14:15:58 -07002511 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002512 $setup_docs = 0;
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07002513 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002514 $setup_docs = 1;
2515 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002516 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002517 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002518 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002519 $realline=$1-1;
2520 if (defined $2) {
2521 $realcnt=$3+1;
2522 } else {
2523 $realcnt=1+1;
2524 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002525 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002526
2527 # Guestimate if this is a continuing comment. Run
2528 # the context looking for a comment "edge". If this
2529 # edge is a close comment then we must be in a comment
2530 # at context start.
2531 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002532 my $cnt = $realcnt;
2533 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2534 next if (defined $rawlines[$ln - 1] &&
2535 $rawlines[$ln - 1] =~ /^-/);
2536 $cnt--;
2537 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002538 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002539 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2540 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2541 ($edge) = $1;
2542 last;
2543 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002544 }
2545 if (defined $edge && $edge eq '*/') {
2546 $in_comment = 1;
2547 }
2548
2549 # Guestimate if this is a continuing comment. If this
2550 # is the start of a diff block and this line starts
2551 # ' *' then it is very likely a comment.
2552 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002553 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002554 {
2555 $in_comment = 1;
2556 }
2557
2558 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2559 sanitise_line_reset($in_comment);
2560
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002561 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002562 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002563 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002564 $line = sanitise_line($rawline);
2565 }
2566 push(@lines, $line);
2567
2568 if ($realcnt > 1) {
2569 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2570 } else {
2571 $realcnt = 0;
2572 }
2573
2574 #print "==>$rawline\n";
2575 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002576
2577 if ($setup_docs && $line =~ /^\+/) {
2578 push(@setup_docs, $line);
2579 }
2580 }
2581
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002582 $prefix = '';
2583
Andy Whitcroft773647a2008-03-28 14:15:58 -07002584 $realcnt = 0;
2585 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002586 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002587 foreach my $line (@lines) {
2588 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002589 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002590 my $sline = $line; #copy of $line
2591 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002592
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002593 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002594 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002595
Joe Perches12c253ab2018-06-07 17:10:58 -07002596# check if it's a mode change, rename or start of a patch
2597 if (!$in_commit_log &&
2598 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2599 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2600 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2601 $is_patch = 1;
2602 }
2603
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002604#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002605 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002606 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2607 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002608 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002609 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002610 $realline=$1-1;
2611 if (defined $2) {
2612 $realcnt=$3+1;
2613 } else {
2614 $realcnt=1+1;
2615 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002616 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002617 $prev_values = 'E';
2618
Andy Whitcroft773647a2008-03-28 14:15:58 -07002619 %suppress_ifbraces = ();
Andy Whitcroft170d3a222008-10-15 22:02:30 -07002620 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002621 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002622 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002623 if ($context =~ /\b(\w+)\s*\(/) {
2624 $context_function = $1;
2625 } else {
2626 undef $context_function;
2627 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002628 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002629
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002630# track the line number as we move through the hunk, note that
2631# new versions of GNU diff omit the leading space on completely
2632# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002633 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002634 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002635 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002636
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002637 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002638 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002639
2640 # Track the previous line.
2641 ($prevline, $stashline) = ($stashline, $line);
2642 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002643 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2644
Andy Whitcroft773647a2008-03-28 14:15:58 -07002645 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002646
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002647 } elsif ($realcnt == 1) {
2648 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002649 }
2650
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002651 my $hunk_line = ($realcnt != 0);
2652
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002653 $here = "#$linenr: " if (!$file);
2654 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002655
Joe Perches2ac73b42014-06-04 16:12:05 -07002656 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002657 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002658 if ($line =~ /^diff --git.*?(\S+)$/) {
2659 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002660 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002661 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002662 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002663 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002664 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002665 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002666 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002667
2668 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002669 if (!$file && $tree && $p1_prefix ne '' &&
2670 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002671 WARN("PATCH_PREFIX",
2672 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002673 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002674
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002675 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002676 ERROR("MODIFIED_INCLUDE_ASM",
2677 "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 -07002678 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002679 $found_file = 1;
2680 }
2681
Joe Perches34d88152015-06-25 15:03:05 -07002682#make up the handle for any error we report on this line
2683 if ($showfile) {
2684 $prefix = "$realfile:$realline: "
2685 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002686 if ($file) {
2687 $prefix = "$filename:$realline: ";
2688 } else {
2689 $prefix = "$filename:$linenr: ";
2690 }
Joe Perches34d88152015-06-25 15:03:05 -07002691 }
2692
Joe Perches2ac73b42014-06-04 16:12:05 -07002693 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002694 if (is_maintained_obsolete($realfile)) {
2695 WARN("OBSOLETE",
2696 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2697 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002698 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002699 $check = 1;
2700 } else {
2701 $check = $check_orig;
2702 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002703 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002704
2705 if ($realfile !~ /^MAINTAINERS/) {
2706 my $last_binding_patch = $is_binding_patch;
2707
2708 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2709
2710 if (($last_binding_patch != -1) &&
2711 ($last_binding_patch ^ $is_binding_patch)) {
2712 WARN("DT_SPLIT_BINDING_PATCH",
Mauro Carvalho Chehab858e6842020-04-15 16:45:25 +02002713 "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 -07002714 }
2715 }
2716
Andy Whitcroft773647a2008-03-28 14:15:58 -07002717 next;
2718 }
2719
Randy Dunlap389834b2007-06-08 13:47:03 -07002720 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002721
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002722 my $hereline = "$here\n$rawline\n";
2723 my $herecurr = "$here\n$rawline\n";
2724 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002725
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002726 $cnt_lines++ if ($realcnt != 0);
2727
Joe Perches490b2922018-08-21 21:58:01 -07002728# Verify the existence of a commit log if appropriate
2729# 2 is used because a $signature is counted in $commit_log_lines
2730 if ($in_commit_log) {
2731 if ($line !~ /^\s*$/) {
2732 $commit_log_lines++; #could be a $signature
2733 }
2734 } elsif ($has_commit_log && $commit_log_lines < 2) {
2735 WARN("COMMIT_MESSAGE",
2736 "Missing commit description - Add an appropriate one\n");
2737 $commit_log_lines = 2; #warn only once
2738 }
2739
Joe Perchese518e9a2015-06-25 15:03:27 -07002740# Check if the commit log has what seems like a diff which can confuse patch
2741 if ($in_commit_log && !$commit_log_has_diff &&
Mrinal Pandey13e45412020-09-04 16:35:52 -07002742 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2743 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
Joe Perchese518e9a2015-06-25 15:03:27 -07002744 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2745 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2746 ERROR("DIFF_IN_COMMIT_MSG",
2747 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2748 $commit_log_has_diff = 1;
2749 }
2750
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002751# Check for incorrect file permissions
2752 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2753 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002754 if ($realfile !~ m@scripts/@ &&
2755 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002756 ERROR("EXECUTE_PERMISSIONS",
2757 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002758 }
2759 }
2760
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002761# Check the patch for a From:
2762 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2763 $author = $1;
Dwaipayan Raye7f929f2020-10-15 20:12:15 -07002764 my $curline = $linenr;
2765 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2766 $author .= $1;
2767 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002768 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2769 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002770 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002771 }
2772
Joe Perches20112472011-07-25 17:13:23 -07002773# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002774 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002775 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002776 $in_commit_log = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002777 if ($author ne '' && $authorsignoff != 1) {
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002778 if (same_email_addresses($1, $author)) {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002779 $authorsignoff = 1;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002780 } else {
2781 my $ctx = $1;
2782 my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
2783 my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
2784
2785 if ($email_address eq $author_address && $email_name eq $author_name) {
2786 $author_sob = $ctx;
2787 $authorsignoff = 2;
2788 } elsif ($email_address eq $author_address) {
2789 $author_sob = $ctx;
2790 $authorsignoff = 3;
2791 } elsif ($email_name eq $author_name) {
2792 $author_sob = $ctx;
2793 $authorsignoff = 4;
2794
2795 my $address1 = $email_address;
2796 my $address2 = $author_address;
2797
2798 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
2799 $address1 = "$1$2";
2800 }
2801 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
2802 $address2 = "$1$2";
2803 }
2804 if ($address1 eq $address2) {
2805 $authorsignoff = 5;
2806 }
2807 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002808 }
2809 }
Joe Perches20112472011-07-25 17:13:23 -07002810 }
2811
Joe Perches44d303e2020-04-06 20:11:10 -07002812# Check for patch separator
2813 if ($line =~ /^---$/) {
2814 $has_patch_separator = 1;
2815 $in_commit_log = 0;
2816 }
2817
Joe Perchese0d975b2014-12-10 15:51:49 -08002818# Check if MAINTAINERS is being updated. If so, there's probably no need to
2819# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2820 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2821 $reported_maintainer_file = 1;
2822 }
2823
Joe Perches20112472011-07-25 17:13:23 -07002824# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002825 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002826 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002827 my $space_before = $1;
2828 my $sign_off = $2;
2829 my $space_after = $3;
2830 my $email = $4;
2831 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2832
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002833 if ($sign_off !~ /$signature_tags/) {
Aditya Srivastava831242a2020-12-15 20:45:12 -08002834 my $suggested_signature = find_standard_signature($sign_off);
2835 if ($suggested_signature eq "") {
2836 WARN("BAD_SIGN_OFF",
2837 "Non-standard signature: $sign_off\n" . $herecurr);
2838 } else {
2839 if (WARN("BAD_SIGN_OFF",
2840 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
2841 $fix) {
2842 $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
2843 }
2844 }
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002845 }
Joe Perches20112472011-07-25 17:13:23 -07002846 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002847 if (WARN("BAD_SIGN_OFF",
2848 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2849 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002850 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002851 "$ucfirst_sign_off $email";
2852 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002853 }
Joe Perches20112472011-07-25 17:13:23 -07002854 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002855 if (WARN("BAD_SIGN_OFF",
2856 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2857 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002858 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002859 "$ucfirst_sign_off $email";
2860 }
2861
Joe Perches20112472011-07-25 17:13:23 -07002862 }
2863 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002864 if (WARN("BAD_SIGN_OFF",
2865 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2866 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002867 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002868 "$ucfirst_sign_off $email";
2869 }
Joe Perches20112472011-07-25 17:13:23 -07002870 }
2871
Joe Perchesdfa05c22020-04-06 20:10:48 -07002872 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002873 my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
Joe Perches20112472011-07-25 17:13:23 -07002874 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002875 ERROR("BAD_SIGN_OFF",
2876 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002877 } else {
2878 my $dequoted = $suggested_email;
2879 $dequoted =~ s/^"//;
2880 $dequoted =~ s/" </ </;
2881 # Don't force email to have quotes
2882 # Allow just an angle bracketed address
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002883 if (!same_email_addresses($email, $suggested_email)) {
2884 if (WARN("BAD_SIGN_OFF",
2885 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
2886 $fix) {
2887 $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
2888 }
2889 }
2890
2891 # Address part shouldn't have comments
2892 my $stripped_address = $email_address;
2893 $stripped_address =~ s/\([^\(\)]*\)//g;
2894 if ($email_address ne $stripped_address) {
2895 if (WARN("BAD_SIGN_OFF",
2896 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
2897 $fix) {
2898 $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
2899 }
2900 }
2901
2902 # Only one name comment should be allowed
2903 my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
2904 if ($comment_count > 1) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002905 WARN("BAD_SIGN_OFF",
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002906 "Use a single name comment in email: '$email'\n" . $herecurr);
2907 }
2908
2909
2910 # stable@vger.kernel.org or stable@kernel.org shouldn't
Dwaipayan Raye73d2712020-12-15 20:44:56 -08002911 # have an email name. In addition comments should strictly
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002912 # begin with a #
2913 if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
2914 if (($comment ne "" && $comment !~ /^#.+/) ||
2915 ($email_name ne "")) {
2916 my $cur_name = $email_name;
2917 my $new_comment = $comment;
2918 $cur_name =~ s/[a-zA-Z\s\-\"]+//g;
2919
2920 # Remove brackets enclosing comment text
2921 # and # from start of comments to get comment text
2922 $new_comment =~ s/^\((.*)\)$/$1/;
2923 $new_comment =~ s/^\[(.*)\]$/$1/;
2924 $new_comment =~ s/^[\s\#]+|\s+$//g;
2925
2926 $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
2927 $new_comment = " # $new_comment" if ($new_comment ne "");
2928 my $new_email = "$email_address$new_comment";
2929
2930 if (WARN("BAD_STABLE_ADDRESS_STYLE",
2931 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
2932 $fix) {
2933 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2934 }
2935 }
2936 } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
2937 my $new_comment = $comment;
2938
2939 # Extract comment text from within brackets or
2940 # c89 style /*...*/ comments
2941 $new_comment =~ s/^\[(.*)\]$/$1/;
2942 $new_comment =~ s/^\/\*(.*)\*\/$/$1/;
2943
2944 $new_comment = trim($new_comment);
2945 $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
2946 $new_comment = "($new_comment)" if ($new_comment ne "");
2947 my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
2948
2949 if (WARN("BAD_SIGN_OFF",
2950 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
2951 $fix) {
2952 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2953 }
Joe Perches20112472011-07-25 17:13:23 -07002954 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002955 }
Joe Perches7e51f192013-09-11 14:23:57 -07002956
2957# Check for duplicate signatures
2958 my $sig_nospace = $line;
2959 $sig_nospace =~ s/\s//g;
2960 $sig_nospace = lc($sig_nospace);
2961 if (defined $signatures{$sig_nospace}) {
2962 WARN("BAD_SIGN_OFF",
2963 "Duplicate signature\n" . $herecurr);
2964 } else {
2965 $signatures{$sig_nospace} = 1;
2966 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002967
2968# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2969 if ($sign_off =~ /^co-developed-by:$/i) {
2970 if ($email eq $author) {
2971 WARN("BAD_SIGN_OFF",
2972 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2973 }
2974 if (!defined $lines[$linenr]) {
2975 WARN("BAD_SIGN_OFF",
2976 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2977 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2978 WARN("BAD_SIGN_OFF",
2979 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2980 } elsif ($1 ne $email) {
2981 WARN("BAD_SIGN_OFF",
2982 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2983 }
2984 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002985 }
2986
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002987# Check email subject for common tools that don't need to be mentioned
2988 if ($in_header_lines &&
2989 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2990 WARN("EMAIL_SUBJECT",
2991 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2992 }
2993
Joe Perches44d303e2020-04-06 20:11:10 -07002994# Check for Gerrit Change-Ids not in any patch context
2995 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Aditya Srivastava7580c5b2020-12-15 20:44:47 -08002996 if (ERROR("GERRIT_CHANGE_ID",
2997 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
2998 $fix) {
2999 fix_delete_line($fixlinenr, $rawline);
3000 }
Christopher Covington7ebd05e2014-04-03 14:49:31 -07003001 }
3002
Joe Perches369c8dd2015-11-06 16:31:34 -08003003# Check if the commit log is in a possible stack dump
3004 if ($in_commit_log && !$commit_log_possible_stack_dump &&
3005 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3006 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3007 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07003008 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3009 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3010 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3011 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08003012 $commit_log_possible_stack_dump = 1;
3013 }
3014
Joe Perches2a076f42015-04-16 12:44:28 -07003015# Check for line lengths > 75 in commit log, warn once
3016 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08003017 length($line) > 75 &&
3018 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3019 # file delta changes
3020 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
3021 # filename then :
Aditya Srivastava27b379a2020-12-15 20:44:59 -08003022 $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
3023 # A Fixes: or Link: line or signature tag line
Joe Perches369c8dd2015-11-06 16:31:34 -08003024 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07003025 WARN("COMMIT_LOG_LONG_LINE",
3026 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
3027 $commit_log_long_line = 1;
3028 }
3029
Joe Perchesbf4daf12015-09-09 15:37:50 -07003030# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08003031 if ($in_commit_log && $commit_log_possible_stack_dump &&
3032 $line =~ /^\s*$/) {
3033 $commit_log_possible_stack_dump = 0;
3034 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07003035
Dwaipayan Ray084a6172020-12-15 20:45:18 -08003036# Check for lines starting with a #
3037 if ($in_commit_log && $line =~ /^#/) {
3038 if (WARN("COMMIT_COMMENT_SYMBOL",
3039 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3040 $fix) {
3041 $fixed[$fixlinenr] =~ s/^/ /;
3042 }
3043 }
3044
Joe Perches0d7835f2015-02-13 14:38:35 -08003045# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08003046 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07003047 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07003048 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07003049 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07003050 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08003051 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3052 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07003053 my $init_char = "c";
3054 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08003055 my $short = 1;
3056 my $long = 0;
3057 my $case = 1;
3058 my $space = 1;
3059 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08003060 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08003061 my $id = '0123456789ab';
3062 my $orig_desc = "commit description";
3063 my $description = "";
3064
Joe Perchesfe043ea2015-09-09 15:37:25 -07003065 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3066 $init_char = $1;
3067 $orig_commit = lc($2);
3068 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
3069 $orig_commit = lc($1);
3070 }
3071
Joe Perches0d7835f2015-02-13 14:38:35 -08003072 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
3073 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
3074 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
3075 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
3076 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
3077 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08003078 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08003079 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
3080 defined $rawlines[$linenr] &&
3081 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
3082 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08003083 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08003084 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
3085 defined $rawlines[$linenr] &&
3086 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
3087 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
3088 $orig_desc = $1;
3089 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
3090 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08003091 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08003092 }
3093
3094 ($id, $description) = git_commit_info($orig_commit,
3095 $id, $orig_desc);
3096
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07003097 if (defined($id) &&
3098 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08003099 ERROR("GIT_COMMIT_ID",
3100 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
3101 }
Joe Perchesd311cd42014-08-06 16:10:57 -07003102 }
3103
Joe Perches13f19372014-08-06 16:10:59 -07003104# Check for added, moved or deleted files
3105 if (!$reported_maintainer_file && !$in_commit_log &&
3106 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
3107 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
3108 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
3109 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08003110 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07003111 $reported_maintainer_file = 1;
3112 WARN("FILE_PATH_CHANGES",
3113 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
3114 }
3115
Rob Herringe400edb2019-09-12 15:18:20 +01003116# Check for adding new DT bindings not in schema format
3117 if (!$in_commit_log &&
3118 ($line =~ /^new file mode\s*\d+\s*$/) &&
3119 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3120 WARN("DT_SCHEMA_BINDING_PATCH",
3121 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
3122 }
3123
Andy Whitcroft00df3442007-06-08 13:47:06 -07003124# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08003125 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003126 ERROR("CORRUPTED_PATCH",
3127 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003128 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003129 }
3130
3131# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3132 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003133 $rawline !~ m/^$UTF8*$/) {
3134 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3135
3136 my $blank = copy_spacing($rawline);
3137 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3138 my $hereptr = "$hereline$ptr\n";
3139
Joe Perches34d99212011-07-25 17:13:26 -07003140 CHK("INVALID_UTF8",
3141 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003142 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003143
Joe Perches15662b32011-10-31 17:13:12 -07003144# Check if it's the start of a commit log
3145# (not a header line and we haven't seen the patch filename)
3146 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07003147 !($rawline =~ /^\s+(?:\S|$)/ ||
3148 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07003149 $in_header_lines = 0;
3150 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07003151 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07003152 }
3153
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003154# Check if there is UTF-8 in a commit log when a mail header has explicitly
3155# declined it, i.e defined some charset where it is missing.
3156 if ($in_header_lines &&
3157 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3158 $1 !~ /utf-8/i) {
3159 $non_utf8_charset = 1;
3160 }
3161
3162 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07003163 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003164 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07003165 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3166 }
3167
Joe Perchesd6430f72016-12-12 16:46:28 -08003168# Check for absolute kernel paths in commit message
3169 if ($tree && $in_commit_log) {
3170 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3171 my $file = $1;
3172
3173 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3174 check_absolute_file($1, $herecurr)) {
3175 #
3176 } else {
3177 check_absolute_file($file, $herecurr);
3178 }
3179 }
3180 }
3181
Kees Cook66b47b42014-10-13 15:51:57 -07003182# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07003183 if (defined($misspellings) &&
3184 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003185 while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07003186 my $typo = $1;
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003187 my $blank = copy_spacing($rawline);
3188 my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
3189 my $hereptr = "$hereline$ptr\n";
Kees Cook66b47b42014-10-13 15:51:57 -07003190 my $typo_fix = $spelling_fix{lc($typo)};
3191 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3192 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07003193 my $msg_level = \&WARN;
3194 $msg_level = \&CHK if ($file);
3195 if (&{$msg_level}("TYPO_SPELLING",
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003196 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07003197 $fix) {
3198 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3199 }
3200 }
3201 }
3202
Matteo Crocea8dd86b2019-09-25 16:46:38 -07003203# check for invalid commit id
3204 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3205 my $id;
3206 my $description;
3207 ($id, $description) = git_commit_info($2, undef, undef);
3208 if (!defined($id)) {
3209 WARN("UNKNOWN_COMMIT_ID",
3210 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3211 }
3212 }
3213
Joe Perches310cd062020-10-15 20:11:52 -07003214# check for repeated words separated by a single space
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003215# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3216 if (($rawline =~ /^\+/ || $in_commit_log) &&
3217 $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003218 pos($rawline) = 1 if (!$in_commit_log);
Joe Perches310cd062020-10-15 20:11:52 -07003219 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3220
3221 my $first = $1;
3222 my $second = $2;
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003223 my $start_pos = $-[1];
3224 my $end_pos = $+[2];
Joe Perches310cd062020-10-15 20:11:52 -07003225 if ($first =~ /(?:struct|union|enum)/) {
3226 pos($rawline) += length($first) + length($second) + 1;
3227 next;
3228 }
3229
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003230 next if (lc($first) ne lc($second));
Joe Perches310cd062020-10-15 20:11:52 -07003231 next if ($first eq 'long');
3232
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003233 # check for character before and after the word matches
3234 my $start_char = '';
3235 my $end_char = '';
3236 $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
3237 $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3238
3239 next if ($start_char =~ /^\S$/);
3240 next if (index(" \t.,;?!", $end_char) == -1);
3241
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003242 # avoid repeating hex occurrences like 'ff ff fe 09 ...'
3243 if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3244 next if (!exists($allow_repeated_words{lc($first)}));
3245 }
3246
Joe Perches310cd062020-10-15 20:11:52 -07003247 if (WARN("REPEATED_WORD",
3248 "Possible repeated word: '$first'\n" . $herecurr) &&
3249 $fix) {
3250 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3251 }
3252 }
3253
3254 # if it's a repeated word on consecutive lines in a comment block
3255 if ($prevline =~ /$;+\s*$/ &&
3256 $prevrawline =~ /($word_pattern)\s*$/) {
3257 my $last_word = $1;
3258 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3259 if (WARN("REPEATED_WORD",
3260 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3261 $fix) {
3262 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3263 }
3264 }
3265 }
3266 }
3267
Andy Whitcroft306708542008-10-15 22:02:28 -07003268# ignore non-hunk lines and lines being removed
3269 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003270
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003271#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003272 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003273 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07003274 if (ERROR("DOS_LINE_ENDINGS",
3275 "DOS line endings\n" . $herevet) &&
3276 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003277 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003278 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003279 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3280 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003281 if (ERROR("TRAILING_WHITESPACE",
3282 "trailing whitespace\n" . $herevet) &&
3283 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003284 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003285 }
3286
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003287 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003288 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07003289
Josh Triplett4783f892013-11-12 15:10:12 -08003290# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003291 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003292 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003293 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3294 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003295 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003296 my $msg_level = \&ERROR;
3297 $msg_level = \&CHK if ($file);
3298 &{$msg_level}("FSF_MAILING_ADDRESS",
3299 "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 -08003300 }
3301
Andi Kleen33549572010-05-24 14:33:29 -07003302# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003303# Only applies when adding the entry originally, after that we do not have
3304# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003305 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003306 # 'choice' is usually the last thing on the line (though
3307 # Kconfig supports named choices), so use a word boundary
3308 # (\b) rather than a whitespace character (\s)
3309 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003310 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003311 my $cnt = $realcnt;
3312 my $ln = $linenr + 1;
3313 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003314 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003315 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003316 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003317 $f = $lines[$ln - 1];
3318 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3319 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003320
3321 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003322 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003323
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003324 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003325 $is_start = 1;
Masahiro Yamada22a4ac02020-06-17 12:02:20 +09003326 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003327 $length = -1;
3328 }
3329
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003330 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003331 $f =~ s/#.*//;
3332 $f =~ s/^\s+//;
3333 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003334
3335 # This only checks context lines in the patch
3336 # and so hopefully shouldn't trigger false
3337 # positives, even though some of these are
3338 # common words in help texts
3339 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3340 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003341 $is_end = 1;
3342 last;
3343 }
Andi Kleen33549572010-05-24 14:33:29 -07003344 $length++;
3345 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003346 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3347 WARN("CONFIG_DESCRIPTION",
3348 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3349 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003350 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003351 }
3352
Joe Perches7ccf41a2020-06-04 16:50:33 -07003353# check MAINTAINERS entries
3354 if ($realfile =~ /^MAINTAINERS$/) {
3355# check MAINTAINERS entries for the right form
3356 if ($rawline =~ /^\+[A-Z]:/ &&
3357 $rawline !~ /^\+[A-Z]:\t\S/) {
3358 if (WARN("MAINTAINERS_STYLE",
3359 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3360 $fix) {
3361 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3362 }
3363 }
3364# check MAINTAINERS entries for the right ordering too
3365 my $preferred_order = 'MRLSWQBCPTFXNK';
3366 if ($rawline =~ /^\+[A-Z]:/ &&
3367 $prevrawline =~ /^[\+ ][A-Z]:/) {
3368 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3369 my $cur = $1;
3370 my $curval = $2;
3371 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3372 my $prev = $1;
3373 my $prevval = $2;
3374 my $curindex = index($preferred_order, $cur);
3375 my $previndex = index($preferred_order, $prev);
3376 if ($curindex < 0) {
3377 WARN("MAINTAINERS_STYLE",
3378 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3379 } else {
3380 if ($previndex >= 0 && $curindex < $previndex) {
3381 WARN("MAINTAINERS_STYLE",
3382 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3383 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3384 ($prev eq 'X' && $cur eq 'X')) &&
3385 ($prevval cmp $curval) > 0) {
3386 WARN("MAINTAINERS_STYLE",
3387 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3388 }
3389 }
Joe Perches628f91a2017-07-10 15:52:07 -07003390 }
3391 }
3392
Christoph Jaeger327953e2015-02-13 14:38:29 -08003393# discourage the use of boolean for type definition attributes of Kconfig options
3394 if ($realfile =~ /Kconfig/ &&
3395 $line =~ /^\+\s*\bboolean\b/) {
3396 WARN("CONFIG_TYPE_BOOLEAN",
3397 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3398 }
3399
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003400 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3401 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3402 my $flag = $1;
3403 my $replacement = {
3404 'EXTRA_AFLAGS' => 'asflags-y',
3405 'EXTRA_CFLAGS' => 'ccflags-y',
3406 'EXTRA_CPPFLAGS' => 'cppflags-y',
3407 'EXTRA_LDFLAGS' => 'ldflags-y',
3408 };
3409
3410 WARN("DEPRECATED_VARIABLE",
3411 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3412 }
3413
Rob Herringbff5da42014-01-23 15:54:51 -08003414# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003415 if (defined $root &&
3416 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3417 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3418
Rob Herringbff5da42014-01-23 15:54:51 -08003419 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3420
Florian Vaussardcc933192014-04-03 14:49:27 -07003421 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003422 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003423
Rob Herringbff5da42014-01-23 15:54:51 -08003424 foreach my $compat (@compats) {
3425 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003426 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3427 my $compat3 = $compat;
3428 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3429 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003430 if ( $? >> 8 ) {
3431 WARN("UNDOCUMENTED_DT_STRING",
3432 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3433 }
3434
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003435 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3436 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003437 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003438 if ( $? >> 8 ) {
3439 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003440 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003441 }
3442 }
3443 }
3444
Rob Herring9f3a8992018-04-10 16:33:13 -07003445# check for using SPDX license tag at beginning of files
3446 if ($realline == $checklicenseline) {
3447 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3448 $checklicenseline = 2;
3449 } elsif ($rawline =~ /^\+/) {
3450 my $comment = "";
3451 if ($realfile =~ /\.(h|s|S)$/) {
3452 $comment = '/*';
3453 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3454 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003455 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003456 $comment = '#';
3457 } elsif ($realfile =~ /\.rst$/) {
3458 $comment = '..';
3459 }
3460
Joe Perchesfdf13692019-03-07 16:28:32 -08003461# check SPDX comment style for .[chsS] files
3462 if ($realfile =~ /\.[chsS]$/ &&
3463 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003464 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003465 WARN("SPDX_LICENSE_TAG",
3466 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3467 }
3468
Rob Herring9f3a8992018-04-10 16:33:13 -07003469 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003470 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3471 WARN("SPDX_LICENSE_TAG",
3472 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003473 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003474 my $spdx_license = $1;
3475 if (!is_SPDX_License_valid($spdx_license)) {
3476 WARN("SPDX_LICENSE_TAG",
3477 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3478 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003479 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3480 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3481 my $msg_level = \&WARN;
3482 $msg_level = \&CHK if ($file);
3483 if (&{$msg_level}("SPDX_LICENSE_TAG",
3484
3485 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3486 $fix) {
3487 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3488 }
3489 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003490 }
3491 }
3492 }
3493
Joe Perchesa0154cd2020-10-15 20:12:19 -07003494# check for embedded filenames
3495 if ($rawline =~ /^\+.*\Q$realfile\E/) {
3496 WARN("EMBEDDED_FILENAME",
3497 "It's generally not useful to have the filename in the file\n" . $herecurr);
3498 }
3499
Andy Whitcroft5368df202008-10-15 22:02:27 -07003500# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003501 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003502
Joe Perchesa8da38a2019-03-07 16:28:42 -08003503# check for using SPDX-License-Identifier on the wrong line number
3504 if ($realline != $checklicenseline &&
3505 $rawline =~ /\bSPDX-License-Identifier:/ &&
3506 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3507 WARN("SPDX_LICENSE_TAG",
3508 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3509 }
3510
Joe Perches47e0c882015-06-25 15:02:57 -07003511# line length limit (with some exclusions)
3512#
3513# There are a few types of lines that may extend beyond $max_line_length:
3514# logging functions like pr_info that end in a string
3515# lines with a single string
3516# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003517# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003518#
3519# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003520# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003521# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3522# LONG_LINE all other lines longer than $max_line_length
3523#
3524# if LONG_LINE is ignored, the other 2 types are also ignored
3525#
3526
Joe Perchesb4749e92015-07-17 16:24:01 -07003527 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003528 my $msg_type = "LONG_LINE";
3529
3530 # Check the allowed long line types first
3531
3532 # logging functions that end in a string that starts
3533 # before $max_line_length
3534 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3535 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3536 $msg_type = "";
3537
3538 # lines with only strings (w/ possible termination)
3539 # #defines with only strings
3540 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3541 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3542 $msg_type = "";
3543
Joe Perchescc147502017-11-17 15:28:44 -08003544 # More special cases
3545 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3546 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003547 $msg_type = "";
3548
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003549 # URL ($rawline is used in case the URL is in a comment)
3550 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3551 $msg_type = "";
3552
Joe Perches47e0c882015-06-25 15:02:57 -07003553 # Otherwise set the alternate message types
3554
3555 # a comment starts before $max_line_length
3556 } elsif ($line =~ /($;[\s$;]*)$/ &&
3557 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3558 $msg_type = "LONG_LINE_COMMENT"
3559
3560 # a quoted string starts before $max_line_length
3561 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3562 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3563 $msg_type = "LONG_LINE_STRING"
3564 }
3565
3566 if ($msg_type ne "" &&
3567 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003568 my $msg_level = \&WARN;
3569 $msg_level = \&CHK if ($file);
3570 &{$msg_level}($msg_type,
3571 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003572 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003573 }
3574
Andy Whitcroft8905a672007-11-28 16:21:06 -08003575# check for adding lines without a newline.
3576 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Tom Rix47ca69b2020-12-15 20:44:40 -08003577 if (WARN("MISSING_EOF_NEWLINE",
3578 "adding a line without newline at end of file\n" . $herecurr) &&
3579 $fix) {
3580 fix_delete_line($fixlinenr+1, "No newline at end of file");
3581 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003582 }
3583
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003584# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003585 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003586
3587# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003588# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003589 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3590 $rawline =~ /^\+\s* \s*/) {
3591 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003592 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003593 if (ERROR("CODE_INDENT",
3594 "code indent should use tabs where possible\n" . $herevet) &&
3595 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003596 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003597 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003598 }
3599
Alberto Panizzo08e44362010-03-05 13:43:54 -08003600# check for space before tabs.
3601 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3602 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003603 if (WARN("SPACE_BEFORE_TAB",
3604 "please, no space before tabs\n" . $herevet) &&
3605 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003606 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003607 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003608 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003609 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003610 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003611 }
3612
Joe Perches6a487212018-04-10 16:34:04 -07003613# check for assignments on the start of a line
3614 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
Aditya Srivastavada7355a2020-12-15 20:45:06 -08003615 my $operator = $1;
3616 if (CHK("ASSIGNMENT_CONTINUATIONS",
3617 "Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3618 $fix && $prevrawline =~ /^\+/) {
3619 # add assignment operator to the previous line, remove from current line
3620 $fixed[$fixlinenr - 1] .= " $operator";
3621 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3622 }
Joe Perches6a487212018-04-10 16:34:04 -07003623 }
3624
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003625# check for && or || at the start of a line
3626 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
Aditya Srivastava8e08f072020-12-15 20:45:09 -08003627 my $operator = $1;
3628 if (CHK("LOGICAL_CONTINUATIONS",
3629 "Logical continuations should be on the previous line\n" . $hereprev) &&
3630 $fix && $prevrawline =~ /^\+/) {
3631 # insert logical operator at last non-comment, non-whitepsace char on previous line
3632 $prevline =~ /[\s$;]*$/;
3633 my $line_end = substr($prevrawline, $-[0]);
3634 $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
3635 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3636 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003637 }
3638
Joe Perchesa91e8992016-05-20 17:04:05 -07003639# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003640 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003641 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003642 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003643 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003644 if (WARN("TABSTOP",
3645 "Statements should start on a tabstop\n" . $herecurr) &&
3646 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003647 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003648 }
3649 }
3650 }
3651
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003652# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003653 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003654 $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 -07003655 $prevline =~ /^\+(\t*)(.*)$/;
3656 my $oldindent = $1;
3657 my $rest = $2;
3658
3659 my $pos = pos_last_openparen($rest);
3660 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003661 $line =~ /^(\+| )([ \t]*)/;
3662 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003663
3664 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003665 "\t" x ($pos / $tabsize) .
3666 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003667 my $goodspaceindent = $oldindent . " " x $pos;
3668
3669 if ($newindent ne $goodtabindent &&
3670 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003671
3672 if (CHK("PARENTHESIS_ALIGNMENT",
3673 "Alignment should match open parenthesis\n" . $hereprev) &&
3674 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003675 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003676 s/^\+[ \t]*/\+$goodtabindent/;
3677 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003678 }
3679 }
3680 }
3681
Joe Perches6ab3a972015-04-16 12:44:05 -07003682# check for space after cast like "(int) foo" or "(struct foo) bar"
3683# avoid checking a few false positives:
3684# "sizeof(<type>)" or "__alignof__(<type>)"
3685# function pointer declarations like "(*foo)(int) = bar;"
3686# structure definitions like "(struct foo) { 0 };"
3687# multiline macros that define functions
3688# known attributes or the __attribute__ keyword
3689 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3690 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003691 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003692 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003693 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003694 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003695 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003696 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003697 }
3698
Joe Perches86406b12015-09-09 15:37:41 -07003699# Block comment styles
3700# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003701 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003702 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003703 $rawline =~ /^\+[ \t]*\*/ &&
Łukasz Stelmachc70735c2020-10-15 20:12:25 -07003704 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
Joe Perches05880602012-10-04 17:13:35 -07003705 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3706 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3707 }
3708
Joe Perches86406b12015-09-09 15:37:41 -07003709# Block comments use * on subsequent lines
3710 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3711 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003712 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003713 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003714 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003715 WARN("BLOCK_COMMENT_STYLE",
3716 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003717 }
3718
Joe Perches86406b12015-09-09 15:37:41 -07003719# Block comments use */ on trailing lines
3720 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003721 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3722 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3723 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003724 WARN("BLOCK_COMMENT_STYLE",
3725 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003726 }
3727
Joe Perches08eb9b82016-10-11 13:51:50 -07003728# Block comment * alignment
3729 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf2075242016-10-11 13:52:05 -07003730 $line =~ /^\+[ \t]*$;/ && #leading comment
3731 $rawline =~ /^\+[ \t]*\*/ && #leading *
3732 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003733 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf2075242016-10-11 13:52:05 -07003734 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3735 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003736 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf2075242016-10-11 13:52:05 -07003737 if (defined($1)) {
3738 $oldindent = expand_tabs($1);
3739 } else {
3740 $prevrawline =~ m@^\+(.*/?)\*@;
3741 $oldindent = expand_tabs($1);
3742 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003743 $rawline =~ m@^\+([ \t]*)\*@;
3744 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003745 $newindent = expand_tabs($newindent);
Joe Perchesaf2075242016-10-11 13:52:05 -07003746 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003747 WARN("BLOCK_COMMENT_STYLE",
3748 "Block comments should align the * on each line\n" . $hereprev);
3749 }
3750 }
3751
Joe Perches7f619192014-08-06 16:10:39 -07003752# check for missing blank lines after struct/union declarations
3753# with exceptions for various attributes and macros
3754 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3755 $line =~ /^\+/ &&
3756 !($line =~ /^\+\s*$/ ||
3757 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3758 $line =~ /^\+\s*MODULE_/i ||
3759 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3760 $line =~ /^\+[a-z_]*init/ ||
3761 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3762 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003763 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003764 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003765 if (CHK("LINE_SPACING",
3766 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3767 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003768 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003769 }
Joe Perches7f619192014-08-06 16:10:39 -07003770 }
3771
Joe Perches365dd4e2014-08-06 16:10:42 -07003772# check for multiple consecutive blank lines
3773 if ($prevline =~ /^[\+ ]\s*$/ &&
3774 $line =~ /^\+\s*$/ &&
3775 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003776 if (CHK("LINE_SPACING",
3777 "Please don't use multiple blank lines\n" . $hereprev) &&
3778 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003779 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003780 }
3781
Joe Perches365dd4e2014-08-06 16:10:42 -07003782 $last_blank_line = $linenr;
3783 }
3784
Joe Perches3b617e32014-04-03 14:49:28 -07003785# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003786 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3787 # actual declarations
3788 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003789 # function pointer declarations
3790 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003791 # foo bar; where foo is some local typedef or #define
3792 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3793 # known declaration macros
3794 $prevline =~ /^\+\s+$declaration_macros/) &&
3795 # for "else if" which can look like "$Ident $Ident"
3796 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3797 # other possible extensions of declaration lines
3798 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3799 # not starting a section or a macro "\" extended line
3800 $prevline =~ /(?:\{\s*|\\)$/) &&
3801 # looks like a declaration
3802 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003803 # function pointer declarations
3804 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003805 # foo bar; where foo is some local typedef or #define
3806 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3807 # known declaration macros
3808 $sline =~ /^\+\s+$declaration_macros/ ||
3809 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003810 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003811 # start or end of block or continuation of declaration
3812 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3813 # bitfield continuation
3814 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3815 # other possible extensions of declaration lines
3816 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3817 # indentation of previous and current line are the same
3818 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003819 if (WARN("LINE_SPACING",
3820 "Missing a blank line after declarations\n" . $hereprev) &&
3821 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003822 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003823 }
Joe Perches3b617e32014-04-03 14:49:28 -07003824 }
3825
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003826# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003827# Exceptions:
3828# 1) within comments
3829# 2) indented preprocessor commands
3830# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003831 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003832 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003833 if (WARN("LEADING_SPACE",
3834 "please, no spaces at the start of a line\n" . $herevet) &&
3835 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003836 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003837 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003838 }
3839
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003840# check we are in a valid C source file if not then ignore this hunk
3841 next if ($realfile !~ /\.(h|c)$/);
3842
Joe Perches5751a242017-11-17 15:28:52 -08003843# check for unusual line ending [ or (
3844 if ($line =~ /^\+.*([\[\(])\s*$/) {
3845 CHK("OPEN_ENDED_LINE",
3846 "Lines should not end with a '$1'\n" . $herecurr);
3847 }
3848
Joe Perches4dbed762017-05-08 15:55:39 -07003849# check if this appears to be the start function declaration, save the name
3850 if ($sline =~ /^\+\{\s*$/ &&
3851 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3852 $context_function = $1;
3853 }
3854
3855# check if this appears to be the end of function declaration
3856 if ($sline =~ /^\+\}\s*$/) {
3857 undef $context_function;
3858 }
3859
Joe Perches032a4c02014-08-06 16:10:29 -07003860# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003861# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003862# if the previous line is a break or return and is indented 1 tab more...
3863 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3864 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003865 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3866 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3867 defined $lines[$linenr] &&
3868 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003869 WARN("UNNECESSARY_ELSE",
3870 "else is not generally useful after a break or return\n" . $hereprev);
3871 }
3872 }
3873
Joe Perchesc00df192014-08-06 16:11:01 -07003874# check indentation of a line with a break;
Joe Perchesdc58bc52020-12-15 20:44:33 -08003875# if the previous line is a goto, return or break
3876# and is indented the same # of tabs
Joe Perchesc00df192014-08-06 16:11:01 -07003877 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3878 my $tabs = $1;
Joe Perchesdc58bc52020-12-15 20:44:33 -08003879 if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
3880 if (WARN("UNNECESSARY_BREAK",
3881 "break is not useful after a $1\n" . $hereprev) &&
3882 $fix) {
3883 fix_delete_line($fixlinenr, $rawline);
3884 }
Joe Perchesc00df192014-08-06 16:11:01 -07003885 }
3886 }
3887
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003888# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003889 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003890 WARN("CVS_KEYWORD",
3891 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003892 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003893
Joe Perches56e77d72013-02-21 16:44:14 -08003894# check for old HOTPLUG __dev<foo> section markings
3895 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3896 WARN("HOTPLUG_SECTION",
3897 "Using $1 is unnecessary\n" . $herecurr);
3898 }
3899
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003900# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003901 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3902 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003903#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003904 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003905 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a222008-10-15 22:02:30 -07003906 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003907 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003908 $stat =~ s/\n./\n /g;
3909 $cond =~ s/\n./\n /g;
3910
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003911#print "linenr<$linenr> <$stat>\n";
3912 # If this statement has no statement boundaries within
3913 # it there is no point in retrying a statement scan
3914 # until we hit end of it.
3915 my $frag = $stat; $frag =~ s/;+\s*$//;
3916 if ($frag !~ /(?:{|;)/) {
3917#print "skip<$line_nr_next>\n";
3918 $suppress_statement = $line_nr_next;
3919 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003920
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003921 # Find the real next line.
3922 $realline_next = $line_nr_next;
3923 if (defined $realline_next &&
3924 (!defined $lines[$realline_next - 1] ||
3925 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3926 $realline_next++;
3927 }
3928
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003929 my $s = $stat;
3930 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003931
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003932 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003933 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003934
3935 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003936 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003937
Andy Whitcroft463f2862009-09-21 17:04:34 -07003938 } elsif ($s =~ /^.\s*else\b/s) {
3939
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003940 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003941 } 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 -07003942 my $type = $1;
3943 $type =~ s/\s+/ /g;
3944 possible($type, "A:" . $s);
3945
Andy Whitcroft8905a672007-11-28 16:21:06 -08003946 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003947 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003948 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003949 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003950
3951 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003952 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003953 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003954 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003955
3956 # Check for any sort of function declaration.
3957 # int foo(something bar, other baz);
3958 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003959 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 -08003960 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003961
Andy Whitcroftcf655042008-03-04 14:28:20 -08003962 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003963 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003964 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003965
Andy Whitcroft8905a672007-11-28 16:21:06 -08003966 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003967 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003968
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003969 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003970 }
3971 }
3972 }
3973
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003974 }
3975
Andy Whitcroft653d4872007-06-23 17:16:34 -07003976#
3977# Checks which may be anchored in the context.
3978#
3979
3980# Check for switch () and associated case and default
3981# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003982 if ($line=~/\bswitch\s*\(.*\)/) {
3983 my $err = '';
3984 my $sep = '';
3985 my @ctx = ctx_block_outer($linenr, $realcnt);
3986 shift(@ctx);
3987 for my $ctx (@ctx) {
3988 my ($clen, $cindent) = line_stats($ctx);
3989 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3990 $indent != $cindent) {
3991 $err .= "$sep$ctx\n";
3992 $sep = '';
3993 } else {
3994 $sep = "[...]\n";
3995 }
3996 }
3997 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003998 ERROR("SWITCH_CASE_INDENT_LEVEL",
3999 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004000 }
4001 }
4002
4003# if/while/etc brace do not go on next line, unless defining a do while loop,
4004# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07004005 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 -07004006 my $pre_ctx = "$1$2";
4007
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004008 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08004009
4010 if ($line =~ /^\+\t{6,}/) {
4011 WARN("DEEP_INDENTATION",
4012 "Too many leading tabs - consider code refactoring\n" . $herecurr);
4013 }
4014
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004015 my $ctx_cnt = $realcnt - $#ctx - 1;
4016 my $ctx = join("\n", @ctx);
4017
Andy Whitcroft548596d2008-07-23 21:29:01 -07004018 my $ctx_ln = $linenr;
4019 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004020
Andy Whitcroft548596d2008-07-23 21:29:01 -07004021 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4022 defined $lines[$ctx_ln - 1] &&
4023 $lines[$ctx_ln - 1] =~ /^-/)) {
4024 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4025 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004026 $ctx_ln++;
4027 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07004028
Andy Whitcroft53210162008-07-23 21:29:03 -07004029 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
4030 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07004031
Joe Perchesd752fcc2014-08-06 16:11:05 -07004032 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004033 ERROR("OPEN_BRACE",
4034 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07004035 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07004036 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004037 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4038 $ctx =~ /\)\s*\;\s*$/ &&
4039 defined $lines[$ctx_ln - 1])
4040 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004041 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
4042 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004043 WARN("TRAILING_SEMICOLON",
4044 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07004045 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004046 }
4047 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004048 }
4049
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004050# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07004051 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 -08004052 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4053 ctx_statement_block($linenr, $realcnt, 0)
4054 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004055 my ($s, $c) = ($stat, $cond);
4056
4057 substr($s, 0, length($c), '');
4058
Joe Perches9f5af482015-09-09 15:37:30 -07004059 # remove inline comments
4060 $s =~ s/$;/ /g;
4061 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004062
4063 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07004064 my @newlines = ($c =~ /\n/gs);
4065 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004066
Joe Perches9f5af482015-09-09 15:37:30 -07004067 # Make sure we remove the line prefixes as we have
4068 # none on the first line, and are going to readd them
4069 # where necessary.
4070 $s =~ s/\n./\n/gs;
4071 while ($s =~ /\n\s+\\\n/) {
4072 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
4073 }
4074
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004075 # We want to check the first line inside the block
4076 # starting at the end of the conditional, so remove:
4077 # 1) any blank line termination
4078 # 2) any opening brace { on end of the line
4079 # 3) any do (...) {
4080 my $continuation = 0;
4081 my $check = 0;
4082 $s =~ s/^.*\bdo\b//;
4083 $s =~ s/^\s*{//;
4084 if ($s =~ s/^\s*\\//) {
4085 $continuation = 1;
4086 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004087 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004088 $check = 1;
4089 $cond_lines++;
4090 }
4091
4092 # Also ignore a loop construct at the end of a
4093 # preprocessor statement.
4094 if (($prevline =~ /^.\s*#\s*define\s/ ||
4095 $prevline =~ /\\\s*$/) && $continuation == 0) {
4096 $check = 0;
4097 }
4098
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004099 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07004100 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004101 while ($cond_ptr != $cond_lines) {
4102 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004103
Andy Whitcroftf16fa282008-10-15 22:02:32 -07004104 # If we see an #else/#elif then the code
4105 # is not linear.
4106 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4107 $check = 0;
4108 }
4109
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004110 # Ignore:
4111 # 1) blank lines, they should be at 0,
4112 # 2) preprocessor lines, and
4113 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07004114 if ($continuation ||
4115 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004116 $s =~ /^\s*#\s*?/ ||
4117 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07004118 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07004119 if ($s =~ s/^.*?\n//) {
4120 $cond_lines++;
4121 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004122 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004123 }
4124
4125 my (undef, $sindent) = line_stats("+" . $s);
4126 my $stat_real = raw_line($linenr, $cond_lines);
4127
4128 # Check if either of these lines are modified, else
4129 # this is not this patch's fault.
4130 if (!defined($stat_real) ||
4131 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
4132 $check = 0;
4133 }
4134 if (defined($stat_real) && $cond_lines > 1) {
4135 $stat_real = "[...]\n$stat_real";
4136 }
4137
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004138 #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 -07004139
Joe Perches9f5af482015-09-09 15:37:30 -07004140 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07004141 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07004142 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07004143 ($sindent == $indent &&
4144 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07004145 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004146 WARN("SUSPECT_CODE_INDENT",
4147 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004148 }
4149 }
4150
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004151 # Track the 'values' across context and added lines.
4152 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004153 my ($curr_values, $curr_vars) =
4154 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004155 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004156 if ($dbg_values) {
4157 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004158 print "$linenr > .$outline\n";
4159 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004160 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004161 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004162 $prev_values = substr($curr_values, -1);
4163
Andy Whitcroft00df3442007-06-08 13:47:06 -07004164#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07004165 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07004166
Joe Perches99ca38c2020-10-15 20:12:09 -07004167# check for self assignments used to avoid compiler warnings
4168# e.g.: int foo = foo, *bar = NULL;
4169# struct foo bar = *(&(bar));
4170 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
4171 my $var = $1;
4172 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
4173 WARN("SELF_ASSIGNMENT",
4174 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
4175 }
4176 }
4177
Joe Perches11ca40a2016-12-12 16:46:31 -08004178# check for dereferences that span multiple lines
4179 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
4180 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
4181 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
4182 my $ref = $1;
4183 $line =~ /^.\s*($Lval)/;
4184 $ref .= $1;
4185 $ref =~ s/\s//g;
4186 WARN("MULTILINE_DEREFERENCE",
4187 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
4188 }
4189
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004190# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07004191 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 -07004192 my $type = $1;
4193 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07004194 $var = "" if (!defined $var);
4195 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004196 my $sign = $1;
4197 my $pointer = $2;
4198
4199 $pointer = "" if (!defined $pointer);
4200
4201 if (WARN("UNSPECIFIED_INT",
4202 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4203 $fix) {
4204 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07004205 my $comp_pointer = $pointer;
4206 $comp_pointer =~ s/\s//g;
4207 $decl .= $comp_pointer;
4208 $decl = rtrim($decl) if ($var eq "");
4209 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004210 }
4211 }
4212 }
4213
Andy Whitcroft653d4872007-06-23 17:16:34 -07004214# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07004215 if ($dbg_type) {
4216 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004217 ERROR("TEST_TYPE",
4218 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004219 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004220 ERROR("TEST_NOT_TYPE",
4221 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004222 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004223 next;
4224 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004225# TEST: allow direct testing of the attribute matcher.
4226 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004227 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004228 ERROR("TEST_ATTR",
4229 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004230 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004231 ERROR("TEST_NOT_ATTR",
4232 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004233 }
4234 next;
4235 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004236
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004237# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07004238 if ($line =~ /^.\s*{/ &&
4239 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07004240 if (ERROR("OPEN_BRACE",
4241 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004242 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4243 fix_delete_line($fixlinenr - 1, $prevrawline);
4244 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004245 my $fixedline = $prevrawline;
4246 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004247 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004248 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004249 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004250 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004251 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004252 }
4253
Andy Whitcroft653d4872007-06-23 17:16:34 -07004254#
4255# Checks which are anchored on the added line.
4256#
4257
4258# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004259 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07004260 my $path = $1;
4261 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004262 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08004263 "malformed #include filename\n" . $herecurr);
4264 }
4265 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4266 ERROR("UAPI_INCLUDE",
4267 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004268 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004269 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004270
4271# no C99 // comments
4272 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07004273 if (ERROR("C99_COMMENTS",
4274 "do not use C99 // comments\n" . $herecurr) &&
4275 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004276 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07004277 if ($line =~ /\/\/(.*)$/) {
4278 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07004279 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07004280 }
4281 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004282 }
4283 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004284 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004285 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004286
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004287# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4288# the whole statement.
4289#print "APW <$lines[$realline_next - 1]>\n";
4290 if (defined $realline_next &&
4291 exists $lines[$realline_next - 1] &&
4292 !defined $suppress_export{$realline_next} &&
Christoph Hellwig36794822021-02-02 13:13:34 +01004293 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004294 # Handle definitions which produce identifiers with
4295 # a prefix:
4296 # XXX(foo);
4297 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004298 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08004299 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004300 $name =~ /^${Ident}_$2/) {
4301#print "FOO C name<$name>\n";
4302 $suppress_export{$realline_next} = 1;
4303
4304 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004305 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07004306 ^.DEFINE_$Ident\(\Q$name\E\)|
4307 ^.DECLARE_$Ident\(\Q$name\E\)|
4308 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004309 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4310 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07004311 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004312#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4313 $suppress_export{$realline_next} = 2;
4314 } else {
4315 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004316 }
4317 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004318 if (!defined $suppress_export{$linenr} &&
4319 $prevline =~ /^.\s*$/ &&
Christoph Hellwig36794822021-02-02 13:13:34 +01004320 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004321#print "FOO B <$lines[$linenr - 1]>\n";
4322 $suppress_export{$linenr} = 2;
4323 }
4324 if (defined $suppress_export{$linenr} &&
4325 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004326 WARN("EXPORT_SYMBOL",
4327 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004328 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004329
Joe Eloff5150bda2010-08-09 17:21:00 -07004330# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004331 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004332 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004333 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004334 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004335 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004336 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004337 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004338# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004339 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004340 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004341 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004342 $herecurr) &&
4343 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004344 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004345 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004346 }
4347
Joe Perches18130872014-08-06 16:11:22 -07004348# check for misordered declarations of char/short/int/long with signed/unsigned
4349 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4350 my $tmp = trim($1);
4351 WARN("MISORDERED_TYPE",
4352 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4353 }
4354
Joe Perches809e0822018-08-21 21:58:12 -07004355# check for unnecessary <signed> int declarations of short/long/long long
4356 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4357 my $type = trim($1);
4358 next if ($type !~ /\bint\b/);
4359 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4360 my $new_type = $type;
4361 $new_type =~ s/\b\s*int\s*\b/ /;
4362 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4363 $new_type =~ s/^const\s+//;
4364 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4365 $new_type = "const $new_type" if ($type =~ /^const\b/);
4366 $new_type =~ s/\s+/ /g;
4367 $new_type = trim($new_type);
4368 if (WARN("UNNECESSARY_INT",
4369 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4370 $fix) {
4371 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4372 }
4373 }
4374
Joe Perchescb710ec2010-10-26 14:23:20 -07004375# check for static const char * arrays.
4376 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004377 WARN("STATIC_CONST_CHAR_ARRAY",
4378 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004379 $herecurr);
Joe Perches77b8c0a82019-01-03 15:26:59 -08004380 }
4381
4382# check for initialized const char arrays that should be static const
4383 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4384 if (WARN("STATIC_CONST_CHAR_ARRAY",
4385 "const array should probably be static const\n" . $herecurr) &&
4386 $fix) {
4387 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4388 }
4389 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004390
4391# check for static char foo[] = "bar" declarations.
4392 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004393 WARN("STATIC_CONST_CHAR_ARRAY",
4394 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004395 $herecurr);
Joe Perches77b8c0a82019-01-03 15:26:59 -08004396 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004397
Joe Perchesab7e23f2015-04-16 12:44:22 -07004398# check for const <foo> const where <foo> is not a pointer or array type
4399 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4400 my $found = $1;
4401 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4402 WARN("CONST_CONST",
4403 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4404 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4405 WARN("CONST_CONST",
4406 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4407 }
4408 }
4409
Joe Perches73169762020-12-15 20:44:30 -08004410# check for const static or static <non ptr type> const declarations
4411# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
4412 if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
4413 $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
4414 if (WARN("STATIC_CONST",
4415 "Move const after static - use 'static const $1'\n" . $herecurr) &&
4416 $fix) {
4417 $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
4418 $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
4419 }
4420 }
4421
Joe Perches9b0fa602014-04-03 14:49:18 -07004422# check for non-global char *foo[] = {"bar", ...} declarations.
4423 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4424 WARN("STATIC_CONST_CHAR_ARRAY",
4425 "char * array declaration might be better as static const\n" .
4426 $herecurr);
4427 }
4428
Joe Perchesb598b672015-04-16 12:44:36 -07004429# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4430 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4431 my $array = $1;
4432 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4433 my $array_div = $1;
4434 if (WARN("ARRAY_SIZE",
4435 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4436 $fix) {
4437 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4438 }
4439 }
4440 }
4441
Joe Perchesb36190c2014-01-27 17:07:18 -08004442# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004443 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004444 if (ERROR("FUNCTION_WITHOUT_ARGS",
4445 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4446 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004447 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004448 }
4449 }
4450
Andy Whitcroft653d4872007-06-23 17:16:34 -07004451# check for new typedefs, only function parameters and sparse annotations
4452# make sense.
4453 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004454 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004455 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004456 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004457 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004458 WARN("NEW_TYPEDEFS",
4459 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004460 }
4461
4462# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004463 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004464 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4465 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004466 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004467
Andy Whitcroft65863862009-01-06 14:41:21 -08004468 # Should start with a space.
4469 $to =~ s/^(\S)/ $1/;
4470 # Should not end with a space.
4471 $to =~ s/\s+$//;
4472 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d12009-01-15 13:51:05 -08004473 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004474 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004475
Joe Perches3705ce52013-07-03 15:05:31 -07004476## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004477 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004478 if (ERROR("POINTER_LOCATION",
4479 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4480 $fix) {
4481 my $sub_from = $ident;
4482 my $sub_to = $ident;
4483 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004484 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004485 s@\Q$sub_from\E@$sub_to@;
4486 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004487 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004488 }
4489 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4490 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004491 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004492
Andy Whitcroft65863862009-01-06 14:41:21 -08004493 # Should start with a space.
4494 $to =~ s/^(\S)/ $1/;
4495 # Should not end with a space.
4496 $to =~ s/\s+$//;
4497 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d12009-01-15 13:51:05 -08004498 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004499 }
4500 # Modifiers should have spaces.
4501 $to =~ s/(\b$Modifier$)/$1 /;
4502
Joe Perches3705ce52013-07-03 15:05:31 -07004503## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004504 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004505 if (ERROR("POINTER_LOCATION",
4506 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4507 $fix) {
4508
4509 my $sub_from = $match;
4510 my $sub_to = $match;
4511 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004512 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004513 s@\Q$sub_from\E@$sub_to@;
4514 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004515 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004516 }
4517
Joe Perches9d3e3c72015-09-09 15:37:27 -07004518# avoid BUG() or BUG_ON()
4519 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004520 my $msg_level = \&WARN;
4521 $msg_level = \&CHK if ($file);
4522 &{$msg_level}("AVOID_BUG",
4523 "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 -07004524 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004525
Joe Perches9d3e3c72015-09-09 15:37:27 -07004526# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004527 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004528 WARN("LINUX_VERSION_CODE",
4529 "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 -08004530 }
4531
Joe Perches17441222011-06-15 15:08:17 -07004532# check for uses of printk_ratelimit
4533 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004534 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004535 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004536 }
4537
Joe Percheseeef5732017-11-17 15:28:41 -08004538# printk should use KERN_* levels
4539 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4540 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4541 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004542 }
4543
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004544# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4545 if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4546 my $printk = $1;
4547 my $modifier = $2;
4548 my $orig = $3;
4549 $modifier = "" if (!defined($modifier));
Joe Perches243f3802012-05-31 16:26:09 -07004550 my $level = lc($orig);
4551 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004552 my $level2 = $level;
4553 $level2 = "dbg" if ($level eq "debug");
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004554 $level .= $modifier;
4555 $level2 .= $modifier;
Joe Perches243f3802012-05-31 16:26:09 -07004556 WARN("PREFER_PR_LEVEL",
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004557 "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 -07004558 }
4559
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004560# prefer dev_<level> to dev_printk(KERN_<LEVEL>
Joe Perchesdc139312013-02-21 16:44:13 -08004561 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4562 my $orig = $1;
4563 my $level = lc($orig);
4564 $level = "warn" if ($level eq "warning");
4565 $level = "dbg" if ($level eq "debug");
4566 WARN("PREFER_DEV_LEVEL",
4567 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4568 }
4569
Nicolas Boichat8020b252020-10-15 20:12:02 -07004570# trace_printk should not be used in production code.
4571 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4572 WARN("TRACE_PRINTK",
4573 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4574 }
4575
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004576# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4577# number of false positives, but assembly files are not checked, so at
4578# least the arch entry code will not trigger this warning.
4579 if ($line =~ /\bENOSYS\b/) {
4580 WARN("ENOSYS",
4581 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4582 }
4583
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004584# ENOTSUPP is not a standard error code and should be avoided in new patches.
4585# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4586# Similarly to ENOSYS warning a small number of false positives is expected.
4587 if (!$file && $line =~ /\bENOTSUPP\b/) {
4588 if (WARN("ENOTSUPP",
4589 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4590 $fix) {
4591 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4592 }
4593 }
4594
Andy Whitcroft653d4872007-06-23 17:16:34 -07004595# function brace can't be on same line, except for #defines of do while,
4596# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004597 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004598 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4599 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4600 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004601 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004602 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004603 $fix) {
4604 fix_delete_line($fixlinenr, $rawline);
4605 my $fixed_line = $rawline;
Dwaipayan Ray03f49352020-12-15 20:45:02 -08004606 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
Joe Perches8d182472014-08-06 16:11:12 -07004607 my $line1 = $1;
4608 my $line2 = $2;
4609 fix_insert_line($fixlinenr, ltrim($line1));
4610 fix_insert_line($fixlinenr, "\+{");
4611 if ($line2 !~ /^\s*$/) {
4612 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4613 }
4614 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004615 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004616
Andy Whitcroft8905a672007-11-28 16:21:06 -08004617# open braces for enum, union and struct go on the same line.
4618 if ($line =~ /^.\s*{/ &&
4619 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004620 if (ERROR("OPEN_BRACE",
4621 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4622 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4623 fix_delete_line($fixlinenr - 1, $prevrawline);
4624 fix_delete_line($fixlinenr, $rawline);
4625 my $fixedline = rtrim($prevrawline) . " {";
4626 fix_insert_line($fixlinenr, $fixedline);
4627 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004628 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004629 if ($fixedline !~ /^\+\s*$/) {
4630 fix_insert_line($fixlinenr, $fixedline);
4631 }
4632 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004633 }
4634
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004635# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004636 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4637 if (WARN("SPACING",
4638 "missing space after $1 definition\n" . $herecurr) &&
4639 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004640 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004641 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4642 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004643 }
4644
Joe Perches31070b52014-01-23 15:54:49 -08004645# Function pointer declarations
4646# check spacing between type, funcptr, and args
4647# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004648 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004649 my $declare = $1;
4650 my $pre_pointer_space = $2;
4651 my $post_pointer_space = $3;
4652 my $funcname = $4;
4653 my $post_funcname_space = $5;
4654 my $pre_args_space = $6;
4655
Joe Perches91f72e92014-04-03 14:49:12 -07004656# the $Declare variable will capture all spaces after the type
4657# so check it for a missing trailing missing space but pointer return types
4658# don't need a space so don't warn for those.
4659 my $post_declare_space = "";
4660 if ($declare =~ /(\s+)$/) {
4661 $post_declare_space = $1;
4662 $declare = rtrim($declare);
4663 }
4664 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004665 WARN("SPACING",
4666 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004667 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004668 }
4669
4670# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004671# This test is not currently implemented because these declarations are
4672# equivalent to
4673# int foo(int bar, ...)
4674# and this is form shouldn't/doesn't generate a checkpatch warning.
4675#
4676# elsif ($declare =~ /\s{2,}$/) {
4677# WARN("SPACING",
4678# "Multiple spaces after return type\n" . $herecurr);
4679# }
Joe Perches31070b52014-01-23 15:54:49 -08004680
4681# unnecessary space "type ( *funcptr)(args...)"
4682 if (defined $pre_pointer_space &&
4683 $pre_pointer_space =~ /^\s/) {
4684 WARN("SPACING",
4685 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4686 }
4687
4688# unnecessary space "type (* funcptr)(args...)"
4689 if (defined $post_pointer_space &&
4690 $post_pointer_space =~ /^\s/) {
4691 WARN("SPACING",
4692 "Unnecessary space before function pointer name\n" . $herecurr);
4693 }
4694
4695# unnecessary space "type (*funcptr )(args...)"
4696 if (defined $post_funcname_space &&
4697 $post_funcname_space =~ /^\s/) {
4698 WARN("SPACING",
4699 "Unnecessary space after function pointer name\n" . $herecurr);
4700 }
4701
4702# unnecessary space "type (*funcptr) (args...)"
4703 if (defined $pre_args_space &&
4704 $pre_args_space =~ /^\s/) {
4705 WARN("SPACING",
4706 "Unnecessary space before function pointer arguments\n" . $herecurr);
4707 }
4708
4709 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004710 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004711 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004712 }
4713 }
4714
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004715# check for spacing round square brackets; allowed:
4716# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004717# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4718# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004719 while ($line =~ /(.*?\s)\[/g) {
4720 my ($where, $prefix) = ($-[1], $1);
4721 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004722 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004723 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004724 if (ERROR("BRACKET_SPACE",
4725 "space prohibited before open square bracket '['\n" . $herecurr) &&
4726 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004727 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004728 s/^(\+.*?)\s+\[/$1\[/;
4729 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004730 }
4731 }
4732
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004733# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004734 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004735 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004736 my $ctx_before = substr($line, 0, $-[1]);
4737 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004738
4739 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004740 if ($name =~ /^(?:
4741 if|for|while|switch|return|case|
4742 volatile|__volatile__|
4743 __attribute__|format|__extension__|
4744 asm|__asm__)$/x)
4745 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004746 # cpp #define statements have non-optional spaces, ie
4747 # if there is a space between the name and the open
4748 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004749 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004750
4751 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004752 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004753
4754 # If this whole things ends with a type its most
4755 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004756 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004757
4758 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004759 if (WARN("SPACING",
4760 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4761 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004762 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004763 s/\b$name\s+\(/$name\(/;
4764 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004765 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004766 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004767
Andy Whitcroft653d4872007-06-23 17:16:34 -07004768# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004769 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004770 my $fixed_line = "";
4771 my $line_fixed = 0;
4772
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004773 my $ops = qr{
4774 <<=|>>=|<=|>=|==|!=|
4775 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4776 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004777 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004778 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004779 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004780 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004781
4782## print("element count: <" . $#elements . ">\n");
4783## foreach my $el (@elements) {
4784## print("el: <$el>\n");
4785## }
4786
4787 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004788 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004789
Joe Perches3705ce52013-07-03 15:05:31 -07004790 foreach my $el (@elements) {
4791 push(@fix_elements, substr($rawline, $off, length($el)));
4792 $off += length($el);
4793 }
4794
4795 $off = 0;
4796
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004797 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004798 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004799
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004800 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004801
4802 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4803
4804## print("n: <$n> good: <$good>\n");
4805
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004806 $off += length($elements[$n]);
4807
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004808 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004809 my $ca = substr($opline, 0, $off);
4810 my $cc = '';
4811 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4812 $cc = substr($opline, $off + length($elements[$n + 1]));
4813 }
4814 my $cb = "$ca$;$cc";
4815
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004816 my $a = '';
4817 $a = 'V' if ($elements[$n] ne '');
4818 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004819 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004820 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4821 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004822 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004823
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004824 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004825
4826 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004827 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004828 $c = 'V' if ($elements[$n + 2] ne '');
4829 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004830 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004831 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4832 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004833 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004834 } else {
4835 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004836 }
4837
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004838 my $ctx = "${a}x${c}";
4839
4840 my $at = "(ctx:$ctx)";
4841
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004842 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004843 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004844
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004845 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004846 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004847
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004848 # Get the full operator variant.
4849 my $opv = $op . substr($curr_vars, $off, 1);
4850
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004851 # Ignore operators passed as parameters.
4852 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004853 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004854
Andy Whitcroftcf655042008-03-04 14:28:20 -08004855# # Ignore comments
4856# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004857
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004858 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004859 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004860 if ($ctx !~ /.x[WEBC]/ &&
4861 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004862 if (ERROR("SPACING",
4863 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004864 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004865 $line_fixed = 1;
4866 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004867 }
4868
4869 # // is a comment
4870 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004871
Joe Perchesb00e4812014-04-03 14:49:33 -07004872 # : when part of a bitfield
4873 } elsif ($opv eq ':B') {
4874 # skip the bitfield test for now
4875
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004876 # No spaces for:
4877 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004878 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004879 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004880 if (ERROR("SPACING",
4881 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004882 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004883 if (defined $fix_elements[$n + 2]) {
4884 $fix_elements[$n + 2] =~ s/^\s+//;
4885 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004886 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004887 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004888 }
4889
Joe Perches23810972014-12-10 15:51:32 -08004890 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004891 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004892 my $rtrim_before = 0;
4893 my $space_after = 0;
4894 if ($ctx =~ /Wx./) {
4895 if (ERROR("SPACING",
4896 "space prohibited before that '$op' $at\n" . $hereptr)) {
4897 $line_fixed = 1;
4898 $rtrim_before = 1;
4899 }
4900 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004901 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004902 if (ERROR("SPACING",
4903 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004904 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004905 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004906 $space_after = 1;
4907 }
4908 }
4909 if ($rtrim_before || $space_after) {
4910 if ($rtrim_before) {
4911 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4912 } else {
4913 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4914 }
4915 if ($space_after) {
4916 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004917 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004918 }
4919
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004920 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004921 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004922 #warn "'*' is part of type\n";
4923
4924 # unary operators should have a space before and
4925 # none after. May be left adjacent to another
4926 # unary operator, or a cast
4927 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004928 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004929 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004930 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004931 if (ERROR("SPACING",
4932 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004933 if ($n != $last_after + 2) {
4934 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4935 $line_fixed = 1;
4936 }
Joe Perches3705ce52013-07-03 15:05:31 -07004937 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004938 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004939 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004940 # A unary '*' may be const
4941
4942 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004943 if (ERROR("SPACING",
4944 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004945 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004946 if (defined $fix_elements[$n + 2]) {
4947 $fix_elements[$n + 2] =~ s/^\s+//;
4948 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004949 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004950 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004951 }
4952
4953 # unary ++ and unary -- are allowed no space on one side.
4954 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004955 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004956 if (ERROR("SPACING",
4957 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004958 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004959 $line_fixed = 1;
4960 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004961 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004962 if ($ctx =~ /Wx[BE]/ ||
4963 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004964 if (ERROR("SPACING",
4965 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004966 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004967 $line_fixed = 1;
4968 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004969 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004970 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004971 if (ERROR("SPACING",
4972 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004973 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004974 if (defined $fix_elements[$n + 2]) {
4975 $fix_elements[$n + 2] =~ s/^\s+//;
4976 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004977 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004978 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004979 }
4980
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004981 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004982 } elsif ($op eq '<<' or $op eq '>>' or
4983 $op eq '&' or $op eq '^' or $op eq '|' or
4984 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004985 $op eq '*' or $op eq '/' or
4986 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004987 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004988 if ($check) {
4989 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4990 if (CHK("SPACING",
4991 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4992 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4993 $fix_elements[$n + 2] =~ s/^\s+//;
4994 $line_fixed = 1;
4995 }
4996 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4997 if (CHK("SPACING",
4998 "space preferred before that '$op' $at\n" . $hereptr)) {
4999 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
5000 $line_fixed = 1;
5001 }
5002 }
5003 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005004 if (ERROR("SPACING",
5005 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005006 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5007 if (defined $fix_elements[$n + 2]) {
5008 $fix_elements[$n + 2] =~ s/^\s+//;
5009 }
Joe Perches3705ce52013-07-03 15:05:31 -07005010 $line_fixed = 1;
5011 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005012 }
5013
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005014 # A colon needs no spaces before when it is
5015 # terminating a case value or a label.
5016 } elsif ($opv eq ':C' || $opv eq ':L') {
5017 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07005018 if (ERROR("SPACING",
5019 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005020 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005021 $line_fixed = 1;
5022 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005023 }
5024
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005025 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08005026 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005027 my $ok = 0;
5028
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005029 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005030 if (($op eq '<' &&
5031 $cc =~ /^\S+\@\S+>/) ||
5032 ($op eq '>' &&
5033 $ca =~ /<\S+\@\S+$/))
5034 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07005035 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005036 }
5037
Joe Perchese0df7e12015-04-16 12:44:53 -07005038 # for asm volatile statements
5039 # ignore a colon with another
5040 # colon immediately before or after
5041 if (($op eq ':') &&
5042 ($ca =~ /:$/ || $cc =~ /^:/)) {
5043 $ok = 1;
5044 }
5045
Joe Perches84731622013-11-12 15:10:05 -08005046 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005047 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07005048 my $msg_level = \&ERROR;
5049 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08005050
Jean Delvare0675a8f2017-09-08 16:16:07 -07005051 if (&{$msg_level}("SPACING",
5052 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005053 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5054 if (defined $fix_elements[$n + 2]) {
5055 $fix_elements[$n + 2] =~ s/^\s+//;
5056 }
Joe Perches3705ce52013-07-03 15:05:31 -07005057 $line_fixed = 1;
5058 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005059 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005060 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005061 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005062
5063## print("n: <$n> GOOD: <$good>\n");
5064
5065 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005066 }
Joe Perches3705ce52013-07-03 15:05:31 -07005067
5068 if (($#elements % 2) == 0) {
5069 $fixed_line = $fixed_line . $fix_elements[$#elements];
5070 }
5071
Joe Perches194f66f2014-08-06 16:11:03 -07005072 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5073 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07005074 }
5075
5076
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005077 }
5078
Joe Perches786b6322013-07-03 15:05:32 -07005079# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08005080 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07005081 if (WARN("SPACING",
5082 "space prohibited before semicolon\n" . $herecurr) &&
5083 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005084 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07005085 s/^(\+.*\S)\s+;/$1;/;
5086 }
5087 }
5088
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005089# check for multiple assignments
5090 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005091 CHK("MULTIPLE_ASSIGNMENTS",
5092 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005093 }
5094
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005095## # check for multiple declarations, allowing for a function declaration
5096## # continuation.
5097## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
5098## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
5099##
5100## # Remove any bracketed sections to ensure we do not
Dwaipayan Raye73d2712020-12-15 20:44:56 -08005101## # falsely report the parameters of functions.
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005102## my $ln = $line;
5103## while ($ln =~ s/\([^\(\)]*\)//g) {
5104## }
5105## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005106## WARN("MULTIPLE_DECLARATION",
5107## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005108## }
5109## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005110
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005111#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07005112 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07005113 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005114 if (ERROR("SPACING",
5115 "space required before the open brace '{'\n" . $herecurr) &&
5116 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07005117 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07005118 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005119 }
5120
Joe Perchesc4a62ef2013-07-03 15:05:28 -07005121## # check for blank lines before declarations
5122## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5123## $prevrawline =~ /^.\s*$/) {
5124## WARN("SPACING",
5125## "No blank lines before declarations\n" . $hereprev);
5126## }
5127##
5128
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005129# closing brace should have a space following it when it has anything
5130# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07005131 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005132 if (ERROR("SPACING",
5133 "space required after that close brace '}'\n" . $herecurr) &&
5134 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005135 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07005136 s/}((?!(?:,|;|\)))\S)/} $1/;
5137 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005138 }
5139
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005140# check spacing on square brackets
5141 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005142 if (ERROR("SPACING",
5143 "space prohibited after that open square bracket '['\n" . $herecurr) &&
5144 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005145 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005146 s/\[\s+/\[/;
5147 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005148 }
5149 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005150 if (ERROR("SPACING",
5151 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
5152 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005153 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005154 s/\s+\]/\]/;
5155 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005156 }
5157
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005158# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005159 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
5160 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005161 if (ERROR("SPACING",
5162 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
5163 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005164 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005165 s/\(\s+/\(/;
5166 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005167 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005168 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005169 $line !~ /for\s*\(.*;\s+\)/ &&
5170 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005171 if (ERROR("SPACING",
5172 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
5173 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005174 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005175 s/\s+\)/\)/;
5176 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005177 }
5178
Joe Perchese2826fd2014-08-06 16:10:48 -07005179# check unnecessary parentheses around addressof/dereference single $Lvals
5180# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5181
5182 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08005183 my $var = $1;
5184 if (CHK("UNNECESSARY_PARENTHESES",
5185 "Unnecessary parentheses around $var\n" . $herecurr) &&
5186 $fix) {
5187 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5188 }
5189 }
5190
5191# check for unnecessary parentheses around function pointer uses
5192# ie: (foo->bar)(); should be foo->bar();
5193# but not "if (foo->bar) (" to avoid some false positives
5194 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5195 my $var = $2;
5196 if (CHK("UNNECESSARY_PARENTHESES",
5197 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5198 $fix) {
5199 my $var2 = deparenthesize($var);
5200 $var2 =~ s/\s//g;
5201 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5202 }
5203 }
Joe Perchese2826fd2014-08-06 16:10:48 -07005204
Joe Perches63b7c732017-09-08 16:16:01 -07005205# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08005206# when !drivers/staging or command-line uses --strict
5207 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07005208 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07005209 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
5210 my $if_stat = $1;
5211 my $test = substr($2, 1, -1);
5212 my $herectx;
5213 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
5214 my $match = $1;
5215 # avoid parentheses around potential macro args
5216 next if ($match =~ /^\s*\w+\s*$/);
5217 if (!defined($herectx)) {
5218 $herectx = $here . "\n";
5219 my $cnt = statement_rawlines($if_stat);
5220 for (my $n = 0; $n < $cnt; $n++) {
5221 my $rl = raw_line($linenr, $n);
5222 $herectx .= $rl . "\n";
5223 last if $rl =~ /^[ \+].*\{/;
5224 }
5225 }
5226 CHK("UNNECESSARY_PARENTHESES",
5227 "Unnecessary parentheses around '$match'\n" . $herectx);
5228 }
5229 }
5230
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005231#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005232 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005233 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07005234 if (WARN("INDENTED_LABEL",
5235 "labels should not be indented\n" . $herecurr) &&
5236 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005237 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005238 s/^(.)\s+/$1/;
5239 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005240 }
5241
Joe Perches40873ab2020-10-15 20:11:56 -07005242# check if a statement with a comma should be two statements like:
5243# foo = bar(), /* comma should be semicolon */
5244# bar = baz();
5245 if (defined($stat) &&
5246 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5247 my $cnt = statement_rawlines($stat);
5248 my $herectx = get_stat_here($linenr, $cnt, $here);
5249 WARN("SUSPECT_COMMA_SEMICOLON",
5250 "Possible comma where semicolon could be used\n" . $herectx);
5251 }
5252
Joe Perches5b9553a2014-04-03 14:49:21 -07005253# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08005254 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005255 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07005256 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07005257 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5258 my $value = $1;
5259 $value = deparenthesize($value);
5260 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5261 ERROR("RETURN_PARENTHESES",
5262 "return is not a function, parentheses are not required\n" . $herecurr);
5263 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005264 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005265 ERROR("SPACING",
5266 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005267 }
5268 }
Joe Perches507e5142013-11-12 15:10:13 -08005269
Joe Perchesb43ae212014-06-23 13:22:07 -07005270# unnecessary return in a void function
5271# at end-of-function, with the previous line a single leading tab, then return;
5272# and the line before that not a goto label target like "out:"
5273 if ($sline =~ /^[ \+]}\s*$/ &&
5274 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5275 $linenr >= 3 &&
5276 $lines[$linenr - 3] =~ /^[ +]/ &&
5277 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07005278 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07005279 "void function return statements are not generally useful\n" . $hereprev);
5280 }
Joe Perches9819cf22014-06-04 16:12:09 -07005281
Joe Perches189248d2014-01-23 15:54:47 -08005282# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07005283 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08005284 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5285 my $openparens = $1;
5286 my $count = $openparens =~ tr@\(@\(@;
5287 my $msg = "";
5288 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5289 my $comp = $4; #Not $1 because of $LvalOrFunc
5290 $msg = " - maybe == should be = ?" if ($comp eq "==");
5291 WARN("UNNECESSARY_PARENTHESES",
5292 "Unnecessary parentheses$msg\n" . $herecurr);
5293 }
5294 }
5295
Joe Perchesc5595fa2015-09-09 15:37:58 -07005296# comparisons with a constant or upper case identifier on the left
5297# avoid cases like "foo + BAR < baz"
5298# only fix matches surrounded by parentheses to avoid incorrect
5299# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07005300 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005301 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5302 my $lead = $1;
5303 my $const = $2;
5304 my $comp = $3;
5305 my $to = $4;
5306 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07005307 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005308 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5309 WARN("CONSTANT_COMPARISON",
5310 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5311 $fix) {
5312 if ($comp eq "<") {
5313 $newcomp = ">";
5314 } elsif ($comp eq "<=") {
5315 $newcomp = ">=";
5316 } elsif ($comp eq ">") {
5317 $newcomp = "<";
5318 } elsif ($comp eq ">=") {
5319 $newcomp = "<=";
5320 }
5321 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5322 }
5323 }
5324
Joe Perchesf34e4a42015-04-16 12:44:19 -07005325# Return of what appears to be an errno should normally be negative
5326 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005327 my $name = $1;
5328 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07005329 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07005330 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005331 }
5332 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005333
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005334# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07005335 if ($line =~ /\b(if|while|for|switch)\(/) {
5336 if (ERROR("SPACING",
5337 "space required before the open parenthesis '('\n" . $herecurr) &&
5338 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005339 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005340 s/\b(if|while|for|switch)\(/$1 \(/;
5341 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005342 }
5343
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07005344# Check for illegal assignment in if conditional -- and check for trailing
5345# statements after the conditional.
Andy Whitcroft170d3a222008-10-15 22:02:30 -07005346 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08005347 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5348 ctx_statement_block($linenr, $realcnt, 0)
5349 if (!defined $stat);
Andy Whitcroft170d3a222008-10-15 22:02:30 -07005350 my ($stat_next) = ctx_statement_block($line_nr_next,
5351 $remain_next, $off_next);
5352 $stat_next =~ s/\n./\n /g;
5353 ##print "stat<$stat> stat_next<$stat_next>\n";
5354
5355 if ($stat_next =~ /^\s*while\b/) {
5356 # If the statement carries leading newlines,
5357 # then count those as offsets.
5358 my ($whitespace) =
5359 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5360 my $offset =
5361 statement_rawlines($whitespace) - 1;
5362
5363 $suppress_whiletrailers{$line_nr_next +
5364 $offset} = 1;
5365 }
5366 }
5367 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005368 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a222008-10-15 22:02:30 -07005369 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005370 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005371
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005372 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches65b64b32020-08-11 18:35:10 -07005373 if (ERROR("ASSIGN_IN_IF",
5374 "do not use assignment in if condition\n" . $herecurr) &&
5375 $fix && $perl_version_ok) {
5376 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5377 my $space = $1;
5378 my $not = $2;
5379 my $statement = $3;
5380 my $assigned = $4;
5381 my $test = $8;
5382 my $against = $9;
5383 my $brace = $15;
5384 fix_delete_line($fixlinenr, $rawline);
5385 fix_insert_line($fixlinenr, "$space$statement;");
5386 my $newline = "${space}if (";
5387 $newline .= '!' if defined($not);
5388 $newline .= '(' if (defined $not && defined($test) && defined($against));
5389 $newline .= "$assigned";
5390 $newline .= " $test $against" if (defined($test) && defined($against));
5391 $newline .= ')' if (defined $not && defined($test) && defined($against));
5392 $newline .= ')';
5393 $newline .= " {" if (defined($brace));
5394 fix_insert_line($fixlinenr + 1, $newline);
5395 }
5396 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005397 }
5398
5399 # Find out what is on the end of the line after the
5400 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005401 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005402 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005403 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005404 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5405 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005406 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005407 # Find out how long the conditional actually is.
5408 my @newlines = ($c =~ /\n/gs);
5409 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005410 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005411
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005412 $stat_real = raw_line($linenr, $cond_lines)
5413 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005414 if (defined($stat_real) && $cond_lines > 1) {
5415 $stat_real = "[...]\n$stat_real";
5416 }
5417
Joe Perches000d1cc12011-07-25 17:13:25 -07005418 ERROR("TRAILING_STATEMENTS",
5419 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005420 }
5421 }
5422
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005423# Check for bitwise tests written as boolean
5424 if ($line =~ /
5425 (?:
5426 (?:\[|\(|\&\&|\|\|)
5427 \s*0[xX][0-9]+\s*
5428 (?:\&\&|\|\|)
5429 |
5430 (?:\&\&|\|\|)
5431 \s*0[xX][0-9]+\s*
5432 (?:\&\&|\|\||\)|\])
5433 )/x)
5434 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005435 WARN("HEXADECIMAL_BOOLEAN_TEST",
5436 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005437 }
5438
Andy Whitcroft8905a672007-11-28 16:21:06 -08005439# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005440 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5441 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005442 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005443 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005444 ERROR("TRAILING_STATEMENTS",
5445 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005446 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005447 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005448# if should not continue a brace
5449 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005450 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005451 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005452 $herecurr);
5453 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005454# case and default should not have general statements after them
5455 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5456 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005457 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005458 \s*return\s+
5459 )/xg)
5460 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005461 ERROR("TRAILING_STATEMENTS",
5462 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005463 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005464
5465 # Check for }<nl>else {, these must be at the same
5466 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005467 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5468 $previndent == $indent) {
5469 if (ERROR("ELSE_AFTER_BRACE",
5470 "else should follow close brace '}'\n" . $hereprev) &&
5471 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5472 fix_delete_line($fixlinenr - 1, $prevrawline);
5473 fix_delete_line($fixlinenr, $rawline);
5474 my $fixedline = $prevrawline;
5475 $fixedline =~ s/}\s*$//;
5476 if ($fixedline !~ /^\+\s*$/) {
5477 fix_insert_line($fixlinenr, $fixedline);
5478 }
5479 $fixedline = $rawline;
5480 $fixedline =~ s/^(.\s*)else/$1} else/;
5481 fix_insert_line($fixlinenr, $fixedline);
5482 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005483 }
5484
Joe Perches8b8856f2014-08-06 16:11:14 -07005485 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5486 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005487 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5488
5489 # Find out what is on the end of the line after the
5490 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005491 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005492 $s =~ s/\n.*//g;
5493
5494 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005495 if (ERROR("WHILE_AFTER_BRACE",
5496 "while should follow close brace '}'\n" . $hereprev) &&
5497 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5498 fix_delete_line($fixlinenr - 1, $prevrawline);
5499 fix_delete_line($fixlinenr, $rawline);
5500 my $fixedline = $prevrawline;
5501 my $trailing = $rawline;
5502 $trailing =~ s/^\+//;
5503 $trailing = trim($trailing);
5504 $fixedline =~ s/}\s*$/} $trailing/;
5505 fix_insert_line($fixlinenr, $fixedline);
5506 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005507 }
5508 }
5509
Joe Perches95e2c602013-07-03 15:05:20 -07005510#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005511 while ($line =~ m{($Constant|$Lval)}g) {
5512 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005513
Joe Perches95e2c602013-07-03 15:05:20 -07005514#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005515 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005516 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Łukasz Stelmach4104a202020-12-15 20:44:27 -08005517#Ignore some autogenerated defines and enum values
5518 $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005519#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005520 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005521#Ignore SI style variants like nS, mV and dB
5522#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5523 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005524#Ignore some three character SI units explicitly, like MiB and KHz
5525 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005526 while ($var =~ m{($Ident)}g) {
5527 my $word = $1;
5528 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005529 if ($check) {
5530 seed_camelcase_includes();
5531 if (!$file && !$camelcase_file_seeded) {
5532 seed_camelcase_file($realfile);
5533 $camelcase_file_seeded = 1;
5534 }
5535 }
Joe Perches7e781f62013-09-11 14:23:55 -07005536 if (!defined $camelcase{$word}) {
5537 $camelcase{$word} = 1;
5538 CHK("CAMELCASE",
5539 "Avoid CamelCase: <$word>\n" . $herecurr);
5540 }
Joe Perches34456862013-07-03 15:05:34 -07005541 }
Joe Perches323c1262012-12-17 16:02:07 -08005542 }
5543 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005544
5545#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005546 if ($line =~ /\#\s*define.*\\\s+$/) {
5547 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5548 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5549 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005550 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005551 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005552 }
5553
Fabian Frederick0e212e02015-04-16 12:44:25 -07005554# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5555# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005556 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005557 my $file = "$1.h";
5558 my $checkfile = "include/linux/$file";
5559 if (-f "$root/$checkfile" &&
5560 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005561 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005562 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005563 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5564 if ($asminclude > 0) {
5565 if ($realfile =~ m{^arch/}) {
5566 CHK("ARCH_INCLUDE_LINUX",
5567 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5568 } else {
5569 WARN("INCLUDE_LINUX",
5570 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5571 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005572 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005573 }
5574 }
5575
Andy Whitcroft653d4872007-06-23 17:16:34 -07005576# multi-statement macros should be enclosed in a do while loop, grab the
5577# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005578# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07005579 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5580 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005581 my $ln = $linenr;
5582 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005583 my ($off, $dstat, $dcond, $rest);
5584 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005585 my $has_flow_statement = 0;
5586 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005587 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005588 ctx_statement_block($linenr, $realcnt, 0);
5589 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005590 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005591 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005592
Joe Perches08a28432014-10-13 15:51:55 -07005593 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005594 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005595
Joe Perchesf59b64b2016-10-11 13:52:08 -07005596 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5597 my $define_args = $1;
5598 my $define_stmt = $dstat;
5599 my @def_args = ();
5600
5601 if (defined $define_args && $define_args ne "") {
5602 $define_args = substr($define_args, 1, length($define_args) - 2);
5603 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005604 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005605 @def_args = split(",", $define_args);
5606 }
5607
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005608 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005609 $dstat =~ s/\\\n.//g;
5610 $dstat =~ s/^\s*//s;
5611 $dstat =~ s/\s*$//s;
5612
5613 # Flatten any parentheses and braces
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005614 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5615 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5616 $dstat =~ s/.\[[^\[\]]*\]/1u/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005617 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005618 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005619
Antonio Borneo342d3d22020-04-06 20:11:01 -07005620 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005621 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5622 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005623 {
5624 }
5625
Joe Perches42e15292016-03-15 14:58:01 -07005626 # Make asm volatile uses seem like a generic function
5627 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5628
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005629 my $exceptions = qr{
5630 $Declare|
5631 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005632 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005633 DECLARE_PER_CPU|
5634 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005635 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005636 union|
5637 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005638 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005639 ^\"|\"$|
5640 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005641 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005642 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005643
5644 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005645 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005646 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005647
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005648 if ($dstat ne '' &&
5649 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5650 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005651 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005652 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005653 $dstat !~ /$exceptions/ &&
5654 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005655 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005656 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005657 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005658 $dstat !~ /^for\s*$Constant$/ && # for (...)
5659 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5660 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005661 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005662 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005663 {
Joe Perchese7955562017-05-08 15:55:48 -07005664 if ($dstat =~ /^\s*if\b/) {
5665 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5666 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5667 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005668 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5669 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5670 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005671 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005672 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005673 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005674
5675 }
Joe Perches52076492016-10-11 13:52:14 -07005676
5677 # Make $define_stmt single line, comment-free, etc
5678 my @stmt_array = split('\n', $define_stmt);
5679 my $first = 1;
5680 $define_stmt = "";
5681 foreach my $l (@stmt_array) {
5682 $l =~ s/\\$//;
5683 if ($first) {
5684 $define_stmt = $l;
5685 $first = 0;
5686 } elsif ($l =~ /^[\+ ]/) {
5687 $define_stmt .= substr($l, 1);
5688 }
5689 }
5690 $define_stmt =~ s/$;//g;
5691 $define_stmt =~ s/\s+/ /g;
5692 $define_stmt = trim($define_stmt);
5693
Joe Perchesf59b64b2016-10-11 13:52:08 -07005694# check if any macro arguments are reused (ignore '...' and 'type')
5695 foreach my $arg (@def_args) {
5696 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005697 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005698 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005699 $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 -07005700 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5701 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005702 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005703 if ($use_cnt > 1) {
5704 CHK("MACRO_ARG_REUSE",
5705 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005706 }
5707# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005708 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005709 ((defined($1) && $1 ne ',') ||
5710 (defined($2) && $2 ne ','))) {
5711 CHK("MACRO_ARG_PRECEDENCE",
5712 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005713 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005714 }
Joe Perches5023d342012-12-17 16:01:47 -08005715
Joe Perches08a28432014-10-13 15:51:55 -07005716# check for macros with flow control, but without ## concatenation
5717# ## concatenation is commonly a macro that defines a function so ignore those
5718 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005719 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005720 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005721
Joe Perches08a28432014-10-13 15:51:55 -07005722 WARN("MACRO_WITH_FLOW_CONTROL",
5723 "Macros with flow control statements should be avoided\n" . "$herectx");
5724 }
5725
Joe Perches481eb482012-12-17 16:01:56 -08005726# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005727
5728 } else {
5729 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005730 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5731 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005732 $line =~ /^\+.*\\$/) {
5733 WARN("LINE_CONTINUATIONS",
5734 "Avoid unnecessary line continuations\n" . $herecurr);
5735 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005736 }
5737
Joe Perchesb13edf72012-07-30 14:41:24 -07005738# do {} while (0) macro tests:
5739# single-statement macros do not need to be enclosed in do while (0) loop,
5740# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005741 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005742 $realfile !~ m@/vmlinux.lds.h$@ &&
5743 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5744 my $ln = $linenr;
5745 my $cnt = $realcnt;
5746 my ($off, $dstat, $dcond, $rest);
5747 my $ctx = '';
5748 ($dstat, $dcond, $ln, $cnt, $off) =
5749 ctx_statement_block($linenr, $realcnt, 0);
5750 $ctx = $dstat;
5751
5752 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005753 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005754
5755 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5756 my $stmts = $2;
5757 my $semis = $3;
5758
5759 $ctx =~ s/\n*$//;
5760 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005761 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005762
Joe Perchesac8e97f2012-08-21 16:15:53 -07005763 if (($stmts =~ tr/;/;/) == 1 &&
5764 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005765 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5766 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5767 }
5768 if (defined $semis && $semis ne "") {
5769 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5770 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5771 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005772 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5773 $ctx =~ s/\n*$//;
5774 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005775 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005776
5777 WARN("TRAILING_SEMICOLON",
5778 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005779 }
5780 }
5781
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005782# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005783 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5784 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005785 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005786 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005787 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5788 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005789 my @allowed = ();
5790 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005791 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005792 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005793 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005794 for my $chunk (@chunks) {
5795 my ($cond, $block) = @{$chunk};
5796
Andy Whitcroft773647a2008-03-28 14:15:58 -07005797 # If the condition carries leading newlines, then count those as offsets.
5798 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5799 my $offset = statement_rawlines($whitespace) - 1;
5800
Joe Perchesaad4f612012-03-23 15:02:19 -07005801 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005802 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5803
5804 # We have looked at and allowed this specific line.
5805 $suppress_ifbraces{$ln + $offset} = 1;
5806
5807 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005808 $ln += statement_rawlines($block) - 1;
5809
Andy Whitcroft773647a2008-03-28 14:15:58 -07005810 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005811
5812 $seen++ if ($block =~ /^\s*{/);
5813
Joe Perchesaad4f612012-03-23 15:02:19 -07005814 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005815 if (statement_lines($cond) > 1) {
5816 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005817 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005818 }
5819 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005820 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005821 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005822 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005823 if (statement_block_size($block) > 1) {
5824 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005825 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005826 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005827 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005828 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005829 if ($seen) {
5830 my $sum_allowed = 0;
5831 foreach (@allowed) {
5832 $sum_allowed += $_;
5833 }
5834 if ($sum_allowed == 0) {
5835 WARN("BRACES",
5836 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5837 } elsif ($sum_allowed != $allow &&
5838 $seen != $allow) {
5839 CHK("BRACES",
5840 "braces {} should be used on all arms of this statement\n" . $herectx);
5841 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005842 }
5843 }
5844 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005845 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005846 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005847 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005848
Andy Whitcroftcf655042008-03-04 14:28:20 -08005849 # Check the pre-context.
5850 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5851 #print "APW: ALLOWED: pre<$1>\n";
5852 $allowed = 1;
5853 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005854
5855 my ($level, $endln, @chunks) =
5856 ctx_statement_full($linenr, $realcnt, $-[0]);
5857
Andy Whitcroftcf655042008-03-04 14:28:20 -08005858 # Check the condition.
5859 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005860 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005861 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005862 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005863 }
5864 if (statement_lines($cond) > 1) {
5865 #print "APW: ALLOWED: cond<$cond>\n";
5866 $allowed = 1;
5867 }
5868 if ($block =~/\b(?:if|for|while)\b/) {
5869 #print "APW: ALLOWED: block<$block>\n";
5870 $allowed = 1;
5871 }
5872 if (statement_block_size($block) > 1) {
5873 #print "APW: ALLOWED: lines block<$block>\n";
5874 $allowed = 1;
5875 }
5876 # Check the post-context.
5877 if (defined $chunks[1]) {
5878 my ($cond, $block) = @{$chunks[1]};
5879 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005880 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005881 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005882 if ($block =~ /^\s*\{/) {
5883 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5884 $allowed = 1;
5885 }
5886 }
5887 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005888 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005889 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005890
Joe Perches000d1cc12011-07-25 17:13:25 -07005891 WARN("BRACES",
5892 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005893 }
5894 }
5895
Joe Perchese4c5bab2017-02-24 15:01:41 -08005896# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005897 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5898 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005899 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5900 }
5901
Joe Perches0979ae62012-12-17 16:01:59 -08005902# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005903 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005904 if (CHK("BRACES",
5905 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5906 $fix && $prevrawline =~ /^\+/) {
5907 fix_delete_line($fixlinenr - 1, $prevrawline);
5908 }
Joe Perches0979ae62012-12-17 16:01:59 -08005909 }
Joe Perches77b9a532013-07-03 15:05:29 -07005910 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005911 if (CHK("BRACES",
5912 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5913 $fix) {
5914 fix_delete_line($fixlinenr, $rawline);
5915 }
Joe Perches0979ae62012-12-17 16:01:59 -08005916 }
5917
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005918# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005919 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5920 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005921 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005922 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005923 }
5924
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005925# Check for user-visible strings broken across lines, which breaks the ability
5926# to grep for the string. Make exceptions when the previous string ends in a
5927# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5928# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005929 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005930 $prevline =~ /"\s*$/ &&
5931 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5932 if (WARN("SPLIT_STRING",
5933 "quoted string split across lines\n" . $hereprev) &&
5934 $fix &&
5935 $prevrawline =~ /^\+.*"\s*$/ &&
5936 $last_coalesced_string_linenr != $linenr - 1) {
5937 my $extracted_string = get_quoted_string($line, $rawline);
5938 my $comma_close = "";
5939 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5940 $comma_close = $1;
5941 }
5942
5943 fix_delete_line($fixlinenr - 1, $prevrawline);
5944 fix_delete_line($fixlinenr, $rawline);
5945 my $fixedline = $prevrawline;
5946 $fixedline =~ s/"\s*$//;
5947 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5948 fix_insert_line($fixlinenr - 1, $fixedline);
5949 $fixedline = $rawline;
5950 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5951 if ($fixedline !~ /\+\s*$/) {
5952 fix_insert_line($fixlinenr, $fixedline);
5953 }
5954 $last_coalesced_string_linenr = $linenr;
5955 }
5956 }
5957
5958# check for missing a space in a string concatenation
5959 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5960 WARN('MISSING_SPACE',
5961 "break quoted strings at a space character\n" . $hereprev);
5962 }
5963
Joe Perchese4b7d302017-05-08 15:55:51 -07005964# check for an embedded function name in a string when the function is known
5965# This does not work very well for -f --file checking as it depends on patch
5966# context providing the function name or a single line form for in-file
5967# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005968 if ($line =~ /^\+.*$String/ &&
5969 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005970 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5971 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005972 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005973 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005974 }
5975
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005976# check for spaces before a quoted newline
5977 if ($rawline =~ /^.*\".*\s\\n/) {
5978 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5979 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5980 $fix) {
5981 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5982 }
5983
5984 }
5985
Joe Perchesf17dba42014-10-13 15:51:51 -07005986# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005987 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5988 if (CHK("CONCATENATED_STRING",
5989 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5990 $fix) {
5991 while ($line =~ /($String)/g) {
5992 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5993 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5994 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5995 }
5996 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005997 }
5998
Joe Perches90ad30e2014-12-10 15:51:59 -08005999# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07006000 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07006001 if (WARN("STRING_FRAGMENTS",
6002 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
6003 $fix) {
6004 while ($line =~ /($String)(?=\s*")/g) {
6005 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6006 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
6007 }
6008 }
Joe Perches90ad30e2014-12-10 15:51:59 -08006009 }
6010
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006011# check for non-standard and hex prefixed decimal printf formats
6012 my $show_L = 1; #don't show the same defect twice
6013 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006014 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006015 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006016 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006017 # check for %L
6018 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006019 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006020 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6021 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006022 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006023 # check for %Z
6024 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6025 WARN("PRINTF_Z",
6026 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6027 $show_Z = 0;
6028 }
6029 # check for 0x<decimal>
6030 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6031 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07006032 "Prefixing 0x with decimal output is defective\n" . $herecurr);
6033 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006034 }
6035
6036# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08006037 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006038 WARN("LINE_CONTINUATIONS",
6039 "Avoid line continuations in quoted strings\n" . $herecurr);
6040 }
6041
Andy Whitcroft00df3442007-06-08 13:47:06 -07006042# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006043 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07006044 WARN("IF_0",
6045 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
6046 }
6047
6048# warn about #if 1
6049 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
6050 WARN("IF_1",
6051 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006052 }
6053
Andy Whitcroft03df4b52012-12-17 16:01:52 -08006054# check for needless "if (<foo>) fn(<foo>)" uses
6055 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07006056 my $tested = quotemeta($1);
6057 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6058 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6059 my $func = $1;
6060 if (WARN('NEEDLESS_IF',
6061 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6062 $fix) {
6063 my $do_fix = 1;
6064 my $leading_tabs = "";
6065 my $new_leading_tabs = "";
6066 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6067 $leading_tabs = $1;
6068 } else {
6069 $do_fix = 0;
6070 }
6071 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6072 $new_leading_tabs = $1;
6073 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6074 $do_fix = 0;
6075 }
6076 } else {
6077 $do_fix = 0;
6078 }
6079 if ($do_fix) {
6080 fix_delete_line($fixlinenr - 1, $prevrawline);
6081 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6082 }
6083 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07006084 }
6085 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006086
Joe Perchesebfdc402014-08-06 16:10:27 -07006087# check for unnecessary "Out of Memory" messages
6088 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6089 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6090 (defined $1 || defined $3) &&
6091 $linenr > 3) {
6092 my $testval = $2;
6093 my $testline = $lines[$linenr - 3];
6094
6095 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6096# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6097
Joe Perchese29a70f2019-03-07 16:28:35 -08006098 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6099 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07006100 WARN("OOM_MESSAGE",
6101 "Possible unnecessary 'out of memory' message\n" . $hereprev);
6102 }
6103 }
6104
Joe Perchesf78d98f2014-10-13 15:52:01 -07006105# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08006106 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07006107 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6108 my $level = $1;
6109 if (WARN("UNNECESSARY_KERN_LEVEL",
6110 "Possible unnecessary $level\n" . $herecurr) &&
6111 $fix) {
6112 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
6113 }
6114 }
6115
Joe Perches45c55e92017-02-24 15:01:31 -08006116# check for logging continuations
6117 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
6118 WARN("LOGGING_CONTINUATION",
6119 "Avoid logging continuation uses where feasible\n" . $herecurr);
6120 }
6121
Dwaipayan Ray70eb2272020-12-15 20:45:15 -08006122# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
6123 if (defined $stat &&
6124 $line =~ /\b$logFunctions\s*\(/ &&
6125 index($stat, '"') >= 0) {
6126 my $lc = $stat =~ tr@\n@@;
6127 $lc = $lc + $linenr;
6128 my $stat_real = get_stat_real($linenr, $lc);
6129 pos($stat_real) = index($stat_real, '"');
6130 while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
6131 my $pspec = $1;
6132 my $h = $2;
6133 my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
6134 if (WARN("UNNECESSARY_MODIFIER",
6135 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
6136 $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
6137 my $nspec = $pspec;
6138 $nspec =~ s/h//g;
6139 $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
6140 }
6141 }
6142 }
6143
Joe Perchesabb08a52014-12-10 15:51:46 -08006144# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07006145 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08006146 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6147 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6148 WARN("MASK_THEN_SHIFT",
6149 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6150 }
6151
Joe Perchesb75ac612014-12-10 15:52:02 -08006152# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07006153 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08006154 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6155 my $val = $1;
6156 my $equal = "!";
6157 $equal = "" if ($4 eq "!=");
6158 if (CHK("COMPARISON_TO_NULL",
6159 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6160 $fix) {
6161 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6162 }
6163 }
6164 }
6165
Joe Perches8716de32013-09-11 14:24:05 -07006166# check for bad placement of section $InitAttribute (e.g.: __initdata)
6167 if ($line =~ /(\b$InitAttribute\b)/) {
6168 my $attr = $1;
6169 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
6170 my $ptr = $1;
6171 my $var = $2;
6172 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
6173 ERROR("MISPLACED_INIT",
6174 "$attr should be placed after $var\n" . $herecurr)) ||
6175 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
6176 WARN("MISPLACED_INIT",
6177 "$attr should be placed after $var\n" . $herecurr))) &&
6178 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006179 $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 -07006180 }
6181 }
6182 }
6183
Joe Perchese970b8842013-11-12 15:10:10 -08006184# check for $InitAttributeData (ie: __initdata) with const
6185 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6186 my $attr = $1;
6187 $attr =~ /($InitAttributePrefix)(.*)/;
6188 my $attr_prefix = $1;
6189 my $attr_type = $2;
6190 if (ERROR("INIT_ATTRIBUTE",
6191 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6192 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006193 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006194 s/$InitAttributeData/${attr_prefix}initconst/;
6195 }
6196 }
6197
6198# check for $InitAttributeConst (ie: __initconst) without const
6199 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6200 my $attr = $1;
6201 if (ERROR("INIT_ATTRIBUTE",
6202 "Use of $attr requires a separate use of const\n" . $herecurr) &&
6203 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006204 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006205 /(^\+\s*(?:static\s+))/;
6206 $lead = rtrim($1);
6207 $lead = "$lead " if ($lead !~ /^\+$/);
6208 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07006209 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08006210 }
6211 }
6212
Joe Perchesc17893c2015-04-16 12:44:42 -07006213# check for __read_mostly with const non-pointer (should just be const)
6214 if ($line =~ /\b__read_mostly\b/ &&
6215 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6216 if (ERROR("CONST_READ_MOSTLY",
6217 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6218 $fix) {
6219 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6220 }
6221 }
6222
Joe Perchesfbdb8132014-04-03 14:49:14 -07006223# don't use __constant_<foo> functions outside of include/uapi/
6224 if ($realfile !~ m@^include/uapi/@ &&
6225 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6226 my $constant_func = $1;
6227 my $func = $constant_func;
6228 $func =~ s/^__constant_//;
6229 if (WARN("CONSTANT_CONVERSION",
6230 "$constant_func should be $func\n" . $herecurr) &&
6231 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006232 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07006233 }
6234 }
6235
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006236# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08006237 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07006238 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006239 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07006240 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006241 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006242 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07006243 }
6244 if ($delay > 2000) {
6245 WARN("LONG_UDELAY",
6246 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006247 }
6248 }
6249
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006250# warn about unexpectedly long msleep's
6251 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6252 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006253 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006254 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006255 }
6256 }
6257
Joe Perches36ec1932013-07-03 15:05:25 -07006258# check for comparisons of jiffies
6259 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6260 WARN("JIFFIES_COMPARISON",
6261 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6262 }
6263
Joe Perches9d7a34a2013-07-03 15:05:26 -07006264# check for comparisons of get_jiffies_64()
6265 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6266 WARN("JIFFIES_COMPARISON",
6267 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6268 }
6269
Andy Whitcroft00df3442007-06-08 13:47:06 -07006270# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006271# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07006272# print "#ifdef in C files should be avoided\n";
6273# print "$herecurr";
6274# $clean = 0;
6275# }
6276
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006277# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006278 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07006279 if (ERROR("SPACING",
6280 "exactly one space required after that #$1\n" . $herecurr) &&
6281 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006282 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07006283 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6284 }
6285
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006286 }
6287
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006288# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006289 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6290 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006291 my $which = $1;
6292 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006293 CHK("UNCOMMENTED_DEFINITION",
6294 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006295 }
6296 }
6297# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006298
6299 my $barriers = qr{
6300 mb|
6301 rmb|
Will Deaconad83ec62019-11-07 14:49:00 +00006302 wmb
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006303 }x;
6304 my $barrier_stems = qr{
6305 mb__before_atomic|
6306 mb__after_atomic|
6307 store_release|
6308 load_acquire|
6309 store_mb|
6310 (?:$barriers)
6311 }x;
6312 my $all_barriers = qr{
6313 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02006314 smp_(?:$barrier_stems)|
6315 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006316 }x;
6317
6318 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006319 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08006320 WARN("MEMORY_BARRIER",
6321 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006322 }
6323 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006324
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02006325 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6326
6327 if ($realfile !~ m@^include/asm-generic/@ &&
6328 $realfile !~ m@/barrier\.h$@ &&
6329 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6330 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6331 WARN("MEMORY_BARRIER",
6332 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6333 }
6334
Joe Perchescb426e92015-06-25 15:02:46 -07006335# check for waitqueue_active without a comment.
6336 if ($line =~ /\bwaitqueue_active\s*\(/) {
6337 if (!ctx_has_comment($first_line, $linenr)) {
6338 WARN("WAITQUEUE_ACTIVE",
6339 "waitqueue_active without comment\n" . $herecurr);
6340 }
6341 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006342
Marco Elver5099a722020-04-01 12:17:14 +02006343# check for data_race without a comment.
6344 if ($line =~ /\bdata_race\s*\(/) {
6345 if (!ctx_has_comment($first_line, $linenr)) {
6346 WARN("DATA_RACE",
6347 "data_race without comment\n" . $herecurr);
6348 }
6349 }
6350
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006351# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006352 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006353 CHK("ARCH_DEFINES",
6354 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006355 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006356
Joe Perches596ed452017-07-12 14:37:02 -07006357# check that the storage class is not after a type
6358 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006359 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07006360 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6361 }
6362# Check that the storage class is at the beginning of a declaration
6363 if ($line =~ /\b$Storage\b/ &&
6364 $line !~ /^.\s*$Storage/ &&
6365 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6366 $1 !~ /[\,\)]\s*$/) {
6367 WARN("STORAGE_CLASS",
6368 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07006369 }
6370
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006371# check the location of the inline attribute, that it is between
6372# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006373 if ($line =~ /\b$Type\s+$Inline\b/ ||
6374 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006375 ERROR("INLINE_LOCATION",
6376 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006377 }
6378
Andy Whitcroft8905a672007-11-28 16:21:06 -08006379# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08006380 if ($realfile !~ m@\binclude/uapi/@ &&
6381 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006382 if (WARN("INLINE",
6383 "plain inline is preferred over $1\n" . $herecurr) &&
6384 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006385 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006386
6387 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006388 }
6389
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006390# Check for compiler attributes
Joe Perches2b7ab452013-11-12 15:10:14 -08006391 if ($realfile !~ m@\binclude/uapi/@ &&
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006392 $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
6393 my $attr = $1;
6394 $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
Joe Perches3d130fd2011-01-12 17:00:00 -08006395
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006396 my %attr_list = (
Joe Perches0830aab2020-12-15 20:44:50 -08006397 "alias" => "__alias",
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006398 "aligned" => "__aligned",
6399 "always_inline" => "__always_inline",
6400 "assume_aligned" => "__assume_aligned",
6401 "cold" => "__cold",
6402 "const" => "__attribute_const__",
6403 "copy" => "__copy",
6404 "designated_init" => "__designated_init",
6405 "externally_visible" => "__visible",
6406 "format" => "printf|scanf",
6407 "gnu_inline" => "__gnu_inline",
6408 "malloc" => "__malloc",
6409 "mode" => "__mode",
6410 "no_caller_saved_registers" => "__no_caller_saved_registers",
6411 "noclone" => "__noclone",
6412 "noinline" => "noinline",
6413 "nonstring" => "__nonstring",
6414 "noreturn" => "__noreturn",
6415 "packed" => "__packed",
6416 "pure" => "__pure",
Joe Perches339f29d2020-12-15 20:44:43 -08006417 "section" => "__section",
Joe Perches0830aab2020-12-15 20:44:50 -08006418 "used" => "__used",
6419 "weak" => "__weak"
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006420 );
Joe Perches39b7e282011-07-25 17:13:24 -07006421
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006422 while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
Joe Perches339f29d2020-12-15 20:44:43 -08006423 my $orig_attr = $1;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006424 my $params = '';
6425 $params = $2 if defined($2);
Joe Perches339f29d2020-12-15 20:44:43 -08006426 my $curr_attr = $orig_attr;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006427 $curr_attr =~ s/^[\s_]+|[\s_]+$//g;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006428 if (exists($attr_list{$curr_attr})) {
Joe Perches339f29d2020-12-15 20:44:43 -08006429 my $new = $attr_list{$curr_attr};
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006430 if ($curr_attr eq "format" && $params) {
6431 $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
Joe Perches339f29d2020-12-15 20:44:43 -08006432 $new = "__$1\($2";
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006433 } else {
Joe Perches339f29d2020-12-15 20:44:43 -08006434 $new = "$new$params";
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006435 }
Joe Perches339f29d2020-12-15 20:44:43 -08006436 if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6437 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
6438 $fix) {
6439 my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6440 $fixed[$fixlinenr] =~ s/$remove//;
6441 $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6442 $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6443 $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
6444 }
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006445 }
6446 }
6447
6448 # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
6449 if ($attr =~ /^_*unused/) {
6450 WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6451 "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
Joe Perchesd5e616f2013-09-11 14:23:54 -07006452 }
Joe Perches6061d942012-03-23 15:02:16 -07006453 }
6454
Joe Perches619a9082014-12-10 15:51:35 -08006455# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006456 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006457 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6458 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6459 $line =~ /\b__weak\b/)) {
6460 ERROR("WEAK_DECLARATION",
6461 "Using weak declarations can have unintended link defects\n" . $herecurr);
6462 }
6463
Tomas Winklerfd39f902016-12-12 16:46:34 -08006464# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006465 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006466 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006467 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6468 my $type = $1;
6469 if ($type =~ /\b($typeC99Typedefs)\b/) {
6470 $type = $1;
6471 my $kernel_type = 'u';
6472 $kernel_type = 's' if ($type =~ /^_*[si]/);
6473 $type =~ /(\d+)/;
6474 $kernel_type .= $1;
6475 if (CHK("PREFER_KERNEL_TYPES",
6476 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6477 $fix) {
6478 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6479 }
6480 }
6481 }
6482
Joe Perches938224b2016-01-20 14:59:15 -08006483# check for cast of C90 native int or longer types constants
6484 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6485 my $cast = $1;
6486 my $const = $2;
6487 if (WARN("TYPECAST_INT_CONSTANT",
6488 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6489 $fix) {
6490 my $suffix = "";
6491 my $newconst = $const;
6492 $newconst =~ s/${Int_type}$//;
6493 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6494 if ($cast =~ /\blong\s+long\b/) {
6495 $suffix .= 'LL';
6496 } elsif ($cast =~ /\blong\b/) {
6497 $suffix .= 'L';
6498 }
6499 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6500 }
6501 }
6502
Joe Perches8f53a9b2010-03-05 13:43:48 -08006503# check for sizeof(&)
6504 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006505 WARN("SIZEOF_ADDRESS",
6506 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006507 }
6508
Joe Perches66c80b62012-07-30 14:41:22 -07006509# check for sizeof without parenthesis
6510 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006511 if (WARN("SIZEOF_PARENTHESIS",
6512 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6513 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006514 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006515 }
Joe Perches66c80b62012-07-30 14:41:22 -07006516 }
6517
Joe Perches88982fe2012-12-17 16:02:00 -08006518# check for struct spinlock declarations
6519 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6520 WARN("USE_SPINLOCK_T",
6521 "struct spinlock should be spinlock_t\n" . $herecurr);
6522 }
6523
Joe Perchesa6962d72013-04-29 16:18:13 -07006524# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006525 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006526 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006527 $fmt =~ s/%%//g;
6528 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006529 if (WARN("PREFER_SEQ_PUTS",
6530 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6531 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006532 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006533 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006534 }
6535 }
6536
Joe Perches478b1792018-04-10 16:33:34 -07006537# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006538 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006539 defined $stat &&
6540 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6541 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006542 my $stat_real;
6543
Joe Perches0b523762017-05-08 15:55:36 -07006544 my $lc = $stat =~ tr@\n@@;
6545 $lc = $lc + $linenr;
6546 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006547 my $specifier;
6548 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006549 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006550 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006551 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6552 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006553
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006554 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006555 $specifier = $1;
6556 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006557 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006558 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006559 ($extension eq "f" &&
6560 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006561 $bad_specifier = $specifier;
6562 last;
6563 }
6564 if ($extension eq "x" && !defined($stat_real)) {
6565 if (!defined($stat_real)) {
6566 $stat_real = get_stat_real($linenr, $lc);
6567 }
6568 WARN("VSPRINTF_SPECIFIER_PX",
6569 "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");
6570 }
6571 }
6572 if ($bad_specifier ne "") {
6573 my $stat_real = get_stat_real($linenr, $lc);
6574 my $ext_type = "Invalid";
6575 my $use = "";
6576 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006577 $use = " - use %pS instead";
6578 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6579 }
6580
6581 WARN("VSPRINTF_POINTER_EXTENSION",
6582 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6583 }
Joe Perches0b523762017-05-08 15:55:36 -07006584 }
6585 }
6586
Andy Whitcroft554e1652012-01-10 15:09:57 -08006587# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006588 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006589 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006590 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006591
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006592 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006593 my $ms_val = $7;
6594 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006595
Andy Whitcroft554e1652012-01-10 15:09:57 -08006596 if ($ms_size =~ /^(0x|)0$/i) {
6597 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006598 "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 -08006599 } elsif ($ms_size =~ /^(0x|)1$/i) {
6600 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006601 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6602 }
6603 }
6604
Joe Perches98a9bba2014-01-23 15:54:52 -08006605# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006606# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006607# defined $stat &&
6608# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6609# if (WARN("PREFER_ETHER_ADDR_COPY",
6610# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6611# $fix) {
6612# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6613# }
6614# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006615
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006616# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006617# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006618# defined $stat &&
6619# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6620# WARN("PREFER_ETHER_ADDR_EQUAL",
6621# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6622# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006623
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006624# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6625# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006626# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006627# defined $stat &&
6628# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6629#
6630# my $ms_val = $7;
6631#
6632# if ($ms_val =~ /^(?:0x|)0+$/i) {
6633# if (WARN("PREFER_ETH_ZERO_ADDR",
6634# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6635# $fix) {
6636# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6637# }
6638# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6639# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6640# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6641# $fix) {
6642# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6643# }
6644# }
6645# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006646
Joe Perches5dbdb2d2020-12-29 15:14:31 -08006647# strlcpy uses that should likely be strscpy
6648 if ($line =~ /\bstrlcpy\s*\(/) {
6649 WARN("STRLCPY",
6650 "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr);
6651 }
6652
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006653# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006654 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006655 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006656 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006657 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006658 my $call = $1;
6659 my $cast1 = deparenthesize($2);
6660 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006661 my $cast2 = deparenthesize($7);
6662 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006663 my $cast;
6664
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006665 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006666 $cast = "$cast1 or $cast2";
6667 } elsif ($cast1 ne "") {
6668 $cast = $cast1;
6669 } else {
6670 $cast = $cast2;
6671 }
6672 WARN("MINMAX",
6673 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006674 }
6675 }
6676
Joe Perches4a273192012-07-30 14:41:20 -07006677# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006678 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006679 defined $stat &&
6680 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6681 my $min = $1;
6682 my $max = $7;
6683 if ($min eq $max) {
6684 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006685 "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 -07006686 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6687 $min > $max) {
6688 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006689 "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 -07006690 }
6691 }
6692
Joe Perches823b7942013-11-12 15:10:15 -08006693# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006694 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006695 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006696 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006697 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6698 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6699 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6700 my $lc = $stat =~ tr@\n@@;
6701 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006702 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006703 WARN("NAKED_SSCANF",
6704 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6705 }
6706
Joe Perchesafc819a2014-06-04 16:12:08 -07006707# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006708 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006709 defined $stat &&
6710 $line =~ /\bsscanf\b/) {
6711 my $lc = $stat =~ tr@\n@@;
6712 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006713 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006714 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6715 my $format = $6;
6716 my $count = $format =~ tr@%@%@;
6717 if ($count == 1 &&
6718 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6719 WARN("SSCANF_TO_KSTRTO",
6720 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6721 }
6722 }
6723 }
6724
Joe Perches70dc8a42013-09-11 14:23:58 -07006725# check for new externs in .h files.
6726 if ($realfile =~ /\.h$/ &&
6727 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006728 if (CHK("AVOID_EXTERNS",
6729 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006730 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006731 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006732 }
6733 }
6734
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006735# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006736 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006737 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006738 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006739 my $function_name = $1;
6740 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006741
6742 my $s = $stat;
6743 if (defined $cond) {
6744 substr($s, 0, length($cond), '');
6745 }
Kees Cookd8b44b52020-06-03 14:18:09 -07006746 if ($s =~ /^\s*;/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006747 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006748 WARN("AVOID_EXTERNS",
6749 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006750 }
6751
6752 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006753 WARN("FUNCTION_ARGUMENTS",
6754 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006755 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006756
6757 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6758 $stat =~ /^.\s*extern\s+/)
6759 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006760 WARN("AVOID_EXTERNS",
6761 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006762 }
6763
Joe Perchesa0ad7592017-07-10 15:52:19 -07006764# check for function declarations that have arguments without identifier names
6765 if (defined $stat &&
Kees Cookd8b44b52020-06-03 14:18:09 -07006766 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6767 $1 ne "void") {
6768 my $args = trim($1);
Joe Perchesca0d8922016-10-11 13:52:16 -07006769 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6770 my $arg = trim($1);
Kees Cookd8b44b52020-06-03 14:18:09 -07006771 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006772 WARN("FUNCTION_ARGUMENTS",
6773 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6774 }
6775 }
6776 }
6777
Joe Perchesa0ad7592017-07-10 15:52:19 -07006778# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006779 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006780 defined $stat &&
6781 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6782 $context_function = $1;
6783
6784# check for multiline function definition with misplaced open brace
6785 my $ok = 0;
6786 my $cnt = statement_rawlines($stat);
6787 my $herectx = $here . "\n";
6788 for (my $n = 0; $n < $cnt; $n++) {
6789 my $rl = raw_line($linenr, $n);
6790 $herectx .= $rl . "\n";
6791 $ok = 1 if ($rl =~ /^[ \+]\{/);
6792 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6793 last if $rl =~ /^[ \+].*\{/;
6794 }
6795 if (!$ok) {
6796 ERROR("OPEN_BRACE",
6797 "open brace '{' following function definitions go on the next line\n" . $herectx);
6798 }
6799 }
6800
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006801# checks for new __setup's
6802 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6803 my $name = $1;
6804
6805 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006806 CHK("UNDOCUMENTED_SETUP",
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07006807 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006808 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006809 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006810
Joe Perchese29a70f2019-03-07 16:28:35 -08006811# check for pointless casting of alloc functions
6812 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006813 WARN("UNNECESSARY_CASTS",
6814 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006815 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006816
Joe Perchesa640d252013-07-03 15:05:21 -07006817# alloc style
6818# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006819 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006820 $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 -07006821 CHK("ALLOC_SIZEOF_STRUCT",
6822 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6823 }
6824
Joe Perches60a55362014-06-04 16:12:07 -07006825# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006826 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006827 defined $stat &&
6828 $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 -07006829 my $oldfunc = $3;
6830 my $a1 = $4;
6831 my $a2 = $10;
6832 my $newfunc = "kmalloc_array";
6833 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006834 my $r1 = $a1;
6835 my $r2 = $a2;
6836 if ($a1 =~ /^sizeof\s*\S/) {
6837 $r1 = $a2;
6838 $r2 = $a1;
6839 }
6840 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6841 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006842 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006843 my $herectx = get_stat_here($linenr, $cnt, $here);
6844
Joe Perches60a55362014-06-04 16:12:07 -07006845 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006846 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6847 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006848 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006849 $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 -07006850 }
6851 }
6852 }
6853
Joe Perches972fdea2013-04-29 16:18:12 -07006854# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006855 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006856 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6857 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006858 WARN("KREALLOC_ARG_REUSE",
6859 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6860 }
6861
Joe Perches5ce59ae2013-02-21 16:44:18 -08006862# check for alloc argument mismatch
6863 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6864 WARN("ALLOC_ARRAY_ARGS",
6865 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6866 }
6867
Joe Perchescaf2a542011-01-12 16:59:56 -08006868# check for multiple semicolons
6869 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006870 if (WARN("ONE_SEMICOLON",
6871 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6872 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006873 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006874 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006875 }
6876
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006877# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6878 if ($realfile !~ m@^include/uapi/@ &&
6879 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006880 my $ull = "";
6881 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6882 if (CHK("BIT_MACRO",
6883 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6884 $fix) {
6885 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6886 }
6887 }
6888
Joe Perches50161262020-08-11 18:35:07 -07006889# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006890 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
Joe Perches50161262020-08-11 18:35:07 -07006891 WARN("IS_ENABLED_CONFIG",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006892 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
Joe Perches50161262020-08-11 18:35:07 -07006893 }
6894
Joe Perches2d632742016-05-20 17:04:00 -07006895# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006896 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 -07006897 my $config = $1;
6898 if (WARN("PREFER_IS_ENABLED",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006899 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
Joe Perches2d632742016-05-20 17:04:00 -07006900 $fix) {
6901 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6902 }
6903 }
6904
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006905# check for /* fallthrough */ like comment, prefer fallthrough;
6906 my @fallthroughs = (
6907 'fallthrough',
6908 '@fallthrough@',
6909 'lint -fallthrough[ \t]*',
6910 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6911 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6912 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6913 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6914 );
6915 if ($raw_comment ne '') {
6916 foreach my $ft (@fallthroughs) {
6917 if ($raw_comment =~ /$ft/) {
6918 my $msg_level = \&WARN;
6919 $msg_level = \&CHK if ($file);
6920 &{$msg_level}("PREFER_FALLTHROUGH",
6921 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6922 last;
6923 }
6924 }
6925 }
6926
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006927# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006928 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006929 defined $stat &&
6930 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006931 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006932 my $herectx = get_stat_here($linenr, $cnt, $here);
6933
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006934 WARN("DEFAULT_NO_BREAK",
6935 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006936 }
6937
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006938# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006939 if ($line =~ /\b__FUNCTION__\b/) {
6940 if (WARN("USE_FUNC",
6941 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6942 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006943 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006944 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006945 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006946
Joe Perches62ec8182015-02-13 14:38:18 -08006947# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6948 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6949 ERROR("DATE_TIME",
6950 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6951 }
6952
Joe Perches2c924882012-03-23 15:02:20 -07006953# check for use of yield()
6954 if ($line =~ /\byield\s*\(\s*\)/) {
6955 WARN("YIELD",
6956 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6957 }
6958
Joe Perches179f8f42013-07-03 15:05:30 -07006959# check for comparisons against true and false
6960 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6961 my $lead = $1;
6962 my $arg = $2;
6963 my $test = $3;
6964 my $otype = $4;
6965 my $trail = $5;
6966 my $op = "!";
6967
6968 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6969
6970 my $type = lc($otype);
6971 if ($type =~ /^(?:true|false)$/) {
6972 if (("$test" eq "==" && "$type" eq "true") ||
6973 ("$test" eq "!=" && "$type" eq "false")) {
6974 $op = "";
6975 }
6976
6977 CHK("BOOL_COMPARISON",
6978 "Using comparison to $otype is error prone\n" . $herecurr);
6979
6980## maybe suggesting a correct construct would better
6981## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6982
6983 }
6984 }
6985
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006986# check for semaphores initialized locked
6987 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006988 WARN("CONSIDER_COMPLETION",
6989 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006990 }
Joe Perches6712d852012-03-23 15:02:20 -07006991
Joe Perches67d0a072011-10-31 17:13:10 -07006992# recommend kstrto* over simple_strto* and strict_strto*
6993 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006994 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006995 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006996 }
Joe Perches6712d852012-03-23 15:02:20 -07006997
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006998# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006999 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007000 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07007001 "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 -07007002 }
Joe Perches6712d852012-03-23 15:02:20 -07007003
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08007004# check for spin_is_locked(), suggest lockdep instead
7005 if ($line =~ /\bspin_is_locked\(/) {
7006 WARN("USE_LOCKDEP",
7007 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
7008 }
7009
Joe Perches9189c7e2018-09-07 15:26:18 -07007010# check for deprecated apis
7011 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
7012 my $deprecated_api = $1;
7013 my $new_api = $deprecated_apis{$deprecated_api};
7014 WARN("DEPRECATED_API",
7015 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
7016 }
7017
Joe Perches0f3c5aa2015-02-13 14:39:05 -08007018# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07007019# and avoid what seem like struct definitions 'struct foo {'
Quentin Monnetced69da12020-08-11 18:35:13 -07007020 if (defined($const_structs) &&
7021 $line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07007022 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007023 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07007024 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08007025 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07007026
7027# use of NR_CPUS is usually wrong
7028# ignore definitions of NR_CPUS and usage to define arrays as likely right
7029 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07007030 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7031 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07007032 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7033 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
7034 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07007035 {
Joe Perches000d1cc12011-07-25 17:13:25 -07007036 WARN("NR_CPUS",
7037 "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 -07007038 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07007039
Joe Perches52ea8502013-11-12 15:10:09 -08007040# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
7041 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
7042 ERROR("DEFINE_ARCH_HAS",
7043 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
7044 }
7045
Joe Perchesacd93622015-02-13 14:38:38 -08007046# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07007047 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08007048 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7049 WARN("LIKELY_MISUSE",
7050 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7051 }
7052
Denis Efremovde3f1862019-09-25 16:49:25 -07007053# nested likely/unlikely calls
7054 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7055 WARN("LIKELY_MISUSE",
7056 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7057 }
7058
Andy Whitcroft691d77b2009-01-06 14:41:16 -08007059# whine mightly about in_atomic
7060 if ($line =~ /\bin_atomic\s*\(/) {
7061 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007062 ERROR("IN_ATOMIC",
7063 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08007064 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007065 WARN("IN_ATOMIC",
7066 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08007067 }
7068 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01007069
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02007070# check for mutex_trylock_recursive usage
7071 if ($line =~ /mutex_trylock_recursive/) {
7072 ERROR("LOCKING",
7073 "recursive locking is bad, do not use this ever.\n" . $herecurr);
7074 }
7075
Peter Zijlstra1704f472010-03-19 01:37:42 +01007076# check for lockdep_set_novalidate_class
7077 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
7078 $line =~ /__lockdep_no_validate__\s*\)/ ) {
7079 if ($realfile !~ m@^kernel/lockdep@ &&
7080 $realfile !~ m@^include/linux/lockdep@ &&
7081 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007082 ERROR("LOCKDEP",
7083 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01007084 }
7085 }
Dave Jones88f88312011-01-12 16:59:59 -08007086
Joe Perchesb392c642015-04-16 12:44:16 -07007087 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7088 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007089 WARN("EXPORTED_WORLD_WRITABLE",
7090 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08007091 }
Joe Perches24358802014-04-03 14:49:13 -07007092
Joe Perches00180462018-02-06 15:38:55 -08007093# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
7094# and whether or not function naming is typical and if
7095# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07007096 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08007097 defined $stat &&
7098 $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*\)/) {
7099 my $var = $1;
7100 my $perms = $2;
7101 my $show = $3;
7102 my $store = $4;
7103 my $octal_perms = perms_to_octal($perms);
7104 if ($show =~ /^${var}_show$/ &&
7105 $store =~ /^${var}_store$/ &&
7106 $octal_perms eq "0644") {
7107 if (WARN("DEVICE_ATTR_RW",
7108 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
7109 $fix) {
7110 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
7111 }
7112 } elsif ($show =~ /^${var}_show$/ &&
7113 $store =~ /^NULL$/ &&
7114 $octal_perms eq "0444") {
7115 if (WARN("DEVICE_ATTR_RO",
7116 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
7117 $fix) {
7118 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
7119 }
7120 } elsif ($show =~ /^NULL$/ &&
7121 $store =~ /^${var}_store$/ &&
7122 $octal_perms eq "0200") {
7123 if (WARN("DEVICE_ATTR_WO",
7124 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
7125 $fix) {
7126 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
7127 }
7128 } elsif ($octal_perms eq "0644" ||
7129 $octal_perms eq "0444" ||
7130 $octal_perms eq "0200") {
7131 my $newshow = "$show";
7132 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
7133 my $newstore = $store;
7134 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
7135 my $rename = "";
7136 if ($show ne $newshow) {
7137 $rename .= " '$show' to '$newshow'";
7138 }
7139 if ($store ne $newstore) {
7140 $rename .= " '$store' to '$newstore'";
7141 }
7142 WARN("DEVICE_ATTR_FUNCTIONS",
7143 "Consider renaming function(s)$rename\n" . $herecurr);
7144 } else {
7145 WARN("DEVICE_ATTR_PERMS",
7146 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
7147 }
7148 }
7149
Joe Perches515a2352014-04-03 14:49:24 -07007150# Mode permission misuses where it seems decimal should be octal
7151# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08007152# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
7153# specific definition of not visible in sysfs.
7154# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
7155# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07007156 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07007157 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07007158 $line =~ /$mode_perms_search/) {
7159 foreach my $entry (@mode_permission_funcs) {
7160 my $func = $entry->[0];
7161 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07007162
Joe Perches459cf0a2016-10-11 13:52:19 -07007163 my $lc = $stat =~ tr@\n@@;
7164 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07007165 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07007166
Joe Perches515a2352014-04-03 14:49:24 -07007167 my $skip_args = "";
7168 if ($arg_pos > 1) {
7169 $arg_pos--;
7170 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
7171 }
Joe Perchesf90774e2016-10-11 13:51:47 -07007172 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07007173 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07007174 my $val = $1;
7175 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08007176 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
7177 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
7178 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07007179 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07007180 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07007181 }
7182 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08007183 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07007184 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07007185 }
Joe Perches24358802014-04-03 14:49:13 -07007186 }
7187 }
7188 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007189
Joe Perches459cf0a2016-10-11 13:52:19 -07007190# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07007191 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08007192 my $oval = $1;
7193 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07007194 if (WARN("SYMBOLIC_PERMS",
7195 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7196 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08007197 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07007198 }
7199 }
7200
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007201# validate content of MODULE_LICENSE against list from include/linux/module.h
7202 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
7203 my $extracted_string = get_quoted_string($line, $rawline);
7204 my $valid_licenses = qr{
7205 GPL|
7206 GPL\ v2|
7207 GPL\ and\ additional\ rights|
7208 Dual\ BSD/GPL|
7209 Dual\ MIT/GPL|
7210 Dual\ MPL/GPL|
7211 Proprietary
7212 }x;
7213 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
7214 WARN("MODULE_LICENSE",
7215 "unknown module license " . $extracted_string . "\n" . $herecurr);
7216 }
7217 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07007218
7219# check for sysctl duplicate constants
7220 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
7221 WARN("DUPLICATED_SYSCTL_CONST",
7222 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
7223 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007224 }
7225
7226 # If we have no input at all, then there is nothing to report on
7227 # so just keep quiet.
7228 if ($#rawlines == -1) {
7229 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007230 }
7231
Andy Whitcroft8905a672007-11-28 16:21:06 -08007232 # In mailback mode only produce a report in the negative, for
7233 # things that appear to be patches.
7234 if ($mailback && ($clean == 1 || !$is_patch)) {
7235 exit(0);
7236 }
7237
Dwaipayan Raye73d2712020-12-15 20:44:56 -08007238 # This is not a patch, and we are in 'no-patch' mode so
Andy Whitcroft8905a672007-11-28 16:21:06 -08007239 # just keep quiet.
7240 if (!$chk_patch && !$is_patch) {
7241 exit(0);
7242 }
7243
Stafford Hornea08ffbe2017-10-03 16:16:51 -07007244 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007245 ERROR("NOT_UNIFIED_DIFF",
7246 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007247 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007248 if ($is_patch && $has_commit_log && $chk_signoff) {
7249 if ($signoff == 0) {
7250 ERROR("MISSING_SIGN_OFF",
7251 "Missing Signed-off-by: line(s)\n");
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07007252 } elsif ($authorsignoff != 1) {
7253 # authorsignoff values:
7254 # 0 -> missing sign off
7255 # 1 -> sign off identical
7256 # 2 -> names and addresses match, comments mismatch
7257 # 3 -> addresses match, names different
7258 # 4 -> names match, addresses different
7259 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
7260
7261 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
7262
7263 if ($authorsignoff == 0) {
7264 ERROR("NO_AUTHOR_SIGN_OFF",
7265 "Missing Signed-off-by: line by nominal patch author '$author'\n");
7266 } elsif ($authorsignoff == 2) {
7267 CHK("FROM_SIGN_OFF_MISMATCH",
7268 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7269 } elsif ($authorsignoff == 3) {
7270 WARN("FROM_SIGN_OFF_MISMATCH",
7271 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7272 } elsif ($authorsignoff == 4) {
7273 WARN("FROM_SIGN_OFF_MISMATCH",
7274 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7275 } elsif ($authorsignoff == 5) {
7276 WARN("FROM_SIGN_OFF_MISMATCH",
7277 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7278 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007279 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007280 }
7281
Andy Whitcroft8905a672007-11-28 16:21:06 -08007282 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007283 if ($summary && !($clean == 1 && $quiet == 1)) {
7284 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08007285 print "total: $cnt_error errors, $cnt_warn warnings, " .
7286 (($check)? "$cnt_chk checks, " : "") .
7287 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07007288 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08007289
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007290 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07007291 # If there were any defects found and not already fixing them
7292 if (!$clean and !$fix) {
7293 print << "EOM"
7294
7295NOTE: For some of the reported defects, checkpatch may be able to
7296 mechanically convert to the typical style using --fix or --fix-inplace.
7297EOM
7298 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007299 # If there were whitespace errors which cleanpatch can fix
7300 # then suggest that.
7301 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07007302 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07007303 print << "EOM"
7304
7305NOTE: Whitespace errors detected.
7306 You may wish to use scripts/cleanpatch or scripts/cleanfile
7307EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007308 }
7309 }
7310
Joe Perchesd752fcc2014-08-06 16:11:05 -07007311 if ($clean == 0 && $fix &&
7312 ("@rawlines" ne "@fixed" ||
7313 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08007314 my $newfile = $filename;
7315 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07007316 my $linecount = 0;
7317 my $f;
7318
Joe Perchesd752fcc2014-08-06 16:11:05 -07007319 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7320
Joe Perches3705ce52013-07-03 15:05:31 -07007321 open($f, '>', $newfile)
7322 or die "$P: Can't open $newfile for write\n";
7323 foreach my $fixed_line (@fixed) {
7324 $linecount++;
7325 if ($file) {
7326 if ($linecount > 3) {
7327 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07007328 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07007329 }
7330 } else {
7331 print $f $fixed_line . "\n";
7332 }
7333 }
7334 close($f);
7335
7336 if (!$quiet) {
7337 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07007338
Joe Perches3705ce52013-07-03 15:05:31 -07007339Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
7340
7341Do _NOT_ trust the results written to this file.
7342Do _NOT_ submit these changes without inspecting them for correctness.
7343
7344This EXPERIMENTAL file is simply a convenience to help rewrite patches.
7345No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07007346EOM
7347 }
7348 }
7349
Joe Perchesd8469f12015-06-25 15:03:00 -07007350 if ($quiet == 0) {
7351 print "\n";
7352 if ($clean == 1) {
7353 print "$vname has no obvious style problems and is ready for submission.\n";
7354 } else {
7355 print "$vname has style problems, please review.\n";
7356 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007357 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007358 return $clean;
7359}