blob: bc80695038192dbda2b44066f794c8806cc09d40 [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Joe Perches882ea1d2018-06-07 17:04:33 -07002# SPDX-License-Identifier: GPL-2.0
3#
Dave Jonesdbf004d2010-01-12 16:59:52 -05004# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07005# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08006# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07007# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Joe Perches882ea1d2018-06-07 17:04:33 -07008# (c) 2010-2018 Joe Perches <joe@perches.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07009
10use strict;
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020011use warnings;
Joe Perchesc707a812013-07-08 16:00:43 -070012use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080013use File::Basename;
14use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070015use Term::ANSIColor qw(:constants);
Geert Uytterhoevencd261492018-08-21 21:57:40 -070016use Encode qw(decode encode);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
18my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080019my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070020
Joe Perches000d1cc12011-07-25 17:13:25 -070021my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070022
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
26my $tree = 1;
27my $chk_signoff = 1;
28my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070029my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080031my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070032my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070034my $git = 0;
Joe Perches0dea9f1e2016-05-20 17:04:19 -070035my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070037my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080038my $summary = 1;
39my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080040my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070041my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070042my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070043my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080044my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070045my $root;
Joe Perches0f7f6352020-10-24 16:59:04 -070046my $gitroot = $ENV{'GIT_DIR'};
47$gitroot = ".git" if !defined($gitroot);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080048my %debug;
Joe Perches34456862013-07-03 15:05:34 -070049my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070050my %use_type = ();
51my @use = ();
52my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070053my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070054my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070055my $configuration_file = ".checkpatch.conf";
Joe Perchesbdc48fa2020-05-29 16:12:21 -070056my $max_line_length = 100;
Dave Hansend62a2012013-09-11 14:23:56 -070057my $ignore_perl_version = 0;
58my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070059my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070060my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070061my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070062my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070063my $conststructsfile = "$D/const_structs.checkpatch";
Quentin Monnetced69da12020-08-11 18:35:13 -070064my $typedefsfile;
John Brooks737c0762017-07-10 15:52:24 -070065my $color = "auto";
Vadim Bendebury98005e82019-03-07 16:28:38 -080066my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
Joe Perchesdbbf8692019-09-25 16:46:52 -070067# git output parsing needs US English output, so first set backtick child process LANGUAGE
68my $git_command ='export LANGUAGE=en_US.UTF-8; git';
Antonio Borneo713a09d2020-04-06 20:11:07 -070069my $tabsize = 8;
Jerome Forissier3e89ad82020-10-15 20:11:49 -070070my ${CONFIG_} = "CONFIG_";
Hannes Eder77f5b102009-09-21 17:04:37 -070071
72sub help {
73 my ($exitcode) = @_;
74
75 print << "EOM";
76Usage: $P [OPTION]... [FILE]...
77Version: $V
78
79Options:
80 -q, --quiet quiet
81 --no-tree run without a kernel tree
82 --no-signoff do not check for 'Signed-off-by' line
83 --patch treat FILE as patchfile (default)
84 --emacs emacs compile window format
85 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070086 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070087 -g, --git treat FILE as a single commit or git revision range
88 single git commit with:
89 <rev>
90 <rev>^
91 <rev>~n
92 multiple git commits with:
93 <rev1>..<rev2>
94 <rev1>...<rev2>
95 <rev>-<count>
96 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070097 -f, --file treat FILE as regular source file
98 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070099 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -0700100 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -0700101 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -0700102 --show-types show the specific message type in the output
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700103 --max-line-length=n set the maximum line length, (default $max_line_length)
104 if exceeded, warn on patches
105 requires --strict for use with --file
Vadim Bendebury56193272014-10-13 15:51:48 -0700106 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700107 --tab-size=n set the number of spaces for tab (default $tabsize)
Hannes Eder77f5b102009-09-21 17:04:37 -0700108 --root=PATH PATH to the kernel tree root
109 --no-summary suppress the per-file summary
110 --mailback only produce a report in case of warnings/errors
111 --summary-file include the filename in summary
112 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
113 'values', 'possible', 'type', and 'attr' (default
114 is all off)
115 --test-only=WORD report only warnings/errors containing WORD
116 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700117 --fix EXPERIMENTAL - may create horrible results
118 If correctable single-line errors exist, create
119 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
120 with potential errors corrected to the preferred
121 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800122 --fix-inplace EXPERIMENTAL - may create horrible results
123 Is the same as --fix, but overwrites the input
124 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700125 --ignore-perl-version override checking of perl version. expect
126 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700127 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700128 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700129 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700130 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700131 --color[=WHEN] Use colors 'always', 'never', or only when output
132 is a terminal ('auto'). Default is 'auto'.
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700133 --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default
134 ${CONFIG_})
Hannes Eder77f5b102009-09-21 17:04:37 -0700135 -h, --help, --version display this help and exit
136
137When FILE is - read standard input.
138EOM
139
140 exit($exitcode);
141}
142
Joe Perches3beb42e2016-05-20 17:04:14 -0700143sub uniq {
144 my %seen;
145 return grep { !$seen{$_}++ } @_;
146}
147
148sub list_types {
149 my ($exitcode) = @_;
150
151 my $count = 0;
152
153 local $/ = undef;
154
155 open(my $script, '<', abs_path($P)) or
156 die "$P: Can't read '$P' $!\n";
157
158 my $text = <$script>;
159 close($script);
160
161 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700162 # Also catch when type or level is passed through a variable
163 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700164 push (@types, $_);
165 }
166 @types = sort(uniq(@types));
167 print("#\tMessage type\n\n");
168 foreach my $type (@types) {
169 print(++$count . "\t" . $type . "\n");
170 }
171
172 exit($exitcode);
173}
174
Joe Perches000d1cc12011-07-25 17:13:25 -0700175my $conf = which_conf($configuration_file);
176if (-f $conf) {
177 my @conf_args;
178 open(my $conffile, '<', "$conf")
179 or warn "$P: Can't find a readable $configuration_file file $!\n";
180
181 while (<$conffile>) {
182 my $line = $_;
183
184 $line =~ s/\s*\n?$//g;
185 $line =~ s/^\s*//g;
186 $line =~ s/\s+/ /g;
187
188 next if ($line =~ m/^\s*#/);
189 next if ($line =~ m/^\s*$/);
190
191 my @words = split(" ", $line);
192 foreach my $word (@words) {
193 last if ($word =~ m/^#/);
194 push (@conf_args, $word);
195 }
196 }
197 close($conffile);
198 unshift(@ARGV, @conf_args) if @conf_args;
199}
200
John Brooks737c0762017-07-10 15:52:24 -0700201# Perl's Getopt::Long allows options to take optional arguments after a space.
202# Prevent --color by itself from consuming other arguments
203foreach (@ARGV) {
204 if ($_ eq "--color" || $_ eq "-color") {
205 $_ = "--color=$color";
206 }
207}
208
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700209GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700210 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700211 'tree!' => \$tree,
212 'signoff!' => \$chk_signoff,
213 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700214 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800215 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700216 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700217 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700218 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700219 'subjective!' => \$check,
220 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700221 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700222 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700223 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700224 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800225 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700226 'min-conf-desc-length=i' => \$min_conf_desc_length,
Antonio Borneo713a09d2020-04-06 20:11:07 -0700227 'tab-size=i' => \$tabsize,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700228 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800229 'summary!' => \$summary,
230 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800231 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700232 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800233 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700234 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800235 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700236 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700237 'codespell!' => \$codespell,
238 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700239 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700240 'color=s' => \$color,
241 'no-color' => \$color, #keep old behaviors of -nocolor
242 'nocolor' => \$color, #keep old behaviors of -nocolor
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700243 'kconfig-prefix=s' => \${CONFIG_},
Hannes Eder77f5b102009-09-21 17:04:37 -0700244 'h|help' => \$help,
245 'version' => \$help
246) or help(1);
247
248help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700249
Joe Perches3beb42e2016-05-20 17:04:14 -0700250list_types(0) if ($list_types);
251
Joe Perches9624b8d2014-01-23 15:54:44 -0800252$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700253$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800254
Joe Perches32f30ca2020-06-04 16:50:40 -0700255die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
256
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700257my $exit = 0;
258
Joe Perches5b579802018-08-21 21:57:33 -0700259my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700260if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700261 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700262 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700263 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700264}
265
Allen Hubbe45107ff2016-08-02 14:04:47 -0700266#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700267if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700268 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700269}
270
John Brooks737c0762017-07-10 15:52:24 -0700271if ($color =~ /^[01]$/) {
272 $color = !$color;
273} elsif ($color =~ /^always$/i) {
274 $color = 1;
275} elsif ($color =~ /^never$/i) {
276 $color = 0;
277} elsif ($color =~ /^auto$/i) {
278 $color = (-t STDOUT);
279} else {
Joe Perches32f30ca2020-06-04 16:50:40 -0700280 die "$P: Invalid color mode: $color\n";
John Brooks737c0762017-07-10 15:52:24 -0700281}
282
Antonio Borneo713a09d2020-04-06 20:11:07 -0700283# skip TAB size 1 to avoid additional checks on $tabsize - 1
Joe Perches32f30ca2020-06-04 16:50:40 -0700284die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
Antonio Borneo713a09d2020-04-06 20:11:07 -0700285
Joe Perches91bfe482013-09-11 14:23:59 -0700286sub hash_save_array_words {
287 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700288
Joe Perches91bfe482013-09-11 14:23:59 -0700289 my @array = split(/,/, join(',', @$arrayRef));
290 foreach my $word (@array) {
291 $word =~ s/\s*\n?$//g;
292 $word =~ s/^\s*//g;
293 $word =~ s/\s+/ /g;
294 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700295
Joe Perches91bfe482013-09-11 14:23:59 -0700296 next if ($word =~ m/^\s*#/);
297 next if ($word =~ m/^\s*$/);
298
299 $hashRef->{$word}++;
300 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700301}
302
Joe Perches91bfe482013-09-11 14:23:59 -0700303sub hash_show_words {
304 my ($hashRef, $prefix) = @_;
305
Joe Perches3c816e42015-06-25 15:03:29 -0700306 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700307 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700308 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700309 print " $word";
310 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700311 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700312 }
313}
314
315hash_save_array_words(\%ignore_type, \@ignore);
316hash_save_array_words(\%use_type, \@use);
317
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800318my $dbg_values = 0;
319my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700320my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700321my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800322for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800323 ## no critic
324 eval "\${dbg_$key} = '$debug{$key}';";
325 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800326}
327
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700328my $rpt_cleaners = 0;
329
Andy Whitcroft8905a672007-11-28 16:21:06 -0800330if ($terse) {
331 $emacs = 1;
332 $quiet++;
333}
334
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700335if ($tree) {
336 if (defined $root) {
337 if (!top_of_kernel_tree($root)) {
338 die "$P: $root: --root does not point at a valid tree\n";
339 }
340 } else {
341 if (top_of_kernel_tree('.')) {
342 $root = '.';
343 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
344 top_of_kernel_tree($1)) {
345 $root = $1;
346 }
347 }
348
349 if (!defined $root) {
350 print "Must be run from the top-level dir. of a kernel tree\n";
351 exit(2);
352 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700353}
354
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700355my $emitted_corrupt = 0;
356
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700357our $Ident = qr{
358 [A-Za-z_][A-Za-z\d_]*
359 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
360 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700361our $Storage = qr{extern|static|asmlinkage};
362our $Sparse = qr{
363 __user|
364 __kernel|
365 __force|
366 __iomem|
367 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800368 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700369 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700370 __refconst|
371 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800372 __rcu|
373 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700374 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800375our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
376our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
377our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
378our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
379our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700380
Wolfram Sang52131292010-03-05 13:43:51 -0800381# Notes to $Attribute:
382# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700383our $Attribute = qr{
384 const|
Joe Perchesb5e87362021-02-25 17:21:40 -0800385 volatile|
Joe Perches03f1df72010-10-26 14:23:16 -0700386 __percpu|
387 __nocast|
388 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200389 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700390 __packed__|
391 __packed2__|
392 __naked|
393 __maybe_unused|
394 __always_unused|
395 __noreturn|
396 __used|
397 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800398 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700399 __noclone|
400 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700401 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700402 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700403 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700404 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700405 ____cacheline_aligned|
406 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800407 ____cacheline_internodealigned_in_smp|
408 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700409 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700410our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700411our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700412our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
413our $Lval = qr{$Ident(?:$Member)*};
414
Joe Perches95e2c602013-07-03 15:05:20 -0700415our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
416our $Binary = qr{(?i)0b[01]+$Int_type?};
417our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
418our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700419our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800420our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800421our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
422our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
423our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800424our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700425our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800426our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700427our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700428our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700429our $Operators = qr{
430 <=|>=|==|!=|
431 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700432 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700433 }x;
434
Joe Perches91cb5192014-04-03 14:49:32 -0700435our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
436
Joe Perchesab7e23f2015-04-16 12:44:22 -0700437our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800438our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700439our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700440our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800441our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700442our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800443our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700444our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800445
Joe Perches15662b32011-10-31 17:13:12 -0700446our $NON_ASCII_UTF8 = qr{
447 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700448 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
449 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
450 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
451 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
452 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
453 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
454}x;
455
Joe Perches15662b32011-10-31 17:13:12 -0700456our $UTF8 = qr{
457 [\x09\x0A\x0D\x20-\x7E] # ASCII
458 | $NON_ASCII_UTF8
459}x;
460
Joe Perchese6176fa2015-06-25 15:02:49 -0700461our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800462our $typeOtherOSTypedefs = qr{(?x:
463 u_(?:char|short|int|long) | # bsd
464 u(?:nchar|short|int|long) # sysv
465)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700466our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700467 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700468 atomic_t
469)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700470our $typeTypedefs = qr{(?x:
471 $typeC99Typedefs\b|
472 $typeOtherOSTypedefs\b|
473 $typeKernelTypedefs\b
474)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700475
Joe Perches6d32f7a2015-11-06 16:31:37 -0800476our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
477
Joe Perches691e6692010-03-05 13:43:51 -0800478our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800479 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700480 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches87bd4992017-11-17 15:28:48 -0800481 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700482 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700483 panic|
Joe Perches06668722013-11-12 15:10:07 -0800484 MODULE_[A-Z_]+|
485 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800486)};
487
Joe Perchese29a70f2019-03-07 16:28:35 -0800488our $allocFunctions = qr{(?x:
489 (?:(?:devm_)?
490 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
491 kstrdup(?:_const)? |
492 kmemdup(?:_nul)?) |
Christophe JAILLET461e1562020-04-20 18:13:58 -0700493 (?:\w+)?alloc_skb(?:_ip_align)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800494 # dev_alloc_skb/netdev_alloc_skb, et al
495 dma_alloc_coherent
496)};
497
Joe Perches20112472011-07-25 17:13:23 -0700498our $signature_tags = qr{(?xi:
499 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800500 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700501 Acked-by:|
502 Tested-by:|
503 Reviewed-by:|
504 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700505 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700506 To:|
507 Cc:
508)};
509
Aditya Srivastava831242a2020-12-15 20:45:12 -0800510sub edit_distance_min {
511 my (@arr) = @_;
512 my $len = scalar @arr;
513 if ((scalar @arr) < 1) {
514 # if underflow, return
515 return;
516 }
517 my $min = $arr[0];
518 for my $i (0 .. ($len-1)) {
519 if ($arr[$i] < $min) {
520 $min = $arr[$i];
521 }
522 }
523 return $min;
524}
525
526sub get_edit_distance {
527 my ($str1, $str2) = @_;
528 $str1 = lc($str1);
529 $str2 = lc($str2);
530 $str1 =~ s/-//g;
531 $str2 =~ s/-//g;
532 my $len1 = length($str1);
533 my $len2 = length($str2);
534 # two dimensional array storing minimum edit distance
535 my @distance;
536 for my $i (0 .. $len1) {
537 for my $j (0 .. $len2) {
538 if ($i == 0) {
539 $distance[$i][$j] = $j;
540 } elsif ($j == 0) {
541 $distance[$i][$j] = $i;
542 } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
543 $distance[$i][$j] = $distance[$i - 1][$j - 1];
544 } else {
545 my $dist1 = $distance[$i][$j - 1]; #insert distance
546 my $dist2 = $distance[$i - 1][$j]; # remove
547 my $dist3 = $distance[$i - 1][$j - 1]; #replace
548 $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
549 }
550 }
551 }
552 return $distance[$len1][$len2];
553}
554
555sub find_standard_signature {
556 my ($sign_off) = @_;
557 my @standard_signature_tags = (
558 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
559 'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
560 );
561 foreach my $signature (@standard_signature_tags) {
562 return $signature if (get_edit_distance($sign_off, $signature) <= 2);
563 }
564
565 return "";
566}
567
Joe Perches18130872014-08-06 16:11:22 -0700568our @typeListMisordered = (
569 qr{char\s+(?:un)?signed},
570 qr{int\s+(?:(?:un)?signed\s+)?short\s},
571 qr{int\s+short(?:\s+(?:un)?signed)},
572 qr{short\s+int(?:\s+(?:un)?signed)},
573 qr{(?:un)?signed\s+int\s+short},
574 qr{short\s+(?:un)?signed},
575 qr{long\s+int\s+(?:un)?signed},
576 qr{int\s+long\s+(?:un)?signed},
577 qr{long\s+(?:un)?signed\s+int},
578 qr{int\s+(?:un)?signed\s+long},
579 qr{int\s+(?:un)?signed},
580 qr{int\s+long\s+long\s+(?:un)?signed},
581 qr{long\s+long\s+int\s+(?:un)?signed},
582 qr{long\s+long\s+(?:un)?signed\s+int},
583 qr{long\s+long\s+(?:un)?signed},
584 qr{long\s+(?:un)?signed},
585);
586
Andy Whitcroft8905a672007-11-28 16:21:06 -0800587our @typeList = (
588 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700589 qr{(?:(?:un)?signed\s+)?char},
590 qr{(?:(?:un)?signed\s+)?short\s+int},
591 qr{(?:(?:un)?signed\s+)?short},
592 qr{(?:(?:un)?signed\s+)?int},
593 qr{(?:(?:un)?signed\s+)?long\s+int},
594 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
595 qr{(?:(?:un)?signed\s+)?long\s+long},
596 qr{(?:(?:un)?signed\s+)?long},
597 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800598 qr{float},
599 qr{double},
600 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800601 qr{struct\s+$Ident},
602 qr{union\s+$Ident},
603 qr{enum\s+$Ident},
604 qr{${Ident}_t},
605 qr{${Ident}_handler},
606 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700607 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800608);
Joe Perches938224b2016-01-20 14:59:15 -0800609
610our $C90_int_types = qr{(?x:
611 long\s+long\s+int\s+(?:un)?signed|
612 long\s+long\s+(?:un)?signed\s+int|
613 long\s+long\s+(?:un)?signed|
614 (?:(?:un)?signed\s+)?long\s+long\s+int|
615 (?:(?:un)?signed\s+)?long\s+long|
616 int\s+long\s+long\s+(?:un)?signed|
617 int\s+(?:(?:un)?signed\s+)?long\s+long|
618
619 long\s+int\s+(?:un)?signed|
620 long\s+(?:un)?signed\s+int|
621 long\s+(?:un)?signed|
622 (?:(?:un)?signed\s+)?long\s+int|
623 (?:(?:un)?signed\s+)?long|
624 int\s+long\s+(?:un)?signed|
625 int\s+(?:(?:un)?signed\s+)?long|
626
627 int\s+(?:un)?signed|
628 (?:(?:un)?signed\s+)?int
629)};
630
Alex Dowad485ff232015-06-25 15:02:52 -0700631our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700632our @typeListWithAttr = (
633 @typeList,
634 qr{struct\s+$InitAttribute\s+$Ident},
635 qr{union\s+$InitAttribute\s+$Ident},
636);
637
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700638our @modifierList = (
639 qr{fastcall},
640);
Alex Dowad485ff232015-06-25 15:02:52 -0700641our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800642
Joe Perches24358802014-04-03 14:49:13 -0700643our @mode_permission_funcs = (
644 ["module_param", 3],
645 ["module_param_(?:array|named|string)", 4],
646 ["module_param_array_named", 5],
647 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
648 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700649 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
650 ["IIO_DEV_ATTR_[A-Z_]+", 1],
651 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
652 ["SENSOR_TEMPLATE(?:_2|)", 3],
653 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700654);
655
Joe Perches1a3dcf22020-08-11 18:35:16 -0700656my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
657
Joe Perches515a2352014-04-03 14:49:24 -0700658#Create a search pattern for all these functions to speed up a loop below
659our $mode_perms_search = "";
660foreach my $entry (@mode_permission_funcs) {
661 $mode_perms_search .= '|' if ($mode_perms_search ne "");
662 $mode_perms_search .= $entry->[0];
663}
Joe Perches00180462018-02-06 15:38:55 -0800664$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700665
Joe Perches9189c7e2018-09-07 15:26:18 -0700666our %deprecated_apis = (
667 "synchronize_rcu_bh" => "synchronize_rcu",
668 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
669 "call_rcu_bh" => "call_rcu",
670 "rcu_barrier_bh" => "rcu_barrier",
671 "synchronize_sched" => "synchronize_rcu",
672 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
673 "call_rcu_sched" => "call_rcu",
674 "rcu_barrier_sched" => "rcu_barrier",
675 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
676 "cond_synchronize_sched" => "cond_synchronize_rcu",
677);
678
679#Create a search pattern for all these strings to speed up a loop below
680our $deprecated_apis_search = "";
681foreach my $entry (keys %deprecated_apis) {
682 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
683 $deprecated_apis_search .= $entry;
684}
685$deprecated_apis_search = "(?:${deprecated_apis_search})";
686
Joe Perchesb392c642015-04-16 12:44:16 -0700687our $mode_perms_world_writable = qr{
688 S_IWUGO |
689 S_IWOTH |
690 S_IRWXUGO |
691 S_IALLUGO |
692 0[0-7][0-7][2367]
693}x;
694
Joe Perchesf90774e2016-10-11 13:51:47 -0700695our %mode_permission_string_types = (
696 "S_IRWXU" => 0700,
697 "S_IRUSR" => 0400,
698 "S_IWUSR" => 0200,
699 "S_IXUSR" => 0100,
700 "S_IRWXG" => 0070,
701 "S_IRGRP" => 0040,
702 "S_IWGRP" => 0020,
703 "S_IXGRP" => 0010,
704 "S_IRWXO" => 0007,
705 "S_IROTH" => 0004,
706 "S_IWOTH" => 0002,
707 "S_IXOTH" => 0001,
708 "S_IRWXUGO" => 0777,
709 "S_IRUGO" => 0444,
710 "S_IWUGO" => 0222,
711 "S_IXUGO" => 0111,
712);
713
714#Create a search pattern for all these strings to speed up a loop below
715our $mode_perms_string_search = "";
716foreach my $entry (keys %mode_permission_string_types) {
717 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
718 $mode_perms_string_search .= $entry;
719}
Joe Perches00180462018-02-06 15:38:55 -0800720our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
721our $multi_mode_perms_string_search = qr{
722 ${single_mode_perms_string_search}
723 (?:\s*\|\s*${single_mode_perms_string_search})*
724}x;
725
726sub perms_to_octal {
727 my ($string) = @_;
728
729 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
730
731 my $val = "";
732 my $oval = "";
733 my $to = 0;
734 my $curpos = 0;
735 my $lastpos = 0;
736 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
737 $curpos = pos($string);
738 my $match = $2;
739 my $omatch = $1;
740 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
741 $lastpos = $curpos;
742 $to |= $mode_permission_string_types{$match};
743 $val .= '\s*\|\s*' if ($val ne "");
744 $val .= $match;
745 $oval .= $omatch;
746 }
747 $oval =~ s/^\s*\|\s*//;
748 $oval =~ s/\s*\|\s*$//;
749 return sprintf("%04o", $to);
750}
Joe Perchesf90774e2016-10-11 13:51:47 -0700751
Wolfram Sang7840a942010-08-09 17:20:57 -0700752our $allowed_asm_includes = qr{(?x:
753 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700754 memory|
755 time|
756 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700757)};
758# memory.h: ARM has a custom one
759
Kees Cook66b47b42014-10-13 15:51:57 -0700760# Load common spelling mistakes and build regular expression list.
761my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700762my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700763
Joe Perches36061e32014-12-10 15:51:43 -0800764if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800765 while (<$spelling>) {
766 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700767
Joe Perches36061e32014-12-10 15:51:43 -0800768 $line =~ s/\s*\n?$//g;
769 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700770
Joe Perches36061e32014-12-10 15:51:43 -0800771 next if ($line =~ m/^\s*#/);
772 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700773
Joe Perches36061e32014-12-10 15:51:43 -0800774 my ($suspect, $fix) = split(/\|\|/, $line);
775
Joe Perches36061e32014-12-10 15:51:43 -0800776 $spelling_fix{$suspect} = $fix;
777 }
778 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800779} else {
780 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700781}
Kees Cook66b47b42014-10-13 15:51:57 -0700782
Joe Perchesebfd7d62015-04-16 12:44:14 -0700783if ($codespell) {
784 if (open(my $spelling, '<', $codespellfile)) {
785 while (<$spelling>) {
786 my $line = $_;
787
788 $line =~ s/\s*\n?$//g;
789 $line =~ s/^\s*//g;
790
791 next if ($line =~ m/^\s*#/);
792 next if ($line =~ m/^\s*$/);
793 next if ($line =~ m/, disabled/i);
794
795 $line =~ s/,.*$//;
796
797 my ($suspect, $fix) = split(/->/, $line);
798
799 $spelling_fix{$suspect} = $fix;
800 }
801 close($spelling);
802 } else {
803 warn "No codespell typos will be found - file '$codespellfile': $!\n";
804 }
805}
806
807$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
808
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700809sub read_words {
810 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700811
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700812 if (open(my $words, '<', $file)) {
813 while (<$words>) {
814 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700815
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700816 $line =~ s/\s*\n?$//g;
817 $line =~ s/^\s*//g;
818
819 next if ($line =~ m/^\s*#/);
820 next if ($line =~ m/^\s*$/);
821 if ($line =~ /\s/) {
822 print("$file: '$line' invalid - ignored\n");
823 next;
824 }
825
Quentin Monnetced69da12020-08-11 18:35:13 -0700826 $$wordsRef .= '|' if (defined $$wordsRef);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700827 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700828 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700829 close($file);
830 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700831 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700832
833 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700834}
835
Quentin Monnetced69da12020-08-11 18:35:13 -0700836my $const_structs;
837if (show_type("CONST_STRUCT")) {
838 read_words(\$const_structs, $conststructsfile)
839 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
840}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700841
Quentin Monnetced69da12020-08-11 18:35:13 -0700842if (defined($typedefsfile)) {
843 my $typeOtherTypedefs;
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700844 read_words(\$typeOtherTypedefs, $typedefsfile)
845 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
Quentin Monnetced69da12020-08-11 18:35:13 -0700846 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700847}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700848
Andy Whitcroft8905a672007-11-28 16:21:06 -0800849sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700850 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
851 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700852 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700853 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700854 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700855 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700856 (?:$typeTypedefs\b)|
857 (?:${all}\b)
858 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800859 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700860 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800861 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800862 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700863 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700864 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800865 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700866 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800867 }x;
Joe Perches18130872014-08-06 16:11:22 -0700868 $NonptrTypeMisordered = qr{
869 (?:$Modifier\s+|const\s+)*
870 (?:
871 (?:${Misordered}\b)
872 )
873 (?:\s+$Modifier|\s+const)*
874 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700875 $NonptrTypeWithAttr = qr{
876 (?:$Modifier\s+|const\s+)*
877 (?:
878 (?:typeof|__typeof__)\s*\([^\)]*\)|
879 (?:$typeTypedefs\b)|
880 (?:${allWithAttr}\b)
881 )
882 (?:\s+$Modifier|\s+const)*
883 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800884 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700885 $NonptrType
Antonio Borneo7b184962020-04-06 20:11:04 -0700886 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700887 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800888 }x;
Joe Perches18130872014-08-06 16:11:22 -0700889 $TypeMisordered = qr{
890 $NonptrTypeMisordered
Antonio Borneo7b184962020-04-06 20:11:04 -0700891 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Perches18130872014-08-06 16:11:22 -0700892 (?:\s+$Inline|\s+$Modifier)*
893 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700894 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700895 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800896}
897build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700898
Joe Perches7d2367a2011-07-25 17:13:22 -0700899our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700900
901# Using $balanced_parens, $LvalOrFunc, or $FuncArg
902# requires at least perl version v5.10.0
903# Any use must be runtime checked with $^V
904
905our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700906our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800907our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700908
Joe Perchesf8422302014-08-06 16:11:31 -0700909our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700910 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700911 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700912 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700913)};
914
Aditya Srivastava8d0325c2020-12-15 20:44:24 -0800915our %allow_repeated_words = (
916 add => '',
917 added => '',
918 bad => '',
919 be => '',
920);
921
Joe Perches7d2367a2011-07-25 17:13:22 -0700922sub deparenthesize {
923 my ($string) = @_;
924 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700925
926 while ($string =~ /^\s*\(.*\)\s*$/) {
927 $string =~ s@^\s*\(\s*@@;
928 $string =~ s@\s*\)\s*$@@;
929 }
930
Joe Perches7d2367a2011-07-25 17:13:22 -0700931 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700932
Joe Perches7d2367a2011-07-25 17:13:22 -0700933 return $string;
934}
935
Joe Perches34456862013-07-03 15:05:34 -0700936sub seed_camelcase_file {
937 my ($file) = @_;
938
939 return if (!(-f $file));
940
941 local $/;
942
943 open(my $include_file, '<', "$file")
944 or warn "$P: Can't read '$file' $!\n";
945 my $text = <$include_file>;
946 close($include_file);
947
948 my @lines = split('\n', $text);
949
950 foreach my $line (@lines) {
951 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
952 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
953 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800954 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
955 $camelcase{$1} = 1;
956 } 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 -0700957 $camelcase{$1} = 1;
958 }
959 }
960}
961
Joe Perchescd28b112019-12-04 16:52:09 -0800962our %maintained_status = ();
963
Joe Perches85b0ee12016-10-11 13:51:44 -0700964sub is_maintained_obsolete {
965 my ($filename) = @_;
966
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800967 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700968
Joe Perchescd28b112019-12-04 16:52:09 -0800969 if (!exists($maintained_status{$filename})) {
970 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
971 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700972
Joe Perchescd28b112019-12-04 16:52:09 -0800973 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700974}
975
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700976sub is_SPDX_License_valid {
977 my ($license) = @_;
978
Joe Perches0f7f6352020-10-24 16:59:04 -0700979 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700980
Joe Perches56294112018-08-21 21:58:04 -0700981 my $root_path = abs_path($root);
982 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700983 return 0 if ($status ne "");
984 return 1;
985}
986
Joe Perches34456862013-07-03 15:05:34 -0700987my $camelcase_seeded = 0;
988sub seed_camelcase_includes {
989 return if ($camelcase_seeded);
990
991 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700992 my $camelcase_cache = "";
993 my @include_files = ();
994
995 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700996
Joe Perches0f7f6352020-10-24 16:59:04 -0700997 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700998 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -0700999 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -07001000 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -07001001 } else {
Joe Perchesc707a812013-07-08 16:00:43 -07001002 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -07001003 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -07001004 @include_files = split('\n', $files);
1005 foreach my $file (@include_files) {
1006 my $date = POSIX::strftime("%Y%m%d%H%M",
1007 localtime((stat $file)[9]));
1008 $last_mod_date = $date if ($last_mod_date < $date);
1009 }
1010 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -07001011 }
Joe Perchesc707a812013-07-08 16:00:43 -07001012
1013 if ($camelcase_cache ne "" && -f $camelcase_cache) {
1014 open(my $camelcase_file, '<', "$camelcase_cache")
1015 or warn "$P: Can't read '$camelcase_cache' $!\n";
1016 while (<$camelcase_file>) {
1017 chomp;
1018 $camelcase{$_} = 1;
1019 }
1020 close($camelcase_file);
1021
1022 return;
1023 }
1024
Joe Perches0f7f6352020-10-24 16:59:04 -07001025 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -07001026 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -07001027 @include_files = split('\n', $files);
1028 }
1029
Joe Perches34456862013-07-03 15:05:34 -07001030 foreach my $file (@include_files) {
1031 seed_camelcase_file($file);
1032 }
Joe Perches351b2a12013-07-03 15:05:36 -07001033
Joe Perchesc707a812013-07-08 16:00:43 -07001034 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -07001035 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -07001036 open(my $camelcase_file, '>', "$camelcase_cache")
1037 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -07001038 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1039 print $camelcase_file ("$_\n");
1040 }
1041 close($camelcase_file);
1042 }
Joe Perches34456862013-07-03 15:05:34 -07001043}
1044
Joe Perchesf5f61322020-10-15 20:12:12 -07001045sub git_is_single_file {
1046 my ($filename) = @_;
1047
1048 return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1049
1050 my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1051 my $count = $output =~ tr/\n//;
1052 return $count eq 1 && $output =~ m{^${filename}$};
1053}
1054
Joe Perchesd311cd42014-08-06 16:10:57 -07001055sub git_commit_info {
1056 my ($commit, $id, $desc) = @_;
1057
Joe Perches0f7f6352020-10-24 16:59:04 -07001058 return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
Joe Perchesd311cd42014-08-06 16:10:57 -07001059
Joe Perchesdbbf8692019-09-25 16:46:52 -07001060 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -07001061 $output =~ s/^\s*//gm;
1062 my @lines = split("\n", $output);
1063
Joe Perches0d7835f2015-02-13 14:38:35 -08001064 return ($id, $desc) if ($#lines < 0);
1065
Sean Christopherson5a7f4452019-09-25 16:46:49 -07001066 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -07001067# Maybe one day convert this block of bash into something that returns
1068# all matching commit ids, but it's very slow...
1069#
1070# echo "checking commits $1..."
1071# git rev-list --remotes | grep -i "^$1" |
1072# while read line ; do
1073# git log --format='%H %s' -1 $line |
1074# echo "commit $(cut -c 1-12,41-)"
1075# done
1076 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07001077 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -07001078 } else {
1079 $id = substr($lines[0], 0, 12);
1080 $desc = substr($lines[0], 41);
1081 }
1082
1083 return ($id, $desc);
1084}
1085
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001086$chk_signoff = 0 if ($file);
1087
Andy Whitcroft00df3442007-06-08 13:47:06 -07001088my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001089my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001090my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001091my @fixed_inserted = ();
1092my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001093my $fixlinenr = -1;
1094
Du, Changbin4a593c32016-05-20 17:04:16 -07001095# If input is git commits, extract all commits from the commit expressions.
1096# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
Joe Perches0f7f6352020-10-24 16:59:04 -07001097die "$P: No git repository found\n" if ($git && !-e "$gitroot");
Du, Changbin4a593c32016-05-20 17:04:16 -07001098
1099if ($git) {
1100 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001101 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001102 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001103 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1104 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001105 } elsif ($commit_expr =~ m/\.\./) {
1106 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001107 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001108 $git_range = "-1 $commit_expr";
1109 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001110 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001111 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001112 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1113 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001114 my $sha1 = $1;
1115 my $subject = $2;
1116 unshift(@commits, $sha1);
1117 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001118 }
1119 }
1120 die "$P: no git commits after extraction!\n" if (@commits == 0);
1121 @ARGV = @commits;
1122}
1123
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001124my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001125$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001126for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001127 my $FILE;
Joe Perchesf5f61322020-10-15 20:12:12 -07001128 my $is_git_file = git_is_single_file($filename);
1129 my $oldfile = $file;
1130 $file = 1 if ($is_git_file);
Du, Changbin4a593c32016-05-20 17:04:16 -07001131 if ($git) {
1132 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1133 die "$P: $filename: git format-patch failed - $!\n";
1134 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001135 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001136 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001137 } elsif ($filename eq '-') {
1138 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001139 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001140 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001141 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001142 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001143 if ($filename eq '-') {
1144 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001145 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001146 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001147 } else {
1148 $vname = $filename;
1149 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001150 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001151 chomp;
1152 push(@rawlines, $_);
Geert Uytterhoevenc7f574d2020-06-04 16:50:43 -07001153 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001154 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001155 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001156
1157 if ($#ARGV > 0 && $quiet == 0) {
1158 print '-' x length($vname) . "\n";
1159 print "$vname\n";
1160 print '-' x length($vname) . "\n";
1161 }
1162
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001163 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001164 $exit = 1;
1165 }
1166 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001167 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001168 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001169 @fixed_inserted = ();
1170 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001171 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001172 @modifierListFile = ();
1173 @typeListFile = ();
1174 build_types();
Joe Perchesf5f61322020-10-15 20:12:12 -07001175 $file = $oldfile if ($is_git_file);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001176}
1177
Joe Perchesd8469f12015-06-25 15:03:00 -07001178if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001179 hash_show_words(\%use_type, "Used");
1180 hash_show_words(\%ignore_type, "Ignored");
1181
Joe Perches5b579802018-08-21 21:57:33 -07001182 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001183 print << "EOM"
1184
1185NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001186 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001187EOM
1188 }
1189 if ($exit) {
1190 print << "EOM"
1191
1192NOTE: If any of the errors are false positives, please report
1193 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1194EOM
1195 }
1196}
1197
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001198exit($exit);
1199
1200sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001201 my ($root) = @_;
1202
1203 my @tree_check = (
1204 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1205 "README", "Documentation", "arch", "include", "drivers",
1206 "fs", "init", "ipc", "kernel", "lib", "scripts",
1207 );
1208
1209 foreach my $check (@tree_check) {
1210 if (! -e $root . '/' . $check) {
1211 return 0;
1212 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001213 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001214 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001215}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001216
Joe Perches20112472011-07-25 17:13:23 -07001217sub parse_email {
1218 my ($formatted_email) = @_;
1219
1220 my $name = "";
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001221 my $quoted = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001222 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001223 my $address = "";
1224 my $comment = "";
1225
1226 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1227 $name = $1;
1228 $address = $2;
1229 $comment = $3 if defined $3;
1230 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1231 $address = $1;
1232 $comment = $2 if defined $2;
1233 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1234 $address = $1;
1235 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001236 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001237 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001238 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001239 $name =~ s/^\"|\"$//g;
1240 # If there's a name left after stripping spaces and
1241 # leading quotes, and the address doesn't have both
1242 # leading and trailing angle brackets, the address
1243 # is invalid. ie:
1244 # "joe smith joe@smith.com" bad
1245 # "joe smith <joe@smith.com" bad
1246 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1247 $name = "";
1248 $address = "";
1249 $comment = "";
1250 }
1251 }
1252
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001253 # Extract comments from names excluding quoted parts
1254 # "John D. (Doe)" - Do not extract
1255 if ($name =~ s/\"(.+)\"//) {
1256 $quoted = $1;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001257 }
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001258 while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1259 $name_comment .= trim($1);
1260 }
1261 $name =~ s/^[ \"]+|[ \"]+$//g;
1262 $name = trim("$quoted $name");
1263
Joe Perches3705ce52013-07-03 15:05:31 -07001264 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001265 $address =~ s/^\<|\>$//g;
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001266 $comment = trim($comment);
Joe Perches20112472011-07-25 17:13:23 -07001267
1268 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1269 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1270 $name = "\"$name\"";
1271 }
1272
Joe Perchesdfa05c22020-04-06 20:10:48 -07001273 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001274}
1275
1276sub format_email {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001277 my ($name, $name_comment, $address, $comment) = @_;
Joe Perches20112472011-07-25 17:13:23 -07001278
1279 my $formatted_email;
1280
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001281 $name =~ s/^[ \"]+|[ \"]+$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001282 $address = trim($address);
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001283 $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
Joe Perches20112472011-07-25 17:13:23 -07001284
1285 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1286 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1287 $name = "\"$name\"";
1288 }
1289
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001290 $name_comment = trim($name_comment);
1291 $name_comment = " $name_comment" if ($name_comment ne "");
1292 $comment = trim($comment);
1293 $comment = " $comment" if ($comment ne "");
1294
Joe Perches20112472011-07-25 17:13:23 -07001295 if ("$name" eq "") {
1296 $formatted_email = "$address";
1297 } else {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001298 $formatted_email = "$name$name_comment <$address>";
Joe Perches20112472011-07-25 17:13:23 -07001299 }
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001300 $formatted_email .= "$comment";
Joe Perches20112472011-07-25 17:13:23 -07001301 return $formatted_email;
1302}
1303
Joe Perchesdfa05c22020-04-06 20:10:48 -07001304sub reformat_email {
1305 my ($email) = @_;
1306
1307 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001308 return format_email($email_name, $name_comment, $email_address, $comment);
Joe Perchesdfa05c22020-04-06 20:10:48 -07001309}
1310
1311sub same_email_addresses {
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001312 my ($email1, $email2) = @_;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001313
1314 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1315 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1316
1317 return $email1_name eq $email2_name &&
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001318 $email1_address eq $email2_address &&
1319 $name1_comment eq $name2_comment &&
1320 $comment1 eq $comment2;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001321}
1322
Joe Perchesd311cd42014-08-06 16:10:57 -07001323sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001324 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001325
Joe Perchesbd474ca2014-08-06 16:11:10 -07001326 foreach my $path (split(/:/, $ENV{PATH})) {
1327 if (-e "$path/$bin") {
1328 return "$path/$bin";
1329 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001330 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001331
Joe Perchesbd474ca2014-08-06 16:11:10 -07001332 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001333}
1334
Joe Perches000d1cc12011-07-25 17:13:25 -07001335sub which_conf {
1336 my ($conf) = @_;
1337
1338 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1339 if (-e "$path/$conf") {
1340 return "$path/$conf";
1341 }
1342 }
1343
1344 return "";
1345}
1346
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001347sub expand_tabs {
1348 my ($str) = @_;
1349
1350 my $res = '';
1351 my $n = 0;
1352 for my $c (split(//, $str)) {
1353 if ($c eq "\t") {
1354 $res .= ' ';
1355 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001356 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001357 $res .= ' ';
1358 }
1359 next;
1360 }
1361 $res .= $c;
1362 $n++;
1363 }
1364
1365 return $res;
1366}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001367sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001368 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001369 return $res;
1370}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001371
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001372sub line_stats {
1373 my ($line) = @_;
1374
1375 # Drop the diff line leader and expand tabs
1376 $line =~ s/^.//;
1377 $line = expand_tabs($line);
1378
1379 # Pick the indent from the front of the line.
1380 my ($white) = ($line =~ /^(\s*)/);
1381
1382 return (length($line), length($white));
1383}
1384
Andy Whitcroft773647a2008-03-28 14:15:58 -07001385my $sanitise_quote = '';
1386
1387sub sanitise_line_reset {
1388 my ($in_comment) = @_;
1389
1390 if ($in_comment) {
1391 $sanitise_quote = '*/';
1392 } else {
1393 $sanitise_quote = '';
1394 }
1395}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001396sub sanitise_line {
1397 my ($line) = @_;
1398
1399 my $res = '';
1400 my $l = '';
1401
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001402 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001403 my $off = 0;
1404 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001405
Andy Whitcroft773647a2008-03-28 14:15:58 -07001406 # Always copy over the diff marker.
1407 $res = substr($line, 0, 1);
1408
1409 for ($off = 1; $off < length($line); $off++) {
1410 $c = substr($line, $off, 1);
1411
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001412 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001413 # and end, all to $;.
1414 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1415 $sanitise_quote = '*/';
1416
1417 substr($res, $off, 2, "$;$;");
1418 $off++;
1419 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001420 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001421 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001422 $sanitise_quote = '';
1423 substr($res, $off, 2, "$;$;");
1424 $off++;
1425 next;
1426 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001427 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1428 $sanitise_quote = '//';
1429
1430 substr($res, $off, 2, $sanitise_quote);
1431 $off++;
1432 next;
1433 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001434
1435 # A \ in a string means ignore the next character.
1436 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1437 $c eq "\\") {
1438 substr($res, $off, 2, 'XX');
1439 $off++;
1440 next;
1441 }
1442 # Regular quotes.
1443 if ($c eq "'" || $c eq '"') {
1444 if ($sanitise_quote eq '') {
1445 $sanitise_quote = $c;
1446
1447 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001448 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001449 } elsif ($sanitise_quote eq $c) {
1450 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001451 }
1452 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001453
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001454 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001455 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1456 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001457 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1458 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001459 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1460 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001461 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001462 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001463 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001464 }
1465
Daniel Walker113f04a2009-09-21 17:04:35 -07001466 if ($sanitise_quote eq '//') {
1467 $sanitise_quote = '';
1468 }
1469
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001470 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001471 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001472 my $clean = 'X' x length($1);
1473 $res =~ s@\<.*\>@<$clean>@;
1474
1475 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001476 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001477 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001478 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001479 }
1480
Joe Perchesdadf6802016-08-02 14:04:33 -07001481 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1482 my $match = $1;
1483 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1484 }
1485
Andy Whitcroft00df3442007-06-08 13:47:06 -07001486 return $res;
1487}
1488
Joe Perchesa6962d72013-04-29 16:18:13 -07001489sub get_quoted_string {
1490 my ($line, $rawline) = @_;
1491
Joe Perches478b1792018-04-10 16:33:34 -07001492 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001493 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001494 return substr($rawline, $-[0], $+[0] - $-[0]);
1495}
1496
Andy Whitcroft8905a672007-11-28 16:21:06 -08001497sub ctx_statement_block {
1498 my ($linenr, $remain, $off) = @_;
1499 my $line = $linenr - 1;
1500 my $blk = '';
1501 my $soff = $off;
1502 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001503 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001504
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001505 my $loff = 0;
1506
Andy Whitcroft8905a672007-11-28 16:21:06 -08001507 my $type = '';
1508 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001509 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001510 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001511 my $c;
1512 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001513
1514 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001515 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001516 @stack = (['', 0]) if ($#stack == -1);
1517
Andy Whitcroft773647a2008-03-28 14:15:58 -07001518 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001519 # If we are about to drop off the end, pull in more
1520 # context.
1521 if ($off >= $len) {
1522 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001523 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001524 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001525 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001526 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001527 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001528 $len = length($blk);
1529 $line++;
1530 last;
1531 }
1532 # Bail if there is no further context.
1533 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001534 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001535 last;
1536 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001537 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1538 $level++;
1539 $type = '#';
1540 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001541 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001542 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001543 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001544 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001545
Andy Whitcroft773647a2008-03-28 14:15:58 -07001546 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001547
1548 # Handle nested #if/#else.
1549 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1550 push(@stack, [ $type, $level ]);
1551 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1552 ($type, $level) = @{$stack[$#stack - 1]};
1553 } elsif ($remainder =~ /^#\s*endif\b/) {
1554 ($type, $level) = @{pop(@stack)};
1555 }
1556
Andy Whitcroft8905a672007-11-28 16:21:06 -08001557 # Statement ends at the ';' or a close '}' at the
1558 # outermost level.
1559 if ($level == 0 && $c eq ';') {
1560 last;
1561 }
1562
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001563 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001564 if ($level == 0 && $coff_set == 0 &&
1565 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1566 $remainder =~ /^(else)(?:\s|{)/ &&
1567 $remainder !~ /^else\s+if\b/) {
1568 $coff = $off + length($1) - 1;
1569 $coff_set = 1;
1570 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1571 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001572 }
1573
Andy Whitcroft8905a672007-11-28 16:21:06 -08001574 if (($type eq '' || $type eq '(') && $c eq '(') {
1575 $level++;
1576 $type = '(';
1577 }
1578 if ($type eq '(' && $c eq ')') {
1579 $level--;
1580 $type = ($level != 0)? '(' : '';
1581
1582 if ($level == 0 && $coff < $soff) {
1583 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001584 $coff_set = 1;
1585 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001586 }
1587 }
1588 if (($type eq '' || $type eq '{') && $c eq '{') {
1589 $level++;
1590 $type = '{';
1591 }
1592 if ($type eq '{' && $c eq '}') {
1593 $level--;
1594 $type = ($level != 0)? '{' : '';
1595
1596 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001597 if (substr($blk, $off + 1, 1) eq ';') {
1598 $off++;
1599 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001600 last;
1601 }
1602 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001603 # Preprocessor commands end at the newline unless escaped.
1604 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1605 $level--;
1606 $type = '';
1607 $off++;
1608 last;
1609 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001610 $off++;
1611 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001612 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001613 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001614 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001615 $line++;
1616 $remain--;
1617 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001618
1619 my $statement = substr($blk, $soff, $off - $soff + 1);
1620 my $condition = substr($blk, $soff, $coff - $soff + 1);
1621
1622 #warn "STATEMENT<$statement>\n";
1623 #warn "CONDITION<$condition>\n";
1624
Andy Whitcroft773647a2008-03-28 14:15:58 -07001625 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001626
1627 return ($statement, $condition,
1628 $line, $remain + 1, $off - $loff + 1, $level);
1629}
1630
Andy Whitcroftcf655042008-03-04 14:28:20 -08001631sub statement_lines {
1632 my ($stmt) = @_;
1633
1634 # Strip the diff line prefixes and rip blank lines at start and end.
1635 $stmt =~ s/(^|\n)./$1/g;
1636 $stmt =~ s/^\s*//;
1637 $stmt =~ s/\s*$//;
1638
1639 my @stmt_lines = ($stmt =~ /\n/g);
1640
1641 return $#stmt_lines + 2;
1642}
1643
1644sub statement_rawlines {
1645 my ($stmt) = @_;
1646
1647 my @stmt_lines = ($stmt =~ /\n/g);
1648
1649 return $#stmt_lines + 2;
1650}
1651
1652sub statement_block_size {
1653 my ($stmt) = @_;
1654
1655 $stmt =~ s/(^|\n)./$1/g;
1656 $stmt =~ s/^\s*{//;
1657 $stmt =~ s/}\s*$//;
1658 $stmt =~ s/^\s*//;
1659 $stmt =~ s/\s*$//;
1660
1661 my @stmt_lines = ($stmt =~ /\n/g);
1662 my @stmt_statements = ($stmt =~ /;/g);
1663
1664 my $stmt_lines = $#stmt_lines + 2;
1665 my $stmt_statements = $#stmt_statements + 1;
1666
1667 if ($stmt_lines > $stmt_statements) {
1668 return $stmt_lines;
1669 } else {
1670 return $stmt_statements;
1671 }
1672}
1673
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001674sub ctx_statement_full {
1675 my ($linenr, $remain, $off) = @_;
1676 my ($statement, $condition, $level);
1677
1678 my (@chunks);
1679
Andy Whitcroftcf655042008-03-04 14:28:20 -08001680 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001681 ($statement, $condition, $linenr, $remain, $off, $level) =
1682 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001683 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001684 push(@chunks, [ $condition, $statement ]);
1685 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1686 return ($level, $linenr, @chunks);
1687 }
1688
1689 # Pull in the following conditional/block pairs and see if they
1690 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001691 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001692 ($statement, $condition, $linenr, $remain, $off, $level) =
1693 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001694 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001695 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001696 #print "C: push\n";
1697 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001698 }
1699
1700 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001701}
1702
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001703sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001704 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001705 my $line;
1706 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001707 my $blk = '';
1708 my @o;
1709 my @c;
1710 my @res = ();
1711
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001712 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001713 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001714 for ($line = $start; $remain > 0; $line++) {
1715 next if ($rawlines[$line] =~ /^-/);
1716 $remain--;
1717
1718 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001719
1720 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001721 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001722 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001723 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001724 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001725 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001726 $level = pop(@stack);
1727 }
1728
Andy Whitcroft01464f32010-10-26 14:23:19 -07001729 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001730 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1731 if ($off > 0) {
1732 $off--;
1733 next;
1734 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001735
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001736 if ($c eq $close && $level > 0) {
1737 $level--;
1738 last if ($level == 0);
1739 } elsif ($c eq $open) {
1740 $level++;
1741 }
1742 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001743
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001744 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001745 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001746 }
1747
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001748 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001749 }
1750
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001751 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001752}
1753sub ctx_block_outer {
1754 my ($linenr, $remain) = @_;
1755
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001756 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1757 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001758}
1759sub ctx_block {
1760 my ($linenr, $remain) = @_;
1761
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001762 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1763 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001764}
1765sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001766 my ($linenr, $remain, $off) = @_;
1767
1768 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1769 return @r;
1770}
1771sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001772 my ($linenr, $remain) = @_;
1773
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001774 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001775}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001776sub ctx_statement_level {
1777 my ($linenr, $remain, $off) = @_;
1778
1779 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1780}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001781
1782sub ctx_locate_comment {
1783 my ($first_line, $end_line) = @_;
1784
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001785 # If c99 comment on the current line, or the line before or after
1786 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1787 return $current_comment if (defined $current_comment);
1788 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1789 return $current_comment if (defined $current_comment);
1790 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1791 return $current_comment if (defined $current_comment);
1792
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001793 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001794 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001795 return $current_comment if (defined $current_comment);
1796
1797 # Look through the context and try and figure out if there is a
1798 # comment.
1799 my $in_comment = 0;
1800 $current_comment = '';
1801 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001802 my $line = $rawlines[$linenr - 1];
1803 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001804 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1805 $in_comment = 1;
1806 }
1807 if ($line =~ m@/\*@) {
1808 $in_comment = 1;
1809 }
1810 if (!$in_comment && $current_comment ne '') {
1811 $current_comment = '';
1812 }
1813 $current_comment .= $line . "\n" if ($in_comment);
1814 if ($line =~ m@\*/@) {
1815 $in_comment = 0;
1816 }
1817 }
1818
1819 chomp($current_comment);
1820 return($current_comment);
1821}
1822sub ctx_has_comment {
1823 my ($first_line, $end_line) = @_;
1824 my $cmt = ctx_locate_comment($first_line, $end_line);
1825
Andy Whitcroft00df3442007-06-08 13:47:06 -07001826 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001827 ##print "CMMT: $cmt\n";
1828
1829 return ($cmt ne '');
1830}
1831
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001832sub raw_line {
1833 my ($linenr, $cnt) = @_;
1834
1835 my $offset = $linenr - 1;
1836 $cnt++;
1837
1838 my $line;
1839 while ($cnt) {
1840 $line = $rawlines[$offset++];
1841 next if (defined($line) && $line =~ /^-/);
1842 $cnt--;
1843 }
1844
1845 return $line;
1846}
1847
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001848sub get_stat_real {
1849 my ($linenr, $lc) = @_;
1850
1851 my $stat_real = raw_line($linenr, 0);
1852 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1853 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1854 }
1855
1856 return $stat_real;
1857}
1858
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001859sub get_stat_here {
1860 my ($linenr, $cnt, $here) = @_;
1861
1862 my $herectx = $here . "\n";
1863 for (my $n = 0; $n < $cnt; $n++) {
1864 $herectx .= raw_line($linenr, $n) . "\n";
1865 }
1866
1867 return $herectx;
1868}
1869
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001870sub cat_vet {
1871 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001872 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001873
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001874 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001875 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1876 $res .= $1;
1877 if ($2 ne '') {
1878 $coded = sprintf("^%c", unpack('C', $2) + 64);
1879 $res .= $coded;
1880 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001881 }
1882 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001883
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001884 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001885}
1886
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001887my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001888my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001889my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001890my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001891
1892sub annotate_reset {
1893 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001894 $av_pending = '_';
1895 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001896 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001897}
1898
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001899sub annotate_values {
1900 my ($stream, $type) = @_;
1901
1902 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001903 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001904 my $cur = $stream;
1905
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001906 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001907
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001908 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001909 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001910 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001911 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001912 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001913 print "WS($1)\n" if ($dbg_values > 1);
1914 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001915 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001916 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001917 }
1918
Florian Micklerc023e4732011-01-12 16:59:58 -08001919 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001920 print "CAST($1)\n" if ($dbg_values > 1);
1921 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001922 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001923
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001924 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001925 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001926 $type = 'T';
1927
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001928 } elsif ($cur =~ /^($Modifier)\s*/) {
1929 print "MODIFIER($1)\n" if ($dbg_values > 1);
1930 $type = 'T';
1931
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001932 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001933 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001934 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001935 push(@av_paren_type, $type);
1936 if ($2 ne '') {
1937 $av_pending = 'N';
1938 }
1939 $type = 'E';
1940
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001941 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001942 print "UNDEF($1)\n" if ($dbg_values > 1);
1943 $av_preprocessor = 1;
1944 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001945
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001946 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001947 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001948 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001949
1950 push(@av_paren_type, $type);
1951 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001952 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001953
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001954 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001955 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1956 $av_preprocessor = 1;
1957
1958 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1959
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001960 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001961
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001962 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001963 print "PRE_END($1)\n" if ($dbg_values > 1);
1964
1965 $av_preprocessor = 1;
1966
1967 # Assume all arms of the conditional end as this
1968 # one does, and continue as if the #endif was not here.
1969 pop(@av_paren_type);
1970 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001971 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001972
1973 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001974 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001975
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001976 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1977 print "ATTR($1)\n" if ($dbg_values > 1);
1978 $av_pending = $type;
1979 $type = 'N';
1980
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001981 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001982 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001983 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001984 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001985 }
1986 $type = 'N';
1987
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001988 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001989 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001990 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001991 $type = 'N';
1992
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001993 } elsif ($cur =~/^(case)/o) {
1994 print "CASE($1)\n" if ($dbg_values > 1);
1995 $av_pend_colon = 'C';
1996 $type = 'N';
1997
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001998 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001999 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002000 $type = 'N';
2001
2002 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002003 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002004 push(@av_paren_type, $av_pending);
2005 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002006 $type = 'N';
2007
2008 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002009 my $new_type = pop(@av_paren_type);
2010 if ($new_type ne '_') {
2011 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002012 print "PAREN('$1') -> $type\n"
2013 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002014 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002015 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002016 }
2017
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07002018 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002019 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07002020 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08002021 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002022
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002023 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
2024 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002025 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002026 } elsif ($type eq 'E') {
2027 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002028 }
2029 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
2030 $type = 'V';
2031
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002032 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002033 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002034 $type = 'V';
2035
2036 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002037 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002038 $type = 'N';
2039
Andy Whitcroftcf655042008-03-04 14:28:20 -08002040 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002041 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002042 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002043 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002044
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002045 } elsif ($cur =~/^(,)/) {
2046 print "COMMA($1)\n" if ($dbg_values > 1);
2047 $type = 'C';
2048
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002049 } elsif ($cur =~ /^(\?)/o) {
2050 print "QUESTION($1)\n" if ($dbg_values > 1);
2051 $type = 'N';
2052
2053 } elsif ($cur =~ /^(:)/o) {
2054 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
2055
2056 substr($var, length($res), 1, $av_pend_colon);
2057 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
2058 $type = 'E';
2059 } else {
2060 $type = 'N';
2061 }
2062 $av_pend_colon = 'O';
2063
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002064 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002065 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002066 $type = 'N';
2067
Andy Whitcroft0d413862008-10-15 22:02:16 -07002068 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002069 my $variant;
2070
2071 print "OPV($1)\n" if ($dbg_values > 1);
2072 if ($type eq 'V') {
2073 $variant = 'B';
2074 } else {
2075 $variant = 'U';
2076 }
2077
2078 substr($var, length($res), 1, $variant);
2079 $type = 'N';
2080
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002081 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002082 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002083 if ($1 ne '++' && $1 ne '--') {
2084 $type = 'N';
2085 }
2086
2087 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002088 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002089 }
2090 if (defined $1) {
2091 $cur = substr($cur, length($1));
2092 $res .= $type x length($1);
2093 }
2094 }
2095
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002096 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002097}
2098
Andy Whitcroft8905a672007-11-28 16:21:06 -08002099sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002100 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002101 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002102 ^(?:
2103 $Modifier|
2104 $Storage|
2105 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002106 DEFINE_\S+
2107 )$|
2108 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002109 goto|
2110 return|
2111 case|
2112 else|
2113 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002114 do|
2115 \#|
2116 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002117 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002118 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002119 )}x;
2120 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2121 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002122 # Check for modifiers.
2123 $possible =~ s/\s*$Storage\s*//g;
2124 $possible =~ s/\s*$Sparse\s*//g;
2125 if ($possible =~ /^\s*$/) {
2126
2127 } elsif ($possible =~ /\s/) {
2128 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002129 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002130 if ($modifier !~ $notPermitted) {
2131 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002132 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002133 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002134 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002135
2136 } else {
2137 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002138 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002139 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002140 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002141 } else {
2142 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002143 }
2144}
2145
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002146my $prefix = '';
2147
Joe Perches000d1cc12011-07-25 17:13:25 -07002148sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002149 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002150
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002151 $type =~ tr/[a-z]/[A-Z]/;
2152
Joe Perchescbec18a2014-04-03 14:49:19 -07002153 return defined $use_type{$type} if (scalar keys %use_type > 0);
2154
2155 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002156}
2157
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002158sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002159 my ($level, $type, $msg) = @_;
2160
2161 if (!show_type($type) ||
2162 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002163 return 0;
2164 }
Joe Perches57230292015-06-25 15:03:03 -07002165 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002166 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002167 if ($level eq 'ERROR') {
2168 $output .= RED;
2169 } elsif ($level eq 'WARNING') {
2170 $output .= YELLOW;
2171 } else {
2172 $output .= GREEN;
2173 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002174 }
Joe Perches57230292015-06-25 15:03:03 -07002175 $output .= $prefix . $level . ':';
2176 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002177 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002178 $output .= "$type:";
2179 }
John Brooks737c0762017-07-10 15:52:24 -07002180 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002181 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002182
2183 if ($showfile) {
2184 my @lines = split("\n", $output, -1);
2185 splice(@lines, 1, 1);
2186 $output = join("\n", @lines);
2187 }
Joe Perches57230292015-06-25 15:03:03 -07002188 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002189
Joe Perches57230292015-06-25 15:03:03 -07002190 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002191
2192 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002193}
Joe Perchescbec18a2014-04-03 14:49:19 -07002194
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002195sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002196 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002197}
Joe Perches000d1cc12011-07-25 17:13:25 -07002198
Joe Perchesd752fcc2014-08-06 16:11:05 -07002199sub fixup_current_range {
2200 my ($lineRef, $offset, $length) = @_;
2201
2202 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2203 my $o = $1;
2204 my $l = $2;
2205 my $no = $o + $offset;
2206 my $nl = $l + $length;
2207 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2208 }
2209}
2210
2211sub fix_inserted_deleted_lines {
2212 my ($linesRef, $insertedRef, $deletedRef) = @_;
2213
2214 my $range_last_linenr = 0;
2215 my $delta_offset = 0;
2216
2217 my $old_linenr = 0;
2218 my $new_linenr = 0;
2219
2220 my $next_insert = 0;
2221 my $next_delete = 0;
2222
2223 my @lines = ();
2224
2225 my $inserted = @{$insertedRef}[$next_insert++];
2226 my $deleted = @{$deletedRef}[$next_delete++];
2227
2228 foreach my $old_line (@{$linesRef}) {
2229 my $save_line = 1;
2230 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002231 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002232 $delta_offset = 0;
2233 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2234 $range_last_linenr = $new_linenr;
2235 fixup_current_range(\$line, $delta_offset, 0);
2236 }
2237
2238 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2239 $deleted = @{$deletedRef}[$next_delete++];
2240 $save_line = 0;
2241 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2242 }
2243
2244 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2245 push(@lines, ${$inserted}{'LINE'});
2246 $inserted = @{$insertedRef}[$next_insert++];
2247 $new_linenr++;
2248 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2249 }
2250
2251 if ($save_line) {
2252 push(@lines, $line);
2253 $new_linenr++;
2254 }
2255
2256 $old_linenr++;
2257 }
2258
2259 return @lines;
2260}
2261
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002262sub fix_insert_line {
2263 my ($linenr, $line) = @_;
2264
2265 my $inserted = {
2266 LINENR => $linenr,
2267 LINE => $line,
2268 };
2269 push(@fixed_inserted, $inserted);
2270}
2271
2272sub fix_delete_line {
2273 my ($linenr, $line) = @_;
2274
2275 my $deleted = {
2276 LINENR => $linenr,
2277 LINE => $line,
2278 };
2279
2280 push(@fixed_deleted, $deleted);
2281}
2282
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002283sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002284 my ($type, $msg) = @_;
2285
2286 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002287 our $clean = 0;
2288 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002289 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002290 }
Joe Perches3705ce52013-07-03 15:05:31 -07002291 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002292}
2293sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002294 my ($type, $msg) = @_;
2295
2296 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002297 our $clean = 0;
2298 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002299 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002300 }
Joe Perches3705ce52013-07-03 15:05:31 -07002301 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002302}
2303sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002304 my ($type, $msg) = @_;
2305
2306 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002307 our $clean = 0;
2308 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002309 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002310 }
Joe Perches3705ce52013-07-03 15:05:31 -07002311 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002312}
2313
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002314sub check_absolute_file {
2315 my ($absolute, $herecurr) = @_;
2316 my $file = $absolute;
2317
2318 ##print "absolute<$absolute>\n";
2319
2320 # See if any suffix of this path is a path within the tree.
2321 while ($file =~ s@^[^/]*/@@) {
2322 if (-f "$root/$file") {
2323 ##print "file<$file>\n";
2324 last;
2325 }
2326 }
2327 if (! -f _) {
2328 return 0;
2329 }
2330
2331 # It is, so see if the prefix is acceptable.
2332 my $prefix = $absolute;
2333 substr($prefix, -length($file)) = '';
2334
2335 ##print "prefix<$prefix>\n";
2336 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002337 WARN("USE_RELATIVE_PATH",
2338 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002339 }
2340}
2341
Joe Perches3705ce52013-07-03 15:05:31 -07002342sub trim {
2343 my ($string) = @_;
2344
Joe Perchesb34c6482013-09-11 14:24:01 -07002345 $string =~ s/^\s+|\s+$//g;
2346
2347 return $string;
2348}
2349
2350sub ltrim {
2351 my ($string) = @_;
2352
2353 $string =~ s/^\s+//;
2354
2355 return $string;
2356}
2357
2358sub rtrim {
2359 my ($string) = @_;
2360
2361 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002362
2363 return $string;
2364}
2365
Joe Perches52ea8502013-11-12 15:10:09 -08002366sub string_find_replace {
2367 my ($string, $find, $replace) = @_;
2368
2369 $string =~ s/$find/$replace/g;
2370
2371 return $string;
2372}
2373
Joe Perches3705ce52013-07-03 15:05:31 -07002374sub tabify {
2375 my ($leading) = @_;
2376
Antonio Borneo713a09d2020-04-06 20:11:07 -07002377 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002378 my $max_spaces_before_tab = $source_indent - 1;
2379 my $spaces_to_tab = " " x $source_indent;
2380
2381 #convert leading spaces to tabs
2382 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2383 #Remove spaces before a tab
2384 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2385
2386 return "$leading";
2387}
2388
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002389sub pos_last_openparen {
2390 my ($line) = @_;
2391
2392 my $pos = 0;
2393
2394 my $opens = $line =~ tr/\(/\(/;
2395 my $closes = $line =~ tr/\)/\)/;
2396
2397 my $last_openparen = 0;
2398
2399 if (($opens == 0) || ($closes >= $opens)) {
2400 return -1;
2401 }
2402
2403 my $len = length($line);
2404
2405 for ($pos = 0; $pos < $len; $pos++) {
2406 my $string = substr($line, $pos);
2407 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2408 $pos += length($1) - 1;
2409 } elsif (substr($line, $pos, 1) eq '(') {
2410 $last_openparen = $pos;
2411 } elsif (index($string, '(') == -1) {
2412 last;
2413 }
2414 }
2415
Joe Perches91cb5192014-04-03 14:49:32 -07002416 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002417}
2418
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002419sub get_raw_comment {
2420 my ($line, $rawline) = @_;
2421 my $comment = '';
2422
2423 for my $i (0 .. (length($line) - 1)) {
2424 if (substr($line, $i, 1) eq "$;") {
2425 $comment .= substr($rawline, $i, 1);
2426 }
2427 }
2428
2429 return $comment;
2430}
2431
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002432sub process {
2433 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002434
2435 my $linenr=0;
2436 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002437 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002438 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002439 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002440
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002441 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002442 my $indent;
2443 my $previndent=0;
2444 my $stashindent=0;
2445
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002446 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002447 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002448 my $author = '';
2449 my $authorsignoff = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002450 my $author_sob = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002451 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002452 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002453 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002454 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002455 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002456 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002457 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002458 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002459 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002460 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002461 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002462 my $non_utf8_charset = 0;
2463
Joe Perches365dd4e2014-08-06 16:10:42 -07002464 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002465 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002466
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002467 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002468 our $cnt_lines = 0;
2469 our $cnt_error = 0;
2470 our $cnt_warn = 0;
2471 our $cnt_chk = 0;
2472
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002473 # Trace the real file/line as we go.
2474 my $realfile = '';
2475 my $realline = 0;
2476 my $realcnt = 0;
2477 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002478 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002479 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002480 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002481 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002482 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002483
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002484 my $prev_values = 'E';
2485
2486 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002487 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002488 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002489 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002490 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002491
Joe Perches7e51f192013-09-11 14:23:57 -07002492 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002493
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002494 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002495 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002496 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002497 my @setup_docs = ();
2498 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002499
Joe Perchesd8b07712013-11-12 15:10:06 -08002500 my $camelcase_file_seeded = 0;
2501
Rob Herring9f3a8992018-04-10 16:33:13 -07002502 my $checklicenseline = 1;
2503
Andy Whitcroft773647a2008-03-28 14:15:58 -07002504 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002505 my $line;
2506 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002507 $linenr++;
2508 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002509
Joe Perches3705ce52013-07-03 15:05:31 -07002510 push(@fixed, $rawline) if ($fix);
2511
Andy Whitcroft773647a2008-03-28 14:15:58 -07002512 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002513 $setup_docs = 0;
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07002514 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002515 $setup_docs = 1;
2516 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002517 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002518 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002519 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002520 $realline=$1-1;
2521 if (defined $2) {
2522 $realcnt=$3+1;
2523 } else {
2524 $realcnt=1+1;
2525 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002526 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002527
2528 # Guestimate if this is a continuing comment. Run
2529 # the context looking for a comment "edge". If this
2530 # edge is a close comment then we must be in a comment
2531 # at context start.
2532 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002533 my $cnt = $realcnt;
2534 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2535 next if (defined $rawlines[$ln - 1] &&
2536 $rawlines[$ln - 1] =~ /^-/);
2537 $cnt--;
2538 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002539 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002540 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2541 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2542 ($edge) = $1;
2543 last;
2544 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002545 }
2546 if (defined $edge && $edge eq '*/') {
2547 $in_comment = 1;
2548 }
2549
2550 # Guestimate if this is a continuing comment. If this
2551 # is the start of a diff block and this line starts
2552 # ' *' then it is very likely a comment.
2553 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002554 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002555 {
2556 $in_comment = 1;
2557 }
2558
2559 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2560 sanitise_line_reset($in_comment);
2561
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002562 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002563 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002564 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002565 $line = sanitise_line($rawline);
2566 }
2567 push(@lines, $line);
2568
2569 if ($realcnt > 1) {
2570 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2571 } else {
2572 $realcnt = 0;
2573 }
2574
2575 #print "==>$rawline\n";
2576 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002577
2578 if ($setup_docs && $line =~ /^\+/) {
2579 push(@setup_docs, $line);
2580 }
2581 }
2582
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002583 $prefix = '';
2584
Andy Whitcroft773647a2008-03-28 14:15:58 -07002585 $realcnt = 0;
2586 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002587 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002588 foreach my $line (@lines) {
2589 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002590 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002591 my $sline = $line; #copy of $line
2592 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002593
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002594 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002595 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002596
Joe Perches12c253a2018-06-07 17:10:58 -07002597# check if it's a mode change, rename or start of a patch
2598 if (!$in_commit_log &&
2599 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2600 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2601 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2602 $is_patch = 1;
2603 }
2604
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002605#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002606 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002607 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2608 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002609 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002610 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002611 $realline=$1-1;
2612 if (defined $2) {
2613 $realcnt=$3+1;
2614 } else {
2615 $realcnt=1+1;
2616 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002617 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002618 $prev_values = 'E';
2619
Andy Whitcroft773647a2008-03-28 14:15:58 -07002620 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002621 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002622 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002623 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002624 if ($context =~ /\b(\w+)\s*\(/) {
2625 $context_function = $1;
2626 } else {
2627 undef $context_function;
2628 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002629 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002630
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002631# track the line number as we move through the hunk, note that
2632# new versions of GNU diff omit the leading space on completely
2633# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002634 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002635 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002636 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002637
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002638 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002639 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002640
2641 # Track the previous line.
2642 ($prevline, $stashline) = ($stashline, $line);
2643 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002644 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2645
Andy Whitcroft773647a2008-03-28 14:15:58 -07002646 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002647
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002648 } elsif ($realcnt == 1) {
2649 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002650 }
2651
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002652 my $hunk_line = ($realcnt != 0);
2653
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002654 $here = "#$linenr: " if (!$file);
2655 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002656
Joe Perches2ac73b42014-06-04 16:12:05 -07002657 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002658 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002659 if ($line =~ /^diff --git.*?(\S+)$/) {
2660 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002661 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002662 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002663 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002664 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002665 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002666 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002667 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002668
2669 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002670 if (!$file && $tree && $p1_prefix ne '' &&
2671 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002672 WARN("PATCH_PREFIX",
2673 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002674 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002675
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002676 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002677 ERROR("MODIFIED_INCLUDE_ASM",
2678 "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 -07002679 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002680 $found_file = 1;
2681 }
2682
Joe Perches34d88152015-06-25 15:03:05 -07002683#make up the handle for any error we report on this line
2684 if ($showfile) {
2685 $prefix = "$realfile:$realline: "
2686 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002687 if ($file) {
2688 $prefix = "$filename:$realline: ";
2689 } else {
2690 $prefix = "$filename:$linenr: ";
2691 }
Joe Perches34d88152015-06-25 15:03:05 -07002692 }
2693
Joe Perches2ac73b42014-06-04 16:12:05 -07002694 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002695 if (is_maintained_obsolete($realfile)) {
2696 WARN("OBSOLETE",
2697 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2698 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002699 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002700 $check = 1;
2701 } else {
2702 $check = $check_orig;
2703 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002704 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002705
2706 if ($realfile !~ /^MAINTAINERS/) {
2707 my $last_binding_patch = $is_binding_patch;
2708
2709 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2710
2711 if (($last_binding_patch != -1) &&
2712 ($last_binding_patch ^ $is_binding_patch)) {
2713 WARN("DT_SPLIT_BINDING_PATCH",
Mauro Carvalho Chehab858e6842020-04-15 16:45:25 +02002714 "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 -07002715 }
2716 }
2717
Andy Whitcroft773647a2008-03-28 14:15:58 -07002718 next;
2719 }
2720
Randy Dunlap389834b2007-06-08 13:47:03 -07002721 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002722
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002723 my $hereline = "$here\n$rawline\n";
2724 my $herecurr = "$here\n$rawline\n";
2725 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002726
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002727 $cnt_lines++ if ($realcnt != 0);
2728
Joe Perches490b2922018-08-21 21:58:01 -07002729# Verify the existence of a commit log if appropriate
2730# 2 is used because a $signature is counted in $commit_log_lines
2731 if ($in_commit_log) {
2732 if ($line !~ /^\s*$/) {
2733 $commit_log_lines++; #could be a $signature
2734 }
2735 } elsif ($has_commit_log && $commit_log_lines < 2) {
2736 WARN("COMMIT_MESSAGE",
2737 "Missing commit description - Add an appropriate one\n");
2738 $commit_log_lines = 2; #warn only once
2739 }
2740
Joe Perchese518e9a2015-06-25 15:03:27 -07002741# Check if the commit log has what seems like a diff which can confuse patch
2742 if ($in_commit_log && !$commit_log_has_diff &&
Mrinal Pandey13e45412020-09-04 16:35:52 -07002743 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2744 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
Joe Perchese518e9a2015-06-25 15:03:27 -07002745 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2746 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2747 ERROR("DIFF_IN_COMMIT_MSG",
2748 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2749 $commit_log_has_diff = 1;
2750 }
2751
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002752# Check for incorrect file permissions
2753 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2754 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002755 if ($realfile !~ m@scripts/@ &&
2756 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002757 ERROR("EXECUTE_PERMISSIONS",
2758 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002759 }
2760 }
2761
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002762# Check the patch for a From:
2763 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2764 $author = $1;
Dwaipayan Raye7f929f2020-10-15 20:12:15 -07002765 my $curline = $linenr;
2766 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2767 $author .= $1;
2768 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002769 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2770 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002771 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002772 }
2773
Joe Perches20112472011-07-25 17:13:23 -07002774# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002775 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002776 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002777 $in_commit_log = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002778 if ($author ne '' && $authorsignoff != 1) {
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002779 if (same_email_addresses($1, $author)) {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002780 $authorsignoff = 1;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002781 } else {
2782 my $ctx = $1;
2783 my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
2784 my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
2785
2786 if ($email_address eq $author_address && $email_name eq $author_name) {
2787 $author_sob = $ctx;
2788 $authorsignoff = 2;
2789 } elsif ($email_address eq $author_address) {
2790 $author_sob = $ctx;
2791 $authorsignoff = 3;
2792 } elsif ($email_name eq $author_name) {
2793 $author_sob = $ctx;
2794 $authorsignoff = 4;
2795
2796 my $address1 = $email_address;
2797 my $address2 = $author_address;
2798
2799 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
2800 $address1 = "$1$2";
2801 }
2802 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
2803 $address2 = "$1$2";
2804 }
2805 if ($address1 eq $address2) {
2806 $authorsignoff = 5;
2807 }
2808 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002809 }
2810 }
Joe Perches20112472011-07-25 17:13:23 -07002811 }
2812
Joe Perches44d303e2020-04-06 20:11:10 -07002813# Check for patch separator
2814 if ($line =~ /^---$/) {
2815 $has_patch_separator = 1;
2816 $in_commit_log = 0;
2817 }
2818
Joe Perchese0d975b2014-12-10 15:51:49 -08002819# Check if MAINTAINERS is being updated. If so, there's probably no need to
2820# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2821 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2822 $reported_maintainer_file = 1;
2823 }
2824
Joe Perches20112472011-07-25 17:13:23 -07002825# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002826 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002827 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002828 my $space_before = $1;
2829 my $sign_off = $2;
2830 my $space_after = $3;
2831 my $email = $4;
2832 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2833
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002834 if ($sign_off !~ /$signature_tags/) {
Aditya Srivastava831242a2020-12-15 20:45:12 -08002835 my $suggested_signature = find_standard_signature($sign_off);
2836 if ($suggested_signature eq "") {
2837 WARN("BAD_SIGN_OFF",
2838 "Non-standard signature: $sign_off\n" . $herecurr);
2839 } else {
2840 if (WARN("BAD_SIGN_OFF",
2841 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
2842 $fix) {
2843 $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
2844 }
2845 }
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002846 }
Joe Perches20112472011-07-25 17:13:23 -07002847 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002848 if (WARN("BAD_SIGN_OFF",
2849 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2850 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002851 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002852 "$ucfirst_sign_off $email";
2853 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002854 }
Joe Perches20112472011-07-25 17:13:23 -07002855 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002856 if (WARN("BAD_SIGN_OFF",
2857 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2858 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002859 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002860 "$ucfirst_sign_off $email";
2861 }
2862
Joe Perches20112472011-07-25 17:13:23 -07002863 }
2864 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002865 if (WARN("BAD_SIGN_OFF",
2866 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2867 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002868 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002869 "$ucfirst_sign_off $email";
2870 }
Joe Perches20112472011-07-25 17:13:23 -07002871 }
2872
Joe Perchesdfa05c22020-04-06 20:10:48 -07002873 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002874 my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
Joe Perches20112472011-07-25 17:13:23 -07002875 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002876 ERROR("BAD_SIGN_OFF",
2877 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002878 } else {
2879 my $dequoted = $suggested_email;
2880 $dequoted =~ s/^"//;
2881 $dequoted =~ s/" </ </;
2882 # Don't force email to have quotes
2883 # Allow just an angle bracketed address
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002884 if (!same_email_addresses($email, $suggested_email)) {
2885 if (WARN("BAD_SIGN_OFF",
2886 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
2887 $fix) {
2888 $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
2889 }
2890 }
2891
2892 # Address part shouldn't have comments
2893 my $stripped_address = $email_address;
2894 $stripped_address =~ s/\([^\(\)]*\)//g;
2895 if ($email_address ne $stripped_address) {
2896 if (WARN("BAD_SIGN_OFF",
2897 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
2898 $fix) {
2899 $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
2900 }
2901 }
2902
2903 # Only one name comment should be allowed
2904 my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
2905 if ($comment_count > 1) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002906 WARN("BAD_SIGN_OFF",
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002907 "Use a single name comment in email: '$email'\n" . $herecurr);
2908 }
2909
2910
2911 # stable@vger.kernel.org or stable@kernel.org shouldn't
Dwaipayan Raye73d2712020-12-15 20:44:56 -08002912 # have an email name. In addition comments should strictly
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002913 # begin with a #
2914 if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
2915 if (($comment ne "" && $comment !~ /^#.+/) ||
2916 ($email_name ne "")) {
2917 my $cur_name = $email_name;
2918 my $new_comment = $comment;
2919 $cur_name =~ s/[a-zA-Z\s\-\"]+//g;
2920
2921 # Remove brackets enclosing comment text
2922 # and # from start of comments to get comment text
2923 $new_comment =~ s/^\((.*)\)$/$1/;
2924 $new_comment =~ s/^\[(.*)\]$/$1/;
2925 $new_comment =~ s/^[\s\#]+|\s+$//g;
2926
2927 $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
2928 $new_comment = " # $new_comment" if ($new_comment ne "");
2929 my $new_email = "$email_address$new_comment";
2930
2931 if (WARN("BAD_STABLE_ADDRESS_STYLE",
2932 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
2933 $fix) {
2934 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2935 }
2936 }
2937 } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
2938 my $new_comment = $comment;
2939
2940 # Extract comment text from within brackets or
2941 # c89 style /*...*/ comments
2942 $new_comment =~ s/^\[(.*)\]$/$1/;
2943 $new_comment =~ s/^\/\*(.*)\*\/$/$1/;
2944
2945 $new_comment = trim($new_comment);
2946 $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
2947 $new_comment = "($new_comment)" if ($new_comment ne "");
2948 my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
2949
2950 if (WARN("BAD_SIGN_OFF",
2951 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
2952 $fix) {
2953 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2954 }
Joe Perches20112472011-07-25 17:13:23 -07002955 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002956 }
Joe Perches7e51f192013-09-11 14:23:57 -07002957
2958# Check for duplicate signatures
2959 my $sig_nospace = $line;
2960 $sig_nospace =~ s/\s//g;
2961 $sig_nospace = lc($sig_nospace);
2962 if (defined $signatures{$sig_nospace}) {
2963 WARN("BAD_SIGN_OFF",
2964 "Duplicate signature\n" . $herecurr);
2965 } else {
2966 $signatures{$sig_nospace} = 1;
2967 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002968
2969# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2970 if ($sign_off =~ /^co-developed-by:$/i) {
2971 if ($email eq $author) {
2972 WARN("BAD_SIGN_OFF",
2973 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2974 }
2975 if (!defined $lines[$linenr]) {
2976 WARN("BAD_SIGN_OFF",
2977 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2978 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2979 WARN("BAD_SIGN_OFF",
2980 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2981 } elsif ($1 ne $email) {
2982 WARN("BAD_SIGN_OFF",
2983 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2984 }
2985 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002986 }
2987
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002988# Check email subject for common tools that don't need to be mentioned
2989 if ($in_header_lines &&
2990 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2991 WARN("EMAIL_SUBJECT",
2992 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2993 }
2994
Joe Perches44d303e2020-04-06 20:11:10 -07002995# Check for Gerrit Change-Ids not in any patch context
2996 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Aditya Srivastava7580c5b2020-12-15 20:44:47 -08002997 if (ERROR("GERRIT_CHANGE_ID",
2998 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
2999 $fix) {
3000 fix_delete_line($fixlinenr, $rawline);
3001 }
Christopher Covington7ebd05e2014-04-03 14:49:31 -07003002 }
3003
Joe Perches369c8dd2015-11-06 16:31:34 -08003004# Check if the commit log is in a possible stack dump
3005 if ($in_commit_log && !$commit_log_possible_stack_dump &&
3006 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3007 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3008 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07003009 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3010 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3011 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3012 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08003013 $commit_log_possible_stack_dump = 1;
3014 }
3015
Joe Perches2a076f42015-04-16 12:44:28 -07003016# Check for line lengths > 75 in commit log, warn once
3017 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08003018 length($line) > 75 &&
3019 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3020 # file delta changes
3021 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
3022 # filename then :
Aditya Srivastava27b379a2020-12-15 20:44:59 -08003023 $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
3024 # A Fixes: or Link: line or signature tag line
Joe Perches369c8dd2015-11-06 16:31:34 -08003025 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07003026 WARN("COMMIT_LOG_LONG_LINE",
3027 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
3028 $commit_log_long_line = 1;
3029 }
3030
Joe Perchesbf4daf12015-09-09 15:37:50 -07003031# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08003032 if ($in_commit_log && $commit_log_possible_stack_dump &&
3033 $line =~ /^\s*$/) {
3034 $commit_log_possible_stack_dump = 0;
3035 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07003036
Dwaipayan Ray084a6172020-12-15 20:45:18 -08003037# Check for lines starting with a #
3038 if ($in_commit_log && $line =~ /^#/) {
3039 if (WARN("COMMIT_COMMENT_SYMBOL",
3040 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3041 $fix) {
3042 $fixed[$fixlinenr] =~ s/^/ /;
3043 }
3044 }
3045
Joe Perches0d7835f2015-02-13 14:38:35 -08003046# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08003047 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07003048 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07003049 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07003050 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07003051 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08003052 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3053 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07003054 my $init_char = "c";
3055 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08003056 my $short = 1;
3057 my $long = 0;
3058 my $case = 1;
3059 my $space = 1;
3060 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08003061 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08003062 my $id = '0123456789ab';
3063 my $orig_desc = "commit description";
3064 my $description = "";
3065
Joe Perchesfe043ea2015-09-09 15:37:25 -07003066 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3067 $init_char = $1;
3068 $orig_commit = lc($2);
3069 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
3070 $orig_commit = lc($1);
3071 }
3072
Joe Perches0d7835f2015-02-13 14:38:35 -08003073 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
3074 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
3075 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
3076 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
3077 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
3078 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08003079 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08003080 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
3081 defined $rawlines[$linenr] &&
3082 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
3083 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08003084 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08003085 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
3086 defined $rawlines[$linenr] &&
3087 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
3088 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
3089 $orig_desc = $1;
3090 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
3091 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08003092 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08003093 }
3094
3095 ($id, $description) = git_commit_info($orig_commit,
3096 $id, $orig_desc);
3097
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07003098 if (defined($id) &&
3099 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08003100 ERROR("GIT_COMMIT_ID",
3101 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
3102 }
Joe Perchesd311cd42014-08-06 16:10:57 -07003103 }
3104
Joe Perches13f19372014-08-06 16:10:59 -07003105# Check for added, moved or deleted files
3106 if (!$reported_maintainer_file && !$in_commit_log &&
3107 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
3108 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
3109 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
3110 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08003111 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07003112 $reported_maintainer_file = 1;
3113 WARN("FILE_PATH_CHANGES",
3114 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
3115 }
3116
Rob Herringe400edb2019-09-12 15:18:20 +01003117# Check for adding new DT bindings not in schema format
3118 if (!$in_commit_log &&
3119 ($line =~ /^new file mode\s*\d+\s*$/) &&
3120 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3121 WARN("DT_SCHEMA_BINDING_PATCH",
3122 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
3123 }
3124
Andy Whitcroft00df3442007-06-08 13:47:06 -07003125# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08003126 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003127 ERROR("CORRUPTED_PATCH",
3128 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003129 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003130 }
3131
3132# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3133 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003134 $rawline !~ m/^$UTF8*$/) {
3135 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3136
3137 my $blank = copy_spacing($rawline);
3138 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3139 my $hereptr = "$hereline$ptr\n";
3140
Joe Perches34d99212011-07-25 17:13:26 -07003141 CHK("INVALID_UTF8",
3142 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003143 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003144
Joe Perches15662b32011-10-31 17:13:12 -07003145# Check if it's the start of a commit log
3146# (not a header line and we haven't seen the patch filename)
3147 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07003148 !($rawline =~ /^\s+(?:\S|$)/ ||
3149 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07003150 $in_header_lines = 0;
3151 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07003152 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07003153 }
3154
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003155# Check if there is UTF-8 in a commit log when a mail header has explicitly
3156# declined it, i.e defined some charset where it is missing.
3157 if ($in_header_lines &&
3158 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3159 $1 !~ /utf-8/i) {
3160 $non_utf8_charset = 1;
3161 }
3162
3163 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07003164 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003165 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07003166 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3167 }
3168
Joe Perchesd6430f72016-12-12 16:46:28 -08003169# Check for absolute kernel paths in commit message
3170 if ($tree && $in_commit_log) {
3171 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3172 my $file = $1;
3173
3174 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3175 check_absolute_file($1, $herecurr)) {
3176 #
3177 } else {
3178 check_absolute_file($file, $herecurr);
3179 }
3180 }
3181 }
3182
Kees Cook66b47b42014-10-13 15:51:57 -07003183# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07003184 if (defined($misspellings) &&
3185 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003186 while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07003187 my $typo = $1;
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003188 my $blank = copy_spacing($rawline);
3189 my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
3190 my $hereptr = "$hereline$ptr\n";
Kees Cook66b47b42014-10-13 15:51:57 -07003191 my $typo_fix = $spelling_fix{lc($typo)};
3192 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3193 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07003194 my $msg_level = \&WARN;
3195 $msg_level = \&CHK if ($file);
3196 if (&{$msg_level}("TYPO_SPELLING",
Dwaipayan Ray7da07c32020-12-15 20:45:21 -08003197 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07003198 $fix) {
3199 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3200 }
3201 }
3202 }
3203
Matteo Crocea8dd86b2019-09-25 16:46:38 -07003204# check for invalid commit id
3205 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3206 my $id;
3207 my $description;
3208 ($id, $description) = git_commit_info($2, undef, undef);
3209 if (!defined($id)) {
3210 WARN("UNKNOWN_COMMIT_ID",
3211 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3212 }
3213 }
3214
Joe Perches310cd062020-10-15 20:11:52 -07003215# check for repeated words separated by a single space
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003216# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3217 if (($rawline =~ /^\+/ || $in_commit_log) &&
3218 $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003219 pos($rawline) = 1 if (!$in_commit_log);
Joe Perches310cd062020-10-15 20:11:52 -07003220 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3221
3222 my $first = $1;
3223 my $second = $2;
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003224 my $start_pos = $-[1];
3225 my $end_pos = $+[2];
Joe Perches310cd062020-10-15 20:11:52 -07003226 if ($first =~ /(?:struct|union|enum)/) {
3227 pos($rawline) += length($first) + length($second) + 1;
3228 next;
3229 }
3230
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003231 next if (lc($first) ne lc($second));
Joe Perches310cd062020-10-15 20:11:52 -07003232 next if ($first eq 'long');
3233
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003234 # check for character before and after the word matches
3235 my $start_char = '';
3236 my $end_char = '';
3237 $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
3238 $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3239
3240 next if ($start_char =~ /^\S$/);
3241 next if (index(" \t.,;?!", $end_char) == -1);
3242
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003243 # avoid repeating hex occurrences like 'ff ff fe 09 ...'
3244 if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3245 next if (!exists($allow_repeated_words{lc($first)}));
3246 }
3247
Joe Perches310cd062020-10-15 20:11:52 -07003248 if (WARN("REPEATED_WORD",
3249 "Possible repeated word: '$first'\n" . $herecurr) &&
3250 $fix) {
3251 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3252 }
3253 }
3254
3255 # if it's a repeated word on consecutive lines in a comment block
3256 if ($prevline =~ /$;+\s*$/ &&
3257 $prevrawline =~ /($word_pattern)\s*$/) {
3258 my $last_word = $1;
3259 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3260 if (WARN("REPEATED_WORD",
3261 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3262 $fix) {
3263 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3264 }
3265 }
3266 }
3267 }
3268
Andy Whitcroft306708542008-10-15 22:02:28 -07003269# ignore non-hunk lines and lines being removed
3270 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003271
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003272#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003273 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003274 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07003275 if (ERROR("DOS_LINE_ENDINGS",
3276 "DOS line endings\n" . $herevet) &&
3277 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003278 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003279 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003280 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3281 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003282 if (ERROR("TRAILING_WHITESPACE",
3283 "trailing whitespace\n" . $herevet) &&
3284 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003285 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003286 }
3287
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003288 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003289 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07003290
Josh Triplett4783f892013-11-12 15:10:12 -08003291# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003292 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003293 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003294 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3295 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003296 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003297 my $msg_level = \&ERROR;
3298 $msg_level = \&CHK if ($file);
3299 &{$msg_level}("FSF_MAILING_ADDRESS",
3300 "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 -08003301 }
3302
Andi Kleen33549572010-05-24 14:33:29 -07003303# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003304# Only applies when adding the entry originally, after that we do not have
3305# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003306 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003307 # 'choice' is usually the last thing on the line (though
3308 # Kconfig supports named choices), so use a word boundary
3309 # (\b) rather than a whitespace character (\s)
3310 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003311 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003312 my $cnt = $realcnt;
3313 my $ln = $linenr + 1;
3314 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003315 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003316 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003317 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003318 $f = $lines[$ln - 1];
3319 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3320 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003321
3322 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003323 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003324
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003325 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003326 $is_start = 1;
Masahiro Yamada22a4ac02020-06-17 12:02:20 +09003327 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003328 $length = -1;
3329 }
3330
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003331 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003332 $f =~ s/#.*//;
3333 $f =~ s/^\s+//;
3334 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003335
3336 # This only checks context lines in the patch
3337 # and so hopefully shouldn't trigger false
3338 # positives, even though some of these are
3339 # common words in help texts
3340 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3341 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003342 $is_end = 1;
3343 last;
3344 }
Andi Kleen33549572010-05-24 14:33:29 -07003345 $length++;
3346 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003347 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3348 WARN("CONFIG_DESCRIPTION",
3349 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3350 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003351 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003352 }
3353
Joe Perches7ccf41a2020-06-04 16:50:33 -07003354# check MAINTAINERS entries
3355 if ($realfile =~ /^MAINTAINERS$/) {
3356# check MAINTAINERS entries for the right form
3357 if ($rawline =~ /^\+[A-Z]:/ &&
3358 $rawline !~ /^\+[A-Z]:\t\S/) {
3359 if (WARN("MAINTAINERS_STYLE",
3360 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3361 $fix) {
3362 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3363 }
3364 }
3365# check MAINTAINERS entries for the right ordering too
3366 my $preferred_order = 'MRLSWQBCPTFXNK';
3367 if ($rawline =~ /^\+[A-Z]:/ &&
3368 $prevrawline =~ /^[\+ ][A-Z]:/) {
3369 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3370 my $cur = $1;
3371 my $curval = $2;
3372 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3373 my $prev = $1;
3374 my $prevval = $2;
3375 my $curindex = index($preferred_order, $cur);
3376 my $previndex = index($preferred_order, $prev);
3377 if ($curindex < 0) {
3378 WARN("MAINTAINERS_STYLE",
3379 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3380 } else {
3381 if ($previndex >= 0 && $curindex < $previndex) {
3382 WARN("MAINTAINERS_STYLE",
3383 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3384 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3385 ($prev eq 'X' && $cur eq 'X')) &&
3386 ($prevval cmp $curval) > 0) {
3387 WARN("MAINTAINERS_STYLE",
3388 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3389 }
3390 }
Joe Perches628f91a2017-07-10 15:52:07 -07003391 }
3392 }
3393
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003394 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3395 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3396 my $flag = $1;
3397 my $replacement = {
3398 'EXTRA_AFLAGS' => 'asflags-y',
3399 'EXTRA_CFLAGS' => 'ccflags-y',
3400 'EXTRA_CPPFLAGS' => 'cppflags-y',
3401 'EXTRA_LDFLAGS' => 'ldflags-y',
3402 };
3403
3404 WARN("DEPRECATED_VARIABLE",
3405 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3406 }
3407
Rob Herringbff5da42014-01-23 15:54:51 -08003408# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003409 if (defined $root &&
3410 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3411 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3412
Rob Herringbff5da42014-01-23 15:54:51 -08003413 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3414
Florian Vaussardcc933192014-04-03 14:49:27 -07003415 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003416 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003417
Rob Herringbff5da42014-01-23 15:54:51 -08003418 foreach my $compat (@compats) {
3419 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003420 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3421 my $compat3 = $compat;
3422 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3423 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003424 if ( $? >> 8 ) {
3425 WARN("UNDOCUMENTED_DT_STRING",
3426 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3427 }
3428
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003429 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3430 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003431 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003432 if ( $? >> 8 ) {
3433 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003434 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003435 }
3436 }
3437 }
3438
Rob Herring9f3a8992018-04-10 16:33:13 -07003439# check for using SPDX license tag at beginning of files
3440 if ($realline == $checklicenseline) {
3441 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3442 $checklicenseline = 2;
3443 } elsif ($rawline =~ /^\+/) {
3444 my $comment = "";
3445 if ($realfile =~ /\.(h|s|S)$/) {
3446 $comment = '/*';
3447 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3448 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003449 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003450 $comment = '#';
3451 } elsif ($realfile =~ /\.rst$/) {
3452 $comment = '..';
3453 }
3454
Joe Perchesfdf13692019-03-07 16:28:32 -08003455# check SPDX comment style for .[chsS] files
3456 if ($realfile =~ /\.[chsS]$/ &&
3457 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003458 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003459 WARN("SPDX_LICENSE_TAG",
3460 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3461 }
3462
Rob Herring9f3a8992018-04-10 16:33:13 -07003463 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003464 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3465 WARN("SPDX_LICENSE_TAG",
3466 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003467 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003468 my $spdx_license = $1;
3469 if (!is_SPDX_License_valid($spdx_license)) {
3470 WARN("SPDX_LICENSE_TAG",
3471 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3472 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003473 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3474 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3475 my $msg_level = \&WARN;
3476 $msg_level = \&CHK if ($file);
3477 if (&{$msg_level}("SPDX_LICENSE_TAG",
3478
3479 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3480 $fix) {
3481 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3482 }
3483 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003484 }
3485 }
3486 }
3487
Joe Perchesa0154cd2020-10-15 20:12:19 -07003488# check for embedded filenames
3489 if ($rawline =~ /^\+.*\Q$realfile\E/) {
3490 WARN("EMBEDDED_FILENAME",
3491 "It's generally not useful to have the filename in the file\n" . $herecurr);
3492 }
3493
Andy Whitcroft5368df202008-10-15 22:02:27 -07003494# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003495 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003496
Joe Perchesa8da38a2019-03-07 16:28:42 -08003497# check for using SPDX-License-Identifier on the wrong line number
3498 if ($realline != $checklicenseline &&
3499 $rawline =~ /\bSPDX-License-Identifier:/ &&
3500 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3501 WARN("SPDX_LICENSE_TAG",
3502 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3503 }
3504
Joe Perches47e0c882015-06-25 15:02:57 -07003505# line length limit (with some exclusions)
3506#
3507# There are a few types of lines that may extend beyond $max_line_length:
3508# logging functions like pr_info that end in a string
3509# lines with a single string
3510# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003511# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003512#
3513# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003514# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003515# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3516# LONG_LINE all other lines longer than $max_line_length
3517#
3518# if LONG_LINE is ignored, the other 2 types are also ignored
3519#
3520
Joe Perchesb4749e92015-07-17 16:24:01 -07003521 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003522 my $msg_type = "LONG_LINE";
3523
3524 # Check the allowed long line types first
3525
3526 # logging functions that end in a string that starts
3527 # before $max_line_length
3528 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3529 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3530 $msg_type = "";
3531
3532 # lines with only strings (w/ possible termination)
3533 # #defines with only strings
3534 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3535 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3536 $msg_type = "";
3537
Joe Perchescc147502017-11-17 15:28:44 -08003538 # More special cases
3539 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3540 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003541 $msg_type = "";
3542
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003543 # URL ($rawline is used in case the URL is in a comment)
3544 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3545 $msg_type = "";
3546
Joe Perches47e0c882015-06-25 15:02:57 -07003547 # Otherwise set the alternate message types
3548
3549 # a comment starts before $max_line_length
3550 } elsif ($line =~ /($;[\s$;]*)$/ &&
3551 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3552 $msg_type = "LONG_LINE_COMMENT"
3553
3554 # a quoted string starts before $max_line_length
3555 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3556 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3557 $msg_type = "LONG_LINE_STRING"
3558 }
3559
3560 if ($msg_type ne "" &&
3561 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003562 my $msg_level = \&WARN;
3563 $msg_level = \&CHK if ($file);
3564 &{$msg_level}($msg_type,
3565 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003566 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003567 }
3568
Andy Whitcroft8905a672007-11-28 16:21:06 -08003569# check for adding lines without a newline.
3570 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Tom Rix47ca69b2020-12-15 20:44:40 -08003571 if (WARN("MISSING_EOF_NEWLINE",
3572 "adding a line without newline at end of file\n" . $herecurr) &&
3573 $fix) {
3574 fix_delete_line($fixlinenr+1, "No newline at end of file");
3575 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003576 }
3577
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003578# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003579 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003580
3581# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003582# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003583 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3584 $rawline =~ /^\+\s* \s*/) {
3585 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003586 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003587 if (ERROR("CODE_INDENT",
3588 "code indent should use tabs where possible\n" . $herevet) &&
3589 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003590 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003591 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003592 }
3593
Alberto Panizzo08e44362010-03-05 13:43:54 -08003594# check for space before tabs.
3595 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3596 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003597 if (WARN("SPACE_BEFORE_TAB",
3598 "please, no space before tabs\n" . $herevet) &&
3599 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003600 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003601 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003602 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003603 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003604 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003605 }
3606
Joe Perches6a487212018-04-10 16:34:04 -07003607# check for assignments on the start of a line
3608 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
Aditya Srivastavada7355a2020-12-15 20:45:06 -08003609 my $operator = $1;
3610 if (CHK("ASSIGNMENT_CONTINUATIONS",
3611 "Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3612 $fix && $prevrawline =~ /^\+/) {
3613 # add assignment operator to the previous line, remove from current line
3614 $fixed[$fixlinenr - 1] .= " $operator";
3615 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3616 }
Joe Perches6a487212018-04-10 16:34:04 -07003617 }
3618
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003619# check for && or || at the start of a line
3620 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
Aditya Srivastava8e08f072020-12-15 20:45:09 -08003621 my $operator = $1;
3622 if (CHK("LOGICAL_CONTINUATIONS",
3623 "Logical continuations should be on the previous line\n" . $hereprev) &&
3624 $fix && $prevrawline =~ /^\+/) {
3625 # insert logical operator at last non-comment, non-whitepsace char on previous line
3626 $prevline =~ /[\s$;]*$/;
3627 my $line_end = substr($prevrawline, $-[0]);
3628 $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
3629 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3630 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003631 }
3632
Joe Perchesa91e8992016-05-20 17:04:05 -07003633# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003634 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003635 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003636 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003637 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003638 if (WARN("TABSTOP",
3639 "Statements should start on a tabstop\n" . $herecurr) &&
3640 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003641 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003642 }
3643 }
3644 }
3645
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003646# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003647 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003648 $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 -07003649 $prevline =~ /^\+(\t*)(.*)$/;
3650 my $oldindent = $1;
3651 my $rest = $2;
3652
3653 my $pos = pos_last_openparen($rest);
3654 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003655 $line =~ /^(\+| )([ \t]*)/;
3656 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003657
3658 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003659 "\t" x ($pos / $tabsize) .
3660 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003661 my $goodspaceindent = $oldindent . " " x $pos;
3662
3663 if ($newindent ne $goodtabindent &&
3664 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003665
3666 if (CHK("PARENTHESIS_ALIGNMENT",
3667 "Alignment should match open parenthesis\n" . $hereprev) &&
3668 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003669 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003670 s/^\+[ \t]*/\+$goodtabindent/;
3671 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003672 }
3673 }
3674 }
3675
Joe Perches6ab3a972015-04-16 12:44:05 -07003676# check for space after cast like "(int) foo" or "(struct foo) bar"
3677# avoid checking a few false positives:
3678# "sizeof(<type>)" or "__alignof__(<type>)"
3679# function pointer declarations like "(*foo)(int) = bar;"
3680# structure definitions like "(struct foo) { 0 };"
3681# multiline macros that define functions
3682# known attributes or the __attribute__ keyword
3683 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3684 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003685 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003686 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003687 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003688 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003689 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003690 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003691 }
3692
Joe Perches86406b12015-09-09 15:37:41 -07003693# Block comment styles
3694# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003695 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003696 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003697 $rawline =~ /^\+[ \t]*\*/ &&
Łukasz Stelmachc70735c2020-10-15 20:12:25 -07003698 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
Joe Perches05880602012-10-04 17:13:35 -07003699 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3700 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3701 }
3702
Joe Perches86406b12015-09-09 15:37:41 -07003703# Block comments use * on subsequent lines
3704 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3705 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003706 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003707 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003708 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003709 WARN("BLOCK_COMMENT_STYLE",
3710 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003711 }
3712
Joe Perches86406b12015-09-09 15:37:41 -07003713# Block comments use */ on trailing lines
3714 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003715 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3716 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3717 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003718 WARN("BLOCK_COMMENT_STYLE",
3719 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003720 }
3721
Joe Perches08eb9b82016-10-11 13:51:50 -07003722# Block comment * alignment
3723 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003724 $line =~ /^\+[ \t]*$;/ && #leading comment
3725 $rawline =~ /^\+[ \t]*\*/ && #leading *
3726 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003727 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003728 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3729 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003730 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003731 if (defined($1)) {
3732 $oldindent = expand_tabs($1);
3733 } else {
3734 $prevrawline =~ m@^\+(.*/?)\*@;
3735 $oldindent = expand_tabs($1);
3736 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003737 $rawline =~ m@^\+([ \t]*)\*@;
3738 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003739 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003740 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003741 WARN("BLOCK_COMMENT_STYLE",
3742 "Block comments should align the * on each line\n" . $hereprev);
3743 }
3744 }
3745
Joe Perches7f619192014-08-06 16:10:39 -07003746# check for missing blank lines after struct/union declarations
3747# with exceptions for various attributes and macros
3748 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3749 $line =~ /^\+/ &&
3750 !($line =~ /^\+\s*$/ ||
3751 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3752 $line =~ /^\+\s*MODULE_/i ||
3753 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3754 $line =~ /^\+[a-z_]*init/ ||
3755 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3756 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003757 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003758 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003759 if (CHK("LINE_SPACING",
3760 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3761 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003762 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003763 }
Joe Perches7f619192014-08-06 16:10:39 -07003764 }
3765
Joe Perches365dd4e2014-08-06 16:10:42 -07003766# check for multiple consecutive blank lines
3767 if ($prevline =~ /^[\+ ]\s*$/ &&
3768 $line =~ /^\+\s*$/ &&
3769 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003770 if (CHK("LINE_SPACING",
3771 "Please don't use multiple blank lines\n" . $hereprev) &&
3772 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003773 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003774 }
3775
Joe Perches365dd4e2014-08-06 16:10:42 -07003776 $last_blank_line = $linenr;
3777 }
3778
Joe Perches3b617e32014-04-03 14:49:28 -07003779# check for missing blank lines after declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08003780# (declarations must have the same indentation and not be at the start of line)
3781 if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
3782 # use temporaries
3783 my $sl = $sline;
3784 my $pl = $prevline;
3785 # remove $Attribute/$Sparse uses to simplify comparisons
3786 $sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3787 $pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3788 if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003789 # function pointer declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08003790 $pl =~ /^\+\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
Joe Perchesb5e87362021-02-25 17:21:40 -08003792 $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003793 # known declaration macros
Joe Perchesb5e87362021-02-25 17:21:40 -08003794 $pl =~ /^\+\s+$declaration_macros/) &&
Joe Perches3f7bac02014-06-04 16:12:04 -07003795 # for "else if" which can look like "$Ident $Ident"
Joe Perchesb5e87362021-02-25 17:21:40 -08003796 !($pl =~ /^\+\s+$c90_Keywords\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003797 # other possible extensions of declaration lines
Joe Perchesb5e87362021-02-25 17:21:40 -08003798 $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003799 # not starting a section or a macro "\" extended line
Joe Perchesb5e87362021-02-25 17:21:40 -08003800 $pl =~ /(?:\{\s*|\\)$/) &&
Joe Perches3f7bac02014-06-04 16:12:04 -07003801 # looks like a declaration
Joe Perchesb5e87362021-02-25 17:21:40 -08003802 !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003803 # function pointer declarations
Joe Perchesb5e87362021-02-25 17:21:40 -08003804 $sl =~ /^\+\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
Joe Perchesb5e87362021-02-25 17:21:40 -08003806 $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003807 # known declaration macros
Joe Perchesb5e87362021-02-25 17:21:40 -08003808 $sl =~ /^\+\s+$declaration_macros/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003809 # start of struct or union or enum
Joe Perchesb5e87362021-02-25 17:21:40 -08003810 $sl =~ /^\+\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
Joe Perchesb5e87362021-02-25 17:21:40 -08003812 $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003813 # bitfield continuation
Joe Perchesb5e87362021-02-25 17:21:40 -08003814 $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003815 # other possible extensions of declaration lines
Joe Perchesb5e87362021-02-25 17:21:40 -08003816 $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
3817 if (WARN("LINE_SPACING",
3818 "Missing a blank line after declarations\n" . $hereprev) &&
3819 $fix) {
3820 fix_insert_line($fixlinenr, "\+");
3821 }
Joe Perchesd752fcc2014-08-06 16:11:05 -07003822 }
Joe Perches3b617e32014-04-03 14:49:28 -07003823 }
3824
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003825# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003826# Exceptions:
3827# 1) within comments
3828# 2) indented preprocessor commands
3829# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003830 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003831 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003832 if (WARN("LEADING_SPACE",
3833 "please, no spaces at the start of a line\n" . $herevet) &&
3834 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003835 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003836 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003837 }
3838
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003839# check we are in a valid C source file if not then ignore this hunk
3840 next if ($realfile !~ /\.(h|c)$/);
3841
Joe Perches5751a242017-11-17 15:28:52 -08003842# check for unusual line ending [ or (
3843 if ($line =~ /^\+.*([\[\(])\s*$/) {
3844 CHK("OPEN_ENDED_LINE",
3845 "Lines should not end with a '$1'\n" . $herecurr);
3846 }
3847
Joe Perches4dbed762017-05-08 15:55:39 -07003848# check if this appears to be the start function declaration, save the name
3849 if ($sline =~ /^\+\{\s*$/ &&
3850 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3851 $context_function = $1;
3852 }
3853
3854# check if this appears to be the end of function declaration
3855 if ($sline =~ /^\+\}\s*$/) {
3856 undef $context_function;
3857 }
3858
Joe Perches032a4c02014-08-06 16:10:29 -07003859# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003860# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003861# if the previous line is a break or return and is indented 1 tab more...
3862 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3863 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003864 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3865 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3866 defined $lines[$linenr] &&
3867 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003868 WARN("UNNECESSARY_ELSE",
3869 "else is not generally useful after a break or return\n" . $hereprev);
3870 }
3871 }
3872
Joe Perchesc00df192014-08-06 16:11:01 -07003873# check indentation of a line with a break;
Joe Perchesdc58bc52020-12-15 20:44:33 -08003874# if the previous line is a goto, return or break
3875# and is indented the same # of tabs
Joe Perchesc00df192014-08-06 16:11:01 -07003876 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3877 my $tabs = $1;
Joe Perchesdc58bc52020-12-15 20:44:33 -08003878 if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
3879 if (WARN("UNNECESSARY_BREAK",
3880 "break is not useful after a $1\n" . $hereprev) &&
3881 $fix) {
3882 fix_delete_line($fixlinenr, $rawline);
3883 }
Joe Perchesc00df192014-08-06 16:11:01 -07003884 }
3885 }
3886
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003887# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003888 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003889 WARN("CVS_KEYWORD",
3890 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003891 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003892
Joe Perches56e77d72013-02-21 16:44:14 -08003893# check for old HOTPLUG __dev<foo> section markings
3894 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3895 WARN("HOTPLUG_SECTION",
3896 "Using $1 is unnecessary\n" . $herecurr);
3897 }
3898
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003899# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003900 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3901 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003902#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003903 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003904 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003905 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003906 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003907 $stat =~ s/\n./\n /g;
3908 $cond =~ s/\n./\n /g;
3909
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003910#print "linenr<$linenr> <$stat>\n";
3911 # If this statement has no statement boundaries within
3912 # it there is no point in retrying a statement scan
3913 # until we hit end of it.
3914 my $frag = $stat; $frag =~ s/;+\s*$//;
3915 if ($frag !~ /(?:{|;)/) {
3916#print "skip<$line_nr_next>\n";
3917 $suppress_statement = $line_nr_next;
3918 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003919
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003920 # Find the real next line.
3921 $realline_next = $line_nr_next;
3922 if (defined $realline_next &&
3923 (!defined $lines[$realline_next - 1] ||
3924 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3925 $realline_next++;
3926 }
3927
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003928 my $s = $stat;
3929 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003930
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003931 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003932 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003933
3934 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003935 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003936
Andy Whitcroft463f2862009-09-21 17:04:34 -07003937 } elsif ($s =~ /^.\s*else\b/s) {
3938
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003939 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003940 } 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 -07003941 my $type = $1;
3942 $type =~ s/\s+/ /g;
3943 possible($type, "A:" . $s);
3944
Andy Whitcroft8905a672007-11-28 16:21:06 -08003945 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003946 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003947 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003948 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003949
3950 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003951 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003952 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003953 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003954
3955 # Check for any sort of function declaration.
3956 # int foo(something bar, other baz);
3957 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003958 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 -08003959 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003960
Andy Whitcroftcf655042008-03-04 14:28:20 -08003961 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003962 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003963 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003964
Andy Whitcroft8905a672007-11-28 16:21:06 -08003965 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003966 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003967
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003968 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003969 }
3970 }
3971 }
3972
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003973 }
3974
Andy Whitcroft653d4872007-06-23 17:16:34 -07003975#
3976# Checks which may be anchored in the context.
3977#
3978
3979# Check for switch () and associated case and default
3980# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003981 if ($line=~/\bswitch\s*\(.*\)/) {
3982 my $err = '';
3983 my $sep = '';
3984 my @ctx = ctx_block_outer($linenr, $realcnt);
3985 shift(@ctx);
3986 for my $ctx (@ctx) {
3987 my ($clen, $cindent) = line_stats($ctx);
3988 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3989 $indent != $cindent) {
3990 $err .= "$sep$ctx\n";
3991 $sep = '';
3992 } else {
3993 $sep = "[...]\n";
3994 }
3995 }
3996 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003997 ERROR("SWITCH_CASE_INDENT_LEVEL",
3998 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003999 }
4000 }
4001
4002# if/while/etc brace do not go on next line, unless defining a do while loop,
4003# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07004004 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 -07004005 my $pre_ctx = "$1$2";
4006
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004007 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08004008
4009 if ($line =~ /^\+\t{6,}/) {
4010 WARN("DEEP_INDENTATION",
4011 "Too many leading tabs - consider code refactoring\n" . $herecurr);
4012 }
4013
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004014 my $ctx_cnt = $realcnt - $#ctx - 1;
4015 my $ctx = join("\n", @ctx);
4016
Andy Whitcroft548596d2008-07-23 21:29:01 -07004017 my $ctx_ln = $linenr;
4018 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004019
Andy Whitcroft548596d2008-07-23 21:29:01 -07004020 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4021 defined $lines[$ctx_ln - 1] &&
4022 $lines[$ctx_ln - 1] =~ /^-/)) {
4023 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4024 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004025 $ctx_ln++;
4026 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07004027
Andy Whitcroft53210162008-07-23 21:29:03 -07004028 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
4029 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07004030
Joe Perchesd752fcc2014-08-06 16:11:05 -07004031 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004032 ERROR("OPEN_BRACE",
4033 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07004034 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07004035 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004036 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4037 $ctx =~ /\)\s*\;\s*$/ &&
4038 defined $lines[$ctx_ln - 1])
4039 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004040 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
4041 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004042 WARN("TRAILING_SEMICOLON",
4043 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07004044 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004045 }
4046 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004047 }
4048
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004049# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07004050 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 -08004051 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4052 ctx_statement_block($linenr, $realcnt, 0)
4053 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004054 my ($s, $c) = ($stat, $cond);
4055
4056 substr($s, 0, length($c), '');
4057
Joe Perches9f5af482015-09-09 15:37:30 -07004058 # remove inline comments
4059 $s =~ s/$;/ /g;
4060 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004061
4062 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07004063 my @newlines = ($c =~ /\n/gs);
4064 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004065
Joe Perches9f5af482015-09-09 15:37:30 -07004066 # Make sure we remove the line prefixes as we have
4067 # none on the first line, and are going to readd them
4068 # where necessary.
4069 $s =~ s/\n./\n/gs;
4070 while ($s =~ /\n\s+\\\n/) {
4071 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
4072 }
4073
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004074 # We want to check the first line inside the block
4075 # starting at the end of the conditional, so remove:
4076 # 1) any blank line termination
4077 # 2) any opening brace { on end of the line
4078 # 3) any do (...) {
4079 my $continuation = 0;
4080 my $check = 0;
4081 $s =~ s/^.*\bdo\b//;
4082 $s =~ s/^\s*{//;
4083 if ($s =~ s/^\s*\\//) {
4084 $continuation = 1;
4085 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004086 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004087 $check = 1;
4088 $cond_lines++;
4089 }
4090
4091 # Also ignore a loop construct at the end of a
4092 # preprocessor statement.
4093 if (($prevline =~ /^.\s*#\s*define\s/ ||
4094 $prevline =~ /\\\s*$/) && $continuation == 0) {
4095 $check = 0;
4096 }
4097
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004098 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07004099 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004100 while ($cond_ptr != $cond_lines) {
4101 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004102
Andy Whitcroftf16fa282008-10-15 22:02:32 -07004103 # If we see an #else/#elif then the code
4104 # is not linear.
4105 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4106 $check = 0;
4107 }
4108
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004109 # Ignore:
4110 # 1) blank lines, they should be at 0,
4111 # 2) preprocessor lines, and
4112 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07004113 if ($continuation ||
4114 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004115 $s =~ /^\s*#\s*?/ ||
4116 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07004117 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07004118 if ($s =~ s/^.*?\n//) {
4119 $cond_lines++;
4120 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004121 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004122 }
4123
4124 my (undef, $sindent) = line_stats("+" . $s);
4125 my $stat_real = raw_line($linenr, $cond_lines);
4126
4127 # Check if either of these lines are modified, else
4128 # this is not this patch's fault.
4129 if (!defined($stat_real) ||
4130 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
4131 $check = 0;
4132 }
4133 if (defined($stat_real) && $cond_lines > 1) {
4134 $stat_real = "[...]\n$stat_real";
4135 }
4136
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004137 #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 -07004138
Joe Perches9f5af482015-09-09 15:37:30 -07004139 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07004140 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07004141 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07004142 ($sindent == $indent &&
4143 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07004144 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004145 WARN("SUSPECT_CODE_INDENT",
4146 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004147 }
4148 }
4149
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004150 # Track the 'values' across context and added lines.
4151 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004152 my ($curr_values, $curr_vars) =
4153 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004154 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004155 if ($dbg_values) {
4156 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004157 print "$linenr > .$outline\n";
4158 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004159 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004160 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004161 $prev_values = substr($curr_values, -1);
4162
Andy Whitcroft00df3442007-06-08 13:47:06 -07004163#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07004164 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07004165
Joe Perches99ca38c2020-10-15 20:12:09 -07004166# check for self assignments used to avoid compiler warnings
4167# e.g.: int foo = foo, *bar = NULL;
4168# struct foo bar = *(&(bar));
4169 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
4170 my $var = $1;
4171 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
4172 WARN("SELF_ASSIGNMENT",
4173 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
4174 }
4175 }
4176
Joe Perches11ca40a2016-12-12 16:46:31 -08004177# check for dereferences that span multiple lines
4178 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
4179 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
4180 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
4181 my $ref = $1;
4182 $line =~ /^.\s*($Lval)/;
4183 $ref .= $1;
4184 $ref =~ s/\s//g;
4185 WARN("MULTILINE_DEREFERENCE",
4186 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
4187 }
4188
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004189# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07004190 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 -07004191 my $type = $1;
4192 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07004193 $var = "" if (!defined $var);
4194 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004195 my $sign = $1;
4196 my $pointer = $2;
4197
4198 $pointer = "" if (!defined $pointer);
4199
4200 if (WARN("UNSPECIFIED_INT",
4201 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4202 $fix) {
4203 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07004204 my $comp_pointer = $pointer;
4205 $comp_pointer =~ s/\s//g;
4206 $decl .= $comp_pointer;
4207 $decl = rtrim($decl) if ($var eq "");
4208 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004209 }
4210 }
4211 }
4212
Andy Whitcroft653d4872007-06-23 17:16:34 -07004213# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07004214 if ($dbg_type) {
4215 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004216 ERROR("TEST_TYPE",
4217 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004218 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004219 ERROR("TEST_NOT_TYPE",
4220 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004221 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004222 next;
4223 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004224# TEST: allow direct testing of the attribute matcher.
4225 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004226 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004227 ERROR("TEST_ATTR",
4228 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004229 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004230 ERROR("TEST_NOT_ATTR",
4231 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004232 }
4233 next;
4234 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004235
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004236# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07004237 if ($line =~ /^.\s*{/ &&
4238 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07004239 if (ERROR("OPEN_BRACE",
4240 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004241 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4242 fix_delete_line($fixlinenr - 1, $prevrawline);
4243 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004244 my $fixedline = $prevrawline;
4245 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004246 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004247 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004248 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004249 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004250 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004251 }
4252
Andy Whitcroft653d4872007-06-23 17:16:34 -07004253#
4254# Checks which are anchored on the added line.
4255#
4256
4257# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004258 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07004259 my $path = $1;
4260 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004261 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08004262 "malformed #include filename\n" . $herecurr);
4263 }
4264 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4265 ERROR("UAPI_INCLUDE",
4266 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004267 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004268 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004269
4270# no C99 // comments
4271 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07004272 if (ERROR("C99_COMMENTS",
4273 "do not use C99 // comments\n" . $herecurr) &&
4274 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004275 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07004276 if ($line =~ /\/\/(.*)$/) {
4277 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07004278 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07004279 }
4280 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004281 }
4282 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004283 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004284 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004285
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004286# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4287# the whole statement.
4288#print "APW <$lines[$realline_next - 1]>\n";
4289 if (defined $realline_next &&
4290 exists $lines[$realline_next - 1] &&
4291 !defined $suppress_export{$realline_next} &&
Christoph Hellwig36794822021-02-02 13:13:34 +01004292 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004293 # Handle definitions which produce identifiers with
4294 # a prefix:
4295 # XXX(foo);
4296 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004297 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08004298 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004299 $name =~ /^${Ident}_$2/) {
4300#print "FOO C name<$name>\n";
4301 $suppress_export{$realline_next} = 1;
4302
4303 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004304 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07004305 ^.DEFINE_$Ident\(\Q$name\E\)|
4306 ^.DECLARE_$Ident\(\Q$name\E\)|
4307 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004308 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4309 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07004310 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004311#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4312 $suppress_export{$realline_next} = 2;
4313 } else {
4314 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004315 }
4316 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004317 if (!defined $suppress_export{$linenr} &&
4318 $prevline =~ /^.\s*$/ &&
Christoph Hellwig36794822021-02-02 13:13:34 +01004319 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004320#print "FOO B <$lines[$linenr - 1]>\n";
4321 $suppress_export{$linenr} = 2;
4322 }
4323 if (defined $suppress_export{$linenr} &&
4324 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004325 WARN("EXPORT_SYMBOL",
4326 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004327 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004328
Joe Eloff5150bda2010-08-09 17:21:00 -07004329# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004330 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004331 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004332 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004333 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004334 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004335 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004336 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004337# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004338 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004339 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004340 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004341 $herecurr) &&
4342 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004343 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004344 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004345 }
4346
Joe Perches18130872014-08-06 16:11:22 -07004347# check for misordered declarations of char/short/int/long with signed/unsigned
4348 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4349 my $tmp = trim($1);
4350 WARN("MISORDERED_TYPE",
4351 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4352 }
4353
Joe Perches809e0822018-08-21 21:58:12 -07004354# check for unnecessary <signed> int declarations of short/long/long long
4355 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4356 my $type = trim($1);
4357 next if ($type !~ /\bint\b/);
4358 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4359 my $new_type = $type;
4360 $new_type =~ s/\b\s*int\s*\b/ /;
4361 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4362 $new_type =~ s/^const\s+//;
4363 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4364 $new_type = "const $new_type" if ($type =~ /^const\b/);
4365 $new_type =~ s/\s+/ /g;
4366 $new_type = trim($new_type);
4367 if (WARN("UNNECESSARY_INT",
4368 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4369 $fix) {
4370 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4371 }
4372 }
4373
Joe Perchescb710ec2010-10-26 14:23:20 -07004374# check for static const char * arrays.
4375 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004376 WARN("STATIC_CONST_CHAR_ARRAY",
4377 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004378 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004379 }
4380
4381# check for initialized const char arrays that should be static const
4382 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4383 if (WARN("STATIC_CONST_CHAR_ARRAY",
4384 "const array should probably be static const\n" . $herecurr) &&
4385 $fix) {
4386 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4387 }
4388 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004389
4390# check for static char foo[] = "bar" declarations.
4391 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004392 WARN("STATIC_CONST_CHAR_ARRAY",
4393 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004394 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004395 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004396
Joe Perchesab7e23f2015-04-16 12:44:22 -07004397# check for const <foo> const where <foo> is not a pointer or array type
4398 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4399 my $found = $1;
4400 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4401 WARN("CONST_CONST",
4402 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4403 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4404 WARN("CONST_CONST",
4405 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4406 }
4407 }
4408
Joe Perches73169762020-12-15 20:44:30 -08004409# check for const static or static <non ptr type> const declarations
4410# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
4411 if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
4412 $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
4413 if (WARN("STATIC_CONST",
4414 "Move const after static - use 'static const $1'\n" . $herecurr) &&
4415 $fix) {
4416 $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
4417 $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
4418 }
4419 }
4420
Joe Perches9b0fa602014-04-03 14:49:18 -07004421# check for non-global char *foo[] = {"bar", ...} declarations.
4422 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4423 WARN("STATIC_CONST_CHAR_ARRAY",
4424 "char * array declaration might be better as static const\n" .
4425 $herecurr);
4426 }
4427
Joe Perchesb598b672015-04-16 12:44:36 -07004428# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4429 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4430 my $array = $1;
4431 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4432 my $array_div = $1;
4433 if (WARN("ARRAY_SIZE",
4434 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4435 $fix) {
4436 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4437 }
4438 }
4439 }
4440
Joe Perchesb36190c2014-01-27 17:07:18 -08004441# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004442 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004443 if (ERROR("FUNCTION_WITHOUT_ARGS",
4444 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4445 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004446 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004447 }
4448 }
4449
Andy Whitcroft653d4872007-06-23 17:16:34 -07004450# check for new typedefs, only function parameters and sparse annotations
4451# make sense.
4452 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004453 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004454 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004455 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004456 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004457 WARN("NEW_TYPEDEFS",
4458 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004459 }
4460
4461# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004462 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004463 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4464 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004465 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004466
Andy Whitcroft65863862009-01-06 14:41:21 -08004467 # Should start with a space.
4468 $to =~ s/^(\S)/ $1/;
4469 # Should not end with a space.
4470 $to =~ s/\s+$//;
4471 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004472 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004473 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004474
Joe Perches3705ce52013-07-03 15:05:31 -07004475## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004476 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004477 if (ERROR("POINTER_LOCATION",
4478 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4479 $fix) {
4480 my $sub_from = $ident;
4481 my $sub_to = $ident;
4482 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004483 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004484 s@\Q$sub_from\E@$sub_to@;
4485 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004486 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004487 }
4488 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4489 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004490 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004491
Andy Whitcroft65863862009-01-06 14:41:21 -08004492 # Should start with a space.
4493 $to =~ s/^(\S)/ $1/;
4494 # Should not end with a space.
4495 $to =~ s/\s+$//;
4496 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004497 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004498 }
4499 # Modifiers should have spaces.
4500 $to =~ s/(\b$Modifier$)/$1 /;
4501
Joe Perches3705ce52013-07-03 15:05:31 -07004502## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004503 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004504 if (ERROR("POINTER_LOCATION",
4505 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4506 $fix) {
4507
4508 my $sub_from = $match;
4509 my $sub_to = $match;
4510 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004511 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004512 s@\Q$sub_from\E@$sub_to@;
4513 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004514 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004515 }
4516
Joe Perches9d3e3c72015-09-09 15:37:27 -07004517# avoid BUG() or BUG_ON()
4518 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004519 my $msg_level = \&WARN;
4520 $msg_level = \&CHK if ($file);
4521 &{$msg_level}("AVOID_BUG",
4522 "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 -07004523 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004524
Joe Perches9d3e3c72015-09-09 15:37:27 -07004525# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004526 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004527 WARN("LINUX_VERSION_CODE",
4528 "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 -08004529 }
4530
Joe Perches17441222011-06-15 15:08:17 -07004531# check for uses of printk_ratelimit
4532 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004533 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004534 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004535 }
4536
Joe Percheseeef5732017-11-17 15:28:41 -08004537# printk should use KERN_* levels
4538 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4539 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4540 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004541 }
4542
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004543# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4544 if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4545 my $printk = $1;
4546 my $modifier = $2;
4547 my $orig = $3;
4548 $modifier = "" if (!defined($modifier));
Joe Perches243f3802012-05-31 16:26:09 -07004549 my $level = lc($orig);
4550 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004551 my $level2 = $level;
4552 $level2 = "dbg" if ($level eq "debug");
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004553 $level .= $modifier;
4554 $level2 .= $modifier;
Joe Perches243f3802012-05-31 16:26:09 -07004555 WARN("PREFER_PR_LEVEL",
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004556 "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 -07004557 }
4558
Joe Perchesf5eea3b2020-12-15 20:45:24 -08004559# prefer dev_<level> to dev_printk(KERN_<LEVEL>
Joe Perchesdc139312013-02-21 16:44:13 -08004560 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4561 my $orig = $1;
4562 my $level = lc($orig);
4563 $level = "warn" if ($level eq "warning");
4564 $level = "dbg" if ($level eq "debug");
4565 WARN("PREFER_DEV_LEVEL",
4566 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4567 }
4568
Nicolas Boichat8020b252020-10-15 20:12:02 -07004569# trace_printk should not be used in production code.
4570 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4571 WARN("TRACE_PRINTK",
4572 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4573 }
4574
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004575# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4576# number of false positives, but assembly files are not checked, so at
4577# least the arch entry code will not trigger this warning.
4578 if ($line =~ /\bENOSYS\b/) {
4579 WARN("ENOSYS",
4580 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4581 }
4582
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004583# ENOTSUPP is not a standard error code and should be avoided in new patches.
4584# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4585# Similarly to ENOSYS warning a small number of false positives is expected.
4586 if (!$file && $line =~ /\bENOTSUPP\b/) {
4587 if (WARN("ENOTSUPP",
4588 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4589 $fix) {
4590 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4591 }
4592 }
4593
Andy Whitcroft653d4872007-06-23 17:16:34 -07004594# function brace can't be on same line, except for #defines of do while,
4595# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004596 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004597 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4598 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4599 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004600 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004601 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004602 $fix) {
4603 fix_delete_line($fixlinenr, $rawline);
4604 my $fixed_line = $rawline;
Dwaipayan Ray03f49352020-12-15 20:45:02 -08004605 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
Joe Perches8d182472014-08-06 16:11:12 -07004606 my $line1 = $1;
4607 my $line2 = $2;
4608 fix_insert_line($fixlinenr, ltrim($line1));
4609 fix_insert_line($fixlinenr, "\+{");
4610 if ($line2 !~ /^\s*$/) {
4611 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4612 }
4613 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004614 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004615
Andy Whitcroft8905a672007-11-28 16:21:06 -08004616# open braces for enum, union and struct go on the same line.
4617 if ($line =~ /^.\s*{/ &&
4618 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004619 if (ERROR("OPEN_BRACE",
4620 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4621 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4622 fix_delete_line($fixlinenr - 1, $prevrawline);
4623 fix_delete_line($fixlinenr, $rawline);
4624 my $fixedline = rtrim($prevrawline) . " {";
4625 fix_insert_line($fixlinenr, $fixedline);
4626 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004627 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004628 if ($fixedline !~ /^\+\s*$/) {
4629 fix_insert_line($fixlinenr, $fixedline);
4630 }
4631 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004632 }
4633
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004634# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004635 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4636 if (WARN("SPACING",
4637 "missing space after $1 definition\n" . $herecurr) &&
4638 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004639 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004640 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4641 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004642 }
4643
Joe Perches31070b52014-01-23 15:54:49 -08004644# Function pointer declarations
4645# check spacing between type, funcptr, and args
4646# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004647 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004648 my $declare = $1;
4649 my $pre_pointer_space = $2;
4650 my $post_pointer_space = $3;
4651 my $funcname = $4;
4652 my $post_funcname_space = $5;
4653 my $pre_args_space = $6;
4654
Joe Perches91f72e92014-04-03 14:49:12 -07004655# the $Declare variable will capture all spaces after the type
4656# so check it for a missing trailing missing space but pointer return types
4657# don't need a space so don't warn for those.
4658 my $post_declare_space = "";
4659 if ($declare =~ /(\s+)$/) {
4660 $post_declare_space = $1;
4661 $declare = rtrim($declare);
4662 }
4663 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004664 WARN("SPACING",
4665 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004666 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004667 }
4668
4669# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004670# This test is not currently implemented because these declarations are
4671# equivalent to
4672# int foo(int bar, ...)
4673# and this is form shouldn't/doesn't generate a checkpatch warning.
4674#
4675# elsif ($declare =~ /\s{2,}$/) {
4676# WARN("SPACING",
4677# "Multiple spaces after return type\n" . $herecurr);
4678# }
Joe Perches31070b52014-01-23 15:54:49 -08004679
4680# unnecessary space "type ( *funcptr)(args...)"
4681 if (defined $pre_pointer_space &&
4682 $pre_pointer_space =~ /^\s/) {
4683 WARN("SPACING",
4684 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4685 }
4686
4687# unnecessary space "type (* funcptr)(args...)"
4688 if (defined $post_pointer_space &&
4689 $post_pointer_space =~ /^\s/) {
4690 WARN("SPACING",
4691 "Unnecessary space before function pointer name\n" . $herecurr);
4692 }
4693
4694# unnecessary space "type (*funcptr )(args...)"
4695 if (defined $post_funcname_space &&
4696 $post_funcname_space =~ /^\s/) {
4697 WARN("SPACING",
4698 "Unnecessary space after function pointer name\n" . $herecurr);
4699 }
4700
4701# unnecessary space "type (*funcptr) (args...)"
4702 if (defined $pre_args_space &&
4703 $pre_args_space =~ /^\s/) {
4704 WARN("SPACING",
4705 "Unnecessary space before function pointer arguments\n" . $herecurr);
4706 }
4707
4708 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004709 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004710 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004711 }
4712 }
4713
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004714# check for spacing round square brackets; allowed:
4715# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004716# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4717# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004718 while ($line =~ /(.*?\s)\[/g) {
4719 my ($where, $prefix) = ($-[1], $1);
4720 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004721 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004722 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004723 if (ERROR("BRACKET_SPACE",
4724 "space prohibited before open square bracket '['\n" . $herecurr) &&
4725 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004726 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004727 s/^(\+.*?)\s+\[/$1\[/;
4728 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004729 }
4730 }
4731
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004732# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004733 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004734 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004735 my $ctx_before = substr($line, 0, $-[1]);
4736 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004737
4738 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004739 if ($name =~ /^(?:
4740 if|for|while|switch|return|case|
4741 volatile|__volatile__|
4742 __attribute__|format|__extension__|
4743 asm|__asm__)$/x)
4744 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004745 # cpp #define statements have non-optional spaces, ie
4746 # if there is a space between the name and the open
4747 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004748 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004749
4750 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004751 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004752
4753 # If this whole things ends with a type its most
4754 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004755 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004756
4757 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004758 if (WARN("SPACING",
4759 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4760 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004761 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004762 s/\b$name\s+\(/$name\(/;
4763 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004764 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004765 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004766
Andy Whitcroft653d4872007-06-23 17:16:34 -07004767# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004768 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004769 my $fixed_line = "";
4770 my $line_fixed = 0;
4771
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004772 my $ops = qr{
4773 <<=|>>=|<=|>=|==|!=|
4774 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4775 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004776 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004777 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004778 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004779 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004780
4781## print("element count: <" . $#elements . ">\n");
4782## foreach my $el (@elements) {
4783## print("el: <$el>\n");
4784## }
4785
4786 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004787 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004788
Joe Perches3705ce52013-07-03 15:05:31 -07004789 foreach my $el (@elements) {
4790 push(@fix_elements, substr($rawline, $off, length($el)));
4791 $off += length($el);
4792 }
4793
4794 $off = 0;
4795
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004796 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004797 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004798
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004799 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004800
4801 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4802
4803## print("n: <$n> good: <$good>\n");
4804
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004805 $off += length($elements[$n]);
4806
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004807 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004808 my $ca = substr($opline, 0, $off);
4809 my $cc = '';
4810 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4811 $cc = substr($opline, $off + length($elements[$n + 1]));
4812 }
4813 my $cb = "$ca$;$cc";
4814
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004815 my $a = '';
4816 $a = 'V' if ($elements[$n] ne '');
4817 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004818 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004819 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4820 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004821 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004822
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004823 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004824
4825 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004826 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004827 $c = 'V' if ($elements[$n + 2] ne '');
4828 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004829 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004830 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4831 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004832 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004833 } else {
4834 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004835 }
4836
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004837 my $ctx = "${a}x${c}";
4838
4839 my $at = "(ctx:$ctx)";
4840
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004841 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004842 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004843
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004844 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004845 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004846
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004847 # Get the full operator variant.
4848 my $opv = $op . substr($curr_vars, $off, 1);
4849
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004850 # Ignore operators passed as parameters.
4851 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004852 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004853
Andy Whitcroftcf655042008-03-04 14:28:20 -08004854# # Ignore comments
4855# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004856
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004857 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004858 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004859 if ($ctx !~ /.x[WEBC]/ &&
4860 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004861 if (ERROR("SPACING",
4862 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004863 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004864 $line_fixed = 1;
4865 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004866 }
4867
4868 # // is a comment
4869 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004870
Joe Perchesb00e4812014-04-03 14:49:33 -07004871 # : when part of a bitfield
4872 } elsif ($opv eq ':B') {
4873 # skip the bitfield test for now
4874
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004875 # No spaces for:
4876 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004877 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004878 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004879 if (ERROR("SPACING",
4880 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004881 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004882 if (defined $fix_elements[$n + 2]) {
4883 $fix_elements[$n + 2] =~ s/^\s+//;
4884 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004885 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004886 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004887 }
4888
Joe Perches23810972014-12-10 15:51:32 -08004889 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004890 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004891 my $rtrim_before = 0;
4892 my $space_after = 0;
4893 if ($ctx =~ /Wx./) {
4894 if (ERROR("SPACING",
4895 "space prohibited before that '$op' $at\n" . $hereptr)) {
4896 $line_fixed = 1;
4897 $rtrim_before = 1;
4898 }
4899 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004900 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004901 if (ERROR("SPACING",
4902 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004903 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004904 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004905 $space_after = 1;
4906 }
4907 }
4908 if ($rtrim_before || $space_after) {
4909 if ($rtrim_before) {
4910 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4911 } else {
4912 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4913 }
4914 if ($space_after) {
4915 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004916 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004917 }
4918
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004919 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004920 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004921 #warn "'*' is part of type\n";
4922
4923 # unary operators should have a space before and
4924 # none after. May be left adjacent to another
4925 # unary operator, or a cast
4926 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004927 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004928 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004929 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004930 if (ERROR("SPACING",
4931 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004932 if ($n != $last_after + 2) {
4933 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4934 $line_fixed = 1;
4935 }
Joe Perches3705ce52013-07-03 15:05:31 -07004936 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004937 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004938 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004939 # A unary '*' may be const
4940
4941 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004942 if (ERROR("SPACING",
4943 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004944 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004945 if (defined $fix_elements[$n + 2]) {
4946 $fix_elements[$n + 2] =~ s/^\s+//;
4947 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004948 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004949 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004950 }
4951
4952 # unary ++ and unary -- are allowed no space on one side.
4953 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004954 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004955 if (ERROR("SPACING",
4956 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004957 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004958 $line_fixed = 1;
4959 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004960 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004961 if ($ctx =~ /Wx[BE]/ ||
4962 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004963 if (ERROR("SPACING",
4964 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004965 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004966 $line_fixed = 1;
4967 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004968 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004969 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004970 if (ERROR("SPACING",
4971 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004972 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004973 if (defined $fix_elements[$n + 2]) {
4974 $fix_elements[$n + 2] =~ s/^\s+//;
4975 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004976 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004977 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004978 }
4979
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004980 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004981 } elsif ($op eq '<<' or $op eq '>>' or
4982 $op eq '&' or $op eq '^' or $op eq '|' or
4983 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004984 $op eq '*' or $op eq '/' or
4985 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004986 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004987 if ($check) {
4988 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4989 if (CHK("SPACING",
4990 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4991 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4992 $fix_elements[$n + 2] =~ s/^\s+//;
4993 $line_fixed = 1;
4994 }
4995 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4996 if (CHK("SPACING",
4997 "space preferred before that '$op' $at\n" . $hereptr)) {
4998 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4999 $line_fixed = 1;
5000 }
5001 }
5002 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005003 if (ERROR("SPACING",
5004 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005005 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5006 if (defined $fix_elements[$n + 2]) {
5007 $fix_elements[$n + 2] =~ s/^\s+//;
5008 }
Joe Perches3705ce52013-07-03 15:05:31 -07005009 $line_fixed = 1;
5010 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005011 }
5012
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005013 # A colon needs no spaces before when it is
5014 # terminating a case value or a label.
5015 } elsif ($opv eq ':C' || $opv eq ':L') {
5016 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07005017 if (ERROR("SPACING",
5018 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005019 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005020 $line_fixed = 1;
5021 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005022 }
5023
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005024 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08005025 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005026 my $ok = 0;
5027
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005028 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005029 if (($op eq '<' &&
5030 $cc =~ /^\S+\@\S+>/) ||
5031 ($op eq '>' &&
5032 $ca =~ /<\S+\@\S+$/))
5033 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07005034 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005035 }
5036
Joe Perchese0df7e12015-04-16 12:44:53 -07005037 # for asm volatile statements
5038 # ignore a colon with another
5039 # colon immediately before or after
5040 if (($op eq ':') &&
5041 ($ca =~ /:$/ || $cc =~ /^:/)) {
5042 $ok = 1;
5043 }
5044
Joe Perches84731622013-11-12 15:10:05 -08005045 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07005046 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07005047 my $msg_level = \&ERROR;
5048 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08005049
Jean Delvare0675a8f2017-09-08 16:16:07 -07005050 if (&{$msg_level}("SPACING",
5051 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07005052 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5053 if (defined $fix_elements[$n + 2]) {
5054 $fix_elements[$n + 2] =~ s/^\s+//;
5055 }
Joe Perches3705ce52013-07-03 15:05:31 -07005056 $line_fixed = 1;
5057 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005058 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005059 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005060 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07005061
5062## print("n: <$n> GOOD: <$good>\n");
5063
5064 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005065 }
Joe Perches3705ce52013-07-03 15:05:31 -07005066
5067 if (($#elements % 2) == 0) {
5068 $fixed_line = $fixed_line . $fix_elements[$#elements];
5069 }
5070
Joe Perches194f66f2014-08-06 16:11:03 -07005071 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5072 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07005073 }
5074
5075
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005076 }
5077
Joe Perches786b6322013-07-03 15:05:32 -07005078# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08005079 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07005080 if (WARN("SPACING",
5081 "space prohibited before semicolon\n" . $herecurr) &&
5082 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005083 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07005084 s/^(\+.*\S)\s+;/$1;/;
5085 }
5086 }
5087
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005088# check for multiple assignments
5089 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005090 CHK("MULTIPLE_ASSIGNMENTS",
5091 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005092 }
5093
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005094## # check for multiple declarations, allowing for a function declaration
5095## # continuation.
5096## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
5097## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
5098##
5099## # Remove any bracketed sections to ensure we do not
Dwaipayan Raye73d2712020-12-15 20:44:56 -08005100## # falsely report the parameters of functions.
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005101## my $ln = $line;
5102## while ($ln =~ s/\([^\(\)]*\)//g) {
5103## }
5104## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005105## WARN("MULTIPLE_DECLARATION",
5106## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005107## }
5108## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005109
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005110#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07005111 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07005112 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005113 if (ERROR("SPACING",
5114 "space required before the open brace '{'\n" . $herecurr) &&
5115 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07005116 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07005117 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005118 }
5119
Joe Perchesc4a62ef2013-07-03 15:05:28 -07005120## # check for blank lines before declarations
5121## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5122## $prevrawline =~ /^.\s*$/) {
5123## WARN("SPACING",
5124## "No blank lines before declarations\n" . $hereprev);
5125## }
5126##
5127
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005128# closing brace should have a space following it when it has anything
5129# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07005130 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005131 if (ERROR("SPACING",
5132 "space required after that close brace '}'\n" . $herecurr) &&
5133 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005134 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07005135 s/}((?!(?:,|;|\)))\S)/} $1/;
5136 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005137 }
5138
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005139# check spacing on square brackets
5140 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005141 if (ERROR("SPACING",
5142 "space prohibited after that open square bracket '['\n" . $herecurr) &&
5143 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005144 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005145 s/\[\s+/\[/;
5146 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005147 }
5148 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005149 if (ERROR("SPACING",
5150 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
5151 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005152 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005153 s/\s+\]/\]/;
5154 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005155 }
5156
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005157# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005158 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
5159 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005160 if (ERROR("SPACING",
5161 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
5162 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005163 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005164 s/\(\s+/\(/;
5165 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005166 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005167 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005168 $line !~ /for\s*\(.*;\s+\)/ &&
5169 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005170 if (ERROR("SPACING",
5171 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
5172 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005173 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005174 s/\s+\)/\)/;
5175 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005176 }
5177
Joe Perchese2826fd2014-08-06 16:10:48 -07005178# check unnecessary parentheses around addressof/dereference single $Lvals
5179# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5180
5181 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08005182 my $var = $1;
5183 if (CHK("UNNECESSARY_PARENTHESES",
5184 "Unnecessary parentheses around $var\n" . $herecurr) &&
5185 $fix) {
5186 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5187 }
5188 }
5189
5190# check for unnecessary parentheses around function pointer uses
5191# ie: (foo->bar)(); should be foo->bar();
5192# but not "if (foo->bar) (" to avoid some false positives
5193 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5194 my $var = $2;
5195 if (CHK("UNNECESSARY_PARENTHESES",
5196 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5197 $fix) {
5198 my $var2 = deparenthesize($var);
5199 $var2 =~ s/\s//g;
5200 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5201 }
5202 }
Joe Perchese2826fd2014-08-06 16:10:48 -07005203
Joe Perches63b7c732017-09-08 16:16:01 -07005204# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08005205# when !drivers/staging or command-line uses --strict
5206 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07005207 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07005208 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
5209 my $if_stat = $1;
5210 my $test = substr($2, 1, -1);
5211 my $herectx;
5212 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
5213 my $match = $1;
5214 # avoid parentheses around potential macro args
5215 next if ($match =~ /^\s*\w+\s*$/);
5216 if (!defined($herectx)) {
5217 $herectx = $here . "\n";
5218 my $cnt = statement_rawlines($if_stat);
5219 for (my $n = 0; $n < $cnt; $n++) {
5220 my $rl = raw_line($linenr, $n);
5221 $herectx .= $rl . "\n";
5222 last if $rl =~ /^[ \+].*\{/;
5223 }
5224 }
5225 CHK("UNNECESSARY_PARENTHESES",
5226 "Unnecessary parentheses around '$match'\n" . $herectx);
5227 }
5228 }
5229
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005230#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005231 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005232 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07005233 if (WARN("INDENTED_LABEL",
5234 "labels should not be indented\n" . $herecurr) &&
5235 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005236 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005237 s/^(.)\s+/$1/;
5238 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005239 }
5240
Joe Perches40873ab2020-10-15 20:11:56 -07005241# check if a statement with a comma should be two statements like:
5242# foo = bar(), /* comma should be semicolon */
5243# bar = baz();
5244 if (defined($stat) &&
5245 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5246 my $cnt = statement_rawlines($stat);
5247 my $herectx = get_stat_here($linenr, $cnt, $here);
5248 WARN("SUSPECT_COMMA_SEMICOLON",
5249 "Possible comma where semicolon could be used\n" . $herectx);
5250 }
5251
Joe Perches5b9553a2014-04-03 14:49:21 -07005252# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08005253 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005254 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07005255 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07005256 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5257 my $value = $1;
5258 $value = deparenthesize($value);
5259 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5260 ERROR("RETURN_PARENTHESES",
5261 "return is not a function, parentheses are not required\n" . $herecurr);
5262 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005263 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005264 ERROR("SPACING",
5265 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005266 }
5267 }
Joe Perches507e5142013-11-12 15:10:13 -08005268
Joe Perchesb43ae212014-06-23 13:22:07 -07005269# unnecessary return in a void function
5270# at end-of-function, with the previous line a single leading tab, then return;
5271# and the line before that not a goto label target like "out:"
5272 if ($sline =~ /^[ \+]}\s*$/ &&
5273 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5274 $linenr >= 3 &&
5275 $lines[$linenr - 3] =~ /^[ +]/ &&
5276 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07005277 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07005278 "void function return statements are not generally useful\n" . $hereprev);
5279 }
Joe Perches9819cf22014-06-04 16:12:09 -07005280
Joe Perches189248d2014-01-23 15:54:47 -08005281# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07005282 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08005283 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5284 my $openparens = $1;
5285 my $count = $openparens =~ tr@\(@\(@;
5286 my $msg = "";
5287 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5288 my $comp = $4; #Not $1 because of $LvalOrFunc
5289 $msg = " - maybe == should be = ?" if ($comp eq "==");
5290 WARN("UNNECESSARY_PARENTHESES",
5291 "Unnecessary parentheses$msg\n" . $herecurr);
5292 }
5293 }
5294
Joe Perchesc5595fa2015-09-09 15:37:58 -07005295# comparisons with a constant or upper case identifier on the left
5296# avoid cases like "foo + BAR < baz"
5297# only fix matches surrounded by parentheses to avoid incorrect
5298# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07005299 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005300 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5301 my $lead = $1;
5302 my $const = $2;
5303 my $comp = $3;
5304 my $to = $4;
5305 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07005306 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005307 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5308 WARN("CONSTANT_COMPARISON",
5309 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5310 $fix) {
5311 if ($comp eq "<") {
5312 $newcomp = ">";
5313 } elsif ($comp eq "<=") {
5314 $newcomp = ">=";
5315 } elsif ($comp eq ">") {
5316 $newcomp = "<";
5317 } elsif ($comp eq ">=") {
5318 $newcomp = "<=";
5319 }
5320 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5321 }
5322 }
5323
Joe Perchesf34e4a42015-04-16 12:44:19 -07005324# Return of what appears to be an errno should normally be negative
5325 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005326 my $name = $1;
5327 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07005328 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07005329 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005330 }
5331 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005332
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005333# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07005334 if ($line =~ /\b(if|while|for|switch)\(/) {
5335 if (ERROR("SPACING",
5336 "space required before the open parenthesis '('\n" . $herecurr) &&
5337 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005338 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005339 s/\b(if|while|for|switch)\(/$1 \(/;
5340 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005341 }
5342
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07005343# Check for illegal assignment in if conditional -- and check for trailing
5344# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005345 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08005346 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5347 ctx_statement_block($linenr, $realcnt, 0)
5348 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005349 my ($stat_next) = ctx_statement_block($line_nr_next,
5350 $remain_next, $off_next);
5351 $stat_next =~ s/\n./\n /g;
5352 ##print "stat<$stat> stat_next<$stat_next>\n";
5353
5354 if ($stat_next =~ /^\s*while\b/) {
5355 # If the statement carries leading newlines,
5356 # then count those as offsets.
5357 my ($whitespace) =
5358 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5359 my $offset =
5360 statement_rawlines($whitespace) - 1;
5361
5362 $suppress_whiletrailers{$line_nr_next +
5363 $offset} = 1;
5364 }
5365 }
5366 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005367 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005368 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005369 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005370
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005371 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches65b64b32020-08-11 18:35:10 -07005372 if (ERROR("ASSIGN_IN_IF",
5373 "do not use assignment in if condition\n" . $herecurr) &&
5374 $fix && $perl_version_ok) {
5375 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5376 my $space = $1;
5377 my $not = $2;
5378 my $statement = $3;
5379 my $assigned = $4;
5380 my $test = $8;
5381 my $against = $9;
5382 my $brace = $15;
5383 fix_delete_line($fixlinenr, $rawline);
5384 fix_insert_line($fixlinenr, "$space$statement;");
5385 my $newline = "${space}if (";
5386 $newline .= '!' if defined($not);
5387 $newline .= '(' if (defined $not && defined($test) && defined($against));
5388 $newline .= "$assigned";
5389 $newline .= " $test $against" if (defined($test) && defined($against));
5390 $newline .= ')' if (defined $not && defined($test) && defined($against));
5391 $newline .= ')';
5392 $newline .= " {" if (defined($brace));
5393 fix_insert_line($fixlinenr + 1, $newline);
5394 }
5395 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005396 }
5397
5398 # Find out what is on the end of the line after the
5399 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005400 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005401 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005402 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005403 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5404 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005405 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005406 # Find out how long the conditional actually is.
5407 my @newlines = ($c =~ /\n/gs);
5408 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005409 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005410
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005411 $stat_real = raw_line($linenr, $cond_lines)
5412 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005413 if (defined($stat_real) && $cond_lines > 1) {
5414 $stat_real = "[...]\n$stat_real";
5415 }
5416
Joe Perches000d1cc12011-07-25 17:13:25 -07005417 ERROR("TRAILING_STATEMENTS",
5418 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005419 }
5420 }
5421
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005422# Check for bitwise tests written as boolean
5423 if ($line =~ /
5424 (?:
5425 (?:\[|\(|\&\&|\|\|)
5426 \s*0[xX][0-9]+\s*
5427 (?:\&\&|\|\|)
5428 |
5429 (?:\&\&|\|\|)
5430 \s*0[xX][0-9]+\s*
5431 (?:\&\&|\|\||\)|\])
5432 )/x)
5433 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005434 WARN("HEXADECIMAL_BOOLEAN_TEST",
5435 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005436 }
5437
Andy Whitcroft8905a672007-11-28 16:21:06 -08005438# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005439 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5440 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005441 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005442 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005443 ERROR("TRAILING_STATEMENTS",
5444 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005445 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005446 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005447# if should not continue a brace
5448 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005449 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005450 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005451 $herecurr);
5452 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005453# case and default should not have general statements after them
5454 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5455 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005456 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005457 \s*return\s+
5458 )/xg)
5459 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005460 ERROR("TRAILING_STATEMENTS",
5461 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005462 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005463
5464 # Check for }<nl>else {, these must be at the same
5465 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005466 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5467 $previndent == $indent) {
5468 if (ERROR("ELSE_AFTER_BRACE",
5469 "else should follow close brace '}'\n" . $hereprev) &&
5470 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5471 fix_delete_line($fixlinenr - 1, $prevrawline);
5472 fix_delete_line($fixlinenr, $rawline);
5473 my $fixedline = $prevrawline;
5474 $fixedline =~ s/}\s*$//;
5475 if ($fixedline !~ /^\+\s*$/) {
5476 fix_insert_line($fixlinenr, $fixedline);
5477 }
5478 $fixedline = $rawline;
5479 $fixedline =~ s/^(.\s*)else/$1} else/;
5480 fix_insert_line($fixlinenr, $fixedline);
5481 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005482 }
5483
Joe Perches8b8856f2014-08-06 16:11:14 -07005484 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5485 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005486 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5487
5488 # Find out what is on the end of the line after the
5489 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005490 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005491 $s =~ s/\n.*//g;
5492
5493 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005494 if (ERROR("WHILE_AFTER_BRACE",
5495 "while should follow close brace '}'\n" . $hereprev) &&
5496 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5497 fix_delete_line($fixlinenr - 1, $prevrawline);
5498 fix_delete_line($fixlinenr, $rawline);
5499 my $fixedline = $prevrawline;
5500 my $trailing = $rawline;
5501 $trailing =~ s/^\+//;
5502 $trailing = trim($trailing);
5503 $fixedline =~ s/}\s*$/} $trailing/;
5504 fix_insert_line($fixlinenr, $fixedline);
5505 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005506 }
5507 }
5508
Joe Perches95e2c602013-07-03 15:05:20 -07005509#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005510 while ($line =~ m{($Constant|$Lval)}g) {
5511 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005512
Joe Perches95e2c602013-07-03 15:05:20 -07005513#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005514 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005515 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Łukasz Stelmach4104a202020-12-15 20:44:27 -08005516#Ignore some autogenerated defines and enum values
5517 $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005518#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005519 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005520#Ignore SI style variants like nS, mV and dB
5521#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5522 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005523#Ignore some three character SI units explicitly, like MiB and KHz
5524 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005525 while ($var =~ m{($Ident)}g) {
5526 my $word = $1;
5527 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005528 if ($check) {
5529 seed_camelcase_includes();
5530 if (!$file && !$camelcase_file_seeded) {
5531 seed_camelcase_file($realfile);
5532 $camelcase_file_seeded = 1;
5533 }
5534 }
Joe Perches7e781f62013-09-11 14:23:55 -07005535 if (!defined $camelcase{$word}) {
5536 $camelcase{$word} = 1;
5537 CHK("CAMELCASE",
5538 "Avoid CamelCase: <$word>\n" . $herecurr);
5539 }
Joe Perches34456862013-07-03 15:05:34 -07005540 }
Joe Perches323c1262012-12-17 16:02:07 -08005541 }
5542 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005543
5544#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005545 if ($line =~ /\#\s*define.*\\\s+$/) {
5546 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5547 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5548 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005549 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005550 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005551 }
5552
Fabian Frederick0e212e02015-04-16 12:44:25 -07005553# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5554# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005555 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005556 my $file = "$1.h";
5557 my $checkfile = "include/linux/$file";
5558 if (-f "$root/$checkfile" &&
5559 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005560 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005561 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005562 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5563 if ($asminclude > 0) {
5564 if ($realfile =~ m{^arch/}) {
5565 CHK("ARCH_INCLUDE_LINUX",
5566 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5567 } else {
5568 WARN("INCLUDE_LINUX",
5569 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5570 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005571 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005572 }
5573 }
5574
Andy Whitcroft653d4872007-06-23 17:16:34 -07005575# multi-statement macros should be enclosed in a do while loop, grab the
5576# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005577# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005578 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5579 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005580 my $ln = $linenr;
5581 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005582 my ($off, $dstat, $dcond, $rest);
5583 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005584 my $has_flow_statement = 0;
5585 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005586 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005587 ctx_statement_block($linenr, $realcnt, 0);
5588 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005589 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005590 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005591
Joe Perches08a28432014-10-13 15:51:55 -07005592 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005593 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005594
Joe Perchesf59b64b2016-10-11 13:52:08 -07005595 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5596 my $define_args = $1;
5597 my $define_stmt = $dstat;
5598 my @def_args = ();
5599
5600 if (defined $define_args && $define_args ne "") {
5601 $define_args = substr($define_args, 1, length($define_args) - 2);
5602 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005603 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005604 @def_args = split(",", $define_args);
5605 }
5606
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005607 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005608 $dstat =~ s/\\\n.//g;
5609 $dstat =~ s/^\s*//s;
5610 $dstat =~ s/\s*$//s;
5611
5612 # Flatten any parentheses and braces
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005613 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5614 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5615 $dstat =~ s/.\[[^\[\]]*\]/1u/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005616 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005617 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005618
Antonio Borneo342d3d22020-04-06 20:11:01 -07005619 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005620 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5621 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005622 {
5623 }
5624
Joe Perches42e15292016-03-15 14:58:01 -07005625 # Make asm volatile uses seem like a generic function
5626 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5627
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005628 my $exceptions = qr{
5629 $Declare|
5630 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005631 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005632 DECLARE_PER_CPU|
5633 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005634 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005635 union|
5636 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005637 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005638 ^\"|\"$|
5639 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005640 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005641 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005642
5643 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005644 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005645 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005646
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005647 if ($dstat ne '' &&
5648 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5649 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005650 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005651 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005652 $dstat !~ /$exceptions/ &&
5653 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005654 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005655 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005656 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005657 $dstat !~ /^for\s*$Constant$/ && # for (...)
5658 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5659 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005660 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005661 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005662 {
Joe Perchese7955562017-05-08 15:55:48 -07005663 if ($dstat =~ /^\s*if\b/) {
5664 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5665 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5666 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005667 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5668 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5669 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005670 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005671 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005672 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005673
5674 }
Joe Perches52076492016-10-11 13:52:14 -07005675
5676 # Make $define_stmt single line, comment-free, etc
5677 my @stmt_array = split('\n', $define_stmt);
5678 my $first = 1;
5679 $define_stmt = "";
5680 foreach my $l (@stmt_array) {
5681 $l =~ s/\\$//;
5682 if ($first) {
5683 $define_stmt = $l;
5684 $first = 0;
5685 } elsif ($l =~ /^[\+ ]/) {
5686 $define_stmt .= substr($l, 1);
5687 }
5688 }
5689 $define_stmt =~ s/$;//g;
5690 $define_stmt =~ s/\s+/ /g;
5691 $define_stmt = trim($define_stmt);
5692
Joe Perchesf59b64b2016-10-11 13:52:08 -07005693# check if any macro arguments are reused (ignore '...' and 'type')
5694 foreach my $arg (@def_args) {
5695 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005696 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005697 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005698 $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 -07005699 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5700 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005701 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005702 if ($use_cnt > 1) {
5703 CHK("MACRO_ARG_REUSE",
5704 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005705 }
5706# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005707 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005708 ((defined($1) && $1 ne ',') ||
5709 (defined($2) && $2 ne ','))) {
5710 CHK("MACRO_ARG_PRECEDENCE",
5711 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005712 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005713 }
Joe Perches5023d342012-12-17 16:01:47 -08005714
Joe Perches08a28432014-10-13 15:51:55 -07005715# check for macros with flow control, but without ## concatenation
5716# ## concatenation is commonly a macro that defines a function so ignore those
5717 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005718 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005719 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005720
Joe Perches08a28432014-10-13 15:51:55 -07005721 WARN("MACRO_WITH_FLOW_CONTROL",
5722 "Macros with flow control statements should be avoided\n" . "$herectx");
5723 }
5724
Joe Perches481eb482012-12-17 16:01:56 -08005725# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005726
5727 } else {
5728 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005729 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5730 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005731 $line =~ /^\+.*\\$/) {
5732 WARN("LINE_CONTINUATIONS",
5733 "Avoid unnecessary line continuations\n" . $herecurr);
5734 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005735 }
5736
Joe Perchesb13edf72012-07-30 14:41:24 -07005737# do {} while (0) macro tests:
5738# single-statement macros do not need to be enclosed in do while (0) loop,
5739# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005740 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005741 $realfile !~ m@/vmlinux.lds.h$@ &&
5742 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5743 my $ln = $linenr;
5744 my $cnt = $realcnt;
5745 my ($off, $dstat, $dcond, $rest);
5746 my $ctx = '';
5747 ($dstat, $dcond, $ln, $cnt, $off) =
5748 ctx_statement_block($linenr, $realcnt, 0);
5749 $ctx = $dstat;
5750
5751 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005752 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005753
5754 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5755 my $stmts = $2;
5756 my $semis = $3;
5757
5758 $ctx =~ s/\n*$//;
5759 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005760 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005761
Joe Perchesac8e97f2012-08-21 16:15:53 -07005762 if (($stmts =~ tr/;/;/) == 1 &&
5763 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005764 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5765 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5766 }
5767 if (defined $semis && $semis ne "") {
5768 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5769 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5770 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005771 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5772 $ctx =~ s/\n*$//;
5773 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005774 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005775
5776 WARN("TRAILING_SEMICOLON",
5777 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005778 }
5779 }
5780
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005781# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005782 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5783 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005784 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005785 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005786 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5787 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005788 my @allowed = ();
5789 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005790 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005791 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005792 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005793 for my $chunk (@chunks) {
5794 my ($cond, $block) = @{$chunk};
5795
Andy Whitcroft773647a2008-03-28 14:15:58 -07005796 # If the condition carries leading newlines, then count those as offsets.
5797 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5798 my $offset = statement_rawlines($whitespace) - 1;
5799
Joe Perchesaad4f612012-03-23 15:02:19 -07005800 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005801 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5802
5803 # We have looked at and allowed this specific line.
5804 $suppress_ifbraces{$ln + $offset} = 1;
5805
5806 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005807 $ln += statement_rawlines($block) - 1;
5808
Andy Whitcroft773647a2008-03-28 14:15:58 -07005809 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005810
5811 $seen++ if ($block =~ /^\s*{/);
5812
Joe Perchesaad4f612012-03-23 15:02:19 -07005813 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005814 if (statement_lines($cond) > 1) {
5815 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005816 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005817 }
5818 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005819 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005820 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005821 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005822 if (statement_block_size($block) > 1) {
5823 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005824 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005825 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005826 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005827 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005828 if ($seen) {
5829 my $sum_allowed = 0;
5830 foreach (@allowed) {
5831 $sum_allowed += $_;
5832 }
5833 if ($sum_allowed == 0) {
5834 WARN("BRACES",
5835 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5836 } elsif ($sum_allowed != $allow &&
5837 $seen != $allow) {
5838 CHK("BRACES",
5839 "braces {} should be used on all arms of this statement\n" . $herectx);
5840 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005841 }
5842 }
5843 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005844 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005845 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005846 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005847
Andy Whitcroftcf655042008-03-04 14:28:20 -08005848 # Check the pre-context.
5849 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5850 #print "APW: ALLOWED: pre<$1>\n";
5851 $allowed = 1;
5852 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005853
5854 my ($level, $endln, @chunks) =
5855 ctx_statement_full($linenr, $realcnt, $-[0]);
5856
Andy Whitcroftcf655042008-03-04 14:28:20 -08005857 # Check the condition.
5858 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005859 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005860 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005861 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005862 }
5863 if (statement_lines($cond) > 1) {
5864 #print "APW: ALLOWED: cond<$cond>\n";
5865 $allowed = 1;
5866 }
5867 if ($block =~/\b(?:if|for|while)\b/) {
5868 #print "APW: ALLOWED: block<$block>\n";
5869 $allowed = 1;
5870 }
5871 if (statement_block_size($block) > 1) {
5872 #print "APW: ALLOWED: lines block<$block>\n";
5873 $allowed = 1;
5874 }
5875 # Check the post-context.
5876 if (defined $chunks[1]) {
5877 my ($cond, $block) = @{$chunks[1]};
5878 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005879 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005880 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005881 if ($block =~ /^\s*\{/) {
5882 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5883 $allowed = 1;
5884 }
5885 }
5886 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005887 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005888 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005889
Joe Perches000d1cc12011-07-25 17:13:25 -07005890 WARN("BRACES",
5891 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005892 }
5893 }
5894
Joe Perchese4c5bab2017-02-24 15:01:41 -08005895# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005896 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5897 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005898 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5899 }
5900
Joe Perches0979ae62012-12-17 16:01:59 -08005901# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005902 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005903 if (CHK("BRACES",
5904 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5905 $fix && $prevrawline =~ /^\+/) {
5906 fix_delete_line($fixlinenr - 1, $prevrawline);
5907 }
Joe Perches0979ae62012-12-17 16:01:59 -08005908 }
Joe Perches77b9a532013-07-03 15:05:29 -07005909 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005910 if (CHK("BRACES",
5911 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5912 $fix) {
5913 fix_delete_line($fixlinenr, $rawline);
5914 }
Joe Perches0979ae62012-12-17 16:01:59 -08005915 }
5916
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005917# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005918 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5919 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005920 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005921 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005922 }
5923
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005924# Check for user-visible strings broken across lines, which breaks the ability
5925# to grep for the string. Make exceptions when the previous string ends in a
5926# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5927# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005928 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005929 $prevline =~ /"\s*$/ &&
5930 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5931 if (WARN("SPLIT_STRING",
5932 "quoted string split across lines\n" . $hereprev) &&
5933 $fix &&
5934 $prevrawline =~ /^\+.*"\s*$/ &&
5935 $last_coalesced_string_linenr != $linenr - 1) {
5936 my $extracted_string = get_quoted_string($line, $rawline);
5937 my $comma_close = "";
5938 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5939 $comma_close = $1;
5940 }
5941
5942 fix_delete_line($fixlinenr - 1, $prevrawline);
5943 fix_delete_line($fixlinenr, $rawline);
5944 my $fixedline = $prevrawline;
5945 $fixedline =~ s/"\s*$//;
5946 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5947 fix_insert_line($fixlinenr - 1, $fixedline);
5948 $fixedline = $rawline;
5949 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5950 if ($fixedline !~ /\+\s*$/) {
5951 fix_insert_line($fixlinenr, $fixedline);
5952 }
5953 $last_coalesced_string_linenr = $linenr;
5954 }
5955 }
5956
5957# check for missing a space in a string concatenation
5958 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5959 WARN('MISSING_SPACE',
5960 "break quoted strings at a space character\n" . $hereprev);
5961 }
5962
Joe Perchese4b7d302017-05-08 15:55:51 -07005963# check for an embedded function name in a string when the function is known
5964# This does not work very well for -f --file checking as it depends on patch
5965# context providing the function name or a single line form for in-file
5966# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005967 if ($line =~ /^\+.*$String/ &&
5968 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005969 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5970 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005971 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005972 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005973 }
5974
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005975# check for spaces before a quoted newline
5976 if ($rawline =~ /^.*\".*\s\\n/) {
5977 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5978 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5979 $fix) {
5980 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5981 }
5982
5983 }
5984
Joe Perchesf17dba42014-10-13 15:51:51 -07005985# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005986 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5987 if (CHK("CONCATENATED_STRING",
5988 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5989 $fix) {
5990 while ($line =~ /($String)/g) {
5991 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5992 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5993 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5994 }
5995 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005996 }
5997
Joe Perches90ad30e2014-12-10 15:51:59 -08005998# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005999 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07006000 if (WARN("STRING_FRAGMENTS",
6001 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
6002 $fix) {
6003 while ($line =~ /($String)(?=\s*")/g) {
6004 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6005 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
6006 }
6007 }
Joe Perches90ad30e2014-12-10 15:51:59 -08006008 }
6009
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006010# check for non-standard and hex prefixed decimal printf formats
6011 my $show_L = 1; #don't show the same defect twice
6012 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006013 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006014 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006015 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006016 # check for %L
6017 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006018 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006019 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6020 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006021 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08006022 # check for %Z
6023 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6024 WARN("PRINTF_Z",
6025 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6026 $show_Z = 0;
6027 }
6028 # check for 0x<decimal>
6029 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6030 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07006031 "Prefixing 0x with decimal output is defective\n" . $herecurr);
6032 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006033 }
6034
6035# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08006036 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08006037 WARN("LINE_CONTINUATIONS",
6038 "Avoid line continuations in quoted strings\n" . $herecurr);
6039 }
6040
Andy Whitcroft00df3442007-06-08 13:47:06 -07006041# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006042 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07006043 WARN("IF_0",
6044 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
6045 }
6046
6047# warn about #if 1
6048 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
6049 WARN("IF_1",
6050 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006051 }
6052
Andy Whitcroft03df4b52012-12-17 16:01:52 -08006053# check for needless "if (<foo>) fn(<foo>)" uses
6054 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07006055 my $tested = quotemeta($1);
6056 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6057 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6058 my $func = $1;
6059 if (WARN('NEEDLESS_IF',
6060 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6061 $fix) {
6062 my $do_fix = 1;
6063 my $leading_tabs = "";
6064 my $new_leading_tabs = "";
6065 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6066 $leading_tabs = $1;
6067 } else {
6068 $do_fix = 0;
6069 }
6070 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6071 $new_leading_tabs = $1;
6072 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6073 $do_fix = 0;
6074 }
6075 } else {
6076 $do_fix = 0;
6077 }
6078 if ($do_fix) {
6079 fix_delete_line($fixlinenr - 1, $prevrawline);
6080 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6081 }
6082 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07006083 }
6084 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006085
Joe Perchesebfdc402014-08-06 16:10:27 -07006086# check for unnecessary "Out of Memory" messages
6087 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6088 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6089 (defined $1 || defined $3) &&
6090 $linenr > 3) {
6091 my $testval = $2;
6092 my $testline = $lines[$linenr - 3];
6093
6094 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6095# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6096
Joe Perchese29a70f2019-03-07 16:28:35 -08006097 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6098 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07006099 WARN("OOM_MESSAGE",
6100 "Possible unnecessary 'out of memory' message\n" . $hereprev);
6101 }
6102 }
6103
Joe Perchesf78d98f2014-10-13 15:52:01 -07006104# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08006105 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07006106 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6107 my $level = $1;
6108 if (WARN("UNNECESSARY_KERN_LEVEL",
6109 "Possible unnecessary $level\n" . $herecurr) &&
6110 $fix) {
6111 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
6112 }
6113 }
6114
Joe Perches45c55e92017-02-24 15:01:31 -08006115# check for logging continuations
6116 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
6117 WARN("LOGGING_CONTINUATION",
6118 "Avoid logging continuation uses where feasible\n" . $herecurr);
6119 }
6120
Dwaipayan Ray70eb2272020-12-15 20:45:15 -08006121# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
6122 if (defined $stat &&
6123 $line =~ /\b$logFunctions\s*\(/ &&
6124 index($stat, '"') >= 0) {
6125 my $lc = $stat =~ tr@\n@@;
6126 $lc = $lc + $linenr;
6127 my $stat_real = get_stat_real($linenr, $lc);
6128 pos($stat_real) = index($stat_real, '"');
6129 while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
6130 my $pspec = $1;
6131 my $h = $2;
6132 my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
6133 if (WARN("UNNECESSARY_MODIFIER",
6134 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
6135 $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
6136 my $nspec = $pspec;
6137 $nspec =~ s/h//g;
6138 $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
6139 }
6140 }
6141 }
6142
Joe Perchesabb08a52014-12-10 15:51:46 -08006143# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07006144 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08006145 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6146 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6147 WARN("MASK_THEN_SHIFT",
6148 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6149 }
6150
Joe Perchesb75ac612014-12-10 15:52:02 -08006151# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07006152 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08006153 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6154 my $val = $1;
6155 my $equal = "!";
6156 $equal = "" if ($4 eq "!=");
6157 if (CHK("COMPARISON_TO_NULL",
6158 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6159 $fix) {
6160 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6161 }
6162 }
6163 }
6164
Joe Perches8716de32013-09-11 14:24:05 -07006165# check for bad placement of section $InitAttribute (e.g.: __initdata)
6166 if ($line =~ /(\b$InitAttribute\b)/) {
6167 my $attr = $1;
6168 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
6169 my $ptr = $1;
6170 my $var = $2;
6171 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
6172 ERROR("MISPLACED_INIT",
6173 "$attr should be placed after $var\n" . $herecurr)) ||
6174 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
6175 WARN("MISPLACED_INIT",
6176 "$attr should be placed after $var\n" . $herecurr))) &&
6177 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006178 $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 -07006179 }
6180 }
6181 }
6182
Joe Perchese970b8842013-11-12 15:10:10 -08006183# check for $InitAttributeData (ie: __initdata) with const
6184 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6185 my $attr = $1;
6186 $attr =~ /($InitAttributePrefix)(.*)/;
6187 my $attr_prefix = $1;
6188 my $attr_type = $2;
6189 if (ERROR("INIT_ATTRIBUTE",
6190 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6191 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006192 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006193 s/$InitAttributeData/${attr_prefix}initconst/;
6194 }
6195 }
6196
6197# check for $InitAttributeConst (ie: __initconst) without const
6198 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6199 my $attr = $1;
6200 if (ERROR("INIT_ATTRIBUTE",
6201 "Use of $attr requires a separate use of const\n" . $herecurr) &&
6202 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006203 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006204 /(^\+\s*(?:static\s+))/;
6205 $lead = rtrim($1);
6206 $lead = "$lead " if ($lead !~ /^\+$/);
6207 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07006208 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08006209 }
6210 }
6211
Joe Perchesc17893c2015-04-16 12:44:42 -07006212# check for __read_mostly with const non-pointer (should just be const)
6213 if ($line =~ /\b__read_mostly\b/ &&
6214 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6215 if (ERROR("CONST_READ_MOSTLY",
6216 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6217 $fix) {
6218 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6219 }
6220 }
6221
Joe Perchesfbdb8132014-04-03 14:49:14 -07006222# don't use __constant_<foo> functions outside of include/uapi/
6223 if ($realfile !~ m@^include/uapi/@ &&
6224 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6225 my $constant_func = $1;
6226 my $func = $constant_func;
6227 $func =~ s/^__constant_//;
6228 if (WARN("CONSTANT_CONVERSION",
6229 "$constant_func should be $func\n" . $herecurr) &&
6230 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006231 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07006232 }
6233 }
6234
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006235# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08006236 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07006237 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006238 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07006239 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006240 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006241 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07006242 }
6243 if ($delay > 2000) {
6244 WARN("LONG_UDELAY",
6245 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006246 }
6247 }
6248
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006249# warn about unexpectedly long msleep's
6250 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6251 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006252 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006253 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006254 }
6255 }
6256
Joe Perches36ec1932013-07-03 15:05:25 -07006257# check for comparisons of jiffies
6258 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6259 WARN("JIFFIES_COMPARISON",
6260 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6261 }
6262
Joe Perches9d7a34a2013-07-03 15:05:26 -07006263# check for comparisons of get_jiffies_64()
6264 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6265 WARN("JIFFIES_COMPARISON",
6266 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6267 }
6268
Andy Whitcroft00df3442007-06-08 13:47:06 -07006269# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006270# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07006271# print "#ifdef in C files should be avoided\n";
6272# print "$herecurr";
6273# $clean = 0;
6274# }
6275
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006276# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006277 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07006278 if (ERROR("SPACING",
6279 "exactly one space required after that #$1\n" . $herecurr) &&
6280 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006281 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07006282 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6283 }
6284
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006285 }
6286
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006287# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006288 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6289 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006290 my $which = $1;
6291 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006292 CHK("UNCOMMENTED_DEFINITION",
6293 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006294 }
6295 }
6296# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006297
6298 my $barriers = qr{
6299 mb|
6300 rmb|
Will Deaconad83ec62019-11-07 14:49:00 +00006301 wmb
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006302 }x;
6303 my $barrier_stems = qr{
6304 mb__before_atomic|
6305 mb__after_atomic|
6306 store_release|
6307 load_acquire|
6308 store_mb|
6309 (?:$barriers)
6310 }x;
6311 my $all_barriers = qr{
6312 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02006313 smp_(?:$barrier_stems)|
6314 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006315 }x;
6316
6317 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006318 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08006319 WARN("MEMORY_BARRIER",
6320 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006321 }
6322 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006323
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02006324 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6325
6326 if ($realfile !~ m@^include/asm-generic/@ &&
6327 $realfile !~ m@/barrier\.h$@ &&
6328 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6329 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6330 WARN("MEMORY_BARRIER",
6331 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6332 }
6333
Joe Perchescb426e92015-06-25 15:02:46 -07006334# check for waitqueue_active without a comment.
6335 if ($line =~ /\bwaitqueue_active\s*\(/) {
6336 if (!ctx_has_comment($first_line, $linenr)) {
6337 WARN("WAITQUEUE_ACTIVE",
6338 "waitqueue_active without comment\n" . $herecurr);
6339 }
6340 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006341
Marco Elver5099a722020-04-01 12:17:14 +02006342# check for data_race without a comment.
6343 if ($line =~ /\bdata_race\s*\(/) {
6344 if (!ctx_has_comment($first_line, $linenr)) {
6345 WARN("DATA_RACE",
6346 "data_race without comment\n" . $herecurr);
6347 }
6348 }
6349
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006350# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006351 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006352 CHK("ARCH_DEFINES",
6353 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006354 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006355
Joe Perches596ed452017-07-12 14:37:02 -07006356# check that the storage class is not after a type
6357 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006358 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07006359 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6360 }
6361# Check that the storage class is at the beginning of a declaration
6362 if ($line =~ /\b$Storage\b/ &&
6363 $line !~ /^.\s*$Storage/ &&
6364 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6365 $1 !~ /[\,\)]\s*$/) {
6366 WARN("STORAGE_CLASS",
6367 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07006368 }
6369
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006370# check the location of the inline attribute, that it is between
6371# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006372 if ($line =~ /\b$Type\s+$Inline\b/ ||
6373 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006374 ERROR("INLINE_LOCATION",
6375 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006376 }
6377
Andy Whitcroft8905a672007-11-28 16:21:06 -08006378# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08006379 if ($realfile !~ m@\binclude/uapi/@ &&
6380 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006381 if (WARN("INLINE",
6382 "plain inline is preferred over $1\n" . $herecurr) &&
6383 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006384 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006385
6386 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006387 }
6388
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006389# Check for compiler attributes
Joe Perches2b7ab452013-11-12 15:10:14 -08006390 if ($realfile !~ m@\binclude/uapi/@ &&
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006391 $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
6392 my $attr = $1;
6393 $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
Joe Perches3d130fd2011-01-12 17:00:00 -08006394
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006395 my %attr_list = (
Joe Perches0830aab2020-12-15 20:44:50 -08006396 "alias" => "__alias",
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006397 "aligned" => "__aligned",
6398 "always_inline" => "__always_inline",
6399 "assume_aligned" => "__assume_aligned",
6400 "cold" => "__cold",
6401 "const" => "__attribute_const__",
6402 "copy" => "__copy",
6403 "designated_init" => "__designated_init",
6404 "externally_visible" => "__visible",
6405 "format" => "printf|scanf",
6406 "gnu_inline" => "__gnu_inline",
6407 "malloc" => "__malloc",
6408 "mode" => "__mode",
6409 "no_caller_saved_registers" => "__no_caller_saved_registers",
6410 "noclone" => "__noclone",
6411 "noinline" => "noinline",
6412 "nonstring" => "__nonstring",
6413 "noreturn" => "__noreturn",
6414 "packed" => "__packed",
6415 "pure" => "__pure",
Joe Perches339f29d2020-12-15 20:44:43 -08006416 "section" => "__section",
Joe Perches0830aab2020-12-15 20:44:50 -08006417 "used" => "__used",
6418 "weak" => "__weak"
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006419 );
Joe Perches39b7e282011-07-25 17:13:24 -07006420
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006421 while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
Joe Perches339f29d2020-12-15 20:44:43 -08006422 my $orig_attr = $1;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006423 my $params = '';
6424 $params = $2 if defined($2);
Joe Perches339f29d2020-12-15 20:44:43 -08006425 my $curr_attr = $orig_attr;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006426 $curr_attr =~ s/^[\s_]+|[\s_]+$//g;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006427 if (exists($attr_list{$curr_attr})) {
Joe Perches339f29d2020-12-15 20:44:43 -08006428 my $new = $attr_list{$curr_attr};
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006429 if ($curr_attr eq "format" && $params) {
6430 $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
Joe Perches339f29d2020-12-15 20:44:43 -08006431 $new = "__$1\($2";
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006432 } else {
Joe Perches339f29d2020-12-15 20:44:43 -08006433 $new = "$new$params";
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006434 }
Joe Perches339f29d2020-12-15 20:44:43 -08006435 if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6436 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
6437 $fix) {
6438 my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6439 $fixed[$fixlinenr] =~ s/$remove//;
6440 $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6441 $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6442 $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
6443 }
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006444 }
6445 }
6446
6447 # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
6448 if ($attr =~ /^_*unused/) {
6449 WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6450 "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
Joe Perchesd5e616f2013-09-11 14:23:54 -07006451 }
Joe Perches6061d942012-03-23 15:02:16 -07006452 }
6453
Joe Perches619a9082014-12-10 15:51:35 -08006454# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006455 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006456 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6457 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6458 $line =~ /\b__weak\b/)) {
6459 ERROR("WEAK_DECLARATION",
6460 "Using weak declarations can have unintended link defects\n" . $herecurr);
6461 }
6462
Tomas Winklerfd39f902016-12-12 16:46:34 -08006463# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006464 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006465 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006466 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6467 my $type = $1;
6468 if ($type =~ /\b($typeC99Typedefs)\b/) {
6469 $type = $1;
6470 my $kernel_type = 'u';
6471 $kernel_type = 's' if ($type =~ /^_*[si]/);
6472 $type =~ /(\d+)/;
6473 $kernel_type .= $1;
6474 if (CHK("PREFER_KERNEL_TYPES",
6475 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6476 $fix) {
6477 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6478 }
6479 }
6480 }
6481
Joe Perches938224b2016-01-20 14:59:15 -08006482# check for cast of C90 native int or longer types constants
6483 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6484 my $cast = $1;
6485 my $const = $2;
6486 if (WARN("TYPECAST_INT_CONSTANT",
6487 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6488 $fix) {
6489 my $suffix = "";
6490 my $newconst = $const;
6491 $newconst =~ s/${Int_type}$//;
6492 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6493 if ($cast =~ /\blong\s+long\b/) {
6494 $suffix .= 'LL';
6495 } elsif ($cast =~ /\blong\b/) {
6496 $suffix .= 'L';
6497 }
6498 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6499 }
6500 }
6501
Joe Perches8f53a9b2010-03-05 13:43:48 -08006502# check for sizeof(&)
6503 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006504 WARN("SIZEOF_ADDRESS",
6505 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006506 }
6507
Joe Perches66c80b62012-07-30 14:41:22 -07006508# check for sizeof without parenthesis
6509 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006510 if (WARN("SIZEOF_PARENTHESIS",
6511 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6512 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006513 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006514 }
Joe Perches66c80b62012-07-30 14:41:22 -07006515 }
6516
Joe Perches88982fe2012-12-17 16:02:00 -08006517# check for struct spinlock declarations
6518 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6519 WARN("USE_SPINLOCK_T",
6520 "struct spinlock should be spinlock_t\n" . $herecurr);
6521 }
6522
Joe Perchesa6962d72013-04-29 16:18:13 -07006523# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006524 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006525 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006526 $fmt =~ s/%%//g;
6527 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006528 if (WARN("PREFER_SEQ_PUTS",
6529 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6530 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006531 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006532 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006533 }
6534 }
6535
Joe Perches478b1792018-04-10 16:33:34 -07006536# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006537 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006538 defined $stat &&
6539 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6540 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006541 my $stat_real;
6542
Joe Perches0b523762017-05-08 15:55:36 -07006543 my $lc = $stat =~ tr@\n@@;
6544 $lc = $lc + $linenr;
6545 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006546 my $specifier;
6547 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006548 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006549 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006550 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6551 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006552
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006553 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006554 $specifier = $1;
6555 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006556 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006557 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006558 ($extension eq "f" &&
6559 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006560 $bad_specifier = $specifier;
6561 last;
6562 }
6563 if ($extension eq "x" && !defined($stat_real)) {
6564 if (!defined($stat_real)) {
6565 $stat_real = get_stat_real($linenr, $lc);
6566 }
6567 WARN("VSPRINTF_SPECIFIER_PX",
6568 "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");
6569 }
6570 }
6571 if ($bad_specifier ne "") {
6572 my $stat_real = get_stat_real($linenr, $lc);
6573 my $ext_type = "Invalid";
6574 my $use = "";
6575 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006576 $use = " - use %pS instead";
6577 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6578 }
6579
6580 WARN("VSPRINTF_POINTER_EXTENSION",
6581 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6582 }
Joe Perches0b523762017-05-08 15:55:36 -07006583 }
6584 }
6585
Andy Whitcroft554e1652012-01-10 15:09:57 -08006586# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006587 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006588 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006589 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006590
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006591 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006592 my $ms_val = $7;
6593 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006594
Andy Whitcroft554e1652012-01-10 15:09:57 -08006595 if ($ms_size =~ /^(0x|)0$/i) {
6596 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006597 "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 -08006598 } elsif ($ms_size =~ /^(0x|)1$/i) {
6599 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006600 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6601 }
6602 }
6603
Joe Perches98a9bba2014-01-23 15:54:52 -08006604# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006605# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006606# defined $stat &&
6607# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6608# if (WARN("PREFER_ETHER_ADDR_COPY",
6609# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6610# $fix) {
6611# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6612# }
6613# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006614
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006615# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006616# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006617# defined $stat &&
6618# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6619# WARN("PREFER_ETHER_ADDR_EQUAL",
6620# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6621# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006622
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006623# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6624# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006625# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006626# defined $stat &&
6627# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6628#
6629# my $ms_val = $7;
6630#
6631# if ($ms_val =~ /^(?:0x|)0+$/i) {
6632# if (WARN("PREFER_ETH_ZERO_ADDR",
6633# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6634# $fix) {
6635# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6636# }
6637# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6638# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6639# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6640# $fix) {
6641# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6642# }
6643# }
6644# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006645
Joe Perches5dbdb2d2020-12-29 15:14:31 -08006646# strlcpy uses that should likely be strscpy
6647 if ($line =~ /\bstrlcpy\s*\(/) {
6648 WARN("STRLCPY",
6649 "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr);
6650 }
6651
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006652# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006653 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006654 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006655 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006656 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006657 my $call = $1;
6658 my $cast1 = deparenthesize($2);
6659 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006660 my $cast2 = deparenthesize($7);
6661 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006662 my $cast;
6663
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006664 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006665 $cast = "$cast1 or $cast2";
6666 } elsif ($cast1 ne "") {
6667 $cast = $cast1;
6668 } else {
6669 $cast = $cast2;
6670 }
6671 WARN("MINMAX",
6672 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006673 }
6674 }
6675
Joe Perches4a273192012-07-30 14:41:20 -07006676# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006677 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006678 defined $stat &&
6679 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6680 my $min = $1;
6681 my $max = $7;
6682 if ($min eq $max) {
6683 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006684 "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 -07006685 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6686 $min > $max) {
6687 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006688 "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 -07006689 }
6690 }
6691
Joe Perches823b7942013-11-12 15:10:15 -08006692# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006693 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006694 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006695 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006696 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6697 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6698 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6699 my $lc = $stat =~ tr@\n@@;
6700 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006701 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006702 WARN("NAKED_SSCANF",
6703 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6704 }
6705
Joe Perchesafc819a2014-06-04 16:12:08 -07006706# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006707 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006708 defined $stat &&
6709 $line =~ /\bsscanf\b/) {
6710 my $lc = $stat =~ tr@\n@@;
6711 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006712 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006713 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6714 my $format = $6;
6715 my $count = $format =~ tr@%@%@;
6716 if ($count == 1 &&
6717 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6718 WARN("SSCANF_TO_KSTRTO",
6719 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6720 }
6721 }
6722 }
6723
Joe Perches70dc8a42013-09-11 14:23:58 -07006724# check for new externs in .h files.
6725 if ($realfile =~ /\.h$/ &&
6726 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006727 if (CHK("AVOID_EXTERNS",
6728 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006729 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006730 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006731 }
6732 }
6733
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006734# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006735 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006736 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006737 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006738 my $function_name = $1;
6739 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006740
6741 my $s = $stat;
6742 if (defined $cond) {
6743 substr($s, 0, length($cond), '');
6744 }
Kees Cookd8b44b52020-06-03 14:18:09 -07006745 if ($s =~ /^\s*;/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006746 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006747 WARN("AVOID_EXTERNS",
6748 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006749 }
6750
6751 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006752 WARN("FUNCTION_ARGUMENTS",
6753 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006754 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006755
6756 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6757 $stat =~ /^.\s*extern\s+/)
6758 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006759 WARN("AVOID_EXTERNS",
6760 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006761 }
6762
Joe Perchesa0ad7592017-07-10 15:52:19 -07006763# check for function declarations that have arguments without identifier names
6764 if (defined $stat &&
Kees Cookd8b44b52020-06-03 14:18:09 -07006765 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6766 $1 ne "void") {
6767 my $args = trim($1);
Joe Perchesca0d8922016-10-11 13:52:16 -07006768 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6769 my $arg = trim($1);
Kees Cookd8b44b52020-06-03 14:18:09 -07006770 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006771 WARN("FUNCTION_ARGUMENTS",
6772 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6773 }
6774 }
6775 }
6776
Joe Perchesa0ad7592017-07-10 15:52:19 -07006777# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006778 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006779 defined $stat &&
6780 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6781 $context_function = $1;
6782
6783# check for multiline function definition with misplaced open brace
6784 my $ok = 0;
6785 my $cnt = statement_rawlines($stat);
6786 my $herectx = $here . "\n";
6787 for (my $n = 0; $n < $cnt; $n++) {
6788 my $rl = raw_line($linenr, $n);
6789 $herectx .= $rl . "\n";
6790 $ok = 1 if ($rl =~ /^[ \+]\{/);
6791 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6792 last if $rl =~ /^[ \+].*\{/;
6793 }
6794 if (!$ok) {
6795 ERROR("OPEN_BRACE",
6796 "open brace '{' following function definitions go on the next line\n" . $herectx);
6797 }
6798 }
6799
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006800# checks for new __setup's
6801 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6802 my $name = $1;
6803
6804 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006805 CHK("UNDOCUMENTED_SETUP",
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07006806 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006807 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006808 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006809
Joe Perchese29a70f2019-03-07 16:28:35 -08006810# check for pointless casting of alloc functions
6811 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006812 WARN("UNNECESSARY_CASTS",
6813 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006814 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006815
Joe Perchesa640d252013-07-03 15:05:21 -07006816# alloc style
6817# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006818 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006819 $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 -07006820 CHK("ALLOC_SIZEOF_STRUCT",
6821 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6822 }
6823
Joe Perches60a55362014-06-04 16:12:07 -07006824# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006825 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006826 defined $stat &&
6827 $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 -07006828 my $oldfunc = $3;
6829 my $a1 = $4;
6830 my $a2 = $10;
6831 my $newfunc = "kmalloc_array";
6832 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006833 my $r1 = $a1;
6834 my $r2 = $a2;
6835 if ($a1 =~ /^sizeof\s*\S/) {
6836 $r1 = $a2;
6837 $r2 = $a1;
6838 }
6839 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6840 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006841 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006842 my $herectx = get_stat_here($linenr, $cnt, $here);
6843
Joe Perches60a55362014-06-04 16:12:07 -07006844 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006845 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6846 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006847 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006848 $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 -07006849 }
6850 }
6851 }
6852
Joe Perches972fdea2013-04-29 16:18:12 -07006853# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006854 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006855 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6856 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006857 WARN("KREALLOC_ARG_REUSE",
6858 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6859 }
6860
Joe Perches5ce59ae2013-02-21 16:44:18 -08006861# check for alloc argument mismatch
6862 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6863 WARN("ALLOC_ARRAY_ARGS",
6864 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6865 }
6866
Joe Perchescaf2a542011-01-12 16:59:56 -08006867# check for multiple semicolons
6868 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006869 if (WARN("ONE_SEMICOLON",
6870 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6871 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006872 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006873 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006874 }
6875
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006876# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6877 if ($realfile !~ m@^include/uapi/@ &&
6878 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006879 my $ull = "";
6880 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6881 if (CHK("BIT_MACRO",
6882 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6883 $fix) {
6884 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6885 }
6886 }
6887
Joe Perches50161262020-08-11 18:35:07 -07006888# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006889 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
Joe Perches50161262020-08-11 18:35:07 -07006890 WARN("IS_ENABLED_CONFIG",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006891 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
Joe Perches50161262020-08-11 18:35:07 -07006892 }
6893
Joe Perches2d632742016-05-20 17:04:00 -07006894# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006895 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 -07006896 my $config = $1;
6897 if (WARN("PREFER_IS_ENABLED",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006898 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
Joe Perches2d632742016-05-20 17:04:00 -07006899 $fix) {
6900 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6901 }
6902 }
6903
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006904# check for /* fallthrough */ like comment, prefer fallthrough;
6905 my @fallthroughs = (
6906 'fallthrough',
6907 '@fallthrough@',
6908 'lint -fallthrough[ \t]*',
6909 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6910 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6911 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6912 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6913 );
6914 if ($raw_comment ne '') {
6915 foreach my $ft (@fallthroughs) {
6916 if ($raw_comment =~ /$ft/) {
6917 my $msg_level = \&WARN;
6918 $msg_level = \&CHK if ($file);
6919 &{$msg_level}("PREFER_FALLTHROUGH",
6920 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6921 last;
6922 }
6923 }
6924 }
6925
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006926# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006927 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006928 defined $stat &&
6929 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006930 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006931 my $herectx = get_stat_here($linenr, $cnt, $here);
6932
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006933 WARN("DEFAULT_NO_BREAK",
6934 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006935 }
6936
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006937# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006938 if ($line =~ /\b__FUNCTION__\b/) {
6939 if (WARN("USE_FUNC",
6940 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6941 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006942 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006943 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006944 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006945
Joe Perches62ec8182015-02-13 14:38:18 -08006946# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6947 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6948 ERROR("DATE_TIME",
6949 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6950 }
6951
Joe Perches2c924882012-03-23 15:02:20 -07006952# check for use of yield()
6953 if ($line =~ /\byield\s*\(\s*\)/) {
6954 WARN("YIELD",
6955 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6956 }
6957
Joe Perches179f8f42013-07-03 15:05:30 -07006958# check for comparisons against true and false
6959 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6960 my $lead = $1;
6961 my $arg = $2;
6962 my $test = $3;
6963 my $otype = $4;
6964 my $trail = $5;
6965 my $op = "!";
6966
6967 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6968
6969 my $type = lc($otype);
6970 if ($type =~ /^(?:true|false)$/) {
6971 if (("$test" eq "==" && "$type" eq "true") ||
6972 ("$test" eq "!=" && "$type" eq "false")) {
6973 $op = "";
6974 }
6975
6976 CHK("BOOL_COMPARISON",
6977 "Using comparison to $otype is error prone\n" . $herecurr);
6978
6979## maybe suggesting a correct construct would better
6980## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6981
6982 }
6983 }
6984
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006985# check for semaphores initialized locked
6986 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006987 WARN("CONSIDER_COMPLETION",
6988 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006989 }
Joe Perches6712d852012-03-23 15:02:20 -07006990
Joe Perches67d0a072011-10-31 17:13:10 -07006991# recommend kstrto* over simple_strto* and strict_strto*
6992 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006993 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006994 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006995 }
Joe Perches6712d852012-03-23 15:02:20 -07006996
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006997# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006998 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006999 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07007000 "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 -07007001 }
Joe Perches6712d852012-03-23 15:02:20 -07007002
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08007003# check for spin_is_locked(), suggest lockdep instead
7004 if ($line =~ /\bspin_is_locked\(/) {
7005 WARN("USE_LOCKDEP",
7006 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
7007 }
7008
Joe Perches9189c7e2018-09-07 15:26:18 -07007009# check for deprecated apis
7010 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
7011 my $deprecated_api = $1;
7012 my $new_api = $deprecated_apis{$deprecated_api};
7013 WARN("DEPRECATED_API",
7014 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
7015 }
7016
Joe Perches0f3c5aa2015-02-13 14:39:05 -08007017# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07007018# and avoid what seem like struct definitions 'struct foo {'
Quentin Monnetced69da12020-08-11 18:35:13 -07007019 if (defined($const_structs) &&
7020 $line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07007021 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007022 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07007023 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08007024 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07007025
7026# use of NR_CPUS is usually wrong
7027# ignore definitions of NR_CPUS and usage to define arrays as likely right
7028 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07007029 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7030 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07007031 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7032 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
7033 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07007034 {
Joe Perches000d1cc12011-07-25 17:13:25 -07007035 WARN("NR_CPUS",
7036 "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 -07007037 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07007038
Joe Perches52ea8502013-11-12 15:10:09 -08007039# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
7040 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
7041 ERROR("DEFINE_ARCH_HAS",
7042 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
7043 }
7044
Joe Perchesacd93622015-02-13 14:38:38 -08007045# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07007046 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08007047 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7048 WARN("LIKELY_MISUSE",
7049 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7050 }
7051
Denis Efremovde3f1862019-09-25 16:49:25 -07007052# nested likely/unlikely calls
7053 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7054 WARN("LIKELY_MISUSE",
7055 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7056 }
7057
Andy Whitcroft691d77b2009-01-06 14:41:16 -08007058# whine mightly about in_atomic
7059 if ($line =~ /\bin_atomic\s*\(/) {
7060 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007061 ERROR("IN_ATOMIC",
7062 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08007063 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007064 WARN("IN_ATOMIC",
7065 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08007066 }
7067 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01007068
7069# check for lockdep_set_novalidate_class
7070 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
7071 $line =~ /__lockdep_no_validate__\s*\)/ ) {
7072 if ($realfile !~ m@^kernel/lockdep@ &&
7073 $realfile !~ m@^include/linux/lockdep@ &&
7074 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007075 ERROR("LOCKDEP",
7076 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01007077 }
7078 }
Dave Jones88f88312011-01-12 16:59:59 -08007079
Joe Perchesb392c642015-04-16 12:44:16 -07007080 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7081 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007082 WARN("EXPORTED_WORLD_WRITABLE",
7083 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08007084 }
Joe Perches24358802014-04-03 14:49:13 -07007085
Joe Perches00180462018-02-06 15:38:55 -08007086# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
7087# and whether or not function naming is typical and if
7088# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07007089 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08007090 defined $stat &&
7091 $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*\)/) {
7092 my $var = $1;
7093 my $perms = $2;
7094 my $show = $3;
7095 my $store = $4;
7096 my $octal_perms = perms_to_octal($perms);
7097 if ($show =~ /^${var}_show$/ &&
7098 $store =~ /^${var}_store$/ &&
7099 $octal_perms eq "0644") {
7100 if (WARN("DEVICE_ATTR_RW",
7101 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
7102 $fix) {
7103 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
7104 }
7105 } elsif ($show =~ /^${var}_show$/ &&
7106 $store =~ /^NULL$/ &&
7107 $octal_perms eq "0444") {
7108 if (WARN("DEVICE_ATTR_RO",
7109 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
7110 $fix) {
7111 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
7112 }
7113 } elsif ($show =~ /^NULL$/ &&
7114 $store =~ /^${var}_store$/ &&
7115 $octal_perms eq "0200") {
7116 if (WARN("DEVICE_ATTR_WO",
7117 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
7118 $fix) {
7119 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
7120 }
7121 } elsif ($octal_perms eq "0644" ||
7122 $octal_perms eq "0444" ||
7123 $octal_perms eq "0200") {
7124 my $newshow = "$show";
7125 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
7126 my $newstore = $store;
7127 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
7128 my $rename = "";
7129 if ($show ne $newshow) {
7130 $rename .= " '$show' to '$newshow'";
7131 }
7132 if ($store ne $newstore) {
7133 $rename .= " '$store' to '$newstore'";
7134 }
7135 WARN("DEVICE_ATTR_FUNCTIONS",
7136 "Consider renaming function(s)$rename\n" . $herecurr);
7137 } else {
7138 WARN("DEVICE_ATTR_PERMS",
7139 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
7140 }
7141 }
7142
Joe Perches515a2352014-04-03 14:49:24 -07007143# Mode permission misuses where it seems decimal should be octal
7144# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08007145# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
7146# specific definition of not visible in sysfs.
7147# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
7148# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07007149 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07007150 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07007151 $line =~ /$mode_perms_search/) {
7152 foreach my $entry (@mode_permission_funcs) {
7153 my $func = $entry->[0];
7154 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07007155
Joe Perches459cf0a2016-10-11 13:52:19 -07007156 my $lc = $stat =~ tr@\n@@;
7157 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07007158 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07007159
Joe Perches515a2352014-04-03 14:49:24 -07007160 my $skip_args = "";
7161 if ($arg_pos > 1) {
7162 $arg_pos--;
7163 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
7164 }
Joe Perchesf90774e2016-10-11 13:51:47 -07007165 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07007166 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07007167 my $val = $1;
7168 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08007169 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
7170 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
7171 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07007172 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07007173 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07007174 }
7175 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08007176 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07007177 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07007178 }
Joe Perches24358802014-04-03 14:49:13 -07007179 }
7180 }
7181 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007182
Joe Perches459cf0a2016-10-11 13:52:19 -07007183# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07007184 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08007185 my $oval = $1;
7186 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07007187 if (WARN("SYMBOLIC_PERMS",
7188 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7189 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08007190 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07007191 }
7192 }
7193
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007194# validate content of MODULE_LICENSE against list from include/linux/module.h
7195 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
7196 my $extracted_string = get_quoted_string($line, $rawline);
7197 my $valid_licenses = qr{
7198 GPL|
7199 GPL\ v2|
7200 GPL\ and\ additional\ rights|
7201 Dual\ BSD/GPL|
7202 Dual\ MIT/GPL|
7203 Dual\ MPL/GPL|
7204 Proprietary
7205 }x;
7206 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
7207 WARN("MODULE_LICENSE",
7208 "unknown module license " . $extracted_string . "\n" . $herecurr);
7209 }
7210 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07007211
7212# check for sysctl duplicate constants
7213 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
7214 WARN("DUPLICATED_SYSCTL_CONST",
7215 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
7216 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007217 }
7218
7219 # If we have no input at all, then there is nothing to report on
7220 # so just keep quiet.
7221 if ($#rawlines == -1) {
7222 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007223 }
7224
Andy Whitcroft8905a672007-11-28 16:21:06 -08007225 # In mailback mode only produce a report in the negative, for
7226 # things that appear to be patches.
7227 if ($mailback && ($clean == 1 || !$is_patch)) {
7228 exit(0);
7229 }
7230
Dwaipayan Raye73d2712020-12-15 20:44:56 -08007231 # This is not a patch, and we are in 'no-patch' mode so
Andy Whitcroft8905a672007-11-28 16:21:06 -08007232 # just keep quiet.
7233 if (!$chk_patch && !$is_patch) {
7234 exit(0);
7235 }
7236
Stafford Hornea08ffbe2017-10-03 16:16:51 -07007237 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007238 ERROR("NOT_UNIFIED_DIFF",
7239 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007240 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007241 if ($is_patch && $has_commit_log && $chk_signoff) {
7242 if ($signoff == 0) {
7243 ERROR("MISSING_SIGN_OFF",
7244 "Missing Signed-off-by: line(s)\n");
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07007245 } elsif ($authorsignoff != 1) {
7246 # authorsignoff values:
7247 # 0 -> missing sign off
7248 # 1 -> sign off identical
7249 # 2 -> names and addresses match, comments mismatch
7250 # 3 -> addresses match, names different
7251 # 4 -> names match, addresses different
7252 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
7253
7254 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
7255
7256 if ($authorsignoff == 0) {
7257 ERROR("NO_AUTHOR_SIGN_OFF",
7258 "Missing Signed-off-by: line by nominal patch author '$author'\n");
7259 } elsif ($authorsignoff == 2) {
7260 CHK("FROM_SIGN_OFF_MISMATCH",
7261 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7262 } elsif ($authorsignoff == 3) {
7263 WARN("FROM_SIGN_OFF_MISMATCH",
7264 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7265 } elsif ($authorsignoff == 4) {
7266 WARN("FROM_SIGN_OFF_MISMATCH",
7267 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7268 } elsif ($authorsignoff == 5) {
7269 WARN("FROM_SIGN_OFF_MISMATCH",
7270 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7271 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007272 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007273 }
7274
Andy Whitcroft8905a672007-11-28 16:21:06 -08007275 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007276 if ($summary && !($clean == 1 && $quiet == 1)) {
7277 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08007278 print "total: $cnt_error errors, $cnt_warn warnings, " .
7279 (($check)? "$cnt_chk checks, " : "") .
7280 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07007281 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08007282
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007283 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07007284 # If there were any defects found and not already fixing them
7285 if (!$clean and !$fix) {
7286 print << "EOM"
7287
7288NOTE: For some of the reported defects, checkpatch may be able to
7289 mechanically convert to the typical style using --fix or --fix-inplace.
7290EOM
7291 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007292 # If there were whitespace errors which cleanpatch can fix
7293 # then suggest that.
7294 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07007295 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07007296 print << "EOM"
7297
7298NOTE: Whitespace errors detected.
7299 You may wish to use scripts/cleanpatch or scripts/cleanfile
7300EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007301 }
7302 }
7303
Joe Perchesd752fcc2014-08-06 16:11:05 -07007304 if ($clean == 0 && $fix &&
7305 ("@rawlines" ne "@fixed" ||
7306 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08007307 my $newfile = $filename;
7308 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07007309 my $linecount = 0;
7310 my $f;
7311
Joe Perchesd752fcc2014-08-06 16:11:05 -07007312 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7313
Joe Perches3705ce52013-07-03 15:05:31 -07007314 open($f, '>', $newfile)
7315 or die "$P: Can't open $newfile for write\n";
7316 foreach my $fixed_line (@fixed) {
7317 $linecount++;
7318 if ($file) {
7319 if ($linecount > 3) {
7320 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07007321 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07007322 }
7323 } else {
7324 print $f $fixed_line . "\n";
7325 }
7326 }
7327 close($f);
7328
7329 if (!$quiet) {
7330 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07007331
Joe Perches3705ce52013-07-03 15:05:31 -07007332Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
7333
7334Do _NOT_ trust the results written to this file.
7335Do _NOT_ submit these changes without inspecting them for correctness.
7336
7337This EXPERIMENTAL file is simply a convenience to help rewrite patches.
7338No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07007339EOM
7340 }
7341 }
7342
Joe Perchesd8469f12015-06-25 15:03:00 -07007343 if ($quiet == 0) {
7344 print "\n";
7345 if ($clean == 1) {
7346 print "$vname has no obvious style problems and is ready for submission.\n";
7347 } else {
7348 print "$vname has style problems, please review.\n";
7349 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007350 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007351 return $clean;
7352}