blob: 7dc094445d8398d538562a8579a65ba83b51a8e3 [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Joe Perches882ea1d2018-06-07 17:04:33 -07002# SPDX-License-Identifier: GPL-2.0
3#
Dave Jonesdbf004d2010-01-12 16:59:52 -05004# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07005# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08006# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07007# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Joe Perches882ea1d2018-06-07 17:04:33 -07008# (c) 2010-2018 Joe Perches <joe@perches.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07009
10use strict;
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020011use warnings;
Joe Perchesc707a812013-07-08 16:00:43 -070012use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080013use File::Basename;
14use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070015use Term::ANSIColor qw(:constants);
Geert Uytterhoevencd261492018-08-21 21:57:40 -070016use Encode qw(decode encode);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
18my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080019my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070020
Joe Perches000d1cc12011-07-25 17:13:25 -070021my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070022
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
26my $tree = 1;
27my $chk_signoff = 1;
28my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070029my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080031my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070032my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070034my $git = 0;
Joe Perches0dea9f1e2016-05-20 17:04:19 -070035my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070037my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080038my $summary = 1;
39my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080040my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070041my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070042my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070043my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080044my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070045my $root;
Joe Perches0f7f6352020-10-24 16:59:04 -070046my $gitroot = $ENV{'GIT_DIR'};
47$gitroot = ".git" if !defined($gitroot);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080048my %debug;
Joe Perches34456862013-07-03 15:05:34 -070049my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070050my %use_type = ();
51my @use = ();
52my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070053my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070054my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070055my $configuration_file = ".checkpatch.conf";
Joe Perchesbdc48fa2020-05-29 16:12:21 -070056my $max_line_length = 100;
Dave Hansend62a2012013-09-11 14:23:56 -070057my $ignore_perl_version = 0;
58my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070059my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070060my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070061my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070062my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070063my $conststructsfile = "$D/const_structs.checkpatch";
Quentin Monnetced69da12020-08-11 18:35:13 -070064my $typedefsfile;
John Brooks737c0762017-07-10 15:52:24 -070065my $color = "auto";
Vadim Bendebury98005e82019-03-07 16:28:38 -080066my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
Joe Perchesdbbf8692019-09-25 16:46:52 -070067# git output parsing needs US English output, so first set backtick child process LANGUAGE
68my $git_command ='export LANGUAGE=en_US.UTF-8; git';
Antonio Borneo713a09d2020-04-06 20:11:07 -070069my $tabsize = 8;
Jerome Forissier3e89ad82020-10-15 20:11:49 -070070my ${CONFIG_} = "CONFIG_";
Hannes Eder77f5b102009-09-21 17:04:37 -070071
72sub help {
73 my ($exitcode) = @_;
74
75 print << "EOM";
76Usage: $P [OPTION]... [FILE]...
77Version: $V
78
79Options:
80 -q, --quiet quiet
81 --no-tree run without a kernel tree
82 --no-signoff do not check for 'Signed-off-by' line
83 --patch treat FILE as patchfile (default)
84 --emacs emacs compile window format
85 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070086 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070087 -g, --git treat FILE as a single commit or git revision range
88 single git commit with:
89 <rev>
90 <rev>^
91 <rev>~n
92 multiple git commits with:
93 <rev1>..<rev2>
94 <rev1>...<rev2>
95 <rev>-<count>
96 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070097 -f, --file treat FILE as regular source file
98 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070099 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -0700100 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -0700101 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -0700102 --show-types show the specific message type in the output
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700103 --max-line-length=n set the maximum line length, (default $max_line_length)
104 if exceeded, warn on patches
105 requires --strict for use with --file
Vadim Bendebury56193272014-10-13 15:51:48 -0700106 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700107 --tab-size=n set the number of spaces for tab (default $tabsize)
Hannes Eder77f5b102009-09-21 17:04:37 -0700108 --root=PATH PATH to the kernel tree root
109 --no-summary suppress the per-file summary
110 --mailback only produce a report in case of warnings/errors
111 --summary-file include the filename in summary
112 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
113 'values', 'possible', 'type', and 'attr' (default
114 is all off)
115 --test-only=WORD report only warnings/errors containing WORD
116 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700117 --fix EXPERIMENTAL - may create horrible results
118 If correctable single-line errors exist, create
119 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
120 with potential errors corrected to the preferred
121 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800122 --fix-inplace EXPERIMENTAL - may create horrible results
123 Is the same as --fix, but overwrites the input
124 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700125 --ignore-perl-version override checking of perl version. expect
126 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700127 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700128 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700129 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700130 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700131 --color[=WHEN] Use colors 'always', 'never', or only when output
132 is a terminal ('auto'). Default is 'auto'.
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700133 --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default
134 ${CONFIG_})
Hannes Eder77f5b102009-09-21 17:04:37 -0700135 -h, --help, --version display this help and exit
136
137When FILE is - read standard input.
138EOM
139
140 exit($exitcode);
141}
142
Joe Perches3beb42e2016-05-20 17:04:14 -0700143sub uniq {
144 my %seen;
145 return grep { !$seen{$_}++ } @_;
146}
147
148sub list_types {
149 my ($exitcode) = @_;
150
151 my $count = 0;
152
153 local $/ = undef;
154
155 open(my $script, '<', abs_path($P)) or
156 die "$P: Can't read '$P' $!\n";
157
158 my $text = <$script>;
159 close($script);
160
161 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700162 # Also catch when type or level is passed through a variable
163 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700164 push (@types, $_);
165 }
166 @types = sort(uniq(@types));
167 print("#\tMessage type\n\n");
168 foreach my $type (@types) {
169 print(++$count . "\t" . $type . "\n");
170 }
171
172 exit($exitcode);
173}
174
Joe Perches000d1cc12011-07-25 17:13:25 -0700175my $conf = which_conf($configuration_file);
176if (-f $conf) {
177 my @conf_args;
178 open(my $conffile, '<', "$conf")
179 or warn "$P: Can't find a readable $configuration_file file $!\n";
180
181 while (<$conffile>) {
182 my $line = $_;
183
184 $line =~ s/\s*\n?$//g;
185 $line =~ s/^\s*//g;
186 $line =~ s/\s+/ /g;
187
188 next if ($line =~ m/^\s*#/);
189 next if ($line =~ m/^\s*$/);
190
191 my @words = split(" ", $line);
192 foreach my $word (@words) {
193 last if ($word =~ m/^#/);
194 push (@conf_args, $word);
195 }
196 }
197 close($conffile);
198 unshift(@ARGV, @conf_args) if @conf_args;
199}
200
John Brooks737c0762017-07-10 15:52:24 -0700201# Perl's Getopt::Long allows options to take optional arguments after a space.
202# Prevent --color by itself from consuming other arguments
203foreach (@ARGV) {
204 if ($_ eq "--color" || $_ eq "-color") {
205 $_ = "--color=$color";
206 }
207}
208
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700209GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700210 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700211 'tree!' => \$tree,
212 'signoff!' => \$chk_signoff,
213 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700214 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800215 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700216 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700217 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700218 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700219 'subjective!' => \$check,
220 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700221 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700222 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700223 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700224 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800225 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700226 'min-conf-desc-length=i' => \$min_conf_desc_length,
Antonio Borneo713a09d2020-04-06 20:11:07 -0700227 'tab-size=i' => \$tabsize,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700228 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800229 'summary!' => \$summary,
230 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800231 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700232 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800233 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700234 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800235 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700236 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700237 'codespell!' => \$codespell,
238 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700239 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700240 'color=s' => \$color,
241 'no-color' => \$color, #keep old behaviors of -nocolor
242 'nocolor' => \$color, #keep old behaviors of -nocolor
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700243 'kconfig-prefix=s' => \${CONFIG_},
Hannes Eder77f5b102009-09-21 17:04:37 -0700244 'h|help' => \$help,
245 'version' => \$help
246) or help(1);
247
248help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700249
Joe Perches3beb42e2016-05-20 17:04:14 -0700250list_types(0) if ($list_types);
251
Joe Perches9624b8d2014-01-23 15:54:44 -0800252$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700253$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800254
Joe Perches32f30ca2020-06-04 16:50:40 -0700255die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
256
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700257my $exit = 0;
258
Joe Perches5b579802018-08-21 21:57:33 -0700259my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700260if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700261 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700262 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700263 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700264}
265
Allen Hubbe45107ff2016-08-02 14:04:47 -0700266#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700267if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700268 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700269}
270
John Brooks737c0762017-07-10 15:52:24 -0700271if ($color =~ /^[01]$/) {
272 $color = !$color;
273} elsif ($color =~ /^always$/i) {
274 $color = 1;
275} elsif ($color =~ /^never$/i) {
276 $color = 0;
277} elsif ($color =~ /^auto$/i) {
278 $color = (-t STDOUT);
279} else {
Joe Perches32f30ca2020-06-04 16:50:40 -0700280 die "$P: Invalid color mode: $color\n";
John Brooks737c0762017-07-10 15:52:24 -0700281}
282
Antonio Borneo713a09d2020-04-06 20:11:07 -0700283# skip TAB size 1 to avoid additional checks on $tabsize - 1
Joe Perches32f30ca2020-06-04 16:50:40 -0700284die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
Antonio Borneo713a09d2020-04-06 20:11:07 -0700285
Joe Perches91bfe482013-09-11 14:23:59 -0700286sub hash_save_array_words {
287 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700288
Joe Perches91bfe482013-09-11 14:23:59 -0700289 my @array = split(/,/, join(',', @$arrayRef));
290 foreach my $word (@array) {
291 $word =~ s/\s*\n?$//g;
292 $word =~ s/^\s*//g;
293 $word =~ s/\s+/ /g;
294 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700295
Joe Perches91bfe482013-09-11 14:23:59 -0700296 next if ($word =~ m/^\s*#/);
297 next if ($word =~ m/^\s*$/);
298
299 $hashRef->{$word}++;
300 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700301}
302
Joe Perches91bfe482013-09-11 14:23:59 -0700303sub hash_show_words {
304 my ($hashRef, $prefix) = @_;
305
Joe Perches3c816e42015-06-25 15:03:29 -0700306 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700307 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700308 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700309 print " $word";
310 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700311 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700312 }
313}
314
315hash_save_array_words(\%ignore_type, \@ignore);
316hash_save_array_words(\%use_type, \@use);
317
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800318my $dbg_values = 0;
319my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700320my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700321my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800322for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800323 ## no critic
324 eval "\${dbg_$key} = '$debug{$key}';";
325 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800326}
327
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700328my $rpt_cleaners = 0;
329
Andy Whitcroft8905a672007-11-28 16:21:06 -0800330if ($terse) {
331 $emacs = 1;
332 $quiet++;
333}
334
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700335if ($tree) {
336 if (defined $root) {
337 if (!top_of_kernel_tree($root)) {
338 die "$P: $root: --root does not point at a valid tree\n";
339 }
340 } else {
341 if (top_of_kernel_tree('.')) {
342 $root = '.';
343 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
344 top_of_kernel_tree($1)) {
345 $root = $1;
346 }
347 }
348
349 if (!defined $root) {
350 print "Must be run from the top-level dir. of a kernel tree\n";
351 exit(2);
352 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700353}
354
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700355my $emitted_corrupt = 0;
356
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700357our $Ident = qr{
358 [A-Za-z_][A-Za-z\d_]*
359 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
360 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700361our $Storage = qr{extern|static|asmlinkage};
362our $Sparse = qr{
363 __user|
364 __kernel|
365 __force|
366 __iomem|
367 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800368 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700369 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700370 __refconst|
371 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800372 __rcu|
373 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700374 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800375our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
376our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
377our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
378our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
379our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700380
Wolfram Sang52131292010-03-05 13:43:51 -0800381# Notes to $Attribute:
382# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700383our $Attribute = qr{
384 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700385 __percpu|
386 __nocast|
387 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200388 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700389 __packed__|
390 __packed2__|
391 __naked|
392 __maybe_unused|
393 __always_unused|
394 __noreturn|
395 __used|
396 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800397 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700398 __noclone|
399 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700400 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700401 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700402 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700403 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700404 ____cacheline_aligned|
405 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800406 ____cacheline_internodealigned_in_smp|
407 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700408 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700409our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700410our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700411our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
412our $Lval = qr{$Ident(?:$Member)*};
413
Joe Perches95e2c602013-07-03 15:05:20 -0700414our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
415our $Binary = qr{(?i)0b[01]+$Int_type?};
416our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
417our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700418our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800419our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800420our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
421our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
422our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800423our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700424our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800425our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700426our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700427our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700428our $Operators = qr{
429 <=|>=|==|!=|
430 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700431 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700432 }x;
433
Joe Perches91cb5192014-04-03 14:49:32 -0700434our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
435
Joe Perchesab7e23f2015-04-16 12:44:22 -0700436our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800437our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700438our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700439our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800440our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700441our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800442our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700443our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800444
Joe Perches15662b32011-10-31 17:13:12 -0700445our $NON_ASCII_UTF8 = qr{
446 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700447 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
448 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
449 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
450 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
451 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
452 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
453}x;
454
Joe Perches15662b32011-10-31 17:13:12 -0700455our $UTF8 = qr{
456 [\x09\x0A\x0D\x20-\x7E] # ASCII
457 | $NON_ASCII_UTF8
458}x;
459
Joe Perchese6176fa2015-06-25 15:02:49 -0700460our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800461our $typeOtherOSTypedefs = qr{(?x:
462 u_(?:char|short|int|long) | # bsd
463 u(?:nchar|short|int|long) # sysv
464)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700465our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700466 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700467 atomic_t
468)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700469our $typeTypedefs = qr{(?x:
470 $typeC99Typedefs\b|
471 $typeOtherOSTypedefs\b|
472 $typeKernelTypedefs\b
473)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700474
Joe Perches6d32f7a2015-11-06 16:31:37 -0800475our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
476
Joe Perches691e6692010-03-05 13:43:51 -0800477our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800478 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700479 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches87bd4992017-11-17 15:28:48 -0800480 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700481 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700482 panic|
Joe Perches06668722013-11-12 15:10:07 -0800483 MODULE_[A-Z_]+|
484 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800485)};
486
Joe Perchese29a70f2019-03-07 16:28:35 -0800487our $allocFunctions = qr{(?x:
488 (?:(?:devm_)?
489 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
490 kstrdup(?:_const)? |
491 kmemdup(?:_nul)?) |
Christophe JAILLET461e1562020-04-20 18:13:58 -0700492 (?:\w+)?alloc_skb(?:_ip_align)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800493 # dev_alloc_skb/netdev_alloc_skb, et al
494 dma_alloc_coherent
495)};
496
Joe Perches20112472011-07-25 17:13:23 -0700497our $signature_tags = qr{(?xi:
498 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800499 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700500 Acked-by:|
501 Tested-by:|
502 Reviewed-by:|
503 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700504 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700505 To:|
506 Cc:
507)};
508
Joe Perches18130872014-08-06 16:11:22 -0700509our @typeListMisordered = (
510 qr{char\s+(?:un)?signed},
511 qr{int\s+(?:(?:un)?signed\s+)?short\s},
512 qr{int\s+short(?:\s+(?:un)?signed)},
513 qr{short\s+int(?:\s+(?:un)?signed)},
514 qr{(?:un)?signed\s+int\s+short},
515 qr{short\s+(?:un)?signed},
516 qr{long\s+int\s+(?:un)?signed},
517 qr{int\s+long\s+(?:un)?signed},
518 qr{long\s+(?:un)?signed\s+int},
519 qr{int\s+(?:un)?signed\s+long},
520 qr{int\s+(?:un)?signed},
521 qr{int\s+long\s+long\s+(?:un)?signed},
522 qr{long\s+long\s+int\s+(?:un)?signed},
523 qr{long\s+long\s+(?:un)?signed\s+int},
524 qr{long\s+long\s+(?:un)?signed},
525 qr{long\s+(?:un)?signed},
526);
527
Andy Whitcroft8905a672007-11-28 16:21:06 -0800528our @typeList = (
529 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700530 qr{(?:(?:un)?signed\s+)?char},
531 qr{(?:(?:un)?signed\s+)?short\s+int},
532 qr{(?:(?:un)?signed\s+)?short},
533 qr{(?:(?:un)?signed\s+)?int},
534 qr{(?:(?:un)?signed\s+)?long\s+int},
535 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
536 qr{(?:(?:un)?signed\s+)?long\s+long},
537 qr{(?:(?:un)?signed\s+)?long},
538 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800539 qr{float},
540 qr{double},
541 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800542 qr{struct\s+$Ident},
543 qr{union\s+$Ident},
544 qr{enum\s+$Ident},
545 qr{${Ident}_t},
546 qr{${Ident}_handler},
547 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700548 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800549);
Joe Perches938224b2016-01-20 14:59:15 -0800550
551our $C90_int_types = qr{(?x:
552 long\s+long\s+int\s+(?:un)?signed|
553 long\s+long\s+(?:un)?signed\s+int|
554 long\s+long\s+(?:un)?signed|
555 (?:(?:un)?signed\s+)?long\s+long\s+int|
556 (?:(?:un)?signed\s+)?long\s+long|
557 int\s+long\s+long\s+(?:un)?signed|
558 int\s+(?:(?:un)?signed\s+)?long\s+long|
559
560 long\s+int\s+(?:un)?signed|
561 long\s+(?:un)?signed\s+int|
562 long\s+(?:un)?signed|
563 (?:(?:un)?signed\s+)?long\s+int|
564 (?:(?:un)?signed\s+)?long|
565 int\s+long\s+(?:un)?signed|
566 int\s+(?:(?:un)?signed\s+)?long|
567
568 int\s+(?:un)?signed|
569 (?:(?:un)?signed\s+)?int
570)};
571
Alex Dowad485ff232015-06-25 15:02:52 -0700572our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700573our @typeListWithAttr = (
574 @typeList,
575 qr{struct\s+$InitAttribute\s+$Ident},
576 qr{union\s+$InitAttribute\s+$Ident},
577);
578
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700579our @modifierList = (
580 qr{fastcall},
581);
Alex Dowad485ff232015-06-25 15:02:52 -0700582our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800583
Joe Perches24358802014-04-03 14:49:13 -0700584our @mode_permission_funcs = (
585 ["module_param", 3],
586 ["module_param_(?:array|named|string)", 4],
587 ["module_param_array_named", 5],
588 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
589 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700590 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
591 ["IIO_DEV_ATTR_[A-Z_]+", 1],
592 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
593 ["SENSOR_TEMPLATE(?:_2|)", 3],
594 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700595);
596
Joe Perches1a3dcf22020-08-11 18:35:16 -0700597my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
598
Joe Perches515a2352014-04-03 14:49:24 -0700599#Create a search pattern for all these functions to speed up a loop below
600our $mode_perms_search = "";
601foreach my $entry (@mode_permission_funcs) {
602 $mode_perms_search .= '|' if ($mode_perms_search ne "");
603 $mode_perms_search .= $entry->[0];
604}
Joe Perches00180462018-02-06 15:38:55 -0800605$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700606
Joe Perches9189c7e2018-09-07 15:26:18 -0700607our %deprecated_apis = (
608 "synchronize_rcu_bh" => "synchronize_rcu",
609 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
610 "call_rcu_bh" => "call_rcu",
611 "rcu_barrier_bh" => "rcu_barrier",
612 "synchronize_sched" => "synchronize_rcu",
613 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
614 "call_rcu_sched" => "call_rcu",
615 "rcu_barrier_sched" => "rcu_barrier",
616 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
617 "cond_synchronize_sched" => "cond_synchronize_rcu",
618);
619
620#Create a search pattern for all these strings to speed up a loop below
621our $deprecated_apis_search = "";
622foreach my $entry (keys %deprecated_apis) {
623 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
624 $deprecated_apis_search .= $entry;
625}
626$deprecated_apis_search = "(?:${deprecated_apis_search})";
627
Joe Perchesb392c642015-04-16 12:44:16 -0700628our $mode_perms_world_writable = qr{
629 S_IWUGO |
630 S_IWOTH |
631 S_IRWXUGO |
632 S_IALLUGO |
633 0[0-7][0-7][2367]
634}x;
635
Joe Perchesf90774e2016-10-11 13:51:47 -0700636our %mode_permission_string_types = (
637 "S_IRWXU" => 0700,
638 "S_IRUSR" => 0400,
639 "S_IWUSR" => 0200,
640 "S_IXUSR" => 0100,
641 "S_IRWXG" => 0070,
642 "S_IRGRP" => 0040,
643 "S_IWGRP" => 0020,
644 "S_IXGRP" => 0010,
645 "S_IRWXO" => 0007,
646 "S_IROTH" => 0004,
647 "S_IWOTH" => 0002,
648 "S_IXOTH" => 0001,
649 "S_IRWXUGO" => 0777,
650 "S_IRUGO" => 0444,
651 "S_IWUGO" => 0222,
652 "S_IXUGO" => 0111,
653);
654
655#Create a search pattern for all these strings to speed up a loop below
656our $mode_perms_string_search = "";
657foreach my $entry (keys %mode_permission_string_types) {
658 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
659 $mode_perms_string_search .= $entry;
660}
Joe Perches00180462018-02-06 15:38:55 -0800661our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
662our $multi_mode_perms_string_search = qr{
663 ${single_mode_perms_string_search}
664 (?:\s*\|\s*${single_mode_perms_string_search})*
665}x;
666
667sub perms_to_octal {
668 my ($string) = @_;
669
670 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
671
672 my $val = "";
673 my $oval = "";
674 my $to = 0;
675 my $curpos = 0;
676 my $lastpos = 0;
677 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
678 $curpos = pos($string);
679 my $match = $2;
680 my $omatch = $1;
681 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
682 $lastpos = $curpos;
683 $to |= $mode_permission_string_types{$match};
684 $val .= '\s*\|\s*' if ($val ne "");
685 $val .= $match;
686 $oval .= $omatch;
687 }
688 $oval =~ s/^\s*\|\s*//;
689 $oval =~ s/\s*\|\s*$//;
690 return sprintf("%04o", $to);
691}
Joe Perchesf90774e2016-10-11 13:51:47 -0700692
Wolfram Sang7840a942010-08-09 17:20:57 -0700693our $allowed_asm_includes = qr{(?x:
694 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700695 memory|
696 time|
697 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700698)};
699# memory.h: ARM has a custom one
700
Kees Cook66b47b42014-10-13 15:51:57 -0700701# Load common spelling mistakes and build regular expression list.
702my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700703my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700704
Joe Perches36061e32014-12-10 15:51:43 -0800705if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800706 while (<$spelling>) {
707 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700708
Joe Perches36061e32014-12-10 15:51:43 -0800709 $line =~ s/\s*\n?$//g;
710 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700711
Joe Perches36061e32014-12-10 15:51:43 -0800712 next if ($line =~ m/^\s*#/);
713 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700714
Joe Perches36061e32014-12-10 15:51:43 -0800715 my ($suspect, $fix) = split(/\|\|/, $line);
716
Joe Perches36061e32014-12-10 15:51:43 -0800717 $spelling_fix{$suspect} = $fix;
718 }
719 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800720} else {
721 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700722}
Kees Cook66b47b42014-10-13 15:51:57 -0700723
Joe Perchesebfd7d62015-04-16 12:44:14 -0700724if ($codespell) {
725 if (open(my $spelling, '<', $codespellfile)) {
726 while (<$spelling>) {
727 my $line = $_;
728
729 $line =~ s/\s*\n?$//g;
730 $line =~ s/^\s*//g;
731
732 next if ($line =~ m/^\s*#/);
733 next if ($line =~ m/^\s*$/);
734 next if ($line =~ m/, disabled/i);
735
736 $line =~ s/,.*$//;
737
738 my ($suspect, $fix) = split(/->/, $line);
739
740 $spelling_fix{$suspect} = $fix;
741 }
742 close($spelling);
743 } else {
744 warn "No codespell typos will be found - file '$codespellfile': $!\n";
745 }
746}
747
748$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
749
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700750sub read_words {
751 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700752
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700753 if (open(my $words, '<', $file)) {
754 while (<$words>) {
755 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700756
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700757 $line =~ s/\s*\n?$//g;
758 $line =~ s/^\s*//g;
759
760 next if ($line =~ m/^\s*#/);
761 next if ($line =~ m/^\s*$/);
762 if ($line =~ /\s/) {
763 print("$file: '$line' invalid - ignored\n");
764 next;
765 }
766
Quentin Monnetced69da12020-08-11 18:35:13 -0700767 $$wordsRef .= '|' if (defined $$wordsRef);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700768 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700769 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700770 close($file);
771 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700772 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700773
774 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700775}
776
Quentin Monnetced69da12020-08-11 18:35:13 -0700777my $const_structs;
778if (show_type("CONST_STRUCT")) {
779 read_words(\$const_structs, $conststructsfile)
780 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
781}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700782
Quentin Monnetced69da12020-08-11 18:35:13 -0700783if (defined($typedefsfile)) {
784 my $typeOtherTypedefs;
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700785 read_words(\$typeOtherTypedefs, $typedefsfile)
786 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
Quentin Monnetced69da12020-08-11 18:35:13 -0700787 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700788}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700789
Andy Whitcroft8905a672007-11-28 16:21:06 -0800790sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700791 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
792 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700793 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700794 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700795 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700796 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700797 (?:$typeTypedefs\b)|
798 (?:${all}\b)
799 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800800 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700801 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800802 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800803 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700804 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700805 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800806 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700807 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800808 }x;
Joe Perches18130872014-08-06 16:11:22 -0700809 $NonptrTypeMisordered = qr{
810 (?:$Modifier\s+|const\s+)*
811 (?:
812 (?:${Misordered}\b)
813 )
814 (?:\s+$Modifier|\s+const)*
815 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700816 $NonptrTypeWithAttr = qr{
817 (?:$Modifier\s+|const\s+)*
818 (?:
819 (?:typeof|__typeof__)\s*\([^\)]*\)|
820 (?:$typeTypedefs\b)|
821 (?:${allWithAttr}\b)
822 )
823 (?:\s+$Modifier|\s+const)*
824 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800825 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700826 $NonptrType
Antonio Borneo7b184962020-04-06 20:11:04 -0700827 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700828 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800829 }x;
Joe Perches18130872014-08-06 16:11:22 -0700830 $TypeMisordered = qr{
831 $NonptrTypeMisordered
Antonio Borneo7b184962020-04-06 20:11:04 -0700832 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Perches18130872014-08-06 16:11:22 -0700833 (?:\s+$Inline|\s+$Modifier)*
834 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700835 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700836 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800837}
838build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700839
Joe Perches7d2367a2011-07-25 17:13:22 -0700840our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700841
842# Using $balanced_parens, $LvalOrFunc, or $FuncArg
843# requires at least perl version v5.10.0
844# Any use must be runtime checked with $^V
845
846our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700847our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800848our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700849
Joe Perchesf8422302014-08-06 16:11:31 -0700850our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700851 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700852 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700853 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700854)};
855
Aditya Srivastava8d0325c2020-12-15 20:44:24 -0800856our %allow_repeated_words = (
857 add => '',
858 added => '',
859 bad => '',
860 be => '',
861);
862
Joe Perches7d2367a2011-07-25 17:13:22 -0700863sub deparenthesize {
864 my ($string) = @_;
865 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700866
867 while ($string =~ /^\s*\(.*\)\s*$/) {
868 $string =~ s@^\s*\(\s*@@;
869 $string =~ s@\s*\)\s*$@@;
870 }
871
Joe Perches7d2367a2011-07-25 17:13:22 -0700872 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700873
Joe Perches7d2367a2011-07-25 17:13:22 -0700874 return $string;
875}
876
Joe Perches34456862013-07-03 15:05:34 -0700877sub seed_camelcase_file {
878 my ($file) = @_;
879
880 return if (!(-f $file));
881
882 local $/;
883
884 open(my $include_file, '<', "$file")
885 or warn "$P: Can't read '$file' $!\n";
886 my $text = <$include_file>;
887 close($include_file);
888
889 my @lines = split('\n', $text);
890
891 foreach my $line (@lines) {
892 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
893 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
894 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800895 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
896 $camelcase{$1} = 1;
897 } 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 -0700898 $camelcase{$1} = 1;
899 }
900 }
901}
902
Joe Perchescd28b112019-12-04 16:52:09 -0800903our %maintained_status = ();
904
Joe Perches85b0ee12016-10-11 13:51:44 -0700905sub is_maintained_obsolete {
906 my ($filename) = @_;
907
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800908 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700909
Joe Perchescd28b112019-12-04 16:52:09 -0800910 if (!exists($maintained_status{$filename})) {
911 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
912 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700913
Joe Perchescd28b112019-12-04 16:52:09 -0800914 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700915}
916
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700917sub is_SPDX_License_valid {
918 my ($license) = @_;
919
Joe Perches0f7f6352020-10-24 16:59:04 -0700920 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700921
Joe Perches56294112018-08-21 21:58:04 -0700922 my $root_path = abs_path($root);
923 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700924 return 0 if ($status ne "");
925 return 1;
926}
927
Joe Perches34456862013-07-03 15:05:34 -0700928my $camelcase_seeded = 0;
929sub seed_camelcase_includes {
930 return if ($camelcase_seeded);
931
932 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700933 my $camelcase_cache = "";
934 my @include_files = ();
935
936 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700937
Joe Perches0f7f6352020-10-24 16:59:04 -0700938 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700939 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -0700940 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700941 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700942 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700943 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700944 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700945 @include_files = split('\n', $files);
946 foreach my $file (@include_files) {
947 my $date = POSIX::strftime("%Y%m%d%H%M",
948 localtime((stat $file)[9]));
949 $last_mod_date = $date if ($last_mod_date < $date);
950 }
951 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700952 }
Joe Perchesc707a812013-07-08 16:00:43 -0700953
954 if ($camelcase_cache ne "" && -f $camelcase_cache) {
955 open(my $camelcase_file, '<', "$camelcase_cache")
956 or warn "$P: Can't read '$camelcase_cache' $!\n";
957 while (<$camelcase_file>) {
958 chomp;
959 $camelcase{$_} = 1;
960 }
961 close($camelcase_file);
962
963 return;
964 }
965
Joe Perches0f7f6352020-10-24 16:59:04 -0700966 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700967 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700968 @include_files = split('\n', $files);
969 }
970
Joe Perches34456862013-07-03 15:05:34 -0700971 foreach my $file (@include_files) {
972 seed_camelcase_file($file);
973 }
Joe Perches351b2a12013-07-03 15:05:36 -0700974
Joe Perchesc707a812013-07-08 16:00:43 -0700975 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700976 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700977 open(my $camelcase_file, '>', "$camelcase_cache")
978 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700979 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
980 print $camelcase_file ("$_\n");
981 }
982 close($camelcase_file);
983 }
Joe Perches34456862013-07-03 15:05:34 -0700984}
985
Joe Perchesf5f61322020-10-15 20:12:12 -0700986sub git_is_single_file {
987 my ($filename) = @_;
988
989 return 0 if ((which("git") eq "") || !(-e "$gitroot"));
990
991 my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
992 my $count = $output =~ tr/\n//;
993 return $count eq 1 && $output =~ m{^${filename}$};
994}
995
Joe Perchesd311cd42014-08-06 16:10:57 -0700996sub git_commit_info {
997 my ($commit, $id, $desc) = @_;
998
Joe Perches0f7f6352020-10-24 16:59:04 -0700999 return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
Joe Perchesd311cd42014-08-06 16:10:57 -07001000
Joe Perchesdbbf8692019-09-25 16:46:52 -07001001 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -07001002 $output =~ s/^\s*//gm;
1003 my @lines = split("\n", $output);
1004
Joe Perches0d7835f2015-02-13 14:38:35 -08001005 return ($id, $desc) if ($#lines < 0);
1006
Sean Christopherson5a7f4452019-09-25 16:46:49 -07001007 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -07001008# Maybe one day convert this block of bash into something that returns
1009# all matching commit ids, but it's very slow...
1010#
1011# echo "checking commits $1..."
1012# git rev-list --remotes | grep -i "^$1" |
1013# while read line ; do
1014# git log --format='%H %s' -1 $line |
1015# echo "commit $(cut -c 1-12,41-)"
1016# done
1017 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07001018 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -07001019 } else {
1020 $id = substr($lines[0], 0, 12);
1021 $desc = substr($lines[0], 41);
1022 }
1023
1024 return ($id, $desc);
1025}
1026
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001027$chk_signoff = 0 if ($file);
1028
Andy Whitcroft00df3442007-06-08 13:47:06 -07001029my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001030my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001031my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001032my @fixed_inserted = ();
1033my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001034my $fixlinenr = -1;
1035
Du, Changbin4a593c32016-05-20 17:04:16 -07001036# If input is git commits, extract all commits from the commit expressions.
1037# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
Joe Perches0f7f6352020-10-24 16:59:04 -07001038die "$P: No git repository found\n" if ($git && !-e "$gitroot");
Du, Changbin4a593c32016-05-20 17:04:16 -07001039
1040if ($git) {
1041 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001042 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001043 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001044 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1045 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001046 } elsif ($commit_expr =~ m/\.\./) {
1047 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001048 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001049 $git_range = "-1 $commit_expr";
1050 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001051 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001052 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001053 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1054 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001055 my $sha1 = $1;
1056 my $subject = $2;
1057 unshift(@commits, $sha1);
1058 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001059 }
1060 }
1061 die "$P: no git commits after extraction!\n" if (@commits == 0);
1062 @ARGV = @commits;
1063}
1064
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001065my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001066$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001067for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001068 my $FILE;
Joe Perchesf5f61322020-10-15 20:12:12 -07001069 my $is_git_file = git_is_single_file($filename);
1070 my $oldfile = $file;
1071 $file = 1 if ($is_git_file);
Du, Changbin4a593c32016-05-20 17:04:16 -07001072 if ($git) {
1073 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1074 die "$P: $filename: git format-patch failed - $!\n";
1075 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001076 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001077 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001078 } elsif ($filename eq '-') {
1079 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001080 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001081 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001082 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001083 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001084 if ($filename eq '-') {
1085 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001086 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001087 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001088 } else {
1089 $vname = $filename;
1090 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001091 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001092 chomp;
1093 push(@rawlines, $_);
Geert Uytterhoevenc7f574d2020-06-04 16:50:43 -07001094 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001095 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001096 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001097
1098 if ($#ARGV > 0 && $quiet == 0) {
1099 print '-' x length($vname) . "\n";
1100 print "$vname\n";
1101 print '-' x length($vname) . "\n";
1102 }
1103
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001104 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001105 $exit = 1;
1106 }
1107 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001108 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001109 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001110 @fixed_inserted = ();
1111 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001112 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001113 @modifierListFile = ();
1114 @typeListFile = ();
1115 build_types();
Joe Perchesf5f61322020-10-15 20:12:12 -07001116 $file = $oldfile if ($is_git_file);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001117}
1118
Joe Perchesd8469f12015-06-25 15:03:00 -07001119if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001120 hash_show_words(\%use_type, "Used");
1121 hash_show_words(\%ignore_type, "Ignored");
1122
Joe Perches5b579802018-08-21 21:57:33 -07001123 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001124 print << "EOM"
1125
1126NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001127 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001128EOM
1129 }
1130 if ($exit) {
1131 print << "EOM"
1132
1133NOTE: If any of the errors are false positives, please report
1134 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1135EOM
1136 }
1137}
1138
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001139exit($exit);
1140
1141sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001142 my ($root) = @_;
1143
1144 my @tree_check = (
1145 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1146 "README", "Documentation", "arch", "include", "drivers",
1147 "fs", "init", "ipc", "kernel", "lib", "scripts",
1148 );
1149
1150 foreach my $check (@tree_check) {
1151 if (! -e $root . '/' . $check) {
1152 return 0;
1153 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001154 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001155 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001156}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001157
Joe Perches20112472011-07-25 17:13:23 -07001158sub parse_email {
1159 my ($formatted_email) = @_;
1160
1161 my $name = "";
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001162 my $quoted = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001163 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001164 my $address = "";
1165 my $comment = "";
1166
1167 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1168 $name = $1;
1169 $address = $2;
1170 $comment = $3 if defined $3;
1171 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1172 $address = $1;
1173 $comment = $2 if defined $2;
1174 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1175 $address = $1;
1176 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001177 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001178 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001179 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001180 $name =~ s/^\"|\"$//g;
1181 # If there's a name left after stripping spaces and
1182 # leading quotes, and the address doesn't have both
1183 # leading and trailing angle brackets, the address
1184 # is invalid. ie:
1185 # "joe smith joe@smith.com" bad
1186 # "joe smith <joe@smith.com" bad
1187 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1188 $name = "";
1189 $address = "";
1190 $comment = "";
1191 }
1192 }
1193
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001194 # Extract comments from names excluding quoted parts
1195 # "John D. (Doe)" - Do not extract
1196 if ($name =~ s/\"(.+)\"//) {
1197 $quoted = $1;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001198 }
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001199 while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1200 $name_comment .= trim($1);
1201 }
1202 $name =~ s/^[ \"]+|[ \"]+$//g;
1203 $name = trim("$quoted $name");
1204
Joe Perches3705ce52013-07-03 15:05:31 -07001205 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001206 $address =~ s/^\<|\>$//g;
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001207 $comment = trim($comment);
Joe Perches20112472011-07-25 17:13:23 -07001208
1209 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1210 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1211 $name = "\"$name\"";
1212 }
1213
Joe Perchesdfa05c22020-04-06 20:10:48 -07001214 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001215}
1216
1217sub format_email {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001218 my ($name, $name_comment, $address, $comment) = @_;
Joe Perches20112472011-07-25 17:13:23 -07001219
1220 my $formatted_email;
1221
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001222 $name =~ s/^[ \"]+|[ \"]+$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001223 $address = trim($address);
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001224 $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
Joe Perches20112472011-07-25 17:13:23 -07001225
1226 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1227 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1228 $name = "\"$name\"";
1229 }
1230
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001231 $name_comment = trim($name_comment);
1232 $name_comment = " $name_comment" if ($name_comment ne "");
1233 $comment = trim($comment);
1234 $comment = " $comment" if ($comment ne "");
1235
Joe Perches20112472011-07-25 17:13:23 -07001236 if ("$name" eq "") {
1237 $formatted_email = "$address";
1238 } else {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001239 $formatted_email = "$name$name_comment <$address>";
Joe Perches20112472011-07-25 17:13:23 -07001240 }
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001241 $formatted_email .= "$comment";
Joe Perches20112472011-07-25 17:13:23 -07001242 return $formatted_email;
1243}
1244
Joe Perchesdfa05c22020-04-06 20:10:48 -07001245sub reformat_email {
1246 my ($email) = @_;
1247
1248 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001249 return format_email($email_name, $name_comment, $email_address, $comment);
Joe Perchesdfa05c22020-04-06 20:10:48 -07001250}
1251
1252sub same_email_addresses {
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08001253 my ($email1, $email2) = @_;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001254
1255 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1256 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1257
1258 return $email1_name eq $email2_name &&
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001259 $email1_address eq $email2_address &&
1260 $name1_comment eq $name2_comment &&
1261 $comment1 eq $comment2;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001262}
1263
Joe Perchesd311cd42014-08-06 16:10:57 -07001264sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001265 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001266
Joe Perchesbd474ca2014-08-06 16:11:10 -07001267 foreach my $path (split(/:/, $ENV{PATH})) {
1268 if (-e "$path/$bin") {
1269 return "$path/$bin";
1270 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001271 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001272
Joe Perchesbd474ca2014-08-06 16:11:10 -07001273 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001274}
1275
Joe Perches000d1cc12011-07-25 17:13:25 -07001276sub which_conf {
1277 my ($conf) = @_;
1278
1279 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1280 if (-e "$path/$conf") {
1281 return "$path/$conf";
1282 }
1283 }
1284
1285 return "";
1286}
1287
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001288sub expand_tabs {
1289 my ($str) = @_;
1290
1291 my $res = '';
1292 my $n = 0;
1293 for my $c (split(//, $str)) {
1294 if ($c eq "\t") {
1295 $res .= ' ';
1296 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001297 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001298 $res .= ' ';
1299 }
1300 next;
1301 }
1302 $res .= $c;
1303 $n++;
1304 }
1305
1306 return $res;
1307}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001308sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001309 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001310 return $res;
1311}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001312
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001313sub line_stats {
1314 my ($line) = @_;
1315
1316 # Drop the diff line leader and expand tabs
1317 $line =~ s/^.//;
1318 $line = expand_tabs($line);
1319
1320 # Pick the indent from the front of the line.
1321 my ($white) = ($line =~ /^(\s*)/);
1322
1323 return (length($line), length($white));
1324}
1325
Andy Whitcroft773647a2008-03-28 14:15:58 -07001326my $sanitise_quote = '';
1327
1328sub sanitise_line_reset {
1329 my ($in_comment) = @_;
1330
1331 if ($in_comment) {
1332 $sanitise_quote = '*/';
1333 } else {
1334 $sanitise_quote = '';
1335 }
1336}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001337sub sanitise_line {
1338 my ($line) = @_;
1339
1340 my $res = '';
1341 my $l = '';
1342
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001343 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001344 my $off = 0;
1345 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001346
Andy Whitcroft773647a2008-03-28 14:15:58 -07001347 # Always copy over the diff marker.
1348 $res = substr($line, 0, 1);
1349
1350 for ($off = 1; $off < length($line); $off++) {
1351 $c = substr($line, $off, 1);
1352
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001353 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001354 # and end, all to $;.
1355 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1356 $sanitise_quote = '*/';
1357
1358 substr($res, $off, 2, "$;$;");
1359 $off++;
1360 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001361 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001362 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001363 $sanitise_quote = '';
1364 substr($res, $off, 2, "$;$;");
1365 $off++;
1366 next;
1367 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001368 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1369 $sanitise_quote = '//';
1370
1371 substr($res, $off, 2, $sanitise_quote);
1372 $off++;
1373 next;
1374 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001375
1376 # A \ in a string means ignore the next character.
1377 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1378 $c eq "\\") {
1379 substr($res, $off, 2, 'XX');
1380 $off++;
1381 next;
1382 }
1383 # Regular quotes.
1384 if ($c eq "'" || $c eq '"') {
1385 if ($sanitise_quote eq '') {
1386 $sanitise_quote = $c;
1387
1388 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001389 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001390 } elsif ($sanitise_quote eq $c) {
1391 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001392 }
1393 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001394
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001395 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001396 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1397 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001398 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1399 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001400 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1401 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001402 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001403 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001404 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001405 }
1406
Daniel Walker113f04a2009-09-21 17:04:35 -07001407 if ($sanitise_quote eq '//') {
1408 $sanitise_quote = '';
1409 }
1410
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001411 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001412 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001413 my $clean = 'X' x length($1);
1414 $res =~ s@\<.*\>@<$clean>@;
1415
1416 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001417 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001418 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001419 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001420 }
1421
Joe Perchesdadf6802016-08-02 14:04:33 -07001422 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1423 my $match = $1;
1424 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1425 }
1426
Andy Whitcroft00df3442007-06-08 13:47:06 -07001427 return $res;
1428}
1429
Joe Perchesa6962d72013-04-29 16:18:13 -07001430sub get_quoted_string {
1431 my ($line, $rawline) = @_;
1432
Joe Perches478b1792018-04-10 16:33:34 -07001433 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001434 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001435 return substr($rawline, $-[0], $+[0] - $-[0]);
1436}
1437
Andy Whitcroft8905a672007-11-28 16:21:06 -08001438sub ctx_statement_block {
1439 my ($linenr, $remain, $off) = @_;
1440 my $line = $linenr - 1;
1441 my $blk = '';
1442 my $soff = $off;
1443 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001444 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001445
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001446 my $loff = 0;
1447
Andy Whitcroft8905a672007-11-28 16:21:06 -08001448 my $type = '';
1449 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001450 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001451 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001452 my $c;
1453 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001454
1455 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001456 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001457 @stack = (['', 0]) if ($#stack == -1);
1458
Andy Whitcroft773647a2008-03-28 14:15:58 -07001459 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001460 # If we are about to drop off the end, pull in more
1461 # context.
1462 if ($off >= $len) {
1463 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001464 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001465 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001466 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001467 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001468 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001469 $len = length($blk);
1470 $line++;
1471 last;
1472 }
1473 # Bail if there is no further context.
1474 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001475 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001476 last;
1477 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001478 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1479 $level++;
1480 $type = '#';
1481 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001482 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001483 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001484 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001485 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001486
Andy Whitcroft773647a2008-03-28 14:15:58 -07001487 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001488
1489 # Handle nested #if/#else.
1490 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1491 push(@stack, [ $type, $level ]);
1492 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1493 ($type, $level) = @{$stack[$#stack - 1]};
1494 } elsif ($remainder =~ /^#\s*endif\b/) {
1495 ($type, $level) = @{pop(@stack)};
1496 }
1497
Andy Whitcroft8905a672007-11-28 16:21:06 -08001498 # Statement ends at the ';' or a close '}' at the
1499 # outermost level.
1500 if ($level == 0 && $c eq ';') {
1501 last;
1502 }
1503
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001504 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001505 if ($level == 0 && $coff_set == 0 &&
1506 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1507 $remainder =~ /^(else)(?:\s|{)/ &&
1508 $remainder !~ /^else\s+if\b/) {
1509 $coff = $off + length($1) - 1;
1510 $coff_set = 1;
1511 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1512 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001513 }
1514
Andy Whitcroft8905a672007-11-28 16:21:06 -08001515 if (($type eq '' || $type eq '(') && $c eq '(') {
1516 $level++;
1517 $type = '(';
1518 }
1519 if ($type eq '(' && $c eq ')') {
1520 $level--;
1521 $type = ($level != 0)? '(' : '';
1522
1523 if ($level == 0 && $coff < $soff) {
1524 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001525 $coff_set = 1;
1526 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001527 }
1528 }
1529 if (($type eq '' || $type eq '{') && $c eq '{') {
1530 $level++;
1531 $type = '{';
1532 }
1533 if ($type eq '{' && $c eq '}') {
1534 $level--;
1535 $type = ($level != 0)? '{' : '';
1536
1537 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001538 if (substr($blk, $off + 1, 1) eq ';') {
1539 $off++;
1540 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001541 last;
1542 }
1543 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001544 # Preprocessor commands end at the newline unless escaped.
1545 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1546 $level--;
1547 $type = '';
1548 $off++;
1549 last;
1550 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001551 $off++;
1552 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001553 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001554 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001555 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001556 $line++;
1557 $remain--;
1558 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001559
1560 my $statement = substr($blk, $soff, $off - $soff + 1);
1561 my $condition = substr($blk, $soff, $coff - $soff + 1);
1562
1563 #warn "STATEMENT<$statement>\n";
1564 #warn "CONDITION<$condition>\n";
1565
Andy Whitcroft773647a2008-03-28 14:15:58 -07001566 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001567
1568 return ($statement, $condition,
1569 $line, $remain + 1, $off - $loff + 1, $level);
1570}
1571
Andy Whitcroftcf655042008-03-04 14:28:20 -08001572sub statement_lines {
1573 my ($stmt) = @_;
1574
1575 # Strip the diff line prefixes and rip blank lines at start and end.
1576 $stmt =~ s/(^|\n)./$1/g;
1577 $stmt =~ s/^\s*//;
1578 $stmt =~ s/\s*$//;
1579
1580 my @stmt_lines = ($stmt =~ /\n/g);
1581
1582 return $#stmt_lines + 2;
1583}
1584
1585sub statement_rawlines {
1586 my ($stmt) = @_;
1587
1588 my @stmt_lines = ($stmt =~ /\n/g);
1589
1590 return $#stmt_lines + 2;
1591}
1592
1593sub statement_block_size {
1594 my ($stmt) = @_;
1595
1596 $stmt =~ s/(^|\n)./$1/g;
1597 $stmt =~ s/^\s*{//;
1598 $stmt =~ s/}\s*$//;
1599 $stmt =~ s/^\s*//;
1600 $stmt =~ s/\s*$//;
1601
1602 my @stmt_lines = ($stmt =~ /\n/g);
1603 my @stmt_statements = ($stmt =~ /;/g);
1604
1605 my $stmt_lines = $#stmt_lines + 2;
1606 my $stmt_statements = $#stmt_statements + 1;
1607
1608 if ($stmt_lines > $stmt_statements) {
1609 return $stmt_lines;
1610 } else {
1611 return $stmt_statements;
1612 }
1613}
1614
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001615sub ctx_statement_full {
1616 my ($linenr, $remain, $off) = @_;
1617 my ($statement, $condition, $level);
1618
1619 my (@chunks);
1620
Andy Whitcroftcf655042008-03-04 14:28:20 -08001621 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001622 ($statement, $condition, $linenr, $remain, $off, $level) =
1623 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001624 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001625 push(@chunks, [ $condition, $statement ]);
1626 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1627 return ($level, $linenr, @chunks);
1628 }
1629
1630 # Pull in the following conditional/block pairs and see if they
1631 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001632 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001633 ($statement, $condition, $linenr, $remain, $off, $level) =
1634 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001635 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001636 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001637 #print "C: push\n";
1638 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001639 }
1640
1641 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001642}
1643
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001644sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001645 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001646 my $line;
1647 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001648 my $blk = '';
1649 my @o;
1650 my @c;
1651 my @res = ();
1652
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001653 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001654 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001655 for ($line = $start; $remain > 0; $line++) {
1656 next if ($rawlines[$line] =~ /^-/);
1657 $remain--;
1658
1659 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001660
1661 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001662 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001663 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001664 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001665 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001666 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001667 $level = pop(@stack);
1668 }
1669
Andy Whitcroft01464f32010-10-26 14:23:19 -07001670 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001671 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1672 if ($off > 0) {
1673 $off--;
1674 next;
1675 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001676
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001677 if ($c eq $close && $level > 0) {
1678 $level--;
1679 last if ($level == 0);
1680 } elsif ($c eq $open) {
1681 $level++;
1682 }
1683 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001684
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001685 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001686 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001687 }
1688
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001689 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001690 }
1691
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001692 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001693}
1694sub ctx_block_outer {
1695 my ($linenr, $remain) = @_;
1696
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001697 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1698 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001699}
1700sub ctx_block {
1701 my ($linenr, $remain) = @_;
1702
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001703 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1704 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001705}
1706sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001707 my ($linenr, $remain, $off) = @_;
1708
1709 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1710 return @r;
1711}
1712sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001713 my ($linenr, $remain) = @_;
1714
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001715 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001716}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001717sub ctx_statement_level {
1718 my ($linenr, $remain, $off) = @_;
1719
1720 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1721}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001722
1723sub ctx_locate_comment {
1724 my ($first_line, $end_line) = @_;
1725
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001726 # If c99 comment on the current line, or the line before or after
1727 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1728 return $current_comment if (defined $current_comment);
1729 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1730 return $current_comment if (defined $current_comment);
1731 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1732 return $current_comment if (defined $current_comment);
1733
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001734 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001735 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001736 return $current_comment if (defined $current_comment);
1737
1738 # Look through the context and try and figure out if there is a
1739 # comment.
1740 my $in_comment = 0;
1741 $current_comment = '';
1742 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001743 my $line = $rawlines[$linenr - 1];
1744 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001745 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1746 $in_comment = 1;
1747 }
1748 if ($line =~ m@/\*@) {
1749 $in_comment = 1;
1750 }
1751 if (!$in_comment && $current_comment ne '') {
1752 $current_comment = '';
1753 }
1754 $current_comment .= $line . "\n" if ($in_comment);
1755 if ($line =~ m@\*/@) {
1756 $in_comment = 0;
1757 }
1758 }
1759
1760 chomp($current_comment);
1761 return($current_comment);
1762}
1763sub ctx_has_comment {
1764 my ($first_line, $end_line) = @_;
1765 my $cmt = ctx_locate_comment($first_line, $end_line);
1766
Andy Whitcroft00df3442007-06-08 13:47:06 -07001767 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001768 ##print "CMMT: $cmt\n";
1769
1770 return ($cmt ne '');
1771}
1772
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001773sub raw_line {
1774 my ($linenr, $cnt) = @_;
1775
1776 my $offset = $linenr - 1;
1777 $cnt++;
1778
1779 my $line;
1780 while ($cnt) {
1781 $line = $rawlines[$offset++];
1782 next if (defined($line) && $line =~ /^-/);
1783 $cnt--;
1784 }
1785
1786 return $line;
1787}
1788
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001789sub get_stat_real {
1790 my ($linenr, $lc) = @_;
1791
1792 my $stat_real = raw_line($linenr, 0);
1793 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1794 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1795 }
1796
1797 return $stat_real;
1798}
1799
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001800sub get_stat_here {
1801 my ($linenr, $cnt, $here) = @_;
1802
1803 my $herectx = $here . "\n";
1804 for (my $n = 0; $n < $cnt; $n++) {
1805 $herectx .= raw_line($linenr, $n) . "\n";
1806 }
1807
1808 return $herectx;
1809}
1810
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001811sub cat_vet {
1812 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001813 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001814
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001815 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001816 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1817 $res .= $1;
1818 if ($2 ne '') {
1819 $coded = sprintf("^%c", unpack('C', $2) + 64);
1820 $res .= $coded;
1821 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001822 }
1823 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001824
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001825 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001826}
1827
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001828my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001829my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001830my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001831my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001832
1833sub annotate_reset {
1834 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001835 $av_pending = '_';
1836 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001837 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001838}
1839
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001840sub annotate_values {
1841 my ($stream, $type) = @_;
1842
1843 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001844 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001845 my $cur = $stream;
1846
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001847 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001848
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001849 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001850 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001851 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001852 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001853 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001854 print "WS($1)\n" if ($dbg_values > 1);
1855 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001856 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001857 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001858 }
1859
Florian Micklerc023e4732011-01-12 16:59:58 -08001860 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001861 print "CAST($1)\n" if ($dbg_values > 1);
1862 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001863 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001864
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001865 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001866 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001867 $type = 'T';
1868
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001869 } elsif ($cur =~ /^($Modifier)\s*/) {
1870 print "MODIFIER($1)\n" if ($dbg_values > 1);
1871 $type = 'T';
1872
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001873 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001874 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001875 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001876 push(@av_paren_type, $type);
1877 if ($2 ne '') {
1878 $av_pending = 'N';
1879 }
1880 $type = 'E';
1881
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001882 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001883 print "UNDEF($1)\n" if ($dbg_values > 1);
1884 $av_preprocessor = 1;
1885 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001886
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001887 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001888 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001889 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001890
1891 push(@av_paren_type, $type);
1892 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001893 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001894
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001895 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001896 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1897 $av_preprocessor = 1;
1898
1899 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1900
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001901 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001902
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001903 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001904 print "PRE_END($1)\n" if ($dbg_values > 1);
1905
1906 $av_preprocessor = 1;
1907
1908 # Assume all arms of the conditional end as this
1909 # one does, and continue as if the #endif was not here.
1910 pop(@av_paren_type);
1911 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001912 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001913
1914 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001915 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001916
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001917 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1918 print "ATTR($1)\n" if ($dbg_values > 1);
1919 $av_pending = $type;
1920 $type = 'N';
1921
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001922 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001923 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001924 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001925 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001926 }
1927 $type = 'N';
1928
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001929 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001930 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001931 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001932 $type = 'N';
1933
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001934 } elsif ($cur =~/^(case)/o) {
1935 print "CASE($1)\n" if ($dbg_values > 1);
1936 $av_pend_colon = 'C';
1937 $type = 'N';
1938
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001939 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001940 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001941 $type = 'N';
1942
1943 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001944 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001945 push(@av_paren_type, $av_pending);
1946 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001947 $type = 'N';
1948
1949 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001950 my $new_type = pop(@av_paren_type);
1951 if ($new_type ne '_') {
1952 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001953 print "PAREN('$1') -> $type\n"
1954 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001955 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001956 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001957 }
1958
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001959 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001960 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001961 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001962 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001963
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001964 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1965 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001966 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001967 } elsif ($type eq 'E') {
1968 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001969 }
1970 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1971 $type = 'V';
1972
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001973 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001974 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001975 $type = 'V';
1976
1977 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001978 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001979 $type = 'N';
1980
Andy Whitcroftcf655042008-03-04 14:28:20 -08001981 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001982 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001983 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001984 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001985
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001986 } elsif ($cur =~/^(,)/) {
1987 print "COMMA($1)\n" if ($dbg_values > 1);
1988 $type = 'C';
1989
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001990 } elsif ($cur =~ /^(\?)/o) {
1991 print "QUESTION($1)\n" if ($dbg_values > 1);
1992 $type = 'N';
1993
1994 } elsif ($cur =~ /^(:)/o) {
1995 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1996
1997 substr($var, length($res), 1, $av_pend_colon);
1998 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1999 $type = 'E';
2000 } else {
2001 $type = 'N';
2002 }
2003 $av_pend_colon = 'O';
2004
Andy Whitcroft8e761b02009-01-06 14:41:19 -08002005 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002006 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002007 $type = 'N';
2008
Andy Whitcroft0d413862008-10-15 22:02:16 -07002009 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002010 my $variant;
2011
2012 print "OPV($1)\n" if ($dbg_values > 1);
2013 if ($type eq 'V') {
2014 $variant = 'B';
2015 } else {
2016 $variant = 'U';
2017 }
2018
2019 substr($var, length($res), 1, $variant);
2020 $type = 'N';
2021
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002022 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002023 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002024 if ($1 ne '++' && $1 ne '--') {
2025 $type = 'N';
2026 }
2027
2028 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002029 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002030 }
2031 if (defined $1) {
2032 $cur = substr($cur, length($1));
2033 $res .= $type x length($1);
2034 }
2035 }
2036
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002037 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002038}
2039
Andy Whitcroft8905a672007-11-28 16:21:06 -08002040sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002041 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002042 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002043 ^(?:
2044 $Modifier|
2045 $Storage|
2046 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002047 DEFINE_\S+
2048 )$|
2049 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002050 goto|
2051 return|
2052 case|
2053 else|
2054 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002055 do|
2056 \#|
2057 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002058 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002059 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002060 )}x;
2061 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2062 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002063 # Check for modifiers.
2064 $possible =~ s/\s*$Storage\s*//g;
2065 $possible =~ s/\s*$Sparse\s*//g;
2066 if ($possible =~ /^\s*$/) {
2067
2068 } elsif ($possible =~ /\s/) {
2069 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002070 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002071 if ($modifier !~ $notPermitted) {
2072 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002073 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002074 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002075 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002076
2077 } else {
2078 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002079 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002080 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002081 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002082 } else {
2083 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002084 }
2085}
2086
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002087my $prefix = '';
2088
Joe Perches000d1cc12011-07-25 17:13:25 -07002089sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002090 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002091
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002092 $type =~ tr/[a-z]/[A-Z]/;
2093
Joe Perchescbec18a2014-04-03 14:49:19 -07002094 return defined $use_type{$type} if (scalar keys %use_type > 0);
2095
2096 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002097}
2098
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002099sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002100 my ($level, $type, $msg) = @_;
2101
2102 if (!show_type($type) ||
2103 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002104 return 0;
2105 }
Joe Perches57230292015-06-25 15:03:03 -07002106 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002107 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002108 if ($level eq 'ERROR') {
2109 $output .= RED;
2110 } elsif ($level eq 'WARNING') {
2111 $output .= YELLOW;
2112 } else {
2113 $output .= GREEN;
2114 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002115 }
Joe Perches57230292015-06-25 15:03:03 -07002116 $output .= $prefix . $level . ':';
2117 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002118 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002119 $output .= "$type:";
2120 }
John Brooks737c0762017-07-10 15:52:24 -07002121 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002122 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002123
2124 if ($showfile) {
2125 my @lines = split("\n", $output, -1);
2126 splice(@lines, 1, 1);
2127 $output = join("\n", @lines);
2128 }
Joe Perches57230292015-06-25 15:03:03 -07002129 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002130
Joe Perches57230292015-06-25 15:03:03 -07002131 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002132
2133 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002134}
Joe Perchescbec18a2014-04-03 14:49:19 -07002135
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002136sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002137 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002138}
Joe Perches000d1cc12011-07-25 17:13:25 -07002139
Joe Perchesd752fcc2014-08-06 16:11:05 -07002140sub fixup_current_range {
2141 my ($lineRef, $offset, $length) = @_;
2142
2143 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2144 my $o = $1;
2145 my $l = $2;
2146 my $no = $o + $offset;
2147 my $nl = $l + $length;
2148 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2149 }
2150}
2151
2152sub fix_inserted_deleted_lines {
2153 my ($linesRef, $insertedRef, $deletedRef) = @_;
2154
2155 my $range_last_linenr = 0;
2156 my $delta_offset = 0;
2157
2158 my $old_linenr = 0;
2159 my $new_linenr = 0;
2160
2161 my $next_insert = 0;
2162 my $next_delete = 0;
2163
2164 my @lines = ();
2165
2166 my $inserted = @{$insertedRef}[$next_insert++];
2167 my $deleted = @{$deletedRef}[$next_delete++];
2168
2169 foreach my $old_line (@{$linesRef}) {
2170 my $save_line = 1;
2171 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002172 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002173 $delta_offset = 0;
2174 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2175 $range_last_linenr = $new_linenr;
2176 fixup_current_range(\$line, $delta_offset, 0);
2177 }
2178
2179 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2180 $deleted = @{$deletedRef}[$next_delete++];
2181 $save_line = 0;
2182 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2183 }
2184
2185 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2186 push(@lines, ${$inserted}{'LINE'});
2187 $inserted = @{$insertedRef}[$next_insert++];
2188 $new_linenr++;
2189 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2190 }
2191
2192 if ($save_line) {
2193 push(@lines, $line);
2194 $new_linenr++;
2195 }
2196
2197 $old_linenr++;
2198 }
2199
2200 return @lines;
2201}
2202
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002203sub fix_insert_line {
2204 my ($linenr, $line) = @_;
2205
2206 my $inserted = {
2207 LINENR => $linenr,
2208 LINE => $line,
2209 };
2210 push(@fixed_inserted, $inserted);
2211}
2212
2213sub fix_delete_line {
2214 my ($linenr, $line) = @_;
2215
2216 my $deleted = {
2217 LINENR => $linenr,
2218 LINE => $line,
2219 };
2220
2221 push(@fixed_deleted, $deleted);
2222}
2223
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002224sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002225 my ($type, $msg) = @_;
2226
2227 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002228 our $clean = 0;
2229 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002230 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002231 }
Joe Perches3705ce52013-07-03 15:05:31 -07002232 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002233}
2234sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002235 my ($type, $msg) = @_;
2236
2237 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002238 our $clean = 0;
2239 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002240 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002241 }
Joe Perches3705ce52013-07-03 15:05:31 -07002242 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002243}
2244sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002245 my ($type, $msg) = @_;
2246
2247 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002248 our $clean = 0;
2249 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002250 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002251 }
Joe Perches3705ce52013-07-03 15:05:31 -07002252 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002253}
2254
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002255sub check_absolute_file {
2256 my ($absolute, $herecurr) = @_;
2257 my $file = $absolute;
2258
2259 ##print "absolute<$absolute>\n";
2260
2261 # See if any suffix of this path is a path within the tree.
2262 while ($file =~ s@^[^/]*/@@) {
2263 if (-f "$root/$file") {
2264 ##print "file<$file>\n";
2265 last;
2266 }
2267 }
2268 if (! -f _) {
2269 return 0;
2270 }
2271
2272 # It is, so see if the prefix is acceptable.
2273 my $prefix = $absolute;
2274 substr($prefix, -length($file)) = '';
2275
2276 ##print "prefix<$prefix>\n";
2277 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002278 WARN("USE_RELATIVE_PATH",
2279 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002280 }
2281}
2282
Joe Perches3705ce52013-07-03 15:05:31 -07002283sub trim {
2284 my ($string) = @_;
2285
Joe Perchesb34c6482013-09-11 14:24:01 -07002286 $string =~ s/^\s+|\s+$//g;
2287
2288 return $string;
2289}
2290
2291sub ltrim {
2292 my ($string) = @_;
2293
2294 $string =~ s/^\s+//;
2295
2296 return $string;
2297}
2298
2299sub rtrim {
2300 my ($string) = @_;
2301
2302 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002303
2304 return $string;
2305}
2306
Joe Perches52ea8502013-11-12 15:10:09 -08002307sub string_find_replace {
2308 my ($string, $find, $replace) = @_;
2309
2310 $string =~ s/$find/$replace/g;
2311
2312 return $string;
2313}
2314
Joe Perches3705ce52013-07-03 15:05:31 -07002315sub tabify {
2316 my ($leading) = @_;
2317
Antonio Borneo713a09d2020-04-06 20:11:07 -07002318 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002319 my $max_spaces_before_tab = $source_indent - 1;
2320 my $spaces_to_tab = " " x $source_indent;
2321
2322 #convert leading spaces to tabs
2323 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2324 #Remove spaces before a tab
2325 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2326
2327 return "$leading";
2328}
2329
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002330sub pos_last_openparen {
2331 my ($line) = @_;
2332
2333 my $pos = 0;
2334
2335 my $opens = $line =~ tr/\(/\(/;
2336 my $closes = $line =~ tr/\)/\)/;
2337
2338 my $last_openparen = 0;
2339
2340 if (($opens == 0) || ($closes >= $opens)) {
2341 return -1;
2342 }
2343
2344 my $len = length($line);
2345
2346 for ($pos = 0; $pos < $len; $pos++) {
2347 my $string = substr($line, $pos);
2348 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2349 $pos += length($1) - 1;
2350 } elsif (substr($line, $pos, 1) eq '(') {
2351 $last_openparen = $pos;
2352 } elsif (index($string, '(') == -1) {
2353 last;
2354 }
2355 }
2356
Joe Perches91cb5192014-04-03 14:49:32 -07002357 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002358}
2359
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002360sub get_raw_comment {
2361 my ($line, $rawline) = @_;
2362 my $comment = '';
2363
2364 for my $i (0 .. (length($line) - 1)) {
2365 if (substr($line, $i, 1) eq "$;") {
2366 $comment .= substr($rawline, $i, 1);
2367 }
2368 }
2369
2370 return $comment;
2371}
2372
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002373sub process {
2374 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002375
2376 my $linenr=0;
2377 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002378 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002379 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002380 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002381
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002382 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002383 my $indent;
2384 my $previndent=0;
2385 my $stashindent=0;
2386
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002387 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002388 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002389 my $author = '';
2390 my $authorsignoff = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002391 my $author_sob = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002392 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002393 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002394 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002395 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002396 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002397 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002398 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002399 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002400 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002401 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002402 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002403 my $non_utf8_charset = 0;
2404
Joe Perches365dd4e2014-08-06 16:10:42 -07002405 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002406 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002407
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002408 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002409 our $cnt_lines = 0;
2410 our $cnt_error = 0;
2411 our $cnt_warn = 0;
2412 our $cnt_chk = 0;
2413
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002414 # Trace the real file/line as we go.
2415 my $realfile = '';
2416 my $realline = 0;
2417 my $realcnt = 0;
2418 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002419 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002420 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002421 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002422 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002423 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002424
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002425 my $prev_values = 'E';
2426
2427 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002428 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002429 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002430 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002431 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002432
Joe Perches7e51f192013-09-11 14:23:57 -07002433 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002434
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002435 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002436 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002437 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002438 my @setup_docs = ();
2439 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002440
Joe Perchesd8b07712013-11-12 15:10:06 -08002441 my $camelcase_file_seeded = 0;
2442
Rob Herring9f3a8992018-04-10 16:33:13 -07002443 my $checklicenseline = 1;
2444
Andy Whitcroft773647a2008-03-28 14:15:58 -07002445 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002446 my $line;
2447 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002448 $linenr++;
2449 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002450
Joe Perches3705ce52013-07-03 15:05:31 -07002451 push(@fixed, $rawline) if ($fix);
2452
Andy Whitcroft773647a2008-03-28 14:15:58 -07002453 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002454 $setup_docs = 0;
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07002455 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002456 $setup_docs = 1;
2457 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002458 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002459 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002460 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002461 $realline=$1-1;
2462 if (defined $2) {
2463 $realcnt=$3+1;
2464 } else {
2465 $realcnt=1+1;
2466 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002467 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002468
2469 # Guestimate if this is a continuing comment. Run
2470 # the context looking for a comment "edge". If this
2471 # edge is a close comment then we must be in a comment
2472 # at context start.
2473 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002474 my $cnt = $realcnt;
2475 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2476 next if (defined $rawlines[$ln - 1] &&
2477 $rawlines[$ln - 1] =~ /^-/);
2478 $cnt--;
2479 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002480 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002481 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2482 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2483 ($edge) = $1;
2484 last;
2485 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002486 }
2487 if (defined $edge && $edge eq '*/') {
2488 $in_comment = 1;
2489 }
2490
2491 # Guestimate if this is a continuing comment. If this
2492 # is the start of a diff block and this line starts
2493 # ' *' then it is very likely a comment.
2494 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002495 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002496 {
2497 $in_comment = 1;
2498 }
2499
2500 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2501 sanitise_line_reset($in_comment);
2502
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002503 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002504 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002505 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002506 $line = sanitise_line($rawline);
2507 }
2508 push(@lines, $line);
2509
2510 if ($realcnt > 1) {
2511 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2512 } else {
2513 $realcnt = 0;
2514 }
2515
2516 #print "==>$rawline\n";
2517 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002518
2519 if ($setup_docs && $line =~ /^\+/) {
2520 push(@setup_docs, $line);
2521 }
2522 }
2523
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002524 $prefix = '';
2525
Andy Whitcroft773647a2008-03-28 14:15:58 -07002526 $realcnt = 0;
2527 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002528 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002529 foreach my $line (@lines) {
2530 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002531 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002532 my $sline = $line; #copy of $line
2533 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002534
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002535 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002536 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002537
Joe Perches12c253a2018-06-07 17:10:58 -07002538# check if it's a mode change, rename or start of a patch
2539 if (!$in_commit_log &&
2540 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2541 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2542 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2543 $is_patch = 1;
2544 }
2545
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002546#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002547 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002548 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2549 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002550 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002551 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002552 $realline=$1-1;
2553 if (defined $2) {
2554 $realcnt=$3+1;
2555 } else {
2556 $realcnt=1+1;
2557 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002558 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002559 $prev_values = 'E';
2560
Andy Whitcroft773647a2008-03-28 14:15:58 -07002561 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002562 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002563 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002564 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002565 if ($context =~ /\b(\w+)\s*\(/) {
2566 $context_function = $1;
2567 } else {
2568 undef $context_function;
2569 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002570 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002571
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002572# track the line number as we move through the hunk, note that
2573# new versions of GNU diff omit the leading space on completely
2574# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002575 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002576 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002577 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002578
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002579 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002580 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002581
2582 # Track the previous line.
2583 ($prevline, $stashline) = ($stashline, $line);
2584 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002585 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2586
Andy Whitcroft773647a2008-03-28 14:15:58 -07002587 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002588
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002589 } elsif ($realcnt == 1) {
2590 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002591 }
2592
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002593 my $hunk_line = ($realcnt != 0);
2594
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002595 $here = "#$linenr: " if (!$file);
2596 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002597
Joe Perches2ac73b42014-06-04 16:12:05 -07002598 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002599 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002600 if ($line =~ /^diff --git.*?(\S+)$/) {
2601 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002602 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002603 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002604 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002605 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002606 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002607 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002608 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002609
2610 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002611 if (!$file && $tree && $p1_prefix ne '' &&
2612 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002613 WARN("PATCH_PREFIX",
2614 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002615 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002616
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002617 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002618 ERROR("MODIFIED_INCLUDE_ASM",
2619 "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 -07002620 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002621 $found_file = 1;
2622 }
2623
Joe Perches34d88152015-06-25 15:03:05 -07002624#make up the handle for any error we report on this line
2625 if ($showfile) {
2626 $prefix = "$realfile:$realline: "
2627 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002628 if ($file) {
2629 $prefix = "$filename:$realline: ";
2630 } else {
2631 $prefix = "$filename:$linenr: ";
2632 }
Joe Perches34d88152015-06-25 15:03:05 -07002633 }
2634
Joe Perches2ac73b42014-06-04 16:12:05 -07002635 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002636 if (is_maintained_obsolete($realfile)) {
2637 WARN("OBSOLETE",
2638 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2639 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002640 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002641 $check = 1;
2642 } else {
2643 $check = $check_orig;
2644 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002645 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002646
2647 if ($realfile !~ /^MAINTAINERS/) {
2648 my $last_binding_patch = $is_binding_patch;
2649
2650 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2651
2652 if (($last_binding_patch != -1) &&
2653 ($last_binding_patch ^ $is_binding_patch)) {
2654 WARN("DT_SPLIT_BINDING_PATCH",
Mauro Carvalho Chehab858e6842020-04-15 16:45:25 +02002655 "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 -07002656 }
2657 }
2658
Andy Whitcroft773647a2008-03-28 14:15:58 -07002659 next;
2660 }
2661
Randy Dunlap389834b2007-06-08 13:47:03 -07002662 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002663
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002664 my $hereline = "$here\n$rawline\n";
2665 my $herecurr = "$here\n$rawline\n";
2666 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002667
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002668 $cnt_lines++ if ($realcnt != 0);
2669
Joe Perches490b2922018-08-21 21:58:01 -07002670# Verify the existence of a commit log if appropriate
2671# 2 is used because a $signature is counted in $commit_log_lines
2672 if ($in_commit_log) {
2673 if ($line !~ /^\s*$/) {
2674 $commit_log_lines++; #could be a $signature
2675 }
2676 } elsif ($has_commit_log && $commit_log_lines < 2) {
2677 WARN("COMMIT_MESSAGE",
2678 "Missing commit description - Add an appropriate one\n");
2679 $commit_log_lines = 2; #warn only once
2680 }
2681
Joe Perchese518e9a2015-06-25 15:03:27 -07002682# Check if the commit log has what seems like a diff which can confuse patch
2683 if ($in_commit_log && !$commit_log_has_diff &&
Mrinal Pandey13e45412020-09-04 16:35:52 -07002684 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2685 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
Joe Perchese518e9a2015-06-25 15:03:27 -07002686 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2687 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2688 ERROR("DIFF_IN_COMMIT_MSG",
2689 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2690 $commit_log_has_diff = 1;
2691 }
2692
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002693# Check for incorrect file permissions
2694 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2695 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002696 if ($realfile !~ m@scripts/@ &&
2697 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002698 ERROR("EXECUTE_PERMISSIONS",
2699 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002700 }
2701 }
2702
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002703# Check the patch for a From:
2704 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2705 $author = $1;
Dwaipayan Raye7f929f2020-10-15 20:12:15 -07002706 my $curline = $linenr;
2707 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2708 $author .= $1;
2709 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002710 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2711 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002712 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002713 }
2714
Joe Perches20112472011-07-25 17:13:23 -07002715# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002716 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002717 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002718 $in_commit_log = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002719 if ($author ne '' && $authorsignoff != 1) {
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002720 if (same_email_addresses($1, $author)) {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002721 $authorsignoff = 1;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002722 } else {
2723 my $ctx = $1;
2724 my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
2725 my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
2726
2727 if ($email_address eq $author_address && $email_name eq $author_name) {
2728 $author_sob = $ctx;
2729 $authorsignoff = 2;
2730 } elsif ($email_address eq $author_address) {
2731 $author_sob = $ctx;
2732 $authorsignoff = 3;
2733 } elsif ($email_name eq $author_name) {
2734 $author_sob = $ctx;
2735 $authorsignoff = 4;
2736
2737 my $address1 = $email_address;
2738 my $address2 = $author_address;
2739
2740 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
2741 $address1 = "$1$2";
2742 }
2743 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
2744 $address2 = "$1$2";
2745 }
2746 if ($address1 eq $address2) {
2747 $authorsignoff = 5;
2748 }
2749 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002750 }
2751 }
Joe Perches20112472011-07-25 17:13:23 -07002752 }
2753
Joe Perches44d303e2020-04-06 20:11:10 -07002754# Check for patch separator
2755 if ($line =~ /^---$/) {
2756 $has_patch_separator = 1;
2757 $in_commit_log = 0;
2758 }
2759
Joe Perchese0d975b2014-12-10 15:51:49 -08002760# Check if MAINTAINERS is being updated. If so, there's probably no need to
2761# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2762 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2763 $reported_maintainer_file = 1;
2764 }
2765
Joe Perches20112472011-07-25 17:13:23 -07002766# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002767 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002768 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002769 my $space_before = $1;
2770 my $sign_off = $2;
2771 my $space_after = $3;
2772 my $email = $4;
2773 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2774
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002775 if ($sign_off !~ /$signature_tags/) {
2776 WARN("BAD_SIGN_OFF",
2777 "Non-standard signature: $sign_off\n" . $herecurr);
2778 }
Joe Perches20112472011-07-25 17:13:23 -07002779 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002780 if (WARN("BAD_SIGN_OFF",
2781 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2782 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002783 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002784 "$ucfirst_sign_off $email";
2785 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002786 }
Joe Perches20112472011-07-25 17:13:23 -07002787 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002788 if (WARN("BAD_SIGN_OFF",
2789 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2790 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002791 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002792 "$ucfirst_sign_off $email";
2793 }
2794
Joe Perches20112472011-07-25 17:13:23 -07002795 }
2796 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002797 if (WARN("BAD_SIGN_OFF",
2798 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2799 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002800 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002801 "$ucfirst_sign_off $email";
2802 }
Joe Perches20112472011-07-25 17:13:23 -07002803 }
2804
Joe Perchesdfa05c22020-04-06 20:10:48 -07002805 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002806 my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
Joe Perches20112472011-07-25 17:13:23 -07002807 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002808 ERROR("BAD_SIGN_OFF",
2809 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002810 } else {
2811 my $dequoted = $suggested_email;
2812 $dequoted =~ s/^"//;
2813 $dequoted =~ s/" </ </;
2814 # Don't force email to have quotes
2815 # Allow just an angle bracketed address
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002816 if (!same_email_addresses($email, $suggested_email)) {
2817 if (WARN("BAD_SIGN_OFF",
2818 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
2819 $fix) {
2820 $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
2821 }
2822 }
2823
2824 # Address part shouldn't have comments
2825 my $stripped_address = $email_address;
2826 $stripped_address =~ s/\([^\(\)]*\)//g;
2827 if ($email_address ne $stripped_address) {
2828 if (WARN("BAD_SIGN_OFF",
2829 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
2830 $fix) {
2831 $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
2832 }
2833 }
2834
2835 # Only one name comment should be allowed
2836 my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
2837 if ($comment_count > 1) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002838 WARN("BAD_SIGN_OFF",
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002839 "Use a single name comment in email: '$email'\n" . $herecurr);
2840 }
2841
2842
2843 # stable@vger.kernel.org or stable@kernel.org shouldn't
Dwaipayan Raye73d2712020-12-15 20:44:56 -08002844 # have an email name. In addition comments should strictly
Dwaipayan Rayfccaebf2020-12-15 20:44:53 -08002845 # begin with a #
2846 if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
2847 if (($comment ne "" && $comment !~ /^#.+/) ||
2848 ($email_name ne "")) {
2849 my $cur_name = $email_name;
2850 my $new_comment = $comment;
2851 $cur_name =~ s/[a-zA-Z\s\-\"]+//g;
2852
2853 # Remove brackets enclosing comment text
2854 # and # from start of comments to get comment text
2855 $new_comment =~ s/^\((.*)\)$/$1/;
2856 $new_comment =~ s/^\[(.*)\]$/$1/;
2857 $new_comment =~ s/^[\s\#]+|\s+$//g;
2858
2859 $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
2860 $new_comment = " # $new_comment" if ($new_comment ne "");
2861 my $new_email = "$email_address$new_comment";
2862
2863 if (WARN("BAD_STABLE_ADDRESS_STYLE",
2864 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
2865 $fix) {
2866 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2867 }
2868 }
2869 } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
2870 my $new_comment = $comment;
2871
2872 # Extract comment text from within brackets or
2873 # c89 style /*...*/ comments
2874 $new_comment =~ s/^\[(.*)\]$/$1/;
2875 $new_comment =~ s/^\/\*(.*)\*\/$/$1/;
2876
2877 $new_comment = trim($new_comment);
2878 $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
2879 $new_comment = "($new_comment)" if ($new_comment ne "");
2880 my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
2881
2882 if (WARN("BAD_SIGN_OFF",
2883 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
2884 $fix) {
2885 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
2886 }
Joe Perches20112472011-07-25 17:13:23 -07002887 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002888 }
Joe Perches7e51f192013-09-11 14:23:57 -07002889
2890# Check for duplicate signatures
2891 my $sig_nospace = $line;
2892 $sig_nospace =~ s/\s//g;
2893 $sig_nospace = lc($sig_nospace);
2894 if (defined $signatures{$sig_nospace}) {
2895 WARN("BAD_SIGN_OFF",
2896 "Duplicate signature\n" . $herecurr);
2897 } else {
2898 $signatures{$sig_nospace} = 1;
2899 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002900
2901# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2902 if ($sign_off =~ /^co-developed-by:$/i) {
2903 if ($email eq $author) {
2904 WARN("BAD_SIGN_OFF",
2905 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2906 }
2907 if (!defined $lines[$linenr]) {
2908 WARN("BAD_SIGN_OFF",
2909 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2910 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2911 WARN("BAD_SIGN_OFF",
2912 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2913 } elsif ($1 ne $email) {
2914 WARN("BAD_SIGN_OFF",
2915 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2916 }
2917 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002918 }
2919
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002920# Check email subject for common tools that don't need to be mentioned
2921 if ($in_header_lines &&
2922 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2923 WARN("EMAIL_SUBJECT",
2924 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2925 }
2926
Joe Perches44d303e2020-04-06 20:11:10 -07002927# Check for Gerrit Change-Ids not in any patch context
2928 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Aditya Srivastava7580c5b2020-12-15 20:44:47 -08002929 if (ERROR("GERRIT_CHANGE_ID",
2930 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
2931 $fix) {
2932 fix_delete_line($fixlinenr, $rawline);
2933 }
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002934 }
2935
Joe Perches369c8dd2015-11-06 16:31:34 -08002936# Check if the commit log is in a possible stack dump
2937 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2938 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2939 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2940 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07002941 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2942 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2943 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2944 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08002945 $commit_log_possible_stack_dump = 1;
2946 }
2947
Joe Perches2a076f42015-04-16 12:44:28 -07002948# Check for line lengths > 75 in commit log, warn once
2949 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002950 length($line) > 75 &&
2951 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2952 # file delta changes
2953 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2954 # filename then :
Aditya Srivastava27b379a2020-12-15 20:44:59 -08002955 $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
2956 # A Fixes: or Link: line or signature tag line
Joe Perches369c8dd2015-11-06 16:31:34 -08002957 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002958 WARN("COMMIT_LOG_LONG_LINE",
2959 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2960 $commit_log_long_line = 1;
2961 }
2962
Joe Perchesbf4daf12015-09-09 15:37:50 -07002963# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002964 if ($in_commit_log && $commit_log_possible_stack_dump &&
2965 $line =~ /^\s*$/) {
2966 $commit_log_possible_stack_dump = 0;
2967 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002968
Joe Perches0d7835f2015-02-13 14:38:35 -08002969# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002970 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07002971 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002972 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002973 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002974 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002975 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2976 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002977 my $init_char = "c";
2978 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002979 my $short = 1;
2980 my $long = 0;
2981 my $case = 1;
2982 my $space = 1;
2983 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002984 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002985 my $id = '0123456789ab';
2986 my $orig_desc = "commit description";
2987 my $description = "";
2988
Joe Perchesfe043ea2015-09-09 15:37:25 -07002989 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2990 $init_char = $1;
2991 $orig_commit = lc($2);
2992 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2993 $orig_commit = lc($1);
2994 }
2995
Joe Perches0d7835f2015-02-13 14:38:35 -08002996 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2997 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2998 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2999 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
3000 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
3001 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08003002 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08003003 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
3004 defined $rawlines[$linenr] &&
3005 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
3006 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08003007 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08003008 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
3009 defined $rawlines[$linenr] &&
3010 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
3011 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
3012 $orig_desc = $1;
3013 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
3014 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08003015 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08003016 }
3017
3018 ($id, $description) = git_commit_info($orig_commit,
3019 $id, $orig_desc);
3020
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07003021 if (defined($id) &&
3022 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08003023 ERROR("GIT_COMMIT_ID",
3024 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
3025 }
Joe Perchesd311cd42014-08-06 16:10:57 -07003026 }
3027
Joe Perches13f19372014-08-06 16:10:59 -07003028# Check for added, moved or deleted files
3029 if (!$reported_maintainer_file && !$in_commit_log &&
3030 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
3031 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
3032 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
3033 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08003034 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07003035 $reported_maintainer_file = 1;
3036 WARN("FILE_PATH_CHANGES",
3037 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
3038 }
3039
Rob Herringe400edb2019-09-12 15:18:20 +01003040# Check for adding new DT bindings not in schema format
3041 if (!$in_commit_log &&
3042 ($line =~ /^new file mode\s*\d+\s*$/) &&
3043 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3044 WARN("DT_SCHEMA_BINDING_PATCH",
3045 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
3046 }
3047
Andy Whitcroft00df3442007-06-08 13:47:06 -07003048# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08003049 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003050 ERROR("CORRUPTED_PATCH",
3051 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003052 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003053 }
3054
3055# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3056 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003057 $rawline !~ m/^$UTF8*$/) {
3058 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3059
3060 my $blank = copy_spacing($rawline);
3061 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3062 my $hereptr = "$hereline$ptr\n";
3063
Joe Perches34d99212011-07-25 17:13:26 -07003064 CHK("INVALID_UTF8",
3065 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003066 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003067
Joe Perches15662b32011-10-31 17:13:12 -07003068# Check if it's the start of a commit log
3069# (not a header line and we haven't seen the patch filename)
3070 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07003071 !($rawline =~ /^\s+(?:\S|$)/ ||
3072 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07003073 $in_header_lines = 0;
3074 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07003075 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07003076 }
3077
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003078# Check if there is UTF-8 in a commit log when a mail header has explicitly
3079# declined it, i.e defined some charset where it is missing.
3080 if ($in_header_lines &&
3081 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3082 $1 !~ /utf-8/i) {
3083 $non_utf8_charset = 1;
3084 }
3085
3086 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07003087 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003088 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07003089 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3090 }
3091
Joe Perchesd6430f72016-12-12 16:46:28 -08003092# Check for absolute kernel paths in commit message
3093 if ($tree && $in_commit_log) {
3094 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3095 my $file = $1;
3096
3097 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3098 check_absolute_file($1, $herecurr)) {
3099 #
3100 } else {
3101 check_absolute_file($file, $herecurr);
3102 }
3103 }
3104 }
3105
Kees Cook66b47b42014-10-13 15:51:57 -07003106# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07003107 if (defined($misspellings) &&
3108 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07003109 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07003110 my $typo = $1;
3111 my $typo_fix = $spelling_fix{lc($typo)};
3112 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3113 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07003114 my $msg_level = \&WARN;
3115 $msg_level = \&CHK if ($file);
3116 if (&{$msg_level}("TYPO_SPELLING",
3117 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07003118 $fix) {
3119 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3120 }
3121 }
3122 }
3123
Matteo Crocea8dd86b2019-09-25 16:46:38 -07003124# check for invalid commit id
3125 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3126 my $id;
3127 my $description;
3128 ($id, $description) = git_commit_info($2, undef, undef);
3129 if (!defined($id)) {
3130 WARN("UNKNOWN_COMMIT_ID",
3131 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3132 }
3133 }
3134
Joe Perches310cd062020-10-15 20:11:52 -07003135# check for repeated words separated by a single space
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003136# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3137 if (($rawline =~ /^\+/ || $in_commit_log) &&
3138 $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003139 pos($rawline) = 1 if (!$in_commit_log);
Joe Perches310cd062020-10-15 20:11:52 -07003140 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3141
3142 my $first = $1;
3143 my $second = $2;
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003144 my $start_pos = $-[1];
3145 my $end_pos = $+[2];
Joe Perches310cd062020-10-15 20:11:52 -07003146 if ($first =~ /(?:struct|union|enum)/) {
3147 pos($rawline) += length($first) + length($second) + 1;
3148 next;
3149 }
3150
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003151 next if (lc($first) ne lc($second));
Joe Perches310cd062020-10-15 20:11:52 -07003152 next if ($first eq 'long');
3153
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003154 # check for character before and after the word matches
3155 my $start_char = '';
3156 my $end_char = '';
3157 $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
3158 $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3159
3160 next if ($start_char =~ /^\S$/);
3161 next if (index(" \t.,;?!", $end_char) == -1);
3162
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003163 # avoid repeating hex occurrences like 'ff ff fe 09 ...'
3164 if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3165 next if (!exists($allow_repeated_words{lc($first)}));
3166 }
3167
Joe Perches310cd062020-10-15 20:11:52 -07003168 if (WARN("REPEATED_WORD",
3169 "Possible repeated word: '$first'\n" . $herecurr) &&
3170 $fix) {
3171 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3172 }
3173 }
3174
3175 # if it's a repeated word on consecutive lines in a comment block
3176 if ($prevline =~ /$;+\s*$/ &&
3177 $prevrawline =~ /($word_pattern)\s*$/) {
3178 my $last_word = $1;
3179 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3180 if (WARN("REPEATED_WORD",
3181 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3182 $fix) {
3183 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3184 }
3185 }
3186 }
3187 }
3188
Andy Whitcroft306708542008-10-15 22:02:28 -07003189# ignore non-hunk lines and lines being removed
3190 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003191
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003192#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003193 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003194 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07003195 if (ERROR("DOS_LINE_ENDINGS",
3196 "DOS line endings\n" . $herevet) &&
3197 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003198 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003199 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003200 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3201 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003202 if (ERROR("TRAILING_WHITESPACE",
3203 "trailing whitespace\n" . $herevet) &&
3204 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003205 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003206 }
3207
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003208 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003209 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07003210
Josh Triplett4783f892013-11-12 15:10:12 -08003211# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003212 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003213 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003214 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3215 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003216 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003217 my $msg_level = \&ERROR;
3218 $msg_level = \&CHK if ($file);
3219 &{$msg_level}("FSF_MAILING_ADDRESS",
3220 "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 -08003221 }
3222
Andi Kleen33549572010-05-24 14:33:29 -07003223# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003224# Only applies when adding the entry originally, after that we do not have
3225# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003226 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003227 # 'choice' is usually the last thing on the line (though
3228 # Kconfig supports named choices), so use a word boundary
3229 # (\b) rather than a whitespace character (\s)
3230 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003231 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003232 my $cnt = $realcnt;
3233 my $ln = $linenr + 1;
3234 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003235 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003236 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003237 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003238 $f = $lines[$ln - 1];
3239 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3240 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003241
3242 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003243 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003244
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003245 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003246 $is_start = 1;
Masahiro Yamada22a4ac02020-06-17 12:02:20 +09003247 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003248 $length = -1;
3249 }
3250
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003251 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003252 $f =~ s/#.*//;
3253 $f =~ s/^\s+//;
3254 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003255
3256 # This only checks context lines in the patch
3257 # and so hopefully shouldn't trigger false
3258 # positives, even though some of these are
3259 # common words in help texts
3260 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3261 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003262 $is_end = 1;
3263 last;
3264 }
Andi Kleen33549572010-05-24 14:33:29 -07003265 $length++;
3266 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003267 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3268 WARN("CONFIG_DESCRIPTION",
3269 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3270 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003271 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003272 }
3273
Joe Perches7ccf41a2020-06-04 16:50:33 -07003274# check MAINTAINERS entries
3275 if ($realfile =~ /^MAINTAINERS$/) {
3276# check MAINTAINERS entries for the right form
3277 if ($rawline =~ /^\+[A-Z]:/ &&
3278 $rawline !~ /^\+[A-Z]:\t\S/) {
3279 if (WARN("MAINTAINERS_STYLE",
3280 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3281 $fix) {
3282 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3283 }
3284 }
3285# check MAINTAINERS entries for the right ordering too
3286 my $preferred_order = 'MRLSWQBCPTFXNK';
3287 if ($rawline =~ /^\+[A-Z]:/ &&
3288 $prevrawline =~ /^[\+ ][A-Z]:/) {
3289 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3290 my $cur = $1;
3291 my $curval = $2;
3292 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3293 my $prev = $1;
3294 my $prevval = $2;
3295 my $curindex = index($preferred_order, $cur);
3296 my $previndex = index($preferred_order, $prev);
3297 if ($curindex < 0) {
3298 WARN("MAINTAINERS_STYLE",
3299 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3300 } else {
3301 if ($previndex >= 0 && $curindex < $previndex) {
3302 WARN("MAINTAINERS_STYLE",
3303 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3304 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3305 ($prev eq 'X' && $cur eq 'X')) &&
3306 ($prevval cmp $curval) > 0) {
3307 WARN("MAINTAINERS_STYLE",
3308 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3309 }
3310 }
Joe Perches628f91a2017-07-10 15:52:07 -07003311 }
3312 }
3313
Christoph Jaeger327953e2015-02-13 14:38:29 -08003314# discourage the use of boolean for type definition attributes of Kconfig options
3315 if ($realfile =~ /Kconfig/ &&
3316 $line =~ /^\+\s*\bboolean\b/) {
3317 WARN("CONFIG_TYPE_BOOLEAN",
3318 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3319 }
3320
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003321 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3322 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3323 my $flag = $1;
3324 my $replacement = {
3325 'EXTRA_AFLAGS' => 'asflags-y',
3326 'EXTRA_CFLAGS' => 'ccflags-y',
3327 'EXTRA_CPPFLAGS' => 'cppflags-y',
3328 'EXTRA_LDFLAGS' => 'ldflags-y',
3329 };
3330
3331 WARN("DEPRECATED_VARIABLE",
3332 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3333 }
3334
Rob Herringbff5da42014-01-23 15:54:51 -08003335# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003336 if (defined $root &&
3337 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3338 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3339
Rob Herringbff5da42014-01-23 15:54:51 -08003340 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3341
Florian Vaussardcc933192014-04-03 14:49:27 -07003342 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003343 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003344
Rob Herringbff5da42014-01-23 15:54:51 -08003345 foreach my $compat (@compats) {
3346 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003347 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3348 my $compat3 = $compat;
3349 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3350 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003351 if ( $? >> 8 ) {
3352 WARN("UNDOCUMENTED_DT_STRING",
3353 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3354 }
3355
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003356 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3357 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003358 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003359 if ( $? >> 8 ) {
3360 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003361 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003362 }
3363 }
3364 }
3365
Rob Herring9f3a8992018-04-10 16:33:13 -07003366# check for using SPDX license tag at beginning of files
3367 if ($realline == $checklicenseline) {
3368 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3369 $checklicenseline = 2;
3370 } elsif ($rawline =~ /^\+/) {
3371 my $comment = "";
3372 if ($realfile =~ /\.(h|s|S)$/) {
3373 $comment = '/*';
3374 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3375 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003376 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003377 $comment = '#';
3378 } elsif ($realfile =~ /\.rst$/) {
3379 $comment = '..';
3380 }
3381
Joe Perchesfdf13692019-03-07 16:28:32 -08003382# check SPDX comment style for .[chsS] files
3383 if ($realfile =~ /\.[chsS]$/ &&
3384 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003385 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003386 WARN("SPDX_LICENSE_TAG",
3387 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3388 }
3389
Rob Herring9f3a8992018-04-10 16:33:13 -07003390 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003391 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3392 WARN("SPDX_LICENSE_TAG",
3393 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003394 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003395 my $spdx_license = $1;
3396 if (!is_SPDX_License_valid($spdx_license)) {
3397 WARN("SPDX_LICENSE_TAG",
3398 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3399 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003400 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3401 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3402 my $msg_level = \&WARN;
3403 $msg_level = \&CHK if ($file);
3404 if (&{$msg_level}("SPDX_LICENSE_TAG",
3405
3406 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3407 $fix) {
3408 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3409 }
3410 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003411 }
3412 }
3413 }
3414
Joe Perchesa0154cd2020-10-15 20:12:19 -07003415# check for embedded filenames
3416 if ($rawline =~ /^\+.*\Q$realfile\E/) {
3417 WARN("EMBEDDED_FILENAME",
3418 "It's generally not useful to have the filename in the file\n" . $herecurr);
3419 }
3420
Andy Whitcroft5368df202008-10-15 22:02:27 -07003421# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003422 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003423
Joe Perchesa8da38a2019-03-07 16:28:42 -08003424# check for using SPDX-License-Identifier on the wrong line number
3425 if ($realline != $checklicenseline &&
3426 $rawline =~ /\bSPDX-License-Identifier:/ &&
3427 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3428 WARN("SPDX_LICENSE_TAG",
3429 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3430 }
3431
Joe Perches47e0c882015-06-25 15:02:57 -07003432# line length limit (with some exclusions)
3433#
3434# There are a few types of lines that may extend beyond $max_line_length:
3435# logging functions like pr_info that end in a string
3436# lines with a single string
3437# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003438# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003439#
3440# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003441# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003442# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3443# LONG_LINE all other lines longer than $max_line_length
3444#
3445# if LONG_LINE is ignored, the other 2 types are also ignored
3446#
3447
Joe Perchesb4749e92015-07-17 16:24:01 -07003448 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003449 my $msg_type = "LONG_LINE";
3450
3451 # Check the allowed long line types first
3452
3453 # logging functions that end in a string that starts
3454 # before $max_line_length
3455 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3456 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3457 $msg_type = "";
3458
3459 # lines with only strings (w/ possible termination)
3460 # #defines with only strings
3461 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3462 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3463 $msg_type = "";
3464
Joe Perchescc147502017-11-17 15:28:44 -08003465 # More special cases
3466 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3467 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003468 $msg_type = "";
3469
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003470 # URL ($rawline is used in case the URL is in a comment)
3471 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3472 $msg_type = "";
3473
Joe Perches47e0c882015-06-25 15:02:57 -07003474 # Otherwise set the alternate message types
3475
3476 # a comment starts before $max_line_length
3477 } elsif ($line =~ /($;[\s$;]*)$/ &&
3478 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3479 $msg_type = "LONG_LINE_COMMENT"
3480
3481 # a quoted string starts before $max_line_length
3482 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3483 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3484 $msg_type = "LONG_LINE_STRING"
3485 }
3486
3487 if ($msg_type ne "" &&
3488 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003489 my $msg_level = \&WARN;
3490 $msg_level = \&CHK if ($file);
3491 &{$msg_level}($msg_type,
3492 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003493 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003494 }
3495
Andy Whitcroft8905a672007-11-28 16:21:06 -08003496# check for adding lines without a newline.
3497 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Tom Rix47ca69b2020-12-15 20:44:40 -08003498 if (WARN("MISSING_EOF_NEWLINE",
3499 "adding a line without newline at end of file\n" . $herecurr) &&
3500 $fix) {
3501 fix_delete_line($fixlinenr+1, "No newline at end of file");
3502 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003503 }
3504
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003505# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003506 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003507
3508# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003509# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003510 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3511 $rawline =~ /^\+\s* \s*/) {
3512 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003513 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003514 if (ERROR("CODE_INDENT",
3515 "code indent should use tabs where possible\n" . $herevet) &&
3516 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003517 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003518 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003519 }
3520
Alberto Panizzo08e44362010-03-05 13:43:54 -08003521# check for space before tabs.
3522 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3523 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003524 if (WARN("SPACE_BEFORE_TAB",
3525 "please, no space before tabs\n" . $herevet) &&
3526 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003527 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003528 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003529 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003530 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003531 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003532 }
3533
Joe Perches6a487212018-04-10 16:34:04 -07003534# check for assignments on the start of a line
3535 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
Aditya Srivastavada7355a2020-12-15 20:45:06 -08003536 my $operator = $1;
3537 if (CHK("ASSIGNMENT_CONTINUATIONS",
3538 "Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3539 $fix && $prevrawline =~ /^\+/) {
3540 # add assignment operator to the previous line, remove from current line
3541 $fixed[$fixlinenr - 1] .= " $operator";
3542 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3543 }
Joe Perches6a487212018-04-10 16:34:04 -07003544 }
3545
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003546# check for && or || at the start of a line
3547 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3548 CHK("LOGICAL_CONTINUATIONS",
3549 "Logical continuations should be on the previous line\n" . $hereprev);
3550 }
3551
Joe Perchesa91e8992016-05-20 17:04:05 -07003552# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003553 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003554 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003555 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003556 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003557 if (WARN("TABSTOP",
3558 "Statements should start on a tabstop\n" . $herecurr) &&
3559 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003560 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003561 }
3562 }
3563 }
3564
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003565# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003566 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003567 $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 -07003568 $prevline =~ /^\+(\t*)(.*)$/;
3569 my $oldindent = $1;
3570 my $rest = $2;
3571
3572 my $pos = pos_last_openparen($rest);
3573 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003574 $line =~ /^(\+| )([ \t]*)/;
3575 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003576
3577 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003578 "\t" x ($pos / $tabsize) .
3579 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003580 my $goodspaceindent = $oldindent . " " x $pos;
3581
3582 if ($newindent ne $goodtabindent &&
3583 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003584
3585 if (CHK("PARENTHESIS_ALIGNMENT",
3586 "Alignment should match open parenthesis\n" . $hereprev) &&
3587 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003588 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003589 s/^\+[ \t]*/\+$goodtabindent/;
3590 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003591 }
3592 }
3593 }
3594
Joe Perches6ab3a972015-04-16 12:44:05 -07003595# check for space after cast like "(int) foo" or "(struct foo) bar"
3596# avoid checking a few false positives:
3597# "sizeof(<type>)" or "__alignof__(<type>)"
3598# function pointer declarations like "(*foo)(int) = bar;"
3599# structure definitions like "(struct foo) { 0 };"
3600# multiline macros that define functions
3601# known attributes or the __attribute__ keyword
3602 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3603 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003604 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003605 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003606 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003607 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003608 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003609 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003610 }
3611
Joe Perches86406b12015-09-09 15:37:41 -07003612# Block comment styles
3613# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003614 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003615 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003616 $rawline =~ /^\+[ \t]*\*/ &&
Łukasz Stelmachc70735c2020-10-15 20:12:25 -07003617 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
Joe Perches05880602012-10-04 17:13:35 -07003618 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3619 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3620 }
3621
Joe Perches86406b12015-09-09 15:37:41 -07003622# Block comments use * on subsequent lines
3623 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3624 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003625 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003626 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003627 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003628 WARN("BLOCK_COMMENT_STYLE",
3629 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003630 }
3631
Joe Perches86406b12015-09-09 15:37:41 -07003632# Block comments use */ on trailing lines
3633 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003634 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3635 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3636 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003637 WARN("BLOCK_COMMENT_STYLE",
3638 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003639 }
3640
Joe Perches08eb9b82016-10-11 13:51:50 -07003641# Block comment * alignment
3642 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003643 $line =~ /^\+[ \t]*$;/ && #leading comment
3644 $rawline =~ /^\+[ \t]*\*/ && #leading *
3645 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003646 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003647 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3648 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003649 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003650 if (defined($1)) {
3651 $oldindent = expand_tabs($1);
3652 } else {
3653 $prevrawline =~ m@^\+(.*/?)\*@;
3654 $oldindent = expand_tabs($1);
3655 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003656 $rawline =~ m@^\+([ \t]*)\*@;
3657 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003658 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003659 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003660 WARN("BLOCK_COMMENT_STYLE",
3661 "Block comments should align the * on each line\n" . $hereprev);
3662 }
3663 }
3664
Joe Perches7f619192014-08-06 16:10:39 -07003665# check for missing blank lines after struct/union declarations
3666# with exceptions for various attributes and macros
3667 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3668 $line =~ /^\+/ &&
3669 !($line =~ /^\+\s*$/ ||
3670 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3671 $line =~ /^\+\s*MODULE_/i ||
3672 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3673 $line =~ /^\+[a-z_]*init/ ||
3674 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3675 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003676 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003677 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003678 if (CHK("LINE_SPACING",
3679 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3680 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003681 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003682 }
Joe Perches7f619192014-08-06 16:10:39 -07003683 }
3684
Joe Perches365dd4e2014-08-06 16:10:42 -07003685# check for multiple consecutive blank lines
3686 if ($prevline =~ /^[\+ ]\s*$/ &&
3687 $line =~ /^\+\s*$/ &&
3688 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003689 if (CHK("LINE_SPACING",
3690 "Please don't use multiple blank lines\n" . $hereprev) &&
3691 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003692 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003693 }
3694
Joe Perches365dd4e2014-08-06 16:10:42 -07003695 $last_blank_line = $linenr;
3696 }
3697
Joe Perches3b617e32014-04-03 14:49:28 -07003698# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003699 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3700 # actual declarations
3701 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003702 # function pointer declarations
3703 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003704 # foo bar; where foo is some local typedef or #define
3705 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3706 # known declaration macros
3707 $prevline =~ /^\+\s+$declaration_macros/) &&
3708 # for "else if" which can look like "$Ident $Ident"
3709 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3710 # other possible extensions of declaration lines
3711 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3712 # not starting a section or a macro "\" extended line
3713 $prevline =~ /(?:\{\s*|\\)$/) &&
3714 # looks like a declaration
3715 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003716 # function pointer declarations
3717 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003718 # foo bar; where foo is some local typedef or #define
3719 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3720 # known declaration macros
3721 $sline =~ /^\+\s+$declaration_macros/ ||
3722 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003723 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003724 # start or end of block or continuation of declaration
3725 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3726 # bitfield continuation
3727 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3728 # other possible extensions of declaration lines
3729 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3730 # indentation of previous and current line are the same
3731 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003732 if (WARN("LINE_SPACING",
3733 "Missing a blank line after declarations\n" . $hereprev) &&
3734 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003735 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003736 }
Joe Perches3b617e32014-04-03 14:49:28 -07003737 }
3738
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003739# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003740# Exceptions:
3741# 1) within comments
3742# 2) indented preprocessor commands
3743# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003744 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003745 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003746 if (WARN("LEADING_SPACE",
3747 "please, no spaces at the start of a line\n" . $herevet) &&
3748 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003749 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003750 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003751 }
3752
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003753# check we are in a valid C source file if not then ignore this hunk
3754 next if ($realfile !~ /\.(h|c)$/);
3755
Joe Perches5751a242017-11-17 15:28:52 -08003756# check for unusual line ending [ or (
3757 if ($line =~ /^\+.*([\[\(])\s*$/) {
3758 CHK("OPEN_ENDED_LINE",
3759 "Lines should not end with a '$1'\n" . $herecurr);
3760 }
3761
Joe Perches4dbed762017-05-08 15:55:39 -07003762# check if this appears to be the start function declaration, save the name
3763 if ($sline =~ /^\+\{\s*$/ &&
3764 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3765 $context_function = $1;
3766 }
3767
3768# check if this appears to be the end of function declaration
3769 if ($sline =~ /^\+\}\s*$/) {
3770 undef $context_function;
3771 }
3772
Joe Perches032a4c02014-08-06 16:10:29 -07003773# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003774# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003775# if the previous line is a break or return and is indented 1 tab more...
3776 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3777 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003778 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3779 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3780 defined $lines[$linenr] &&
3781 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003782 WARN("UNNECESSARY_ELSE",
3783 "else is not generally useful after a break or return\n" . $hereprev);
3784 }
3785 }
3786
Joe Perchesc00df192014-08-06 16:11:01 -07003787# check indentation of a line with a break;
Joe Perchesdc58bc52020-12-15 20:44:33 -08003788# if the previous line is a goto, return or break
3789# and is indented the same # of tabs
Joe Perchesc00df192014-08-06 16:11:01 -07003790 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3791 my $tabs = $1;
Joe Perchesdc58bc52020-12-15 20:44:33 -08003792 if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
3793 if (WARN("UNNECESSARY_BREAK",
3794 "break is not useful after a $1\n" . $hereprev) &&
3795 $fix) {
3796 fix_delete_line($fixlinenr, $rawline);
3797 }
Joe Perchesc00df192014-08-06 16:11:01 -07003798 }
3799 }
3800
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003801# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003802 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003803 WARN("CVS_KEYWORD",
3804 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003805 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003806
Joe Perches56e77d72013-02-21 16:44:14 -08003807# check for old HOTPLUG __dev<foo> section markings
3808 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3809 WARN("HOTPLUG_SECTION",
3810 "Using $1 is unnecessary\n" . $herecurr);
3811 }
3812
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003813# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003814 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3815 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003816#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003817 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003818 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003819 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003820 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003821 $stat =~ s/\n./\n /g;
3822 $cond =~ s/\n./\n /g;
3823
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003824#print "linenr<$linenr> <$stat>\n";
3825 # If this statement has no statement boundaries within
3826 # it there is no point in retrying a statement scan
3827 # until we hit end of it.
3828 my $frag = $stat; $frag =~ s/;+\s*$//;
3829 if ($frag !~ /(?:{|;)/) {
3830#print "skip<$line_nr_next>\n";
3831 $suppress_statement = $line_nr_next;
3832 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003833
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003834 # Find the real next line.
3835 $realline_next = $line_nr_next;
3836 if (defined $realline_next &&
3837 (!defined $lines[$realline_next - 1] ||
3838 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3839 $realline_next++;
3840 }
3841
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003842 my $s = $stat;
3843 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003844
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003845 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003846 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003847
3848 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003849 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003850
Andy Whitcroft463f2862009-09-21 17:04:34 -07003851 } elsif ($s =~ /^.\s*else\b/s) {
3852
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003853 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003854 } 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 -07003855 my $type = $1;
3856 $type =~ s/\s+/ /g;
3857 possible($type, "A:" . $s);
3858
Andy Whitcroft8905a672007-11-28 16:21:06 -08003859 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003860 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003861 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003862 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003863
3864 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003865 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003866 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003867 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003868
3869 # Check for any sort of function declaration.
3870 # int foo(something bar, other baz);
3871 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003872 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 -08003873 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003874
Andy Whitcroftcf655042008-03-04 14:28:20 -08003875 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003876 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003877 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003878
Andy Whitcroft8905a672007-11-28 16:21:06 -08003879 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003880 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003881
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003882 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003883 }
3884 }
3885 }
3886
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003887 }
3888
Andy Whitcroft653d4872007-06-23 17:16:34 -07003889#
3890# Checks which may be anchored in the context.
3891#
3892
3893# Check for switch () and associated case and default
3894# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003895 if ($line=~/\bswitch\s*\(.*\)/) {
3896 my $err = '';
3897 my $sep = '';
3898 my @ctx = ctx_block_outer($linenr, $realcnt);
3899 shift(@ctx);
3900 for my $ctx (@ctx) {
3901 my ($clen, $cindent) = line_stats($ctx);
3902 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3903 $indent != $cindent) {
3904 $err .= "$sep$ctx\n";
3905 $sep = '';
3906 } else {
3907 $sep = "[...]\n";
3908 }
3909 }
3910 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003911 ERROR("SWITCH_CASE_INDENT_LEVEL",
3912 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003913 }
3914 }
3915
3916# if/while/etc brace do not go on next line, unless defining a do while loop,
3917# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003918 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 -07003919 my $pre_ctx = "$1$2";
3920
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003921 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003922
3923 if ($line =~ /^\+\t{6,}/) {
3924 WARN("DEEP_INDENTATION",
3925 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3926 }
3927
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003928 my $ctx_cnt = $realcnt - $#ctx - 1;
3929 my $ctx = join("\n", @ctx);
3930
Andy Whitcroft548596d2008-07-23 21:29:01 -07003931 my $ctx_ln = $linenr;
3932 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003933
Andy Whitcroft548596d2008-07-23 21:29:01 -07003934 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3935 defined $lines[$ctx_ln - 1] &&
3936 $lines[$ctx_ln - 1] =~ /^-/)) {
3937 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3938 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003939 $ctx_ln++;
3940 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003941
Andy Whitcroft53210162008-07-23 21:29:03 -07003942 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3943 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003944
Joe Perchesd752fcc2014-08-06 16:11:05 -07003945 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003946 ERROR("OPEN_BRACE",
3947 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003948 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003949 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003950 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3951 $ctx =~ /\)\s*\;\s*$/ &&
3952 defined $lines[$ctx_ln - 1])
3953 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003954 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3955 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003956 WARN("TRAILING_SEMICOLON",
3957 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003958 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003959 }
3960 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003961 }
3962
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003963# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003964 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 -08003965 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3966 ctx_statement_block($linenr, $realcnt, 0)
3967 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003968 my ($s, $c) = ($stat, $cond);
3969
3970 substr($s, 0, length($c), '');
3971
Joe Perches9f5af482015-09-09 15:37:30 -07003972 # remove inline comments
3973 $s =~ s/$;/ /g;
3974 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003975
3976 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003977 my @newlines = ($c =~ /\n/gs);
3978 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003979
Joe Perches9f5af482015-09-09 15:37:30 -07003980 # Make sure we remove the line prefixes as we have
3981 # none on the first line, and are going to readd them
3982 # where necessary.
3983 $s =~ s/\n./\n/gs;
3984 while ($s =~ /\n\s+\\\n/) {
3985 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3986 }
3987
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003988 # We want to check the first line inside the block
3989 # starting at the end of the conditional, so remove:
3990 # 1) any blank line termination
3991 # 2) any opening brace { on end of the line
3992 # 3) any do (...) {
3993 my $continuation = 0;
3994 my $check = 0;
3995 $s =~ s/^.*\bdo\b//;
3996 $s =~ s/^\s*{//;
3997 if ($s =~ s/^\s*\\//) {
3998 $continuation = 1;
3999 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004000 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004001 $check = 1;
4002 $cond_lines++;
4003 }
4004
4005 # Also ignore a loop construct at the end of a
4006 # preprocessor statement.
4007 if (($prevline =~ /^.\s*#\s*define\s/ ||
4008 $prevline =~ /\\\s*$/) && $continuation == 0) {
4009 $check = 0;
4010 }
4011
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004012 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07004013 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004014 while ($cond_ptr != $cond_lines) {
4015 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004016
Andy Whitcroftf16fa282008-10-15 22:02:32 -07004017 # If we see an #else/#elif then the code
4018 # is not linear.
4019 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4020 $check = 0;
4021 }
4022
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004023 # Ignore:
4024 # 1) blank lines, they should be at 0,
4025 # 2) preprocessor lines, and
4026 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07004027 if ($continuation ||
4028 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004029 $s =~ /^\s*#\s*?/ ||
4030 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07004031 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07004032 if ($s =~ s/^.*?\n//) {
4033 $cond_lines++;
4034 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004035 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004036 }
4037
4038 my (undef, $sindent) = line_stats("+" . $s);
4039 my $stat_real = raw_line($linenr, $cond_lines);
4040
4041 # Check if either of these lines are modified, else
4042 # this is not this patch's fault.
4043 if (!defined($stat_real) ||
4044 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
4045 $check = 0;
4046 }
4047 if (defined($stat_real) && $cond_lines > 1) {
4048 $stat_real = "[...]\n$stat_real";
4049 }
4050
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07004051 #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 -07004052
Joe Perches9f5af482015-09-09 15:37:30 -07004053 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07004054 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07004055 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07004056 ($sindent == $indent &&
4057 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07004058 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004059 WARN("SUSPECT_CODE_INDENT",
4060 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07004061 }
4062 }
4063
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004064 # Track the 'values' across context and added lines.
4065 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004066 my ($curr_values, $curr_vars) =
4067 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004068 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004069 if ($dbg_values) {
4070 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004071 print "$linenr > .$outline\n";
4072 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004073 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004074 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004075 $prev_values = substr($curr_values, -1);
4076
Andy Whitcroft00df3442007-06-08 13:47:06 -07004077#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07004078 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07004079
Joe Perches99ca38c2020-10-15 20:12:09 -07004080# check for self assignments used to avoid compiler warnings
4081# e.g.: int foo = foo, *bar = NULL;
4082# struct foo bar = *(&(bar));
4083 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
4084 my $var = $1;
4085 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
4086 WARN("SELF_ASSIGNMENT",
4087 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
4088 }
4089 }
4090
Joe Perches11ca40a2016-12-12 16:46:31 -08004091# check for dereferences that span multiple lines
4092 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
4093 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
4094 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
4095 my $ref = $1;
4096 $line =~ /^.\s*($Lval)/;
4097 $ref .= $1;
4098 $ref =~ s/\s//g;
4099 WARN("MULTILINE_DEREFERENCE",
4100 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
4101 }
4102
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004103# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07004104 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 -07004105 my $type = $1;
4106 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07004107 $var = "" if (!defined $var);
4108 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004109 my $sign = $1;
4110 my $pointer = $2;
4111
4112 $pointer = "" if (!defined $pointer);
4113
4114 if (WARN("UNSPECIFIED_INT",
4115 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4116 $fix) {
4117 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07004118 my $comp_pointer = $pointer;
4119 $comp_pointer =~ s/\s//g;
4120 $decl .= $comp_pointer;
4121 $decl = rtrim($decl) if ($var eq "");
4122 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004123 }
4124 }
4125 }
4126
Andy Whitcroft653d4872007-06-23 17:16:34 -07004127# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07004128 if ($dbg_type) {
4129 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004130 ERROR("TEST_TYPE",
4131 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004132 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004133 ERROR("TEST_NOT_TYPE",
4134 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004135 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004136 next;
4137 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004138# TEST: allow direct testing of the attribute matcher.
4139 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004140 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004141 ERROR("TEST_ATTR",
4142 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004143 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004144 ERROR("TEST_NOT_ATTR",
4145 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004146 }
4147 next;
4148 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004149
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004150# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07004151 if ($line =~ /^.\s*{/ &&
4152 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07004153 if (ERROR("OPEN_BRACE",
4154 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004155 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4156 fix_delete_line($fixlinenr - 1, $prevrawline);
4157 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004158 my $fixedline = $prevrawline;
4159 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004160 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004161 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004162 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004163 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004164 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004165 }
4166
Andy Whitcroft653d4872007-06-23 17:16:34 -07004167#
4168# Checks which are anchored on the added line.
4169#
4170
4171# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004172 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07004173 my $path = $1;
4174 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004175 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08004176 "malformed #include filename\n" . $herecurr);
4177 }
4178 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4179 ERROR("UAPI_INCLUDE",
4180 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004181 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004182 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004183
4184# no C99 // comments
4185 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07004186 if (ERROR("C99_COMMENTS",
4187 "do not use C99 // comments\n" . $herecurr) &&
4188 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004189 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07004190 if ($line =~ /\/\/(.*)$/) {
4191 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07004192 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07004193 }
4194 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004195 }
4196 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004197 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004198 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004199
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004200# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4201# the whole statement.
4202#print "APW <$lines[$realline_next - 1]>\n";
4203 if (defined $realline_next &&
4204 exists $lines[$realline_next - 1] &&
4205 !defined $suppress_export{$realline_next} &&
4206 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4207 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004208 # Handle definitions which produce identifiers with
4209 # a prefix:
4210 # XXX(foo);
4211 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004212 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08004213 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004214 $name =~ /^${Ident}_$2/) {
4215#print "FOO C name<$name>\n";
4216 $suppress_export{$realline_next} = 1;
4217
4218 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004219 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07004220 ^.DEFINE_$Ident\(\Q$name\E\)|
4221 ^.DECLARE_$Ident\(\Q$name\E\)|
4222 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004223 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4224 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07004225 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004226#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4227 $suppress_export{$realline_next} = 2;
4228 } else {
4229 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004230 }
4231 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004232 if (!defined $suppress_export{$linenr} &&
4233 $prevline =~ /^.\s*$/ &&
4234 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4235 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4236#print "FOO B <$lines[$linenr - 1]>\n";
4237 $suppress_export{$linenr} = 2;
4238 }
4239 if (defined $suppress_export{$linenr} &&
4240 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004241 WARN("EXPORT_SYMBOL",
4242 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004243 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004244
Joe Eloff5150bda2010-08-09 17:21:00 -07004245# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004246 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004247 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004248 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004249 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004250 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004251 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004252 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004253# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004254 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004255 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004256 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004257 $herecurr) &&
4258 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004259 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004260 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004261 }
4262
Joe Perches18130872014-08-06 16:11:22 -07004263# check for misordered declarations of char/short/int/long with signed/unsigned
4264 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4265 my $tmp = trim($1);
4266 WARN("MISORDERED_TYPE",
4267 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4268 }
4269
Joe Perches809e0822018-08-21 21:58:12 -07004270# check for unnecessary <signed> int declarations of short/long/long long
4271 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4272 my $type = trim($1);
4273 next if ($type !~ /\bint\b/);
4274 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4275 my $new_type = $type;
4276 $new_type =~ s/\b\s*int\s*\b/ /;
4277 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4278 $new_type =~ s/^const\s+//;
4279 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4280 $new_type = "const $new_type" if ($type =~ /^const\b/);
4281 $new_type =~ s/\s+/ /g;
4282 $new_type = trim($new_type);
4283 if (WARN("UNNECESSARY_INT",
4284 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4285 $fix) {
4286 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4287 }
4288 }
4289
Joe Perchescb710ec2010-10-26 14:23:20 -07004290# check for static const char * arrays.
4291 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004292 WARN("STATIC_CONST_CHAR_ARRAY",
4293 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004294 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004295 }
4296
4297# check for initialized const char arrays that should be static const
4298 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4299 if (WARN("STATIC_CONST_CHAR_ARRAY",
4300 "const array should probably be static const\n" . $herecurr) &&
4301 $fix) {
4302 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4303 }
4304 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004305
4306# check for static char foo[] = "bar" declarations.
4307 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004308 WARN("STATIC_CONST_CHAR_ARRAY",
4309 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004310 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004311 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004312
Joe Perchesab7e23f2015-04-16 12:44:22 -07004313# check for const <foo> const where <foo> is not a pointer or array type
4314 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4315 my $found = $1;
4316 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4317 WARN("CONST_CONST",
4318 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4319 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4320 WARN("CONST_CONST",
4321 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4322 }
4323 }
4324
Joe Perches73169762020-12-15 20:44:30 -08004325# check for const static or static <non ptr type> const declarations
4326# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
4327 if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
4328 $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
4329 if (WARN("STATIC_CONST",
4330 "Move const after static - use 'static const $1'\n" . $herecurr) &&
4331 $fix) {
4332 $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
4333 $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
4334 }
4335 }
4336
Joe Perches9b0fa602014-04-03 14:49:18 -07004337# check for non-global char *foo[] = {"bar", ...} declarations.
4338 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4339 WARN("STATIC_CONST_CHAR_ARRAY",
4340 "char * array declaration might be better as static const\n" .
4341 $herecurr);
4342 }
4343
Joe Perchesb598b672015-04-16 12:44:36 -07004344# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4345 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4346 my $array = $1;
4347 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4348 my $array_div = $1;
4349 if (WARN("ARRAY_SIZE",
4350 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4351 $fix) {
4352 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4353 }
4354 }
4355 }
4356
Joe Perchesb36190c2014-01-27 17:07:18 -08004357# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004358 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004359 if (ERROR("FUNCTION_WITHOUT_ARGS",
4360 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4361 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004362 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004363 }
4364 }
4365
Andy Whitcroft653d4872007-06-23 17:16:34 -07004366# check for new typedefs, only function parameters and sparse annotations
4367# make sense.
4368 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004369 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004370 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004371 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004372 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004373 WARN("NEW_TYPEDEFS",
4374 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004375 }
4376
4377# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004378 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004379 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4380 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004381 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004382
Andy Whitcroft65863862009-01-06 14:41:21 -08004383 # Should start with a space.
4384 $to =~ s/^(\S)/ $1/;
4385 # Should not end with a space.
4386 $to =~ s/\s+$//;
4387 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004388 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004389 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004390
Joe Perches3705ce52013-07-03 15:05:31 -07004391## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004392 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004393 if (ERROR("POINTER_LOCATION",
4394 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4395 $fix) {
4396 my $sub_from = $ident;
4397 my $sub_to = $ident;
4398 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004399 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004400 s@\Q$sub_from\E@$sub_to@;
4401 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004402 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004403 }
4404 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4405 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004406 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004407
Andy Whitcroft65863862009-01-06 14:41:21 -08004408 # Should start with a space.
4409 $to =~ s/^(\S)/ $1/;
4410 # Should not end with a space.
4411 $to =~ s/\s+$//;
4412 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004413 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004414 }
4415 # Modifiers should have spaces.
4416 $to =~ s/(\b$Modifier$)/$1 /;
4417
Joe Perches3705ce52013-07-03 15:05:31 -07004418## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004419 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004420 if (ERROR("POINTER_LOCATION",
4421 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4422 $fix) {
4423
4424 my $sub_from = $match;
4425 my $sub_to = $match;
4426 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004427 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004428 s@\Q$sub_from\E@$sub_to@;
4429 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004430 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004431 }
4432
Joe Perches9d3e3c72015-09-09 15:37:27 -07004433# avoid BUG() or BUG_ON()
4434 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004435 my $msg_level = \&WARN;
4436 $msg_level = \&CHK if ($file);
4437 &{$msg_level}("AVOID_BUG",
4438 "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 -07004439 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004440
Joe Perches9d3e3c72015-09-09 15:37:27 -07004441# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004442 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004443 WARN("LINUX_VERSION_CODE",
4444 "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 -08004445 }
4446
Joe Perches17441222011-06-15 15:08:17 -07004447# check for uses of printk_ratelimit
4448 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004449 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004450 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004451 }
4452
Joe Percheseeef5732017-11-17 15:28:41 -08004453# printk should use KERN_* levels
4454 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4455 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4456 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004457 }
4458
Joe Perches243f3802012-05-31 16:26:09 -07004459 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4460 my $orig = $1;
4461 my $level = lc($orig);
4462 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004463 my $level2 = $level;
4464 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004465 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004466 "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 -07004467 }
4468
Joe Perchesdc139312013-02-21 16:44:13 -08004469 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4470 my $orig = $1;
4471 my $level = lc($orig);
4472 $level = "warn" if ($level eq "warning");
4473 $level = "dbg" if ($level eq "debug");
4474 WARN("PREFER_DEV_LEVEL",
4475 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4476 }
4477
Nicolas Boichat8020b252020-10-15 20:12:02 -07004478# trace_printk should not be used in production code.
4479 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4480 WARN("TRACE_PRINTK",
4481 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4482 }
4483
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004484# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4485# number of false positives, but assembly files are not checked, so at
4486# least the arch entry code will not trigger this warning.
4487 if ($line =~ /\bENOSYS\b/) {
4488 WARN("ENOSYS",
4489 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4490 }
4491
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004492# ENOTSUPP is not a standard error code and should be avoided in new patches.
4493# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4494# Similarly to ENOSYS warning a small number of false positives is expected.
4495 if (!$file && $line =~ /\bENOTSUPP\b/) {
4496 if (WARN("ENOTSUPP",
4497 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4498 $fix) {
4499 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4500 }
4501 }
4502
Andy Whitcroft653d4872007-06-23 17:16:34 -07004503# function brace can't be on same line, except for #defines of do while,
4504# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004505 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004506 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4507 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4508 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004509 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004510 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004511 $fix) {
4512 fix_delete_line($fixlinenr, $rawline);
4513 my $fixed_line = $rawline;
Dwaipayan Ray03f49352020-12-15 20:45:02 -08004514 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
Joe Perches8d182472014-08-06 16:11:12 -07004515 my $line1 = $1;
4516 my $line2 = $2;
4517 fix_insert_line($fixlinenr, ltrim($line1));
4518 fix_insert_line($fixlinenr, "\+{");
4519 if ($line2 !~ /^\s*$/) {
4520 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4521 }
4522 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004523 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004524
Andy Whitcroft8905a672007-11-28 16:21:06 -08004525# open braces for enum, union and struct go on the same line.
4526 if ($line =~ /^.\s*{/ &&
4527 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004528 if (ERROR("OPEN_BRACE",
4529 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4530 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4531 fix_delete_line($fixlinenr - 1, $prevrawline);
4532 fix_delete_line($fixlinenr, $rawline);
4533 my $fixedline = rtrim($prevrawline) . " {";
4534 fix_insert_line($fixlinenr, $fixedline);
4535 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004536 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004537 if ($fixedline !~ /^\+\s*$/) {
4538 fix_insert_line($fixlinenr, $fixedline);
4539 }
4540 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004541 }
4542
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004543# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004544 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4545 if (WARN("SPACING",
4546 "missing space after $1 definition\n" . $herecurr) &&
4547 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004548 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004549 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4550 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004551 }
4552
Joe Perches31070b52014-01-23 15:54:49 -08004553# Function pointer declarations
4554# check spacing between type, funcptr, and args
4555# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004556 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004557 my $declare = $1;
4558 my $pre_pointer_space = $2;
4559 my $post_pointer_space = $3;
4560 my $funcname = $4;
4561 my $post_funcname_space = $5;
4562 my $pre_args_space = $6;
4563
Joe Perches91f72e92014-04-03 14:49:12 -07004564# the $Declare variable will capture all spaces after the type
4565# so check it for a missing trailing missing space but pointer return types
4566# don't need a space so don't warn for those.
4567 my $post_declare_space = "";
4568 if ($declare =~ /(\s+)$/) {
4569 $post_declare_space = $1;
4570 $declare = rtrim($declare);
4571 }
4572 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004573 WARN("SPACING",
4574 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004575 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004576 }
4577
4578# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004579# This test is not currently implemented because these declarations are
4580# equivalent to
4581# int foo(int bar, ...)
4582# and this is form shouldn't/doesn't generate a checkpatch warning.
4583#
4584# elsif ($declare =~ /\s{2,}$/) {
4585# WARN("SPACING",
4586# "Multiple spaces after return type\n" . $herecurr);
4587# }
Joe Perches31070b52014-01-23 15:54:49 -08004588
4589# unnecessary space "type ( *funcptr)(args...)"
4590 if (defined $pre_pointer_space &&
4591 $pre_pointer_space =~ /^\s/) {
4592 WARN("SPACING",
4593 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4594 }
4595
4596# unnecessary space "type (* funcptr)(args...)"
4597 if (defined $post_pointer_space &&
4598 $post_pointer_space =~ /^\s/) {
4599 WARN("SPACING",
4600 "Unnecessary space before function pointer name\n" . $herecurr);
4601 }
4602
4603# unnecessary space "type (*funcptr )(args...)"
4604 if (defined $post_funcname_space &&
4605 $post_funcname_space =~ /^\s/) {
4606 WARN("SPACING",
4607 "Unnecessary space after function pointer name\n" . $herecurr);
4608 }
4609
4610# unnecessary space "type (*funcptr) (args...)"
4611 if (defined $pre_args_space &&
4612 $pre_args_space =~ /^\s/) {
4613 WARN("SPACING",
4614 "Unnecessary space before function pointer arguments\n" . $herecurr);
4615 }
4616
4617 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004618 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004619 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004620 }
4621 }
4622
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004623# check for spacing round square brackets; allowed:
4624# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004625# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4626# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004627 while ($line =~ /(.*?\s)\[/g) {
4628 my ($where, $prefix) = ($-[1], $1);
4629 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004630 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004631 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004632 if (ERROR("BRACKET_SPACE",
4633 "space prohibited before open square bracket '['\n" . $herecurr) &&
4634 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004635 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004636 s/^(\+.*?)\s+\[/$1\[/;
4637 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004638 }
4639 }
4640
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004641# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004642 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004643 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004644 my $ctx_before = substr($line, 0, $-[1]);
4645 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004646
4647 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004648 if ($name =~ /^(?:
4649 if|for|while|switch|return|case|
4650 volatile|__volatile__|
4651 __attribute__|format|__extension__|
4652 asm|__asm__)$/x)
4653 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004654 # cpp #define statements have non-optional spaces, ie
4655 # if there is a space between the name and the open
4656 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004657 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004658
4659 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004660 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004661
4662 # If this whole things ends with a type its most
4663 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004664 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004665
4666 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004667 if (WARN("SPACING",
4668 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4669 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004670 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004671 s/\b$name\s+\(/$name\(/;
4672 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004673 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004674 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004675
Andy Whitcroft653d4872007-06-23 17:16:34 -07004676# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004677 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004678 my $fixed_line = "";
4679 my $line_fixed = 0;
4680
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004681 my $ops = qr{
4682 <<=|>>=|<=|>=|==|!=|
4683 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4684 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004685 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004686 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004687 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004688 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004689
4690## print("element count: <" . $#elements . ">\n");
4691## foreach my $el (@elements) {
4692## print("el: <$el>\n");
4693## }
4694
4695 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004696 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004697
Joe Perches3705ce52013-07-03 15:05:31 -07004698 foreach my $el (@elements) {
4699 push(@fix_elements, substr($rawline, $off, length($el)));
4700 $off += length($el);
4701 }
4702
4703 $off = 0;
4704
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004705 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004706 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004707
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004708 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004709
4710 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4711
4712## print("n: <$n> good: <$good>\n");
4713
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004714 $off += length($elements[$n]);
4715
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004716 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004717 my $ca = substr($opline, 0, $off);
4718 my $cc = '';
4719 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4720 $cc = substr($opline, $off + length($elements[$n + 1]));
4721 }
4722 my $cb = "$ca$;$cc";
4723
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004724 my $a = '';
4725 $a = 'V' if ($elements[$n] ne '');
4726 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004727 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004728 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4729 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004730 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004731
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004732 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004733
4734 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004735 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004736 $c = 'V' if ($elements[$n + 2] ne '');
4737 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004738 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004739 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4740 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004741 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004742 } else {
4743 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004744 }
4745
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004746 my $ctx = "${a}x${c}";
4747
4748 my $at = "(ctx:$ctx)";
4749
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004750 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004751 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004752
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004753 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004754 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004755
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004756 # Get the full operator variant.
4757 my $opv = $op . substr($curr_vars, $off, 1);
4758
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004759 # Ignore operators passed as parameters.
4760 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004761 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004762
Andy Whitcroftcf655042008-03-04 14:28:20 -08004763# # Ignore comments
4764# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004765
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004766 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004767 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004768 if ($ctx !~ /.x[WEBC]/ &&
4769 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004770 if (ERROR("SPACING",
4771 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004772 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004773 $line_fixed = 1;
4774 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004775 }
4776
4777 # // is a comment
4778 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004779
Joe Perchesb00e4812014-04-03 14:49:33 -07004780 # : when part of a bitfield
4781 } elsif ($opv eq ':B') {
4782 # skip the bitfield test for now
4783
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004784 # No spaces for:
4785 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004786 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004787 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004788 if (ERROR("SPACING",
4789 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004790 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004791 if (defined $fix_elements[$n + 2]) {
4792 $fix_elements[$n + 2] =~ s/^\s+//;
4793 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004794 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004795 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004796 }
4797
Joe Perches23810972014-12-10 15:51:32 -08004798 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004799 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004800 my $rtrim_before = 0;
4801 my $space_after = 0;
4802 if ($ctx =~ /Wx./) {
4803 if (ERROR("SPACING",
4804 "space prohibited before that '$op' $at\n" . $hereptr)) {
4805 $line_fixed = 1;
4806 $rtrim_before = 1;
4807 }
4808 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004809 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004810 if (ERROR("SPACING",
4811 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004812 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004813 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004814 $space_after = 1;
4815 }
4816 }
4817 if ($rtrim_before || $space_after) {
4818 if ($rtrim_before) {
4819 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4820 } else {
4821 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4822 }
4823 if ($space_after) {
4824 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004825 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004826 }
4827
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004828 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004829 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004830 #warn "'*' is part of type\n";
4831
4832 # unary operators should have a space before and
4833 # none after. May be left adjacent to another
4834 # unary operator, or a cast
4835 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004836 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004837 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004838 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004839 if (ERROR("SPACING",
4840 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004841 if ($n != $last_after + 2) {
4842 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4843 $line_fixed = 1;
4844 }
Joe Perches3705ce52013-07-03 15:05:31 -07004845 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004846 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004847 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004848 # A unary '*' may be const
4849
4850 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004851 if (ERROR("SPACING",
4852 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004853 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004854 if (defined $fix_elements[$n + 2]) {
4855 $fix_elements[$n + 2] =~ s/^\s+//;
4856 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004857 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004858 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004859 }
4860
4861 # unary ++ and unary -- are allowed no space on one side.
4862 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004863 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004864 if (ERROR("SPACING",
4865 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004866 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004867 $line_fixed = 1;
4868 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004869 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004870 if ($ctx =~ /Wx[BE]/ ||
4871 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004872 if (ERROR("SPACING",
4873 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004874 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004875 $line_fixed = 1;
4876 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004877 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004878 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004879 if (ERROR("SPACING",
4880 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004881 $good = $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 Whitcroft773647a2008-03-28 14:15:58 -07004887 }
4888
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004889 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004890 } elsif ($op eq '<<' or $op eq '>>' or
4891 $op eq '&' or $op eq '^' or $op eq '|' or
4892 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004893 $op eq '*' or $op eq '/' or
4894 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004895 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004896 if ($check) {
4897 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4898 if (CHK("SPACING",
4899 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4900 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4901 $fix_elements[$n + 2] =~ s/^\s+//;
4902 $line_fixed = 1;
4903 }
4904 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4905 if (CHK("SPACING",
4906 "space preferred before that '$op' $at\n" . $hereptr)) {
4907 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4908 $line_fixed = 1;
4909 }
4910 }
4911 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004912 if (ERROR("SPACING",
4913 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004914 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4915 if (defined $fix_elements[$n + 2]) {
4916 $fix_elements[$n + 2] =~ s/^\s+//;
4917 }
Joe Perches3705ce52013-07-03 15:05:31 -07004918 $line_fixed = 1;
4919 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004920 }
4921
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004922 # A colon needs no spaces before when it is
4923 # terminating a case value or a label.
4924 } elsif ($opv eq ':C' || $opv eq ':L') {
4925 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004926 if (ERROR("SPACING",
4927 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004928 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004929 $line_fixed = 1;
4930 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004931 }
4932
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004933 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004934 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004935 my $ok = 0;
4936
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004937 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004938 if (($op eq '<' &&
4939 $cc =~ /^\S+\@\S+>/) ||
4940 ($op eq '>' &&
4941 $ca =~ /<\S+\@\S+$/))
4942 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07004943 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004944 }
4945
Joe Perchese0df7e12015-04-16 12:44:53 -07004946 # for asm volatile statements
4947 # ignore a colon with another
4948 # colon immediately before or after
4949 if (($op eq ':') &&
4950 ($ca =~ /:$/ || $cc =~ /^:/)) {
4951 $ok = 1;
4952 }
4953
Joe Perches84731622013-11-12 15:10:05 -08004954 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004955 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004956 my $msg_level = \&ERROR;
4957 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004958
Jean Delvare0675a8f2017-09-08 16:16:07 -07004959 if (&{$msg_level}("SPACING",
4960 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004961 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4962 if (defined $fix_elements[$n + 2]) {
4963 $fix_elements[$n + 2] =~ s/^\s+//;
4964 }
Joe Perches3705ce52013-07-03 15:05:31 -07004965 $line_fixed = 1;
4966 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004967 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004968 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004969 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004970
4971## print("n: <$n> GOOD: <$good>\n");
4972
4973 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004974 }
Joe Perches3705ce52013-07-03 15:05:31 -07004975
4976 if (($#elements % 2) == 0) {
4977 $fixed_line = $fixed_line . $fix_elements[$#elements];
4978 }
4979
Joe Perches194f66f2014-08-06 16:11:03 -07004980 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4981 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004982 }
4983
4984
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004985 }
4986
Joe Perches786b6322013-07-03 15:05:32 -07004987# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004988 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004989 if (WARN("SPACING",
4990 "space prohibited before semicolon\n" . $herecurr) &&
4991 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004992 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004993 s/^(\+.*\S)\s+;/$1;/;
4994 }
4995 }
4996
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004997# check for multiple assignments
4998 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004999 CHK("MULTIPLE_ASSIGNMENTS",
5000 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005001 }
5002
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005003## # check for multiple declarations, allowing for a function declaration
5004## # continuation.
5005## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
5006## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
5007##
5008## # Remove any bracketed sections to ensure we do not
Dwaipayan Raye73d2712020-12-15 20:44:56 -08005009## # falsely report the parameters of functions.
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005010## my $ln = $line;
5011## while ($ln =~ s/\([^\(\)]*\)//g) {
5012## }
5013## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005014## WARN("MULTIPLE_DECLARATION",
5015## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005016## }
5017## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005018
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005019#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07005020 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07005021 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005022 if (ERROR("SPACING",
5023 "space required before the open brace '{'\n" . $herecurr) &&
5024 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07005025 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07005026 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005027 }
5028
Joe Perchesc4a62ef2013-07-03 15:05:28 -07005029## # check for blank lines before declarations
5030## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5031## $prevrawline =~ /^.\s*$/) {
5032## WARN("SPACING",
5033## "No blank lines before declarations\n" . $hereprev);
5034## }
5035##
5036
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005037# closing brace should have a space following it when it has anything
5038# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07005039 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005040 if (ERROR("SPACING",
5041 "space required after that close brace '}'\n" . $herecurr) &&
5042 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005043 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07005044 s/}((?!(?:,|;|\)))\S)/} $1/;
5045 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005046 }
5047
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005048# check spacing on square brackets
5049 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005050 if (ERROR("SPACING",
5051 "space prohibited after that open square bracket '['\n" . $herecurr) &&
5052 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005053 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005054 s/\[\s+/\[/;
5055 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005056 }
5057 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005058 if (ERROR("SPACING",
5059 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
5060 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005061 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005062 s/\s+\]/\]/;
5063 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005064 }
5065
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005066# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005067 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
5068 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005069 if (ERROR("SPACING",
5070 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
5071 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005072 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005073 s/\(\s+/\(/;
5074 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005075 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005076 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005077 $line !~ /for\s*\(.*;\s+\)/ &&
5078 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005079 if (ERROR("SPACING",
5080 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
5081 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005082 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005083 s/\s+\)/\)/;
5084 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005085 }
5086
Joe Perchese2826fd2014-08-06 16:10:48 -07005087# check unnecessary parentheses around addressof/dereference single $Lvals
5088# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5089
5090 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08005091 my $var = $1;
5092 if (CHK("UNNECESSARY_PARENTHESES",
5093 "Unnecessary parentheses around $var\n" . $herecurr) &&
5094 $fix) {
5095 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5096 }
5097 }
5098
5099# check for unnecessary parentheses around function pointer uses
5100# ie: (foo->bar)(); should be foo->bar();
5101# but not "if (foo->bar) (" to avoid some false positives
5102 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5103 my $var = $2;
5104 if (CHK("UNNECESSARY_PARENTHESES",
5105 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5106 $fix) {
5107 my $var2 = deparenthesize($var);
5108 $var2 =~ s/\s//g;
5109 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5110 }
5111 }
Joe Perchese2826fd2014-08-06 16:10:48 -07005112
Joe Perches63b7c732017-09-08 16:16:01 -07005113# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08005114# when !drivers/staging or command-line uses --strict
5115 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07005116 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07005117 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
5118 my $if_stat = $1;
5119 my $test = substr($2, 1, -1);
5120 my $herectx;
5121 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
5122 my $match = $1;
5123 # avoid parentheses around potential macro args
5124 next if ($match =~ /^\s*\w+\s*$/);
5125 if (!defined($herectx)) {
5126 $herectx = $here . "\n";
5127 my $cnt = statement_rawlines($if_stat);
5128 for (my $n = 0; $n < $cnt; $n++) {
5129 my $rl = raw_line($linenr, $n);
5130 $herectx .= $rl . "\n";
5131 last if $rl =~ /^[ \+].*\{/;
5132 }
5133 }
5134 CHK("UNNECESSARY_PARENTHESES",
5135 "Unnecessary parentheses around '$match'\n" . $herectx);
5136 }
5137 }
5138
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005139#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005140 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005141 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07005142 if (WARN("INDENTED_LABEL",
5143 "labels should not be indented\n" . $herecurr) &&
5144 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005145 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005146 s/^(.)\s+/$1/;
5147 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005148 }
5149
Joe Perches40873ab2020-10-15 20:11:56 -07005150# check if a statement with a comma should be two statements like:
5151# foo = bar(), /* comma should be semicolon */
5152# bar = baz();
5153 if (defined($stat) &&
5154 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5155 my $cnt = statement_rawlines($stat);
5156 my $herectx = get_stat_here($linenr, $cnt, $here);
5157 WARN("SUSPECT_COMMA_SEMICOLON",
5158 "Possible comma where semicolon could be used\n" . $herectx);
5159 }
5160
Joe Perches5b9553a2014-04-03 14:49:21 -07005161# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08005162 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005163 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07005164 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07005165 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5166 my $value = $1;
5167 $value = deparenthesize($value);
5168 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5169 ERROR("RETURN_PARENTHESES",
5170 "return is not a function, parentheses are not required\n" . $herecurr);
5171 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005172 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005173 ERROR("SPACING",
5174 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005175 }
5176 }
Joe Perches507e5142013-11-12 15:10:13 -08005177
Joe Perchesb43ae212014-06-23 13:22:07 -07005178# unnecessary return in a void function
5179# at end-of-function, with the previous line a single leading tab, then return;
5180# and the line before that not a goto label target like "out:"
5181 if ($sline =~ /^[ \+]}\s*$/ &&
5182 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5183 $linenr >= 3 &&
5184 $lines[$linenr - 3] =~ /^[ +]/ &&
5185 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07005186 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07005187 "void function return statements are not generally useful\n" . $hereprev);
5188 }
Joe Perches9819cf22014-06-04 16:12:09 -07005189
Joe Perches189248d2014-01-23 15:54:47 -08005190# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07005191 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08005192 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5193 my $openparens = $1;
5194 my $count = $openparens =~ tr@\(@\(@;
5195 my $msg = "";
5196 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5197 my $comp = $4; #Not $1 because of $LvalOrFunc
5198 $msg = " - maybe == should be = ?" if ($comp eq "==");
5199 WARN("UNNECESSARY_PARENTHESES",
5200 "Unnecessary parentheses$msg\n" . $herecurr);
5201 }
5202 }
5203
Joe Perchesc5595fa2015-09-09 15:37:58 -07005204# comparisons with a constant or upper case identifier on the left
5205# avoid cases like "foo + BAR < baz"
5206# only fix matches surrounded by parentheses to avoid incorrect
5207# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07005208 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005209 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5210 my $lead = $1;
5211 my $const = $2;
5212 my $comp = $3;
5213 my $to = $4;
5214 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07005215 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005216 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5217 WARN("CONSTANT_COMPARISON",
5218 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5219 $fix) {
5220 if ($comp eq "<") {
5221 $newcomp = ">";
5222 } elsif ($comp eq "<=") {
5223 $newcomp = ">=";
5224 } elsif ($comp eq ">") {
5225 $newcomp = "<";
5226 } elsif ($comp eq ">=") {
5227 $newcomp = "<=";
5228 }
5229 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5230 }
5231 }
5232
Joe Perchesf34e4a42015-04-16 12:44:19 -07005233# Return of what appears to be an errno should normally be negative
5234 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005235 my $name = $1;
5236 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07005237 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07005238 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005239 }
5240 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005241
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005242# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07005243 if ($line =~ /\b(if|while|for|switch)\(/) {
5244 if (ERROR("SPACING",
5245 "space required before the open parenthesis '('\n" . $herecurr) &&
5246 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005247 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005248 s/\b(if|while|for|switch)\(/$1 \(/;
5249 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005250 }
5251
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07005252# Check for illegal assignment in if conditional -- and check for trailing
5253# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005254 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08005255 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5256 ctx_statement_block($linenr, $realcnt, 0)
5257 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005258 my ($stat_next) = ctx_statement_block($line_nr_next,
5259 $remain_next, $off_next);
5260 $stat_next =~ s/\n./\n /g;
5261 ##print "stat<$stat> stat_next<$stat_next>\n";
5262
5263 if ($stat_next =~ /^\s*while\b/) {
5264 # If the statement carries leading newlines,
5265 # then count those as offsets.
5266 my ($whitespace) =
5267 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5268 my $offset =
5269 statement_rawlines($whitespace) - 1;
5270
5271 $suppress_whiletrailers{$line_nr_next +
5272 $offset} = 1;
5273 }
5274 }
5275 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005276 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005277 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005278 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005279
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005280 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches65b64b32020-08-11 18:35:10 -07005281 if (ERROR("ASSIGN_IN_IF",
5282 "do not use assignment in if condition\n" . $herecurr) &&
5283 $fix && $perl_version_ok) {
5284 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5285 my $space = $1;
5286 my $not = $2;
5287 my $statement = $3;
5288 my $assigned = $4;
5289 my $test = $8;
5290 my $against = $9;
5291 my $brace = $15;
5292 fix_delete_line($fixlinenr, $rawline);
5293 fix_insert_line($fixlinenr, "$space$statement;");
5294 my $newline = "${space}if (";
5295 $newline .= '!' if defined($not);
5296 $newline .= '(' if (defined $not && defined($test) && defined($against));
5297 $newline .= "$assigned";
5298 $newline .= " $test $against" if (defined($test) && defined($against));
5299 $newline .= ')' if (defined $not && defined($test) && defined($against));
5300 $newline .= ')';
5301 $newline .= " {" if (defined($brace));
5302 fix_insert_line($fixlinenr + 1, $newline);
5303 }
5304 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005305 }
5306
5307 # Find out what is on the end of the line after the
5308 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005309 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005310 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005311 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005312 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5313 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005314 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005315 # Find out how long the conditional actually is.
5316 my @newlines = ($c =~ /\n/gs);
5317 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005318 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005319
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005320 $stat_real = raw_line($linenr, $cond_lines)
5321 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005322 if (defined($stat_real) && $cond_lines > 1) {
5323 $stat_real = "[...]\n$stat_real";
5324 }
5325
Joe Perches000d1cc12011-07-25 17:13:25 -07005326 ERROR("TRAILING_STATEMENTS",
5327 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005328 }
5329 }
5330
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005331# Check for bitwise tests written as boolean
5332 if ($line =~ /
5333 (?:
5334 (?:\[|\(|\&\&|\|\|)
5335 \s*0[xX][0-9]+\s*
5336 (?:\&\&|\|\|)
5337 |
5338 (?:\&\&|\|\|)
5339 \s*0[xX][0-9]+\s*
5340 (?:\&\&|\|\||\)|\])
5341 )/x)
5342 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005343 WARN("HEXADECIMAL_BOOLEAN_TEST",
5344 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005345 }
5346
Andy Whitcroft8905a672007-11-28 16:21:06 -08005347# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005348 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5349 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005350 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005351 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005352 ERROR("TRAILING_STATEMENTS",
5353 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005354 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005355 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005356# if should not continue a brace
5357 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005358 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005359 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005360 $herecurr);
5361 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005362# case and default should not have general statements after them
5363 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5364 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005365 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005366 \s*return\s+
5367 )/xg)
5368 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005369 ERROR("TRAILING_STATEMENTS",
5370 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005371 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005372
5373 # Check for }<nl>else {, these must be at the same
5374 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005375 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5376 $previndent == $indent) {
5377 if (ERROR("ELSE_AFTER_BRACE",
5378 "else should follow close brace '}'\n" . $hereprev) &&
5379 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5380 fix_delete_line($fixlinenr - 1, $prevrawline);
5381 fix_delete_line($fixlinenr, $rawline);
5382 my $fixedline = $prevrawline;
5383 $fixedline =~ s/}\s*$//;
5384 if ($fixedline !~ /^\+\s*$/) {
5385 fix_insert_line($fixlinenr, $fixedline);
5386 }
5387 $fixedline = $rawline;
5388 $fixedline =~ s/^(.\s*)else/$1} else/;
5389 fix_insert_line($fixlinenr, $fixedline);
5390 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005391 }
5392
Joe Perches8b8856f2014-08-06 16:11:14 -07005393 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5394 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005395 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5396
5397 # Find out what is on the end of the line after the
5398 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005399 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005400 $s =~ s/\n.*//g;
5401
5402 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005403 if (ERROR("WHILE_AFTER_BRACE",
5404 "while should follow close brace '}'\n" . $hereprev) &&
5405 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5406 fix_delete_line($fixlinenr - 1, $prevrawline);
5407 fix_delete_line($fixlinenr, $rawline);
5408 my $fixedline = $prevrawline;
5409 my $trailing = $rawline;
5410 $trailing =~ s/^\+//;
5411 $trailing = trim($trailing);
5412 $fixedline =~ s/}\s*$/} $trailing/;
5413 fix_insert_line($fixlinenr, $fixedline);
5414 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005415 }
5416 }
5417
Joe Perches95e2c602013-07-03 15:05:20 -07005418#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005419 while ($line =~ m{($Constant|$Lval)}g) {
5420 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005421
Joe Perches95e2c602013-07-03 15:05:20 -07005422#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005423 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005424 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Łukasz Stelmach4104a202020-12-15 20:44:27 -08005425#Ignore some autogenerated defines and enum values
5426 $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005427#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005428 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005429#Ignore SI style variants like nS, mV and dB
5430#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5431 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005432#Ignore some three character SI units explicitly, like MiB and KHz
5433 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005434 while ($var =~ m{($Ident)}g) {
5435 my $word = $1;
5436 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005437 if ($check) {
5438 seed_camelcase_includes();
5439 if (!$file && !$camelcase_file_seeded) {
5440 seed_camelcase_file($realfile);
5441 $camelcase_file_seeded = 1;
5442 }
5443 }
Joe Perches7e781f62013-09-11 14:23:55 -07005444 if (!defined $camelcase{$word}) {
5445 $camelcase{$word} = 1;
5446 CHK("CAMELCASE",
5447 "Avoid CamelCase: <$word>\n" . $herecurr);
5448 }
Joe Perches34456862013-07-03 15:05:34 -07005449 }
Joe Perches323c1262012-12-17 16:02:07 -08005450 }
5451 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005452
5453#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005454 if ($line =~ /\#\s*define.*\\\s+$/) {
5455 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5456 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5457 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005458 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005459 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005460 }
5461
Fabian Frederick0e212e02015-04-16 12:44:25 -07005462# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5463# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005464 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005465 my $file = "$1.h";
5466 my $checkfile = "include/linux/$file";
5467 if (-f "$root/$checkfile" &&
5468 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005469 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005470 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005471 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5472 if ($asminclude > 0) {
5473 if ($realfile =~ m{^arch/}) {
5474 CHK("ARCH_INCLUDE_LINUX",
5475 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5476 } else {
5477 WARN("INCLUDE_LINUX",
5478 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5479 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005480 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005481 }
5482 }
5483
Andy Whitcroft653d4872007-06-23 17:16:34 -07005484# multi-statement macros should be enclosed in a do while loop, grab the
5485# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005486# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005487 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5488 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005489 my $ln = $linenr;
5490 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005491 my ($off, $dstat, $dcond, $rest);
5492 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005493 my $has_flow_statement = 0;
5494 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005495 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005496 ctx_statement_block($linenr, $realcnt, 0);
5497 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005498 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005499 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005500
Joe Perches08a28432014-10-13 15:51:55 -07005501 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005502 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005503
Joe Perchesf59b64b2016-10-11 13:52:08 -07005504 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5505 my $define_args = $1;
5506 my $define_stmt = $dstat;
5507 my @def_args = ();
5508
5509 if (defined $define_args && $define_args ne "") {
5510 $define_args = substr($define_args, 1, length($define_args) - 2);
5511 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005512 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005513 @def_args = split(",", $define_args);
5514 }
5515
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005516 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005517 $dstat =~ s/\\\n.//g;
5518 $dstat =~ s/^\s*//s;
5519 $dstat =~ s/\s*$//s;
5520
5521 # Flatten any parentheses and braces
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005522 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5523 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5524 $dstat =~ s/.\[[^\[\]]*\]/1u/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005525 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005526 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005527
Antonio Borneo342d3d22020-04-06 20:11:01 -07005528 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005529 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5530 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005531 {
5532 }
5533
Joe Perches42e15292016-03-15 14:58:01 -07005534 # Make asm volatile uses seem like a generic function
5535 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5536
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005537 my $exceptions = qr{
5538 $Declare|
5539 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005540 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005541 DECLARE_PER_CPU|
5542 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005543 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005544 union|
5545 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005546 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005547 ^\"|\"$|
5548 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005549 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005550 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005551
5552 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005553 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005554 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005555
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005556 if ($dstat ne '' &&
5557 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5558 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005559 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005560 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005561 $dstat !~ /$exceptions/ &&
5562 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005563 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005564 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005565 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005566 $dstat !~ /^for\s*$Constant$/ && # for (...)
5567 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5568 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005569 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005570 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005571 {
Joe Perchese7955562017-05-08 15:55:48 -07005572 if ($dstat =~ /^\s*if\b/) {
5573 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5574 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5575 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005576 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5577 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5578 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005579 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005580 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005581 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005582
5583 }
Joe Perches52076492016-10-11 13:52:14 -07005584
5585 # Make $define_stmt single line, comment-free, etc
5586 my @stmt_array = split('\n', $define_stmt);
5587 my $first = 1;
5588 $define_stmt = "";
5589 foreach my $l (@stmt_array) {
5590 $l =~ s/\\$//;
5591 if ($first) {
5592 $define_stmt = $l;
5593 $first = 0;
5594 } elsif ($l =~ /^[\+ ]/) {
5595 $define_stmt .= substr($l, 1);
5596 }
5597 }
5598 $define_stmt =~ s/$;//g;
5599 $define_stmt =~ s/\s+/ /g;
5600 $define_stmt = trim($define_stmt);
5601
Joe Perchesf59b64b2016-10-11 13:52:08 -07005602# check if any macro arguments are reused (ignore '...' and 'type')
5603 foreach my $arg (@def_args) {
5604 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005605 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005606 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005607 $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 -07005608 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5609 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005610 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005611 if ($use_cnt > 1) {
5612 CHK("MACRO_ARG_REUSE",
5613 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005614 }
5615# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005616 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005617 ((defined($1) && $1 ne ',') ||
5618 (defined($2) && $2 ne ','))) {
5619 CHK("MACRO_ARG_PRECEDENCE",
5620 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005621 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005622 }
Joe Perches5023d342012-12-17 16:01:47 -08005623
Joe Perches08a28432014-10-13 15:51:55 -07005624# check for macros with flow control, but without ## concatenation
5625# ## concatenation is commonly a macro that defines a function so ignore those
5626 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005627 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005628 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005629
Joe Perches08a28432014-10-13 15:51:55 -07005630 WARN("MACRO_WITH_FLOW_CONTROL",
5631 "Macros with flow control statements should be avoided\n" . "$herectx");
5632 }
5633
Joe Perches481eb482012-12-17 16:01:56 -08005634# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005635
5636 } else {
5637 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005638 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5639 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005640 $line =~ /^\+.*\\$/) {
5641 WARN("LINE_CONTINUATIONS",
5642 "Avoid unnecessary line continuations\n" . $herecurr);
5643 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005644 }
5645
Joe Perchesb13edf72012-07-30 14:41:24 -07005646# do {} while (0) macro tests:
5647# single-statement macros do not need to be enclosed in do while (0) loop,
5648# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005649 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005650 $realfile !~ m@/vmlinux.lds.h$@ &&
5651 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5652 my $ln = $linenr;
5653 my $cnt = $realcnt;
5654 my ($off, $dstat, $dcond, $rest);
5655 my $ctx = '';
5656 ($dstat, $dcond, $ln, $cnt, $off) =
5657 ctx_statement_block($linenr, $realcnt, 0);
5658 $ctx = $dstat;
5659
5660 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005661 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005662
5663 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5664 my $stmts = $2;
5665 my $semis = $3;
5666
5667 $ctx =~ s/\n*$//;
5668 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005669 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005670
Joe Perchesac8e97f2012-08-21 16:15:53 -07005671 if (($stmts =~ tr/;/;/) == 1 &&
5672 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005673 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5674 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5675 }
5676 if (defined $semis && $semis ne "") {
5677 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5678 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5679 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005680 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5681 $ctx =~ s/\n*$//;
5682 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005683 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005684
5685 WARN("TRAILING_SEMICOLON",
5686 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005687 }
5688 }
5689
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005690# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005691 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5692 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005693 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005694 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005695 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5696 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005697 my @allowed = ();
5698 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005699 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005700 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005701 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005702 for my $chunk (@chunks) {
5703 my ($cond, $block) = @{$chunk};
5704
Andy Whitcroft773647a2008-03-28 14:15:58 -07005705 # If the condition carries leading newlines, then count those as offsets.
5706 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5707 my $offset = statement_rawlines($whitespace) - 1;
5708
Joe Perchesaad4f612012-03-23 15:02:19 -07005709 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005710 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5711
5712 # We have looked at and allowed this specific line.
5713 $suppress_ifbraces{$ln + $offset} = 1;
5714
5715 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005716 $ln += statement_rawlines($block) - 1;
5717
Andy Whitcroft773647a2008-03-28 14:15:58 -07005718 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005719
5720 $seen++ if ($block =~ /^\s*{/);
5721
Joe Perchesaad4f612012-03-23 15:02:19 -07005722 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005723 if (statement_lines($cond) > 1) {
5724 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005725 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005726 }
5727 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005728 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005729 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005730 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005731 if (statement_block_size($block) > 1) {
5732 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005733 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005734 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005735 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005736 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005737 if ($seen) {
5738 my $sum_allowed = 0;
5739 foreach (@allowed) {
5740 $sum_allowed += $_;
5741 }
5742 if ($sum_allowed == 0) {
5743 WARN("BRACES",
5744 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5745 } elsif ($sum_allowed != $allow &&
5746 $seen != $allow) {
5747 CHK("BRACES",
5748 "braces {} should be used on all arms of this statement\n" . $herectx);
5749 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005750 }
5751 }
5752 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005753 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005754 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005755 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005756
Andy Whitcroftcf655042008-03-04 14:28:20 -08005757 # Check the pre-context.
5758 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5759 #print "APW: ALLOWED: pre<$1>\n";
5760 $allowed = 1;
5761 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005762
5763 my ($level, $endln, @chunks) =
5764 ctx_statement_full($linenr, $realcnt, $-[0]);
5765
Andy Whitcroftcf655042008-03-04 14:28:20 -08005766 # Check the condition.
5767 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005768 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005769 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005770 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005771 }
5772 if (statement_lines($cond) > 1) {
5773 #print "APW: ALLOWED: cond<$cond>\n";
5774 $allowed = 1;
5775 }
5776 if ($block =~/\b(?:if|for|while)\b/) {
5777 #print "APW: ALLOWED: block<$block>\n";
5778 $allowed = 1;
5779 }
5780 if (statement_block_size($block) > 1) {
5781 #print "APW: ALLOWED: lines block<$block>\n";
5782 $allowed = 1;
5783 }
5784 # Check the post-context.
5785 if (defined $chunks[1]) {
5786 my ($cond, $block) = @{$chunks[1]};
5787 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005788 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005789 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005790 if ($block =~ /^\s*\{/) {
5791 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5792 $allowed = 1;
5793 }
5794 }
5795 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005796 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005797 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005798
Joe Perches000d1cc12011-07-25 17:13:25 -07005799 WARN("BRACES",
5800 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005801 }
5802 }
5803
Joe Perchese4c5bab2017-02-24 15:01:41 -08005804# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005805 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5806 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005807 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5808 }
5809
Joe Perches0979ae62012-12-17 16:01:59 -08005810# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005811 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005812 if (CHK("BRACES",
5813 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5814 $fix && $prevrawline =~ /^\+/) {
5815 fix_delete_line($fixlinenr - 1, $prevrawline);
5816 }
Joe Perches0979ae62012-12-17 16:01:59 -08005817 }
Joe Perches77b9a532013-07-03 15:05:29 -07005818 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005819 if (CHK("BRACES",
5820 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5821 $fix) {
5822 fix_delete_line($fixlinenr, $rawline);
5823 }
Joe Perches0979ae62012-12-17 16:01:59 -08005824 }
5825
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005826# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005827 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5828 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005829 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005830 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005831 }
5832
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005833# Check for user-visible strings broken across lines, which breaks the ability
5834# to grep for the string. Make exceptions when the previous string ends in a
5835# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5836# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005837 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005838 $prevline =~ /"\s*$/ &&
5839 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5840 if (WARN("SPLIT_STRING",
5841 "quoted string split across lines\n" . $hereprev) &&
5842 $fix &&
5843 $prevrawline =~ /^\+.*"\s*$/ &&
5844 $last_coalesced_string_linenr != $linenr - 1) {
5845 my $extracted_string = get_quoted_string($line, $rawline);
5846 my $comma_close = "";
5847 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5848 $comma_close = $1;
5849 }
5850
5851 fix_delete_line($fixlinenr - 1, $prevrawline);
5852 fix_delete_line($fixlinenr, $rawline);
5853 my $fixedline = $prevrawline;
5854 $fixedline =~ s/"\s*$//;
5855 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5856 fix_insert_line($fixlinenr - 1, $fixedline);
5857 $fixedline = $rawline;
5858 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5859 if ($fixedline !~ /\+\s*$/) {
5860 fix_insert_line($fixlinenr, $fixedline);
5861 }
5862 $last_coalesced_string_linenr = $linenr;
5863 }
5864 }
5865
5866# check for missing a space in a string concatenation
5867 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5868 WARN('MISSING_SPACE',
5869 "break quoted strings at a space character\n" . $hereprev);
5870 }
5871
Joe Perchese4b7d302017-05-08 15:55:51 -07005872# check for an embedded function name in a string when the function is known
5873# This does not work very well for -f --file checking as it depends on patch
5874# context providing the function name or a single line form for in-file
5875# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005876 if ($line =~ /^\+.*$String/ &&
5877 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005878 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5879 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005880 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005881 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005882 }
5883
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005884# check for spaces before a quoted newline
5885 if ($rawline =~ /^.*\".*\s\\n/) {
5886 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5887 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5888 $fix) {
5889 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5890 }
5891
5892 }
5893
Joe Perchesf17dba42014-10-13 15:51:51 -07005894# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005895 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5896 if (CHK("CONCATENATED_STRING",
5897 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5898 $fix) {
5899 while ($line =~ /($String)/g) {
5900 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5901 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5902 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5903 }
5904 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005905 }
5906
Joe Perches90ad30e2014-12-10 15:51:59 -08005907# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005908 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005909 if (WARN("STRING_FRAGMENTS",
5910 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5911 $fix) {
5912 while ($line =~ /($String)(?=\s*")/g) {
5913 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5914 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5915 }
5916 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005917 }
5918
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005919# check for non-standard and hex prefixed decimal printf formats
5920 my $show_L = 1; #don't show the same defect twice
5921 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005922 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005923 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005924 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005925 # check for %L
5926 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005927 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005928 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5929 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005930 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005931 # check for %Z
5932 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5933 WARN("PRINTF_Z",
5934 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5935 $show_Z = 0;
5936 }
5937 # check for 0x<decimal>
5938 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5939 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005940 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5941 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005942 }
5943
5944# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005945 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005946 WARN("LINE_CONTINUATIONS",
5947 "Avoid line continuations in quoted strings\n" . $herecurr);
5948 }
5949
Andy Whitcroft00df3442007-06-08 13:47:06 -07005950# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005951 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005952 WARN("IF_0",
5953 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5954 }
5955
5956# warn about #if 1
5957 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5958 WARN("IF_1",
5959 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005960 }
5961
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005962# check for needless "if (<foo>) fn(<foo>)" uses
5963 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005964 my $tested = quotemeta($1);
5965 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5966 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5967 my $func = $1;
5968 if (WARN('NEEDLESS_IF',
5969 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5970 $fix) {
5971 my $do_fix = 1;
5972 my $leading_tabs = "";
5973 my $new_leading_tabs = "";
5974 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5975 $leading_tabs = $1;
5976 } else {
5977 $do_fix = 0;
5978 }
5979 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5980 $new_leading_tabs = $1;
5981 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5982 $do_fix = 0;
5983 }
5984 } else {
5985 $do_fix = 0;
5986 }
5987 if ($do_fix) {
5988 fix_delete_line($fixlinenr - 1, $prevrawline);
5989 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5990 }
5991 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005992 }
5993 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005994
Joe Perchesebfdc402014-08-06 16:10:27 -07005995# check for unnecessary "Out of Memory" messages
5996 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5997 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5998 (defined $1 || defined $3) &&
5999 $linenr > 3) {
6000 my $testval = $2;
6001 my $testline = $lines[$linenr - 3];
6002
6003 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6004# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6005
Joe Perchese29a70f2019-03-07 16:28:35 -08006006 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6007 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07006008 WARN("OOM_MESSAGE",
6009 "Possible unnecessary 'out of memory' message\n" . $hereprev);
6010 }
6011 }
6012
Joe Perchesf78d98f2014-10-13 15:52:01 -07006013# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08006014 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07006015 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6016 my $level = $1;
6017 if (WARN("UNNECESSARY_KERN_LEVEL",
6018 "Possible unnecessary $level\n" . $herecurr) &&
6019 $fix) {
6020 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
6021 }
6022 }
6023
Joe Perches45c55e92017-02-24 15:01:31 -08006024# check for logging continuations
6025 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
6026 WARN("LOGGING_CONTINUATION",
6027 "Avoid logging continuation uses where feasible\n" . $herecurr);
6028 }
6029
Joe Perchesabb08a52014-12-10 15:51:46 -08006030# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07006031 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08006032 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6033 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6034 WARN("MASK_THEN_SHIFT",
6035 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6036 }
6037
Joe Perchesb75ac612014-12-10 15:52:02 -08006038# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07006039 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08006040 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6041 my $val = $1;
6042 my $equal = "!";
6043 $equal = "" if ($4 eq "!=");
6044 if (CHK("COMPARISON_TO_NULL",
6045 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6046 $fix) {
6047 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6048 }
6049 }
6050 }
6051
Joe Perches8716de32013-09-11 14:24:05 -07006052# check for bad placement of section $InitAttribute (e.g.: __initdata)
6053 if ($line =~ /(\b$InitAttribute\b)/) {
6054 my $attr = $1;
6055 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
6056 my $ptr = $1;
6057 my $var = $2;
6058 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
6059 ERROR("MISPLACED_INIT",
6060 "$attr should be placed after $var\n" . $herecurr)) ||
6061 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
6062 WARN("MISPLACED_INIT",
6063 "$attr should be placed after $var\n" . $herecurr))) &&
6064 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006065 $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 -07006066 }
6067 }
6068 }
6069
Joe Perchese970b8842013-11-12 15:10:10 -08006070# check for $InitAttributeData (ie: __initdata) with const
6071 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6072 my $attr = $1;
6073 $attr =~ /($InitAttributePrefix)(.*)/;
6074 my $attr_prefix = $1;
6075 my $attr_type = $2;
6076 if (ERROR("INIT_ATTRIBUTE",
6077 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6078 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006079 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006080 s/$InitAttributeData/${attr_prefix}initconst/;
6081 }
6082 }
6083
6084# check for $InitAttributeConst (ie: __initconst) without const
6085 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6086 my $attr = $1;
6087 if (ERROR("INIT_ATTRIBUTE",
6088 "Use of $attr requires a separate use of const\n" . $herecurr) &&
6089 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006090 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006091 /(^\+\s*(?:static\s+))/;
6092 $lead = rtrim($1);
6093 $lead = "$lead " if ($lead !~ /^\+$/);
6094 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07006095 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08006096 }
6097 }
6098
Joe Perchesc17893c2015-04-16 12:44:42 -07006099# check for __read_mostly with const non-pointer (should just be const)
6100 if ($line =~ /\b__read_mostly\b/ &&
6101 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6102 if (ERROR("CONST_READ_MOSTLY",
6103 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6104 $fix) {
6105 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6106 }
6107 }
6108
Joe Perchesfbdb8132014-04-03 14:49:14 -07006109# don't use __constant_<foo> functions outside of include/uapi/
6110 if ($realfile !~ m@^include/uapi/@ &&
6111 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6112 my $constant_func = $1;
6113 my $func = $constant_func;
6114 $func =~ s/^__constant_//;
6115 if (WARN("CONSTANT_CONVERSION",
6116 "$constant_func should be $func\n" . $herecurr) &&
6117 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006118 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07006119 }
6120 }
6121
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006122# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08006123 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07006124 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006125 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07006126 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006127 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006128 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07006129 }
6130 if ($delay > 2000) {
6131 WARN("LONG_UDELAY",
6132 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006133 }
6134 }
6135
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006136# warn about unexpectedly long msleep's
6137 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6138 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006139 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006140 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006141 }
6142 }
6143
Joe Perches36ec1932013-07-03 15:05:25 -07006144# check for comparisons of jiffies
6145 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6146 WARN("JIFFIES_COMPARISON",
6147 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6148 }
6149
Joe Perches9d7a34a2013-07-03 15:05:26 -07006150# check for comparisons of get_jiffies_64()
6151 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6152 WARN("JIFFIES_COMPARISON",
6153 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6154 }
6155
Andy Whitcroft00df3442007-06-08 13:47:06 -07006156# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006157# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07006158# print "#ifdef in C files should be avoided\n";
6159# print "$herecurr";
6160# $clean = 0;
6161# }
6162
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006163# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006164 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07006165 if (ERROR("SPACING",
6166 "exactly one space required after that #$1\n" . $herecurr) &&
6167 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006168 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07006169 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6170 }
6171
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006172 }
6173
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006174# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006175 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6176 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006177 my $which = $1;
6178 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006179 CHK("UNCOMMENTED_DEFINITION",
6180 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006181 }
6182 }
6183# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006184
6185 my $barriers = qr{
6186 mb|
6187 rmb|
Will Deaconad83ec62019-11-07 14:49:00 +00006188 wmb
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006189 }x;
6190 my $barrier_stems = qr{
6191 mb__before_atomic|
6192 mb__after_atomic|
6193 store_release|
6194 load_acquire|
6195 store_mb|
6196 (?:$barriers)
6197 }x;
6198 my $all_barriers = qr{
6199 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02006200 smp_(?:$barrier_stems)|
6201 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006202 }x;
6203
6204 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006205 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08006206 WARN("MEMORY_BARRIER",
6207 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006208 }
6209 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006210
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02006211 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6212
6213 if ($realfile !~ m@^include/asm-generic/@ &&
6214 $realfile !~ m@/barrier\.h$@ &&
6215 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6216 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6217 WARN("MEMORY_BARRIER",
6218 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6219 }
6220
Joe Perchescb426e92015-06-25 15:02:46 -07006221# check for waitqueue_active without a comment.
6222 if ($line =~ /\bwaitqueue_active\s*\(/) {
6223 if (!ctx_has_comment($first_line, $linenr)) {
6224 WARN("WAITQUEUE_ACTIVE",
6225 "waitqueue_active without comment\n" . $herecurr);
6226 }
6227 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006228
Marco Elver5099a722020-04-01 12:17:14 +02006229# check for data_race without a comment.
6230 if ($line =~ /\bdata_race\s*\(/) {
6231 if (!ctx_has_comment($first_line, $linenr)) {
6232 WARN("DATA_RACE",
6233 "data_race without comment\n" . $herecurr);
6234 }
6235 }
6236
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006237# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006238 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006239 CHK("ARCH_DEFINES",
6240 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006241 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006242
Joe Perches596ed452017-07-12 14:37:02 -07006243# check that the storage class is not after a type
6244 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006245 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07006246 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6247 }
6248# Check that the storage class is at the beginning of a declaration
6249 if ($line =~ /\b$Storage\b/ &&
6250 $line !~ /^.\s*$Storage/ &&
6251 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6252 $1 !~ /[\,\)]\s*$/) {
6253 WARN("STORAGE_CLASS",
6254 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07006255 }
6256
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006257# check the location of the inline attribute, that it is between
6258# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006259 if ($line =~ /\b$Type\s+$Inline\b/ ||
6260 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006261 ERROR("INLINE_LOCATION",
6262 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006263 }
6264
Andy Whitcroft8905a672007-11-28 16:21:06 -08006265# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08006266 if ($realfile !~ m@\binclude/uapi/@ &&
6267 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006268 if (WARN("INLINE",
6269 "plain inline is preferred over $1\n" . $herecurr) &&
6270 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006271 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006272
6273 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006274 }
6275
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006276# Check for compiler attributes
Joe Perches2b7ab452013-11-12 15:10:14 -08006277 if ($realfile !~ m@\binclude/uapi/@ &&
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006278 $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
6279 my $attr = $1;
6280 $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
Joe Perches3d130fd2011-01-12 17:00:00 -08006281
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006282 my %attr_list = (
Joe Perches0830aab2020-12-15 20:44:50 -08006283 "alias" => "__alias",
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006284 "aligned" => "__aligned",
6285 "always_inline" => "__always_inline",
6286 "assume_aligned" => "__assume_aligned",
6287 "cold" => "__cold",
6288 "const" => "__attribute_const__",
6289 "copy" => "__copy",
6290 "designated_init" => "__designated_init",
6291 "externally_visible" => "__visible",
6292 "format" => "printf|scanf",
6293 "gnu_inline" => "__gnu_inline",
6294 "malloc" => "__malloc",
6295 "mode" => "__mode",
6296 "no_caller_saved_registers" => "__no_caller_saved_registers",
6297 "noclone" => "__noclone",
6298 "noinline" => "noinline",
6299 "nonstring" => "__nonstring",
6300 "noreturn" => "__noreturn",
6301 "packed" => "__packed",
6302 "pure" => "__pure",
Joe Perches339f29d2020-12-15 20:44:43 -08006303 "section" => "__section",
Joe Perches0830aab2020-12-15 20:44:50 -08006304 "used" => "__used",
6305 "weak" => "__weak"
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006306 );
Joe Perches39b7e282011-07-25 17:13:24 -07006307
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006308 while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
Joe Perches339f29d2020-12-15 20:44:43 -08006309 my $orig_attr = $1;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006310 my $params = '';
6311 $params = $2 if defined($2);
Joe Perches339f29d2020-12-15 20:44:43 -08006312 my $curr_attr = $orig_attr;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006313 $curr_attr =~ s/^[\s_]+|[\s_]+$//g;
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006314 if (exists($attr_list{$curr_attr})) {
Joe Perches339f29d2020-12-15 20:44:43 -08006315 my $new = $attr_list{$curr_attr};
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006316 if ($curr_attr eq "format" && $params) {
6317 $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
Joe Perches339f29d2020-12-15 20:44:43 -08006318 $new = "__$1\($2";
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006319 } else {
Joe Perches339f29d2020-12-15 20:44:43 -08006320 $new = "$new$params";
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006321 }
Joe Perches339f29d2020-12-15 20:44:43 -08006322 if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6323 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
6324 $fix) {
6325 my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6326 $fixed[$fixlinenr] =~ s/$remove//;
6327 $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6328 $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6329 $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
6330 }
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006331 }
6332 }
6333
6334 # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
6335 if ($attr =~ /^_*unused/) {
6336 WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6337 "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
Joe Perchesd5e616f2013-09-11 14:23:54 -07006338 }
Joe Perches6061d942012-03-23 15:02:16 -07006339 }
6340
Joe Perches619a9082014-12-10 15:51:35 -08006341# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006342 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006343 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6344 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6345 $line =~ /\b__weak\b/)) {
6346 ERROR("WEAK_DECLARATION",
6347 "Using weak declarations can have unintended link defects\n" . $herecurr);
6348 }
6349
Tomas Winklerfd39f902016-12-12 16:46:34 -08006350# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006351 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006352 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006353 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6354 my $type = $1;
6355 if ($type =~ /\b($typeC99Typedefs)\b/) {
6356 $type = $1;
6357 my $kernel_type = 'u';
6358 $kernel_type = 's' if ($type =~ /^_*[si]/);
6359 $type =~ /(\d+)/;
6360 $kernel_type .= $1;
6361 if (CHK("PREFER_KERNEL_TYPES",
6362 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6363 $fix) {
6364 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6365 }
6366 }
6367 }
6368
Joe Perches938224b2016-01-20 14:59:15 -08006369# check for cast of C90 native int or longer types constants
6370 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6371 my $cast = $1;
6372 my $const = $2;
6373 if (WARN("TYPECAST_INT_CONSTANT",
6374 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6375 $fix) {
6376 my $suffix = "";
6377 my $newconst = $const;
6378 $newconst =~ s/${Int_type}$//;
6379 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6380 if ($cast =~ /\blong\s+long\b/) {
6381 $suffix .= 'LL';
6382 } elsif ($cast =~ /\blong\b/) {
6383 $suffix .= 'L';
6384 }
6385 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6386 }
6387 }
6388
Joe Perches8f53a9b2010-03-05 13:43:48 -08006389# check for sizeof(&)
6390 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006391 WARN("SIZEOF_ADDRESS",
6392 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006393 }
6394
Joe Perches66c80b62012-07-30 14:41:22 -07006395# check for sizeof without parenthesis
6396 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006397 if (WARN("SIZEOF_PARENTHESIS",
6398 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6399 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006400 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006401 }
Joe Perches66c80b62012-07-30 14:41:22 -07006402 }
6403
Joe Perches88982fe2012-12-17 16:02:00 -08006404# check for struct spinlock declarations
6405 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6406 WARN("USE_SPINLOCK_T",
6407 "struct spinlock should be spinlock_t\n" . $herecurr);
6408 }
6409
Joe Perchesa6962d72013-04-29 16:18:13 -07006410# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006411 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006412 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006413 $fmt =~ s/%%//g;
6414 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006415 if (WARN("PREFER_SEQ_PUTS",
6416 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6417 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006418 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006419 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006420 }
6421 }
6422
Joe Perches478b1792018-04-10 16:33:34 -07006423# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006424 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006425 defined $stat &&
6426 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6427 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006428 my $stat_real;
6429
Joe Perches0b523762017-05-08 15:55:36 -07006430 my $lc = $stat =~ tr@\n@@;
6431 $lc = $lc + $linenr;
6432 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006433 my $specifier;
6434 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006435 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006436 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006437 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6438 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006439
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006440 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006441 $specifier = $1;
6442 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006443 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006444 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006445 ($extension eq "f" &&
6446 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006447 $bad_specifier = $specifier;
6448 last;
6449 }
6450 if ($extension eq "x" && !defined($stat_real)) {
6451 if (!defined($stat_real)) {
6452 $stat_real = get_stat_real($linenr, $lc);
6453 }
6454 WARN("VSPRINTF_SPECIFIER_PX",
6455 "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");
6456 }
6457 }
6458 if ($bad_specifier ne "") {
6459 my $stat_real = get_stat_real($linenr, $lc);
6460 my $ext_type = "Invalid";
6461 my $use = "";
6462 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006463 $use = " - use %pS instead";
6464 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6465 }
6466
6467 WARN("VSPRINTF_POINTER_EXTENSION",
6468 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6469 }
Joe Perches0b523762017-05-08 15:55:36 -07006470 }
6471 }
6472
Andy Whitcroft554e1652012-01-10 15:09:57 -08006473# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006474 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006475 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006476 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006477
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006478 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006479 my $ms_val = $7;
6480 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006481
Andy Whitcroft554e1652012-01-10 15:09:57 -08006482 if ($ms_size =~ /^(0x|)0$/i) {
6483 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006484 "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 -08006485 } elsif ($ms_size =~ /^(0x|)1$/i) {
6486 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006487 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6488 }
6489 }
6490
Joe Perches98a9bba2014-01-23 15:54:52 -08006491# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006492# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006493# defined $stat &&
6494# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6495# if (WARN("PREFER_ETHER_ADDR_COPY",
6496# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6497# $fix) {
6498# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6499# }
6500# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006501
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006502# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006503# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006504# defined $stat &&
6505# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6506# WARN("PREFER_ETHER_ADDR_EQUAL",
6507# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6508# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006509
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006510# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6511# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006512# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006513# defined $stat &&
6514# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6515#
6516# my $ms_val = $7;
6517#
6518# if ($ms_val =~ /^(?:0x|)0+$/i) {
6519# if (WARN("PREFER_ETH_ZERO_ADDR",
6520# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6521# $fix) {
6522# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6523# }
6524# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6525# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6526# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6527# $fix) {
6528# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6529# }
6530# }
6531# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006532
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006533# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006534 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006535 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006536 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006537 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006538 my $call = $1;
6539 my $cast1 = deparenthesize($2);
6540 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006541 my $cast2 = deparenthesize($7);
6542 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006543 my $cast;
6544
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006545 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006546 $cast = "$cast1 or $cast2";
6547 } elsif ($cast1 ne "") {
6548 $cast = $cast1;
6549 } else {
6550 $cast = $cast2;
6551 }
6552 WARN("MINMAX",
6553 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006554 }
6555 }
6556
Joe Perches4a273192012-07-30 14:41:20 -07006557# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006558 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006559 defined $stat &&
6560 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6561 my $min = $1;
6562 my $max = $7;
6563 if ($min eq $max) {
6564 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006565 "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 -07006566 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6567 $min > $max) {
6568 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006569 "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 -07006570 }
6571 }
6572
Joe Perches823b7942013-11-12 15:10:15 -08006573# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006574 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006575 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006576 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006577 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6578 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6579 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6580 my $lc = $stat =~ tr@\n@@;
6581 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006582 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006583 WARN("NAKED_SSCANF",
6584 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6585 }
6586
Joe Perchesafc819a2014-06-04 16:12:08 -07006587# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006588 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006589 defined $stat &&
6590 $line =~ /\bsscanf\b/) {
6591 my $lc = $stat =~ tr@\n@@;
6592 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006593 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006594 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6595 my $format = $6;
6596 my $count = $format =~ tr@%@%@;
6597 if ($count == 1 &&
6598 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6599 WARN("SSCANF_TO_KSTRTO",
6600 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6601 }
6602 }
6603 }
6604
Joe Perches70dc8a42013-09-11 14:23:58 -07006605# check for new externs in .h files.
6606 if ($realfile =~ /\.h$/ &&
6607 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006608 if (CHK("AVOID_EXTERNS",
6609 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006610 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006611 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006612 }
6613 }
6614
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006615# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006616 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006617 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006618 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006619 my $function_name = $1;
6620 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006621
6622 my $s = $stat;
6623 if (defined $cond) {
6624 substr($s, 0, length($cond), '');
6625 }
Kees Cookd8b44b52020-06-03 14:18:09 -07006626 if ($s =~ /^\s*;/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006627 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006628 WARN("AVOID_EXTERNS",
6629 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006630 }
6631
6632 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006633 WARN("FUNCTION_ARGUMENTS",
6634 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006635 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006636
6637 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6638 $stat =~ /^.\s*extern\s+/)
6639 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006640 WARN("AVOID_EXTERNS",
6641 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006642 }
6643
Joe Perchesa0ad7592017-07-10 15:52:19 -07006644# check for function declarations that have arguments without identifier names
6645 if (defined $stat &&
Kees Cookd8b44b52020-06-03 14:18:09 -07006646 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6647 $1 ne "void") {
6648 my $args = trim($1);
Joe Perchesca0d8922016-10-11 13:52:16 -07006649 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6650 my $arg = trim($1);
Kees Cookd8b44b52020-06-03 14:18:09 -07006651 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006652 WARN("FUNCTION_ARGUMENTS",
6653 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6654 }
6655 }
6656 }
6657
Joe Perchesa0ad7592017-07-10 15:52:19 -07006658# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006659 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006660 defined $stat &&
6661 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6662 $context_function = $1;
6663
6664# check for multiline function definition with misplaced open brace
6665 my $ok = 0;
6666 my $cnt = statement_rawlines($stat);
6667 my $herectx = $here . "\n";
6668 for (my $n = 0; $n < $cnt; $n++) {
6669 my $rl = raw_line($linenr, $n);
6670 $herectx .= $rl . "\n";
6671 $ok = 1 if ($rl =~ /^[ \+]\{/);
6672 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6673 last if $rl =~ /^[ \+].*\{/;
6674 }
6675 if (!$ok) {
6676 ERROR("OPEN_BRACE",
6677 "open brace '{' following function definitions go on the next line\n" . $herectx);
6678 }
6679 }
6680
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006681# checks for new __setup's
6682 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6683 my $name = $1;
6684
6685 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006686 CHK("UNDOCUMENTED_SETUP",
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07006687 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006688 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006689 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006690
Joe Perchese29a70f2019-03-07 16:28:35 -08006691# check for pointless casting of alloc functions
6692 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006693 WARN("UNNECESSARY_CASTS",
6694 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006695 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006696
Joe Perchesa640d252013-07-03 15:05:21 -07006697# alloc style
6698# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006699 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006700 $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 -07006701 CHK("ALLOC_SIZEOF_STRUCT",
6702 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6703 }
6704
Joe Perches60a55362014-06-04 16:12:07 -07006705# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006706 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006707 defined $stat &&
6708 $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 -07006709 my $oldfunc = $3;
6710 my $a1 = $4;
6711 my $a2 = $10;
6712 my $newfunc = "kmalloc_array";
6713 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006714 my $r1 = $a1;
6715 my $r2 = $a2;
6716 if ($a1 =~ /^sizeof\s*\S/) {
6717 $r1 = $a2;
6718 $r2 = $a1;
6719 }
6720 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6721 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006722 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006723 my $herectx = get_stat_here($linenr, $cnt, $here);
6724
Joe Perches60a55362014-06-04 16:12:07 -07006725 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006726 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6727 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006728 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006729 $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 -07006730 }
6731 }
6732 }
6733
Joe Perches972fdea2013-04-29 16:18:12 -07006734# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006735 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006736 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6737 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006738 WARN("KREALLOC_ARG_REUSE",
6739 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6740 }
6741
Joe Perches5ce59ae2013-02-21 16:44:18 -08006742# check for alloc argument mismatch
6743 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6744 WARN("ALLOC_ARRAY_ARGS",
6745 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6746 }
6747
Joe Perchescaf2a542011-01-12 16:59:56 -08006748# check for multiple semicolons
6749 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006750 if (WARN("ONE_SEMICOLON",
6751 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6752 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006753 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006754 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006755 }
6756
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006757# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6758 if ($realfile !~ m@^include/uapi/@ &&
6759 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006760 my $ull = "";
6761 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6762 if (CHK("BIT_MACRO",
6763 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6764 $fix) {
6765 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6766 }
6767 }
6768
Joe Perches50161262020-08-11 18:35:07 -07006769# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006770 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
Joe Perches50161262020-08-11 18:35:07 -07006771 WARN("IS_ENABLED_CONFIG",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006772 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
Joe Perches50161262020-08-11 18:35:07 -07006773 }
6774
Joe Perches2d632742016-05-20 17:04:00 -07006775# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006776 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 -07006777 my $config = $1;
6778 if (WARN("PREFER_IS_ENABLED",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006779 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
Joe Perches2d632742016-05-20 17:04:00 -07006780 $fix) {
6781 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6782 }
6783 }
6784
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006785# check for /* fallthrough */ like comment, prefer fallthrough;
6786 my @fallthroughs = (
6787 'fallthrough',
6788 '@fallthrough@',
6789 'lint -fallthrough[ \t]*',
6790 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6791 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6792 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6793 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6794 );
6795 if ($raw_comment ne '') {
6796 foreach my $ft (@fallthroughs) {
6797 if ($raw_comment =~ /$ft/) {
6798 my $msg_level = \&WARN;
6799 $msg_level = \&CHK if ($file);
6800 &{$msg_level}("PREFER_FALLTHROUGH",
6801 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6802 last;
6803 }
6804 }
6805 }
6806
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006807# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006808 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006809 defined $stat &&
6810 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006811 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006812 my $herectx = get_stat_here($linenr, $cnt, $here);
6813
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006814 WARN("DEFAULT_NO_BREAK",
6815 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006816 }
6817
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006818# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006819 if ($line =~ /\b__FUNCTION__\b/) {
6820 if (WARN("USE_FUNC",
6821 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6822 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006823 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006824 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006825 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006826
Joe Perches62ec8182015-02-13 14:38:18 -08006827# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6828 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6829 ERROR("DATE_TIME",
6830 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6831 }
6832
Joe Perches2c924882012-03-23 15:02:20 -07006833# check for use of yield()
6834 if ($line =~ /\byield\s*\(\s*\)/) {
6835 WARN("YIELD",
6836 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6837 }
6838
Joe Perches179f8f42013-07-03 15:05:30 -07006839# check for comparisons against true and false
6840 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6841 my $lead = $1;
6842 my $arg = $2;
6843 my $test = $3;
6844 my $otype = $4;
6845 my $trail = $5;
6846 my $op = "!";
6847
6848 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6849
6850 my $type = lc($otype);
6851 if ($type =~ /^(?:true|false)$/) {
6852 if (("$test" eq "==" && "$type" eq "true") ||
6853 ("$test" eq "!=" && "$type" eq "false")) {
6854 $op = "";
6855 }
6856
6857 CHK("BOOL_COMPARISON",
6858 "Using comparison to $otype is error prone\n" . $herecurr);
6859
6860## maybe suggesting a correct construct would better
6861## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6862
6863 }
6864 }
6865
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006866# check for semaphores initialized locked
6867 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006868 WARN("CONSIDER_COMPLETION",
6869 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006870 }
Joe Perches6712d852012-03-23 15:02:20 -07006871
Joe Perches67d0a072011-10-31 17:13:10 -07006872# recommend kstrto* over simple_strto* and strict_strto*
6873 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006874 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006875 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006876 }
Joe Perches6712d852012-03-23 15:02:20 -07006877
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006878# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006879 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006880 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006881 "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 -07006882 }
Joe Perches6712d852012-03-23 15:02:20 -07006883
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006884# check for spin_is_locked(), suggest lockdep instead
6885 if ($line =~ /\bspin_is_locked\(/) {
6886 WARN("USE_LOCKDEP",
6887 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6888 }
6889
Joe Perches9189c7e2018-09-07 15:26:18 -07006890# check for deprecated apis
6891 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6892 my $deprecated_api = $1;
6893 my $new_api = $deprecated_apis{$deprecated_api};
6894 WARN("DEPRECATED_API",
6895 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6896 }
6897
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006898# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006899# and avoid what seem like struct definitions 'struct foo {'
Quentin Monnetced69da12020-08-11 18:35:13 -07006900 if (defined($const_structs) &&
6901 $line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006902 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006903 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006904 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006905 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006906
6907# use of NR_CPUS is usually wrong
6908# ignore definitions of NR_CPUS and usage to define arrays as likely right
6909 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006910 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6911 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006912 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6913 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6914 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006915 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006916 WARN("NR_CPUS",
6917 "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 -07006918 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006919
Joe Perches52ea8502013-11-12 15:10:09 -08006920# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6921 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6922 ERROR("DEFINE_ARCH_HAS",
6923 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6924 }
6925
Joe Perchesacd93622015-02-13 14:38:38 -08006926# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006927 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006928 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6929 WARN("LIKELY_MISUSE",
6930 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6931 }
6932
Denis Efremovde3f1862019-09-25 16:49:25 -07006933# nested likely/unlikely calls
6934 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6935 WARN("LIKELY_MISUSE",
6936 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6937 }
6938
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006939# whine mightly about in_atomic
6940 if ($line =~ /\bin_atomic\s*\(/) {
6941 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006942 ERROR("IN_ATOMIC",
6943 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006944 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006945 WARN("IN_ATOMIC",
6946 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006947 }
6948 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006949
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006950# check for mutex_trylock_recursive usage
6951 if ($line =~ /mutex_trylock_recursive/) {
6952 ERROR("LOCKING",
6953 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6954 }
6955
Peter Zijlstra1704f472010-03-19 01:37:42 +01006956# check for lockdep_set_novalidate_class
6957 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6958 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6959 if ($realfile !~ m@^kernel/lockdep@ &&
6960 $realfile !~ m@^include/linux/lockdep@ &&
6961 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006962 ERROR("LOCKDEP",
6963 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006964 }
6965 }
Dave Jones88f88312011-01-12 16:59:59 -08006966
Joe Perchesb392c642015-04-16 12:44:16 -07006967 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6968 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006969 WARN("EXPORTED_WORLD_WRITABLE",
6970 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006971 }
Joe Perches24358802014-04-03 14:49:13 -07006972
Joe Perches00180462018-02-06 15:38:55 -08006973# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6974# and whether or not function naming is typical and if
6975# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006976 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006977 defined $stat &&
6978 $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*\)/) {
6979 my $var = $1;
6980 my $perms = $2;
6981 my $show = $3;
6982 my $store = $4;
6983 my $octal_perms = perms_to_octal($perms);
6984 if ($show =~ /^${var}_show$/ &&
6985 $store =~ /^${var}_store$/ &&
6986 $octal_perms eq "0644") {
6987 if (WARN("DEVICE_ATTR_RW",
6988 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6989 $fix) {
6990 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6991 }
6992 } elsif ($show =~ /^${var}_show$/ &&
6993 $store =~ /^NULL$/ &&
6994 $octal_perms eq "0444") {
6995 if (WARN("DEVICE_ATTR_RO",
6996 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6997 $fix) {
6998 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6999 }
7000 } elsif ($show =~ /^NULL$/ &&
7001 $store =~ /^${var}_store$/ &&
7002 $octal_perms eq "0200") {
7003 if (WARN("DEVICE_ATTR_WO",
7004 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
7005 $fix) {
7006 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
7007 }
7008 } elsif ($octal_perms eq "0644" ||
7009 $octal_perms eq "0444" ||
7010 $octal_perms eq "0200") {
7011 my $newshow = "$show";
7012 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
7013 my $newstore = $store;
7014 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
7015 my $rename = "";
7016 if ($show ne $newshow) {
7017 $rename .= " '$show' to '$newshow'";
7018 }
7019 if ($store ne $newstore) {
7020 $rename .= " '$store' to '$newstore'";
7021 }
7022 WARN("DEVICE_ATTR_FUNCTIONS",
7023 "Consider renaming function(s)$rename\n" . $herecurr);
7024 } else {
7025 WARN("DEVICE_ATTR_PERMS",
7026 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
7027 }
7028 }
7029
Joe Perches515a2352014-04-03 14:49:24 -07007030# Mode permission misuses where it seems decimal should be octal
7031# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08007032# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
7033# specific definition of not visible in sysfs.
7034# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
7035# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07007036 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07007037 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07007038 $line =~ /$mode_perms_search/) {
7039 foreach my $entry (@mode_permission_funcs) {
7040 my $func = $entry->[0];
7041 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07007042
Joe Perches459cf0a2016-10-11 13:52:19 -07007043 my $lc = $stat =~ tr@\n@@;
7044 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07007045 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07007046
Joe Perches515a2352014-04-03 14:49:24 -07007047 my $skip_args = "";
7048 if ($arg_pos > 1) {
7049 $arg_pos--;
7050 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
7051 }
Joe Perchesf90774e2016-10-11 13:51:47 -07007052 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07007053 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07007054 my $val = $1;
7055 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08007056 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
7057 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
7058 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07007059 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07007060 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07007061 }
7062 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08007063 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07007064 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07007065 }
Joe Perches24358802014-04-03 14:49:13 -07007066 }
7067 }
7068 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007069
Joe Perches459cf0a2016-10-11 13:52:19 -07007070# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07007071 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08007072 my $oval = $1;
7073 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07007074 if (WARN("SYMBOLIC_PERMS",
7075 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7076 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08007077 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07007078 }
7079 }
7080
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007081# validate content of MODULE_LICENSE against list from include/linux/module.h
7082 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
7083 my $extracted_string = get_quoted_string($line, $rawline);
7084 my $valid_licenses = qr{
7085 GPL|
7086 GPL\ v2|
7087 GPL\ and\ additional\ rights|
7088 Dual\ BSD/GPL|
7089 Dual\ MIT/GPL|
7090 Dual\ MPL/GPL|
7091 Proprietary
7092 }x;
7093 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
7094 WARN("MODULE_LICENSE",
7095 "unknown module license " . $extracted_string . "\n" . $herecurr);
7096 }
7097 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07007098
7099# check for sysctl duplicate constants
7100 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
7101 WARN("DUPLICATED_SYSCTL_CONST",
7102 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
7103 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007104 }
7105
7106 # If we have no input at all, then there is nothing to report on
7107 # so just keep quiet.
7108 if ($#rawlines == -1) {
7109 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007110 }
7111
Andy Whitcroft8905a672007-11-28 16:21:06 -08007112 # In mailback mode only produce a report in the negative, for
7113 # things that appear to be patches.
7114 if ($mailback && ($clean == 1 || !$is_patch)) {
7115 exit(0);
7116 }
7117
Dwaipayan Raye73d2712020-12-15 20:44:56 -08007118 # This is not a patch, and we are in 'no-patch' mode so
Andy Whitcroft8905a672007-11-28 16:21:06 -08007119 # just keep quiet.
7120 if (!$chk_patch && !$is_patch) {
7121 exit(0);
7122 }
7123
Stafford Hornea08ffbe2017-10-03 16:16:51 -07007124 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007125 ERROR("NOT_UNIFIED_DIFF",
7126 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007127 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007128 if ($is_patch && $has_commit_log && $chk_signoff) {
7129 if ($signoff == 0) {
7130 ERROR("MISSING_SIGN_OFF",
7131 "Missing Signed-off-by: line(s)\n");
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07007132 } elsif ($authorsignoff != 1) {
7133 # authorsignoff values:
7134 # 0 -> missing sign off
7135 # 1 -> sign off identical
7136 # 2 -> names and addresses match, comments mismatch
7137 # 3 -> addresses match, names different
7138 # 4 -> names match, addresses different
7139 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
7140
7141 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
7142
7143 if ($authorsignoff == 0) {
7144 ERROR("NO_AUTHOR_SIGN_OFF",
7145 "Missing Signed-off-by: line by nominal patch author '$author'\n");
7146 } elsif ($authorsignoff == 2) {
7147 CHK("FROM_SIGN_OFF_MISMATCH",
7148 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7149 } elsif ($authorsignoff == 3) {
7150 WARN("FROM_SIGN_OFF_MISMATCH",
7151 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7152 } elsif ($authorsignoff == 4) {
7153 WARN("FROM_SIGN_OFF_MISMATCH",
7154 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7155 } elsif ($authorsignoff == 5) {
7156 WARN("FROM_SIGN_OFF_MISMATCH",
7157 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7158 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007159 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007160 }
7161
Andy Whitcroft8905a672007-11-28 16:21:06 -08007162 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007163 if ($summary && !($clean == 1 && $quiet == 1)) {
7164 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08007165 print "total: $cnt_error errors, $cnt_warn warnings, " .
7166 (($check)? "$cnt_chk checks, " : "") .
7167 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07007168 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08007169
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007170 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07007171 # If there were any defects found and not already fixing them
7172 if (!$clean and !$fix) {
7173 print << "EOM"
7174
7175NOTE: For some of the reported defects, checkpatch may be able to
7176 mechanically convert to the typical style using --fix or --fix-inplace.
7177EOM
7178 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007179 # If there were whitespace errors which cleanpatch can fix
7180 # then suggest that.
7181 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07007182 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07007183 print << "EOM"
7184
7185NOTE: Whitespace errors detected.
7186 You may wish to use scripts/cleanpatch or scripts/cleanfile
7187EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007188 }
7189 }
7190
Joe Perchesd752fcc2014-08-06 16:11:05 -07007191 if ($clean == 0 && $fix &&
7192 ("@rawlines" ne "@fixed" ||
7193 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08007194 my $newfile = $filename;
7195 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07007196 my $linecount = 0;
7197 my $f;
7198
Joe Perchesd752fcc2014-08-06 16:11:05 -07007199 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7200
Joe Perches3705ce52013-07-03 15:05:31 -07007201 open($f, '>', $newfile)
7202 or die "$P: Can't open $newfile for write\n";
7203 foreach my $fixed_line (@fixed) {
7204 $linecount++;
7205 if ($file) {
7206 if ($linecount > 3) {
7207 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07007208 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07007209 }
7210 } else {
7211 print $f $fixed_line . "\n";
7212 }
7213 }
7214 close($f);
7215
7216 if (!$quiet) {
7217 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07007218
Joe Perches3705ce52013-07-03 15:05:31 -07007219Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
7220
7221Do _NOT_ trust the results written to this file.
7222Do _NOT_ submit these changes without inspecting them for correctness.
7223
7224This EXPERIMENTAL file is simply a convenience to help rewrite patches.
7225No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07007226EOM
7227 }
7228 }
7229
Joe Perchesd8469f12015-06-25 15:03:00 -07007230 if ($quiet == 0) {
7231 print "\n";
7232 if ($clean == 1) {
7233 print "$vname has no obvious style problems and is ready for submission.\n";
7234 } else {
7235 print "$vname has style problems, please review.\n";
7236 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007237 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007238 return $clean;
7239}