blob: 0ad235ee96f9121c044ed217158e951f31b8d549 [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
Joe Perches7d2367a2011-07-25 17:13:22 -0700856sub deparenthesize {
857 my ($string) = @_;
858 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700859
860 while ($string =~ /^\s*\(.*\)\s*$/) {
861 $string =~ s@^\s*\(\s*@@;
862 $string =~ s@\s*\)\s*$@@;
863 }
864
Joe Perches7d2367a2011-07-25 17:13:22 -0700865 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700866
Joe Perches7d2367a2011-07-25 17:13:22 -0700867 return $string;
868}
869
Joe Perches34456862013-07-03 15:05:34 -0700870sub seed_camelcase_file {
871 my ($file) = @_;
872
873 return if (!(-f $file));
874
875 local $/;
876
877 open(my $include_file, '<', "$file")
878 or warn "$P: Can't read '$file' $!\n";
879 my $text = <$include_file>;
880 close($include_file);
881
882 my @lines = split('\n', $text);
883
884 foreach my $line (@lines) {
885 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
886 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
887 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800888 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
889 $camelcase{$1} = 1;
890 } 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 -0700891 $camelcase{$1} = 1;
892 }
893 }
894}
895
Joe Perchescd28b112019-12-04 16:52:09 -0800896our %maintained_status = ();
897
Joe Perches85b0ee12016-10-11 13:51:44 -0700898sub is_maintained_obsolete {
899 my ($filename) = @_;
900
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800901 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700902
Joe Perchescd28b112019-12-04 16:52:09 -0800903 if (!exists($maintained_status{$filename})) {
904 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
905 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700906
Joe Perchescd28b112019-12-04 16:52:09 -0800907 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700908}
909
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700910sub is_SPDX_License_valid {
911 my ($license) = @_;
912
Joe Perches0f7f6352020-10-24 16:59:04 -0700913 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700914
Joe Perches56294112018-08-21 21:58:04 -0700915 my $root_path = abs_path($root);
916 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700917 return 0 if ($status ne "");
918 return 1;
919}
920
Joe Perches34456862013-07-03 15:05:34 -0700921my $camelcase_seeded = 0;
922sub seed_camelcase_includes {
923 return if ($camelcase_seeded);
924
925 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700926 my $camelcase_cache = "";
927 my @include_files = ();
928
929 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700930
Joe Perches0f7f6352020-10-24 16:59:04 -0700931 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700932 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -0700933 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700934 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700935 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700936 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700937 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700938 @include_files = split('\n', $files);
939 foreach my $file (@include_files) {
940 my $date = POSIX::strftime("%Y%m%d%H%M",
941 localtime((stat $file)[9]));
942 $last_mod_date = $date if ($last_mod_date < $date);
943 }
944 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700945 }
Joe Perchesc707a812013-07-08 16:00:43 -0700946
947 if ($camelcase_cache ne "" && -f $camelcase_cache) {
948 open(my $camelcase_file, '<', "$camelcase_cache")
949 or warn "$P: Can't read '$camelcase_cache' $!\n";
950 while (<$camelcase_file>) {
951 chomp;
952 $camelcase{$_} = 1;
953 }
954 close($camelcase_file);
955
956 return;
957 }
958
Joe Perches0f7f6352020-10-24 16:59:04 -0700959 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700960 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700961 @include_files = split('\n', $files);
962 }
963
Joe Perches34456862013-07-03 15:05:34 -0700964 foreach my $file (@include_files) {
965 seed_camelcase_file($file);
966 }
Joe Perches351b2a12013-07-03 15:05:36 -0700967
Joe Perchesc707a812013-07-08 16:00:43 -0700968 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700969 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700970 open(my $camelcase_file, '>', "$camelcase_cache")
971 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700972 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
973 print $camelcase_file ("$_\n");
974 }
975 close($camelcase_file);
976 }
Joe Perches34456862013-07-03 15:05:34 -0700977}
978
Joe Perchesf5f61322020-10-15 20:12:12 -0700979sub git_is_single_file {
980 my ($filename) = @_;
981
982 return 0 if ((which("git") eq "") || !(-e "$gitroot"));
983
984 my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
985 my $count = $output =~ tr/\n//;
986 return $count eq 1 && $output =~ m{^${filename}$};
987}
988
Joe Perchesd311cd42014-08-06 16:10:57 -0700989sub git_commit_info {
990 my ($commit, $id, $desc) = @_;
991
Joe Perches0f7f6352020-10-24 16:59:04 -0700992 return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
Joe Perchesd311cd42014-08-06 16:10:57 -0700993
Joe Perchesdbbf8692019-09-25 16:46:52 -0700994 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -0700995 $output =~ s/^\s*//gm;
996 my @lines = split("\n", $output);
997
Joe Perches0d7835f2015-02-13 14:38:35 -0800998 return ($id, $desc) if ($#lines < 0);
999
Sean Christopherson5a7f4452019-09-25 16:46:49 -07001000 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -07001001# Maybe one day convert this block of bash into something that returns
1002# all matching commit ids, but it's very slow...
1003#
1004# echo "checking commits $1..."
1005# git rev-list --remotes | grep -i "^$1" |
1006# while read line ; do
1007# git log --format='%H %s' -1 $line |
1008# echo "commit $(cut -c 1-12,41-)"
1009# done
1010 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07001011 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -07001012 } else {
1013 $id = substr($lines[0], 0, 12);
1014 $desc = substr($lines[0], 41);
1015 }
1016
1017 return ($id, $desc);
1018}
1019
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001020$chk_signoff = 0 if ($file);
1021
Andy Whitcroft00df3442007-06-08 13:47:06 -07001022my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001023my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001024my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001025my @fixed_inserted = ();
1026my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001027my $fixlinenr = -1;
1028
Du, Changbin4a593c32016-05-20 17:04:16 -07001029# If input is git commits, extract all commits from the commit expressions.
1030# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
Joe Perches0f7f6352020-10-24 16:59:04 -07001031die "$P: No git repository found\n" if ($git && !-e "$gitroot");
Du, Changbin4a593c32016-05-20 17:04:16 -07001032
1033if ($git) {
1034 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001035 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001036 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001037 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1038 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001039 } elsif ($commit_expr =~ m/\.\./) {
1040 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001041 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001042 $git_range = "-1 $commit_expr";
1043 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001044 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001045 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001046 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1047 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001048 my $sha1 = $1;
1049 my $subject = $2;
1050 unshift(@commits, $sha1);
1051 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001052 }
1053 }
1054 die "$P: no git commits after extraction!\n" if (@commits == 0);
1055 @ARGV = @commits;
1056}
1057
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001058my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001059$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001060for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001061 my $FILE;
Joe Perchesf5f61322020-10-15 20:12:12 -07001062 my $is_git_file = git_is_single_file($filename);
1063 my $oldfile = $file;
1064 $file = 1 if ($is_git_file);
Du, Changbin4a593c32016-05-20 17:04:16 -07001065 if ($git) {
1066 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1067 die "$P: $filename: git format-patch failed - $!\n";
1068 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001069 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001070 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001071 } elsif ($filename eq '-') {
1072 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001073 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001074 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001075 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001076 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001077 if ($filename eq '-') {
1078 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001079 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001080 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001081 } else {
1082 $vname = $filename;
1083 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001084 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001085 chomp;
1086 push(@rawlines, $_);
Geert Uytterhoevenc7f574d2020-06-04 16:50:43 -07001087 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001088 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001089 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001090
1091 if ($#ARGV > 0 && $quiet == 0) {
1092 print '-' x length($vname) . "\n";
1093 print "$vname\n";
1094 print '-' x length($vname) . "\n";
1095 }
1096
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001097 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001098 $exit = 1;
1099 }
1100 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001101 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001102 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001103 @fixed_inserted = ();
1104 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001105 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001106 @modifierListFile = ();
1107 @typeListFile = ();
1108 build_types();
Joe Perchesf5f61322020-10-15 20:12:12 -07001109 $file = $oldfile if ($is_git_file);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001110}
1111
Joe Perchesd8469f12015-06-25 15:03:00 -07001112if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001113 hash_show_words(\%use_type, "Used");
1114 hash_show_words(\%ignore_type, "Ignored");
1115
Joe Perches5b579802018-08-21 21:57:33 -07001116 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001117 print << "EOM"
1118
1119NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001120 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001121EOM
1122 }
1123 if ($exit) {
1124 print << "EOM"
1125
1126NOTE: If any of the errors are false positives, please report
1127 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1128EOM
1129 }
1130}
1131
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001132exit($exit);
1133
1134sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001135 my ($root) = @_;
1136
1137 my @tree_check = (
1138 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1139 "README", "Documentation", "arch", "include", "drivers",
1140 "fs", "init", "ipc", "kernel", "lib", "scripts",
1141 );
1142
1143 foreach my $check (@tree_check) {
1144 if (! -e $root . '/' . $check) {
1145 return 0;
1146 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001147 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001148 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001149}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001150
Joe Perches20112472011-07-25 17:13:23 -07001151sub parse_email {
1152 my ($formatted_email) = @_;
1153
1154 my $name = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001155 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001156 my $address = "";
1157 my $comment = "";
1158
1159 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1160 $name = $1;
1161 $address = $2;
1162 $comment = $3 if defined $3;
1163 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1164 $address = $1;
1165 $comment = $2 if defined $2;
1166 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1167 $address = $1;
1168 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001169 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001170 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001171 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001172 $name =~ s/^\"|\"$//g;
1173 # If there's a name left after stripping spaces and
1174 # leading quotes, and the address doesn't have both
1175 # leading and trailing angle brackets, the address
1176 # is invalid. ie:
1177 # "joe smith joe@smith.com" bad
1178 # "joe smith <joe@smith.com" bad
1179 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1180 $name = "";
1181 $address = "";
1182 $comment = "";
1183 }
1184 }
1185
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001186 $comment = trim($comment);
Joe Perches3705ce52013-07-03 15:05:31 -07001187 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001188 $name =~ s/^\"|\"$//g;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001189 if ($name =~ s/(\s*\([^\)]+\))\s*//) {
Joe Perchesdfa05c22020-04-06 20:10:48 -07001190 $name_comment = trim($1);
1191 }
Joe Perches3705ce52013-07-03 15:05:31 -07001192 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001193 $address =~ s/^\<|\>$//g;
1194
1195 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1196 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1197 $name = "\"$name\"";
1198 }
1199
Joe Perchesdfa05c22020-04-06 20:10:48 -07001200 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001201}
1202
1203sub format_email {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001204 my ($name, $name_comment, $address, $comment) = @_;
Joe Perches20112472011-07-25 17:13:23 -07001205
1206 my $formatted_email;
1207
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001208 $name_comment = trim($name_comment);
1209 $comment = trim($comment);
Joe Perches3705ce52013-07-03 15:05:31 -07001210 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001211 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001212 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001213
1214 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1215 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1216 $name = "\"$name\"";
1217 }
1218
1219 if ("$name" eq "") {
1220 $formatted_email = "$address";
1221 } else {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001222 $formatted_email = "$name$name_comment <$address>";
Joe Perches20112472011-07-25 17:13:23 -07001223 }
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001224 $formatted_email .= "$comment";
Joe Perches20112472011-07-25 17:13:23 -07001225 return $formatted_email;
1226}
1227
Joe Perchesdfa05c22020-04-06 20:10:48 -07001228sub reformat_email {
1229 my ($email) = @_;
1230
1231 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001232 return format_email($email_name, $name_comment, $email_address, $comment);
Joe Perchesdfa05c22020-04-06 20:10:48 -07001233}
1234
1235sub same_email_addresses {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001236 my ($email1, $email2, $match_comment) = @_;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001237
1238 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1239 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1240
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001241 if ($match_comment != 1) {
1242 return $email1_name eq $email2_name &&
1243 $email1_address eq $email2_address;
1244 }
Joe Perchesdfa05c22020-04-06 20:10:48 -07001245 return $email1_name eq $email2_name &&
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001246 $email1_address eq $email2_address &&
1247 $name1_comment eq $name2_comment &&
1248 $comment1 eq $comment2;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001249}
1250
Joe Perchesd311cd42014-08-06 16:10:57 -07001251sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001252 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001253
Joe Perchesbd474ca2014-08-06 16:11:10 -07001254 foreach my $path (split(/:/, $ENV{PATH})) {
1255 if (-e "$path/$bin") {
1256 return "$path/$bin";
1257 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001258 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001259
Joe Perchesbd474ca2014-08-06 16:11:10 -07001260 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001261}
1262
Joe Perches000d1cc12011-07-25 17:13:25 -07001263sub which_conf {
1264 my ($conf) = @_;
1265
1266 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1267 if (-e "$path/$conf") {
1268 return "$path/$conf";
1269 }
1270 }
1271
1272 return "";
1273}
1274
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001275sub expand_tabs {
1276 my ($str) = @_;
1277
1278 my $res = '';
1279 my $n = 0;
1280 for my $c (split(//, $str)) {
1281 if ($c eq "\t") {
1282 $res .= ' ';
1283 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001284 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001285 $res .= ' ';
1286 }
1287 next;
1288 }
1289 $res .= $c;
1290 $n++;
1291 }
1292
1293 return $res;
1294}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001295sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001296 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001297 return $res;
1298}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001299
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001300sub line_stats {
1301 my ($line) = @_;
1302
1303 # Drop the diff line leader and expand tabs
1304 $line =~ s/^.//;
1305 $line = expand_tabs($line);
1306
1307 # Pick the indent from the front of the line.
1308 my ($white) = ($line =~ /^(\s*)/);
1309
1310 return (length($line), length($white));
1311}
1312
Andy Whitcroft773647a2008-03-28 14:15:58 -07001313my $sanitise_quote = '';
1314
1315sub sanitise_line_reset {
1316 my ($in_comment) = @_;
1317
1318 if ($in_comment) {
1319 $sanitise_quote = '*/';
1320 } else {
1321 $sanitise_quote = '';
1322 }
1323}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001324sub sanitise_line {
1325 my ($line) = @_;
1326
1327 my $res = '';
1328 my $l = '';
1329
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001330 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001331 my $off = 0;
1332 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001333
Andy Whitcroft773647a2008-03-28 14:15:58 -07001334 # Always copy over the diff marker.
1335 $res = substr($line, 0, 1);
1336
1337 for ($off = 1; $off < length($line); $off++) {
1338 $c = substr($line, $off, 1);
1339
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001340 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001341 # and end, all to $;.
1342 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1343 $sanitise_quote = '*/';
1344
1345 substr($res, $off, 2, "$;$;");
1346 $off++;
1347 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001348 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001349 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001350 $sanitise_quote = '';
1351 substr($res, $off, 2, "$;$;");
1352 $off++;
1353 next;
1354 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001355 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1356 $sanitise_quote = '//';
1357
1358 substr($res, $off, 2, $sanitise_quote);
1359 $off++;
1360 next;
1361 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001362
1363 # A \ in a string means ignore the next character.
1364 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1365 $c eq "\\") {
1366 substr($res, $off, 2, 'XX');
1367 $off++;
1368 next;
1369 }
1370 # Regular quotes.
1371 if ($c eq "'" || $c eq '"') {
1372 if ($sanitise_quote eq '') {
1373 $sanitise_quote = $c;
1374
1375 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001376 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001377 } elsif ($sanitise_quote eq $c) {
1378 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001379 }
1380 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001381
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001382 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001383 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1384 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001385 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1386 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001387 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1388 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001389 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001390 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001391 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001392 }
1393
Daniel Walker113f04a2009-09-21 17:04:35 -07001394 if ($sanitise_quote eq '//') {
1395 $sanitise_quote = '';
1396 }
1397
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001398 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001399 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001400 my $clean = 'X' x length($1);
1401 $res =~ s@\<.*\>@<$clean>@;
1402
1403 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001404 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001405 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001406 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001407 }
1408
Joe Perchesdadf6802016-08-02 14:04:33 -07001409 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1410 my $match = $1;
1411 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1412 }
1413
Andy Whitcroft00df3442007-06-08 13:47:06 -07001414 return $res;
1415}
1416
Joe Perchesa6962d72013-04-29 16:18:13 -07001417sub get_quoted_string {
1418 my ($line, $rawline) = @_;
1419
Joe Perches478b1792018-04-10 16:33:34 -07001420 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001421 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001422 return substr($rawline, $-[0], $+[0] - $-[0]);
1423}
1424
Andy Whitcroft8905a672007-11-28 16:21:06 -08001425sub ctx_statement_block {
1426 my ($linenr, $remain, $off) = @_;
1427 my $line = $linenr - 1;
1428 my $blk = '';
1429 my $soff = $off;
1430 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001431 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001432
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001433 my $loff = 0;
1434
Andy Whitcroft8905a672007-11-28 16:21:06 -08001435 my $type = '';
1436 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001437 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001438 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001439 my $c;
1440 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001441
1442 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001443 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001444 @stack = (['', 0]) if ($#stack == -1);
1445
Andy Whitcroft773647a2008-03-28 14:15:58 -07001446 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001447 # If we are about to drop off the end, pull in more
1448 # context.
1449 if ($off >= $len) {
1450 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001451 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001452 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001453 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001454 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001455 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001456 $len = length($blk);
1457 $line++;
1458 last;
1459 }
1460 # Bail if there is no further context.
1461 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001462 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001463 last;
1464 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001465 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1466 $level++;
1467 $type = '#';
1468 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001469 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001470 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001471 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001472 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001473
Andy Whitcroft773647a2008-03-28 14:15:58 -07001474 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001475
1476 # Handle nested #if/#else.
1477 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1478 push(@stack, [ $type, $level ]);
1479 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1480 ($type, $level) = @{$stack[$#stack - 1]};
1481 } elsif ($remainder =~ /^#\s*endif\b/) {
1482 ($type, $level) = @{pop(@stack)};
1483 }
1484
Andy Whitcroft8905a672007-11-28 16:21:06 -08001485 # Statement ends at the ';' or a close '}' at the
1486 # outermost level.
1487 if ($level == 0 && $c eq ';') {
1488 last;
1489 }
1490
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001491 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001492 if ($level == 0 && $coff_set == 0 &&
1493 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1494 $remainder =~ /^(else)(?:\s|{)/ &&
1495 $remainder !~ /^else\s+if\b/) {
1496 $coff = $off + length($1) - 1;
1497 $coff_set = 1;
1498 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1499 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001500 }
1501
Andy Whitcroft8905a672007-11-28 16:21:06 -08001502 if (($type eq '' || $type eq '(') && $c eq '(') {
1503 $level++;
1504 $type = '(';
1505 }
1506 if ($type eq '(' && $c eq ')') {
1507 $level--;
1508 $type = ($level != 0)? '(' : '';
1509
1510 if ($level == 0 && $coff < $soff) {
1511 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001512 $coff_set = 1;
1513 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001514 }
1515 }
1516 if (($type eq '' || $type eq '{') && $c eq '{') {
1517 $level++;
1518 $type = '{';
1519 }
1520 if ($type eq '{' && $c eq '}') {
1521 $level--;
1522 $type = ($level != 0)? '{' : '';
1523
1524 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001525 if (substr($blk, $off + 1, 1) eq ';') {
1526 $off++;
1527 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001528 last;
1529 }
1530 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001531 # Preprocessor commands end at the newline unless escaped.
1532 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1533 $level--;
1534 $type = '';
1535 $off++;
1536 last;
1537 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001538 $off++;
1539 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001540 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001541 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001542 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001543 $line++;
1544 $remain--;
1545 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001546
1547 my $statement = substr($blk, $soff, $off - $soff + 1);
1548 my $condition = substr($blk, $soff, $coff - $soff + 1);
1549
1550 #warn "STATEMENT<$statement>\n";
1551 #warn "CONDITION<$condition>\n";
1552
Andy Whitcroft773647a2008-03-28 14:15:58 -07001553 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001554
1555 return ($statement, $condition,
1556 $line, $remain + 1, $off - $loff + 1, $level);
1557}
1558
Andy Whitcroftcf655042008-03-04 14:28:20 -08001559sub statement_lines {
1560 my ($stmt) = @_;
1561
1562 # Strip the diff line prefixes and rip blank lines at start and end.
1563 $stmt =~ s/(^|\n)./$1/g;
1564 $stmt =~ s/^\s*//;
1565 $stmt =~ s/\s*$//;
1566
1567 my @stmt_lines = ($stmt =~ /\n/g);
1568
1569 return $#stmt_lines + 2;
1570}
1571
1572sub statement_rawlines {
1573 my ($stmt) = @_;
1574
1575 my @stmt_lines = ($stmt =~ /\n/g);
1576
1577 return $#stmt_lines + 2;
1578}
1579
1580sub statement_block_size {
1581 my ($stmt) = @_;
1582
1583 $stmt =~ s/(^|\n)./$1/g;
1584 $stmt =~ s/^\s*{//;
1585 $stmt =~ s/}\s*$//;
1586 $stmt =~ s/^\s*//;
1587 $stmt =~ s/\s*$//;
1588
1589 my @stmt_lines = ($stmt =~ /\n/g);
1590 my @stmt_statements = ($stmt =~ /;/g);
1591
1592 my $stmt_lines = $#stmt_lines + 2;
1593 my $stmt_statements = $#stmt_statements + 1;
1594
1595 if ($stmt_lines > $stmt_statements) {
1596 return $stmt_lines;
1597 } else {
1598 return $stmt_statements;
1599 }
1600}
1601
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001602sub ctx_statement_full {
1603 my ($linenr, $remain, $off) = @_;
1604 my ($statement, $condition, $level);
1605
1606 my (@chunks);
1607
Andy Whitcroftcf655042008-03-04 14:28:20 -08001608 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001609 ($statement, $condition, $linenr, $remain, $off, $level) =
1610 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001611 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001612 push(@chunks, [ $condition, $statement ]);
1613 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1614 return ($level, $linenr, @chunks);
1615 }
1616
1617 # Pull in the following conditional/block pairs and see if they
1618 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001619 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001620 ($statement, $condition, $linenr, $remain, $off, $level) =
1621 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001622 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001623 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001624 #print "C: push\n";
1625 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001626 }
1627
1628 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001629}
1630
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001631sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001632 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001633 my $line;
1634 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001635 my $blk = '';
1636 my @o;
1637 my @c;
1638 my @res = ();
1639
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001640 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001641 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001642 for ($line = $start; $remain > 0; $line++) {
1643 next if ($rawlines[$line] =~ /^-/);
1644 $remain--;
1645
1646 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001647
1648 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001649 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001650 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001651 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001652 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001653 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001654 $level = pop(@stack);
1655 }
1656
Andy Whitcroft01464f32010-10-26 14:23:19 -07001657 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001658 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1659 if ($off > 0) {
1660 $off--;
1661 next;
1662 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001663
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001664 if ($c eq $close && $level > 0) {
1665 $level--;
1666 last if ($level == 0);
1667 } elsif ($c eq $open) {
1668 $level++;
1669 }
1670 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001671
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001672 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001673 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001674 }
1675
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001676 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001677 }
1678
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001679 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001680}
1681sub ctx_block_outer {
1682 my ($linenr, $remain) = @_;
1683
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001684 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1685 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001686}
1687sub ctx_block {
1688 my ($linenr, $remain) = @_;
1689
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001690 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1691 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001692}
1693sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001694 my ($linenr, $remain, $off) = @_;
1695
1696 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1697 return @r;
1698}
1699sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001700 my ($linenr, $remain) = @_;
1701
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001702 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001703}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001704sub ctx_statement_level {
1705 my ($linenr, $remain, $off) = @_;
1706
1707 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1708}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001709
1710sub ctx_locate_comment {
1711 my ($first_line, $end_line) = @_;
1712
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001713 # If c99 comment on the current line, or the line before or after
1714 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1715 return $current_comment if (defined $current_comment);
1716 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1717 return $current_comment if (defined $current_comment);
1718 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1719 return $current_comment if (defined $current_comment);
1720
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001721 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001722 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001723 return $current_comment if (defined $current_comment);
1724
1725 # Look through the context and try and figure out if there is a
1726 # comment.
1727 my $in_comment = 0;
1728 $current_comment = '';
1729 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001730 my $line = $rawlines[$linenr - 1];
1731 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001732 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1733 $in_comment = 1;
1734 }
1735 if ($line =~ m@/\*@) {
1736 $in_comment = 1;
1737 }
1738 if (!$in_comment && $current_comment ne '') {
1739 $current_comment = '';
1740 }
1741 $current_comment .= $line . "\n" if ($in_comment);
1742 if ($line =~ m@\*/@) {
1743 $in_comment = 0;
1744 }
1745 }
1746
1747 chomp($current_comment);
1748 return($current_comment);
1749}
1750sub ctx_has_comment {
1751 my ($first_line, $end_line) = @_;
1752 my $cmt = ctx_locate_comment($first_line, $end_line);
1753
Andy Whitcroft00df3442007-06-08 13:47:06 -07001754 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001755 ##print "CMMT: $cmt\n";
1756
1757 return ($cmt ne '');
1758}
1759
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001760sub raw_line {
1761 my ($linenr, $cnt) = @_;
1762
1763 my $offset = $linenr - 1;
1764 $cnt++;
1765
1766 my $line;
1767 while ($cnt) {
1768 $line = $rawlines[$offset++];
1769 next if (defined($line) && $line =~ /^-/);
1770 $cnt--;
1771 }
1772
1773 return $line;
1774}
1775
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001776sub get_stat_real {
1777 my ($linenr, $lc) = @_;
1778
1779 my $stat_real = raw_line($linenr, 0);
1780 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1781 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1782 }
1783
1784 return $stat_real;
1785}
1786
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001787sub get_stat_here {
1788 my ($linenr, $cnt, $here) = @_;
1789
1790 my $herectx = $here . "\n";
1791 for (my $n = 0; $n < $cnt; $n++) {
1792 $herectx .= raw_line($linenr, $n) . "\n";
1793 }
1794
1795 return $herectx;
1796}
1797
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001798sub cat_vet {
1799 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001800 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001801
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001802 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001803 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1804 $res .= $1;
1805 if ($2 ne '') {
1806 $coded = sprintf("^%c", unpack('C', $2) + 64);
1807 $res .= $coded;
1808 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001809 }
1810 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001811
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001812 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001813}
1814
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001815my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001816my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001817my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001818my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001819
1820sub annotate_reset {
1821 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001822 $av_pending = '_';
1823 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001824 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001825}
1826
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001827sub annotate_values {
1828 my ($stream, $type) = @_;
1829
1830 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001831 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001832 my $cur = $stream;
1833
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001834 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001835
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001836 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001837 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001838 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001839 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001840 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001841 print "WS($1)\n" if ($dbg_values > 1);
1842 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001843 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001844 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001845 }
1846
Florian Micklerc023e4732011-01-12 16:59:58 -08001847 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001848 print "CAST($1)\n" if ($dbg_values > 1);
1849 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001850 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001851
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001852 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001853 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001854 $type = 'T';
1855
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001856 } elsif ($cur =~ /^($Modifier)\s*/) {
1857 print "MODIFIER($1)\n" if ($dbg_values > 1);
1858 $type = 'T';
1859
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001860 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001861 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001862 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001863 push(@av_paren_type, $type);
1864 if ($2 ne '') {
1865 $av_pending = 'N';
1866 }
1867 $type = 'E';
1868
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001869 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001870 print "UNDEF($1)\n" if ($dbg_values > 1);
1871 $av_preprocessor = 1;
1872 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001873
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001874 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001875 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001876 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001877
1878 push(@av_paren_type, $type);
1879 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001880 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001881
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001882 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001883 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1884 $av_preprocessor = 1;
1885
1886 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1887
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001888 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001889
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001890 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001891 print "PRE_END($1)\n" if ($dbg_values > 1);
1892
1893 $av_preprocessor = 1;
1894
1895 # Assume all arms of the conditional end as this
1896 # one does, and continue as if the #endif was not here.
1897 pop(@av_paren_type);
1898 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001899 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001900
1901 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001902 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001903
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001904 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1905 print "ATTR($1)\n" if ($dbg_values > 1);
1906 $av_pending = $type;
1907 $type = 'N';
1908
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001909 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001910 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001911 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001912 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001913 }
1914 $type = 'N';
1915
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001916 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001917 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001918 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001919 $type = 'N';
1920
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001921 } elsif ($cur =~/^(case)/o) {
1922 print "CASE($1)\n" if ($dbg_values > 1);
1923 $av_pend_colon = 'C';
1924 $type = 'N';
1925
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001926 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001927 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001928 $type = 'N';
1929
1930 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001931 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001932 push(@av_paren_type, $av_pending);
1933 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001934 $type = 'N';
1935
1936 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001937 my $new_type = pop(@av_paren_type);
1938 if ($new_type ne '_') {
1939 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001940 print "PAREN('$1') -> $type\n"
1941 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001942 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001943 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001944 }
1945
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001946 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001947 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001948 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001949 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001950
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001951 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1952 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001953 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001954 } elsif ($type eq 'E') {
1955 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001956 }
1957 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1958 $type = 'V';
1959
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001960 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001961 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001962 $type = 'V';
1963
1964 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001965 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001966 $type = 'N';
1967
Andy Whitcroftcf655042008-03-04 14:28:20 -08001968 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001969 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001970 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001971 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001972
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001973 } elsif ($cur =~/^(,)/) {
1974 print "COMMA($1)\n" if ($dbg_values > 1);
1975 $type = 'C';
1976
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001977 } elsif ($cur =~ /^(\?)/o) {
1978 print "QUESTION($1)\n" if ($dbg_values > 1);
1979 $type = 'N';
1980
1981 } elsif ($cur =~ /^(:)/o) {
1982 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1983
1984 substr($var, length($res), 1, $av_pend_colon);
1985 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1986 $type = 'E';
1987 } else {
1988 $type = 'N';
1989 }
1990 $av_pend_colon = 'O';
1991
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001992 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001993 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001994 $type = 'N';
1995
Andy Whitcroft0d413862008-10-15 22:02:16 -07001996 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001997 my $variant;
1998
1999 print "OPV($1)\n" if ($dbg_values > 1);
2000 if ($type eq 'V') {
2001 $variant = 'B';
2002 } else {
2003 $variant = 'U';
2004 }
2005
2006 substr($var, length($res), 1, $variant);
2007 $type = 'N';
2008
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002009 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002010 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002011 if ($1 ne '++' && $1 ne '--') {
2012 $type = 'N';
2013 }
2014
2015 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002016 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002017 }
2018 if (defined $1) {
2019 $cur = substr($cur, length($1));
2020 $res .= $type x length($1);
2021 }
2022 }
2023
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002024 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002025}
2026
Andy Whitcroft8905a672007-11-28 16:21:06 -08002027sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002028 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002029 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002030 ^(?:
2031 $Modifier|
2032 $Storage|
2033 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002034 DEFINE_\S+
2035 )$|
2036 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002037 goto|
2038 return|
2039 case|
2040 else|
2041 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002042 do|
2043 \#|
2044 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002045 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002046 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002047 )}x;
2048 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2049 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002050 # Check for modifiers.
2051 $possible =~ s/\s*$Storage\s*//g;
2052 $possible =~ s/\s*$Sparse\s*//g;
2053 if ($possible =~ /^\s*$/) {
2054
2055 } elsif ($possible =~ /\s/) {
2056 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002057 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002058 if ($modifier !~ $notPermitted) {
2059 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002060 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002061 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002062 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002063
2064 } else {
2065 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002066 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002067 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002068 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002069 } else {
2070 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002071 }
2072}
2073
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002074my $prefix = '';
2075
Joe Perches000d1cc12011-07-25 17:13:25 -07002076sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002077 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002078
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002079 $type =~ tr/[a-z]/[A-Z]/;
2080
Joe Perchescbec18a2014-04-03 14:49:19 -07002081 return defined $use_type{$type} if (scalar keys %use_type > 0);
2082
2083 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002084}
2085
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002086sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002087 my ($level, $type, $msg) = @_;
2088
2089 if (!show_type($type) ||
2090 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002091 return 0;
2092 }
Joe Perches57230292015-06-25 15:03:03 -07002093 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002094 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002095 if ($level eq 'ERROR') {
2096 $output .= RED;
2097 } elsif ($level eq 'WARNING') {
2098 $output .= YELLOW;
2099 } else {
2100 $output .= GREEN;
2101 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002102 }
Joe Perches57230292015-06-25 15:03:03 -07002103 $output .= $prefix . $level . ':';
2104 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002105 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002106 $output .= "$type:";
2107 }
John Brooks737c0762017-07-10 15:52:24 -07002108 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002109 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002110
2111 if ($showfile) {
2112 my @lines = split("\n", $output, -1);
2113 splice(@lines, 1, 1);
2114 $output = join("\n", @lines);
2115 }
Joe Perches57230292015-06-25 15:03:03 -07002116 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002117
Joe Perches57230292015-06-25 15:03:03 -07002118 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002119
2120 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002121}
Joe Perchescbec18a2014-04-03 14:49:19 -07002122
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002123sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002124 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002125}
Joe Perches000d1cc12011-07-25 17:13:25 -07002126
Joe Perchesd752fcc2014-08-06 16:11:05 -07002127sub fixup_current_range {
2128 my ($lineRef, $offset, $length) = @_;
2129
2130 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2131 my $o = $1;
2132 my $l = $2;
2133 my $no = $o + $offset;
2134 my $nl = $l + $length;
2135 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2136 }
2137}
2138
2139sub fix_inserted_deleted_lines {
2140 my ($linesRef, $insertedRef, $deletedRef) = @_;
2141
2142 my $range_last_linenr = 0;
2143 my $delta_offset = 0;
2144
2145 my $old_linenr = 0;
2146 my $new_linenr = 0;
2147
2148 my $next_insert = 0;
2149 my $next_delete = 0;
2150
2151 my @lines = ();
2152
2153 my $inserted = @{$insertedRef}[$next_insert++];
2154 my $deleted = @{$deletedRef}[$next_delete++];
2155
2156 foreach my $old_line (@{$linesRef}) {
2157 my $save_line = 1;
2158 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002159 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002160 $delta_offset = 0;
2161 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2162 $range_last_linenr = $new_linenr;
2163 fixup_current_range(\$line, $delta_offset, 0);
2164 }
2165
2166 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2167 $deleted = @{$deletedRef}[$next_delete++];
2168 $save_line = 0;
2169 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2170 }
2171
2172 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2173 push(@lines, ${$inserted}{'LINE'});
2174 $inserted = @{$insertedRef}[$next_insert++];
2175 $new_linenr++;
2176 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2177 }
2178
2179 if ($save_line) {
2180 push(@lines, $line);
2181 $new_linenr++;
2182 }
2183
2184 $old_linenr++;
2185 }
2186
2187 return @lines;
2188}
2189
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002190sub fix_insert_line {
2191 my ($linenr, $line) = @_;
2192
2193 my $inserted = {
2194 LINENR => $linenr,
2195 LINE => $line,
2196 };
2197 push(@fixed_inserted, $inserted);
2198}
2199
2200sub fix_delete_line {
2201 my ($linenr, $line) = @_;
2202
2203 my $deleted = {
2204 LINENR => $linenr,
2205 LINE => $line,
2206 };
2207
2208 push(@fixed_deleted, $deleted);
2209}
2210
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002211sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002212 my ($type, $msg) = @_;
2213
2214 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002215 our $clean = 0;
2216 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002217 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002218 }
Joe Perches3705ce52013-07-03 15:05:31 -07002219 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002220}
2221sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002222 my ($type, $msg) = @_;
2223
2224 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002225 our $clean = 0;
2226 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002227 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002228 }
Joe Perches3705ce52013-07-03 15:05:31 -07002229 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002230}
2231sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002232 my ($type, $msg) = @_;
2233
2234 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002235 our $clean = 0;
2236 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002237 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002238 }
Joe Perches3705ce52013-07-03 15:05:31 -07002239 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002240}
2241
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002242sub check_absolute_file {
2243 my ($absolute, $herecurr) = @_;
2244 my $file = $absolute;
2245
2246 ##print "absolute<$absolute>\n";
2247
2248 # See if any suffix of this path is a path within the tree.
2249 while ($file =~ s@^[^/]*/@@) {
2250 if (-f "$root/$file") {
2251 ##print "file<$file>\n";
2252 last;
2253 }
2254 }
2255 if (! -f _) {
2256 return 0;
2257 }
2258
2259 # It is, so see if the prefix is acceptable.
2260 my $prefix = $absolute;
2261 substr($prefix, -length($file)) = '';
2262
2263 ##print "prefix<$prefix>\n";
2264 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002265 WARN("USE_RELATIVE_PATH",
2266 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002267 }
2268}
2269
Joe Perches3705ce52013-07-03 15:05:31 -07002270sub trim {
2271 my ($string) = @_;
2272
Joe Perchesb34c6482013-09-11 14:24:01 -07002273 $string =~ s/^\s+|\s+$//g;
2274
2275 return $string;
2276}
2277
2278sub ltrim {
2279 my ($string) = @_;
2280
2281 $string =~ s/^\s+//;
2282
2283 return $string;
2284}
2285
2286sub rtrim {
2287 my ($string) = @_;
2288
2289 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002290
2291 return $string;
2292}
2293
Joe Perches52ea8502013-11-12 15:10:09 -08002294sub string_find_replace {
2295 my ($string, $find, $replace) = @_;
2296
2297 $string =~ s/$find/$replace/g;
2298
2299 return $string;
2300}
2301
Joe Perches3705ce52013-07-03 15:05:31 -07002302sub tabify {
2303 my ($leading) = @_;
2304
Antonio Borneo713a09d2020-04-06 20:11:07 -07002305 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002306 my $max_spaces_before_tab = $source_indent - 1;
2307 my $spaces_to_tab = " " x $source_indent;
2308
2309 #convert leading spaces to tabs
2310 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2311 #Remove spaces before a tab
2312 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2313
2314 return "$leading";
2315}
2316
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002317sub pos_last_openparen {
2318 my ($line) = @_;
2319
2320 my $pos = 0;
2321
2322 my $opens = $line =~ tr/\(/\(/;
2323 my $closes = $line =~ tr/\)/\)/;
2324
2325 my $last_openparen = 0;
2326
2327 if (($opens == 0) || ($closes >= $opens)) {
2328 return -1;
2329 }
2330
2331 my $len = length($line);
2332
2333 for ($pos = 0; $pos < $len; $pos++) {
2334 my $string = substr($line, $pos);
2335 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2336 $pos += length($1) - 1;
2337 } elsif (substr($line, $pos, 1) eq '(') {
2338 $last_openparen = $pos;
2339 } elsif (index($string, '(') == -1) {
2340 last;
2341 }
2342 }
2343
Joe Perches91cb5192014-04-03 14:49:32 -07002344 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002345}
2346
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002347sub get_raw_comment {
2348 my ($line, $rawline) = @_;
2349 my $comment = '';
2350
2351 for my $i (0 .. (length($line) - 1)) {
2352 if (substr($line, $i, 1) eq "$;") {
2353 $comment .= substr($rawline, $i, 1);
2354 }
2355 }
2356
2357 return $comment;
2358}
2359
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002360sub process {
2361 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002362
2363 my $linenr=0;
2364 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002365 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002366 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002367 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002368
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002369 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002370 my $indent;
2371 my $previndent=0;
2372 my $stashindent=0;
2373
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002374 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002375 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002376 my $author = '';
2377 my $authorsignoff = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002378 my $author_sob = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002379 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002380 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002381 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002382 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002383 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002384 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002385 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002386 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002387 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002388 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002389 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002390 my $non_utf8_charset = 0;
2391
Joe Perches365dd4e2014-08-06 16:10:42 -07002392 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002393 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002394
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002395 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002396 our $cnt_lines = 0;
2397 our $cnt_error = 0;
2398 our $cnt_warn = 0;
2399 our $cnt_chk = 0;
2400
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002401 # Trace the real file/line as we go.
2402 my $realfile = '';
2403 my $realline = 0;
2404 my $realcnt = 0;
2405 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002406 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002407 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002408 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002409 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002410 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002411
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002412 my $prev_values = 'E';
2413
2414 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002415 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002416 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002417 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002418 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002419
Joe Perches7e51f192013-09-11 14:23:57 -07002420 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002421
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002422 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002423 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002424 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002425 my @setup_docs = ();
2426 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002427
Joe Perchesd8b07712013-11-12 15:10:06 -08002428 my $camelcase_file_seeded = 0;
2429
Rob Herring9f3a8992018-04-10 16:33:13 -07002430 my $checklicenseline = 1;
2431
Andy Whitcroft773647a2008-03-28 14:15:58 -07002432 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002433 my $line;
2434 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002435 $linenr++;
2436 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002437
Joe Perches3705ce52013-07-03 15:05:31 -07002438 push(@fixed, $rawline) if ($fix);
2439
Andy Whitcroft773647a2008-03-28 14:15:58 -07002440 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002441 $setup_docs = 0;
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07002442 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002443 $setup_docs = 1;
2444 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002445 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002446 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002447 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002448 $realline=$1-1;
2449 if (defined $2) {
2450 $realcnt=$3+1;
2451 } else {
2452 $realcnt=1+1;
2453 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002454 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002455
2456 # Guestimate if this is a continuing comment. Run
2457 # the context looking for a comment "edge". If this
2458 # edge is a close comment then we must be in a comment
2459 # at context start.
2460 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002461 my $cnt = $realcnt;
2462 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2463 next if (defined $rawlines[$ln - 1] &&
2464 $rawlines[$ln - 1] =~ /^-/);
2465 $cnt--;
2466 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002467 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002468 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2469 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2470 ($edge) = $1;
2471 last;
2472 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002473 }
2474 if (defined $edge && $edge eq '*/') {
2475 $in_comment = 1;
2476 }
2477
2478 # Guestimate if this is a continuing comment. If this
2479 # is the start of a diff block and this line starts
2480 # ' *' then it is very likely a comment.
2481 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002482 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002483 {
2484 $in_comment = 1;
2485 }
2486
2487 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2488 sanitise_line_reset($in_comment);
2489
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002490 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002491 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002492 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002493 $line = sanitise_line($rawline);
2494 }
2495 push(@lines, $line);
2496
2497 if ($realcnt > 1) {
2498 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2499 } else {
2500 $realcnt = 0;
2501 }
2502
2503 #print "==>$rawline\n";
2504 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002505
2506 if ($setup_docs && $line =~ /^\+/) {
2507 push(@setup_docs, $line);
2508 }
2509 }
2510
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002511 $prefix = '';
2512
Andy Whitcroft773647a2008-03-28 14:15:58 -07002513 $realcnt = 0;
2514 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002515 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002516 foreach my $line (@lines) {
2517 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002518 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002519 my $sline = $line; #copy of $line
2520 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002521
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002522 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002523 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002524
Joe Perches12c253a2018-06-07 17:10:58 -07002525# check if it's a mode change, rename or start of a patch
2526 if (!$in_commit_log &&
2527 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2528 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2529 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2530 $is_patch = 1;
2531 }
2532
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002533#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002534 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002535 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2536 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002537 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002538 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002539 $realline=$1-1;
2540 if (defined $2) {
2541 $realcnt=$3+1;
2542 } else {
2543 $realcnt=1+1;
2544 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002545 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002546 $prev_values = 'E';
2547
Andy Whitcroft773647a2008-03-28 14:15:58 -07002548 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002549 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002550 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002551 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002552 if ($context =~ /\b(\w+)\s*\(/) {
2553 $context_function = $1;
2554 } else {
2555 undef $context_function;
2556 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002557 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002558
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002559# track the line number as we move through the hunk, note that
2560# new versions of GNU diff omit the leading space on completely
2561# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002562 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002563 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002564 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002565
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002566 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002567 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002568
2569 # Track the previous line.
2570 ($prevline, $stashline) = ($stashline, $line);
2571 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002572 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2573
Andy Whitcroft773647a2008-03-28 14:15:58 -07002574 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002575
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002576 } elsif ($realcnt == 1) {
2577 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002578 }
2579
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002580 my $hunk_line = ($realcnt != 0);
2581
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002582 $here = "#$linenr: " if (!$file);
2583 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002584
Joe Perches2ac73b42014-06-04 16:12:05 -07002585 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002586 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002587 if ($line =~ /^diff --git.*?(\S+)$/) {
2588 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002589 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002590 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002591 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002592 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002593 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002594 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002595 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002596
2597 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002598 if (!$file && $tree && $p1_prefix ne '' &&
2599 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002600 WARN("PATCH_PREFIX",
2601 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002602 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002603
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002604 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002605 ERROR("MODIFIED_INCLUDE_ASM",
2606 "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 -07002607 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002608 $found_file = 1;
2609 }
2610
Joe Perches34d88152015-06-25 15:03:05 -07002611#make up the handle for any error we report on this line
2612 if ($showfile) {
2613 $prefix = "$realfile:$realline: "
2614 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002615 if ($file) {
2616 $prefix = "$filename:$realline: ";
2617 } else {
2618 $prefix = "$filename:$linenr: ";
2619 }
Joe Perches34d88152015-06-25 15:03:05 -07002620 }
2621
Joe Perches2ac73b42014-06-04 16:12:05 -07002622 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002623 if (is_maintained_obsolete($realfile)) {
2624 WARN("OBSOLETE",
2625 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2626 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002627 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002628 $check = 1;
2629 } else {
2630 $check = $check_orig;
2631 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002632 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002633
2634 if ($realfile !~ /^MAINTAINERS/) {
2635 my $last_binding_patch = $is_binding_patch;
2636
2637 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2638
2639 if (($last_binding_patch != -1) &&
2640 ($last_binding_patch ^ $is_binding_patch)) {
2641 WARN("DT_SPLIT_BINDING_PATCH",
Mauro Carvalho Chehab858e6842020-04-15 16:45:25 +02002642 "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 -07002643 }
2644 }
2645
Andy Whitcroft773647a2008-03-28 14:15:58 -07002646 next;
2647 }
2648
Randy Dunlap389834b2007-06-08 13:47:03 -07002649 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002650
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002651 my $hereline = "$here\n$rawline\n";
2652 my $herecurr = "$here\n$rawline\n";
2653 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002654
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002655 $cnt_lines++ if ($realcnt != 0);
2656
Joe Perches490b2922018-08-21 21:58:01 -07002657# Verify the existence of a commit log if appropriate
2658# 2 is used because a $signature is counted in $commit_log_lines
2659 if ($in_commit_log) {
2660 if ($line !~ /^\s*$/) {
2661 $commit_log_lines++; #could be a $signature
2662 }
2663 } elsif ($has_commit_log && $commit_log_lines < 2) {
2664 WARN("COMMIT_MESSAGE",
2665 "Missing commit description - Add an appropriate one\n");
2666 $commit_log_lines = 2; #warn only once
2667 }
2668
Joe Perchese518e9a2015-06-25 15:03:27 -07002669# Check if the commit log has what seems like a diff which can confuse patch
2670 if ($in_commit_log && !$commit_log_has_diff &&
Mrinal Pandey13e45412020-09-04 16:35:52 -07002671 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2672 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
Joe Perchese518e9a2015-06-25 15:03:27 -07002673 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2674 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2675 ERROR("DIFF_IN_COMMIT_MSG",
2676 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2677 $commit_log_has_diff = 1;
2678 }
2679
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002680# Check for incorrect file permissions
2681 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2682 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002683 if ($realfile !~ m@scripts/@ &&
2684 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002685 ERROR("EXECUTE_PERMISSIONS",
2686 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002687 }
2688 }
2689
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002690# Check the patch for a From:
2691 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2692 $author = $1;
Dwaipayan Raye7f929f2020-10-15 20:12:15 -07002693 my $curline = $linenr;
2694 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2695 $author .= $1;
2696 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002697 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2698 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002699 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002700 }
2701
Joe Perches20112472011-07-25 17:13:23 -07002702# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002703 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002704 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002705 $in_commit_log = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002706 if ($author ne '' && $authorsignoff != 1) {
2707 if (same_email_addresses($1, $author, 1)) {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002708 $authorsignoff = 1;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002709 } else {
2710 my $ctx = $1;
2711 my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
2712 my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
2713
2714 if ($email_address eq $author_address && $email_name eq $author_name) {
2715 $author_sob = $ctx;
2716 $authorsignoff = 2;
2717 } elsif ($email_address eq $author_address) {
2718 $author_sob = $ctx;
2719 $authorsignoff = 3;
2720 } elsif ($email_name eq $author_name) {
2721 $author_sob = $ctx;
2722 $authorsignoff = 4;
2723
2724 my $address1 = $email_address;
2725 my $address2 = $author_address;
2726
2727 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
2728 $address1 = "$1$2";
2729 }
2730 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
2731 $address2 = "$1$2";
2732 }
2733 if ($address1 eq $address2) {
2734 $authorsignoff = 5;
2735 }
2736 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002737 }
2738 }
Joe Perches20112472011-07-25 17:13:23 -07002739 }
2740
Joe Perches44d303e2020-04-06 20:11:10 -07002741# Check for patch separator
2742 if ($line =~ /^---$/) {
2743 $has_patch_separator = 1;
2744 $in_commit_log = 0;
2745 }
2746
Joe Perchese0d975b2014-12-10 15:51:49 -08002747# Check if MAINTAINERS is being updated. If so, there's probably no need to
2748# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2749 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2750 $reported_maintainer_file = 1;
2751 }
2752
Joe Perches20112472011-07-25 17:13:23 -07002753# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002754 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002755 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002756 my $space_before = $1;
2757 my $sign_off = $2;
2758 my $space_after = $3;
2759 my $email = $4;
2760 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2761
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002762 if ($sign_off !~ /$signature_tags/) {
2763 WARN("BAD_SIGN_OFF",
2764 "Non-standard signature: $sign_off\n" . $herecurr);
2765 }
Joe Perches20112472011-07-25 17:13:23 -07002766 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002767 if (WARN("BAD_SIGN_OFF",
2768 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2769 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002770 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002771 "$ucfirst_sign_off $email";
2772 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002773 }
Joe Perches20112472011-07-25 17:13:23 -07002774 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002775 if (WARN("BAD_SIGN_OFF",
2776 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2777 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002778 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002779 "$ucfirst_sign_off $email";
2780 }
2781
Joe Perches20112472011-07-25 17:13:23 -07002782 }
2783 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002784 if (WARN("BAD_SIGN_OFF",
2785 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2786 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002787 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002788 "$ucfirst_sign_off $email";
2789 }
Joe Perches20112472011-07-25 17:13:23 -07002790 }
2791
Joe Perchesdfa05c22020-04-06 20:10:48 -07002792 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002793 my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
Joe Perches20112472011-07-25 17:13:23 -07002794 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002795 ERROR("BAD_SIGN_OFF",
2796 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002797 } else {
2798 my $dequoted = $suggested_email;
2799 $dequoted =~ s/^"//;
2800 $dequoted =~ s/" </ </;
2801 # Don't force email to have quotes
2802 # Allow just an angle bracketed address
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002803 if (!same_email_addresses($email, $suggested_email, 0)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002804 WARN("BAD_SIGN_OFF",
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002805 "email address '$email' might be better as '$suggested_email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002806 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002807 }
Joe Perches7e51f192013-09-11 14:23:57 -07002808
2809# Check for duplicate signatures
2810 my $sig_nospace = $line;
2811 $sig_nospace =~ s/\s//g;
2812 $sig_nospace = lc($sig_nospace);
2813 if (defined $signatures{$sig_nospace}) {
2814 WARN("BAD_SIGN_OFF",
2815 "Duplicate signature\n" . $herecurr);
2816 } else {
2817 $signatures{$sig_nospace} = 1;
2818 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002819
2820# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2821 if ($sign_off =~ /^co-developed-by:$/i) {
2822 if ($email eq $author) {
2823 WARN("BAD_SIGN_OFF",
2824 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2825 }
2826 if (!defined $lines[$linenr]) {
2827 WARN("BAD_SIGN_OFF",
2828 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2829 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2830 WARN("BAD_SIGN_OFF",
2831 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2832 } elsif ($1 ne $email) {
2833 WARN("BAD_SIGN_OFF",
2834 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2835 }
2836 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002837 }
2838
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002839# Check email subject for common tools that don't need to be mentioned
2840 if ($in_header_lines &&
2841 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2842 WARN("EMAIL_SUBJECT",
2843 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2844 }
2845
Joe Perches44d303e2020-04-06 20:11:10 -07002846# Check for Gerrit Change-Ids not in any patch context
2847 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002848 ERROR("GERRIT_CHANGE_ID",
Joe Perches44d303e2020-04-06 20:11:10 -07002849 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002850 }
2851
Joe Perches369c8dd2015-11-06 16:31:34 -08002852# Check if the commit log is in a possible stack dump
2853 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2854 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2855 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2856 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07002857 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2858 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2859 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2860 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08002861 $commit_log_possible_stack_dump = 1;
2862 }
2863
Joe Perches2a076f42015-04-16 12:44:28 -07002864# Check for line lengths > 75 in commit log, warn once
2865 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002866 length($line) > 75 &&
2867 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2868 # file delta changes
2869 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2870 # filename then :
2871 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2872 # A Fixes: or Link: line
2873 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002874 WARN("COMMIT_LOG_LONG_LINE",
2875 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2876 $commit_log_long_line = 1;
2877 }
2878
Joe Perchesbf4daf12015-09-09 15:37:50 -07002879# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002880 if ($in_commit_log && $commit_log_possible_stack_dump &&
2881 $line =~ /^\s*$/) {
2882 $commit_log_possible_stack_dump = 0;
2883 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002884
Joe Perches0d7835f2015-02-13 14:38:35 -08002885# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002886 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07002887 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002888 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002889 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002890 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002891 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2892 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002893 my $init_char = "c";
2894 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002895 my $short = 1;
2896 my $long = 0;
2897 my $case = 1;
2898 my $space = 1;
2899 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002900 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002901 my $id = '0123456789ab';
2902 my $orig_desc = "commit description";
2903 my $description = "";
2904
Joe Perchesfe043ea2015-09-09 15:37:25 -07002905 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2906 $init_char = $1;
2907 $orig_commit = lc($2);
2908 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2909 $orig_commit = lc($1);
2910 }
2911
Joe Perches0d7835f2015-02-13 14:38:35 -08002912 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2913 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2914 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2915 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2916 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2917 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002918 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002919 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2920 defined $rawlines[$linenr] &&
2921 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2922 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002923 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002924 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2925 defined $rawlines[$linenr] &&
2926 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2927 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2928 $orig_desc = $1;
2929 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2930 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002931 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002932 }
2933
2934 ($id, $description) = git_commit_info($orig_commit,
2935 $id, $orig_desc);
2936
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002937 if (defined($id) &&
2938 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002939 ERROR("GIT_COMMIT_ID",
2940 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2941 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002942 }
2943
Joe Perches13f19372014-08-06 16:10:59 -07002944# Check for added, moved or deleted files
2945 if (!$reported_maintainer_file && !$in_commit_log &&
2946 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2947 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2948 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2949 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002950 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002951 $reported_maintainer_file = 1;
2952 WARN("FILE_PATH_CHANGES",
2953 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2954 }
2955
Rob Herringe400edb2019-09-12 15:18:20 +01002956# Check for adding new DT bindings not in schema format
2957 if (!$in_commit_log &&
2958 ($line =~ /^new file mode\s*\d+\s*$/) &&
2959 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2960 WARN("DT_SCHEMA_BINDING_PATCH",
2961 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2962 }
2963
Andy Whitcroft00df3442007-06-08 13:47:06 -07002964# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002965 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002966 ERROR("CORRUPTED_PATCH",
2967 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002968 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002969 }
2970
2971# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2972 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002973 $rawline !~ m/^$UTF8*$/) {
2974 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2975
2976 my $blank = copy_spacing($rawline);
2977 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2978 my $hereptr = "$hereline$ptr\n";
2979
Joe Perches34d99212011-07-25 17:13:26 -07002980 CHK("INVALID_UTF8",
2981 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002982 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002983
Joe Perches15662b32011-10-31 17:13:12 -07002984# Check if it's the start of a commit log
2985# (not a header line and we haven't seen the patch filename)
2986 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002987 !($rawline =~ /^\s+(?:\S|$)/ ||
2988 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002989 $in_header_lines = 0;
2990 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002991 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002992 }
2993
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002994# Check if there is UTF-8 in a commit log when a mail header has explicitly
2995# declined it, i.e defined some charset where it is missing.
2996 if ($in_header_lines &&
2997 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2998 $1 !~ /utf-8/i) {
2999 $non_utf8_charset = 1;
3000 }
3001
3002 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07003003 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003004 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07003005 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3006 }
3007
Joe Perchesd6430f72016-12-12 16:46:28 -08003008# Check for absolute kernel paths in commit message
3009 if ($tree && $in_commit_log) {
3010 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3011 my $file = $1;
3012
3013 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3014 check_absolute_file($1, $herecurr)) {
3015 #
3016 } else {
3017 check_absolute_file($file, $herecurr);
3018 }
3019 }
3020 }
3021
Kees Cook66b47b42014-10-13 15:51:57 -07003022# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07003023 if (defined($misspellings) &&
3024 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07003025 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07003026 my $typo = $1;
3027 my $typo_fix = $spelling_fix{lc($typo)};
3028 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3029 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07003030 my $msg_level = \&WARN;
3031 $msg_level = \&CHK if ($file);
3032 if (&{$msg_level}("TYPO_SPELLING",
3033 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07003034 $fix) {
3035 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3036 }
3037 }
3038 }
3039
Matteo Crocea8dd86b2019-09-25 16:46:38 -07003040# check for invalid commit id
3041 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3042 my $id;
3043 my $description;
3044 ($id, $description) = git_commit_info($2, undef, undef);
3045 if (!defined($id)) {
3046 WARN("UNKNOWN_COMMIT_ID",
3047 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3048 }
3049 }
3050
Joe Perches310cd062020-10-15 20:11:52 -07003051# check for repeated words separated by a single space
3052 if ($rawline =~ /^\+/ || $in_commit_log) {
3053 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3054
3055 my $first = $1;
3056 my $second = $2;
3057
3058 if ($first =~ /(?:struct|union|enum)/) {
3059 pos($rawline) += length($first) + length($second) + 1;
3060 next;
3061 }
3062
3063 next if ($first ne $second);
3064 next if ($first eq 'long');
3065
3066 if (WARN("REPEATED_WORD",
3067 "Possible repeated word: '$first'\n" . $herecurr) &&
3068 $fix) {
3069 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3070 }
3071 }
3072
3073 # if it's a repeated word on consecutive lines in a comment block
3074 if ($prevline =~ /$;+\s*$/ &&
3075 $prevrawline =~ /($word_pattern)\s*$/) {
3076 my $last_word = $1;
3077 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3078 if (WARN("REPEATED_WORD",
3079 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3080 $fix) {
3081 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3082 }
3083 }
3084 }
3085 }
3086
Andy Whitcroft306708542008-10-15 22:02:28 -07003087# ignore non-hunk lines and lines being removed
3088 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003089
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003090#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003091 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003092 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07003093 if (ERROR("DOS_LINE_ENDINGS",
3094 "DOS line endings\n" . $herevet) &&
3095 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003096 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003097 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003098 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3099 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003100 if (ERROR("TRAILING_WHITESPACE",
3101 "trailing whitespace\n" . $herevet) &&
3102 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003103 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003104 }
3105
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003106 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003107 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07003108
Josh Triplett4783f892013-11-12 15:10:12 -08003109# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003110 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003111 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003112 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3113 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003114 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003115 my $msg_level = \&ERROR;
3116 $msg_level = \&CHK if ($file);
3117 &{$msg_level}("FSF_MAILING_ADDRESS",
3118 "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 -08003119 }
3120
Andi Kleen33549572010-05-24 14:33:29 -07003121# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003122# Only applies when adding the entry originally, after that we do not have
3123# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003124 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003125 # 'choice' is usually the last thing on the line (though
3126 # Kconfig supports named choices), so use a word boundary
3127 # (\b) rather than a whitespace character (\s)
3128 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003129 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003130 my $cnt = $realcnt;
3131 my $ln = $linenr + 1;
3132 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003133 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003134 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003135 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003136 $f = $lines[$ln - 1];
3137 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3138 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003139
3140 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003141 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003142
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003143 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003144 $is_start = 1;
Masahiro Yamada22a4ac02020-06-17 12:02:20 +09003145 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003146 $length = -1;
3147 }
3148
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003149 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003150 $f =~ s/#.*//;
3151 $f =~ s/^\s+//;
3152 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003153
3154 # This only checks context lines in the patch
3155 # and so hopefully shouldn't trigger false
3156 # positives, even though some of these are
3157 # common words in help texts
3158 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3159 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003160 $is_end = 1;
3161 last;
3162 }
Andi Kleen33549572010-05-24 14:33:29 -07003163 $length++;
3164 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003165 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3166 WARN("CONFIG_DESCRIPTION",
3167 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3168 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003169 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003170 }
3171
Joe Perches7ccf41a2020-06-04 16:50:33 -07003172# check MAINTAINERS entries
3173 if ($realfile =~ /^MAINTAINERS$/) {
3174# check MAINTAINERS entries for the right form
3175 if ($rawline =~ /^\+[A-Z]:/ &&
3176 $rawline !~ /^\+[A-Z]:\t\S/) {
3177 if (WARN("MAINTAINERS_STYLE",
3178 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3179 $fix) {
3180 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3181 }
3182 }
3183# check MAINTAINERS entries for the right ordering too
3184 my $preferred_order = 'MRLSWQBCPTFXNK';
3185 if ($rawline =~ /^\+[A-Z]:/ &&
3186 $prevrawline =~ /^[\+ ][A-Z]:/) {
3187 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3188 my $cur = $1;
3189 my $curval = $2;
3190 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3191 my $prev = $1;
3192 my $prevval = $2;
3193 my $curindex = index($preferred_order, $cur);
3194 my $previndex = index($preferred_order, $prev);
3195 if ($curindex < 0) {
3196 WARN("MAINTAINERS_STYLE",
3197 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3198 } else {
3199 if ($previndex >= 0 && $curindex < $previndex) {
3200 WARN("MAINTAINERS_STYLE",
3201 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3202 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3203 ($prev eq 'X' && $cur eq 'X')) &&
3204 ($prevval cmp $curval) > 0) {
3205 WARN("MAINTAINERS_STYLE",
3206 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3207 }
3208 }
Joe Perches628f91a2017-07-10 15:52:07 -07003209 }
3210 }
3211
Christoph Jaeger327953e2015-02-13 14:38:29 -08003212# discourage the use of boolean for type definition attributes of Kconfig options
3213 if ($realfile =~ /Kconfig/ &&
3214 $line =~ /^\+\s*\bboolean\b/) {
3215 WARN("CONFIG_TYPE_BOOLEAN",
3216 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3217 }
3218
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003219 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3220 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3221 my $flag = $1;
3222 my $replacement = {
3223 'EXTRA_AFLAGS' => 'asflags-y',
3224 'EXTRA_CFLAGS' => 'ccflags-y',
3225 'EXTRA_CPPFLAGS' => 'cppflags-y',
3226 'EXTRA_LDFLAGS' => 'ldflags-y',
3227 };
3228
3229 WARN("DEPRECATED_VARIABLE",
3230 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3231 }
3232
Rob Herringbff5da42014-01-23 15:54:51 -08003233# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003234 if (defined $root &&
3235 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3236 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3237
Rob Herringbff5da42014-01-23 15:54:51 -08003238 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3239
Florian Vaussardcc933192014-04-03 14:49:27 -07003240 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003241 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003242
Rob Herringbff5da42014-01-23 15:54:51 -08003243 foreach my $compat (@compats) {
3244 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003245 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3246 my $compat3 = $compat;
3247 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3248 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003249 if ( $? >> 8 ) {
3250 WARN("UNDOCUMENTED_DT_STRING",
3251 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3252 }
3253
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003254 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3255 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003256 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003257 if ( $? >> 8 ) {
3258 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003259 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003260 }
3261 }
3262 }
3263
Rob Herring9f3a8992018-04-10 16:33:13 -07003264# check for using SPDX license tag at beginning of files
3265 if ($realline == $checklicenseline) {
3266 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3267 $checklicenseline = 2;
3268 } elsif ($rawline =~ /^\+/) {
3269 my $comment = "";
3270 if ($realfile =~ /\.(h|s|S)$/) {
3271 $comment = '/*';
3272 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3273 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003274 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003275 $comment = '#';
3276 } elsif ($realfile =~ /\.rst$/) {
3277 $comment = '..';
3278 }
3279
Joe Perchesfdf13692019-03-07 16:28:32 -08003280# check SPDX comment style for .[chsS] files
3281 if ($realfile =~ /\.[chsS]$/ &&
3282 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003283 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003284 WARN("SPDX_LICENSE_TAG",
3285 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3286 }
3287
Rob Herring9f3a8992018-04-10 16:33:13 -07003288 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003289 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3290 WARN("SPDX_LICENSE_TAG",
3291 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003292 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003293 my $spdx_license = $1;
3294 if (!is_SPDX_License_valid($spdx_license)) {
3295 WARN("SPDX_LICENSE_TAG",
3296 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3297 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003298 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3299 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3300 my $msg_level = \&WARN;
3301 $msg_level = \&CHK if ($file);
3302 if (&{$msg_level}("SPDX_LICENSE_TAG",
3303
3304 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3305 $fix) {
3306 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3307 }
3308 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003309 }
3310 }
3311 }
3312
Joe Perchesa0154cd2020-10-15 20:12:19 -07003313# check for embedded filenames
3314 if ($rawline =~ /^\+.*\Q$realfile\E/) {
3315 WARN("EMBEDDED_FILENAME",
3316 "It's generally not useful to have the filename in the file\n" . $herecurr);
3317 }
3318
Andy Whitcroft5368df22008-10-15 22:02:27 -07003319# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003320 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07003321
Joe Perchesa8da38a2019-03-07 16:28:42 -08003322# check for using SPDX-License-Identifier on the wrong line number
3323 if ($realline != $checklicenseline &&
3324 $rawline =~ /\bSPDX-License-Identifier:/ &&
3325 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3326 WARN("SPDX_LICENSE_TAG",
3327 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3328 }
3329
Joe Perches47e0c882015-06-25 15:02:57 -07003330# line length limit (with some exclusions)
3331#
3332# There are a few types of lines that may extend beyond $max_line_length:
3333# logging functions like pr_info that end in a string
3334# lines with a single string
3335# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003336# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003337#
3338# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003339# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003340# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3341# LONG_LINE all other lines longer than $max_line_length
3342#
3343# if LONG_LINE is ignored, the other 2 types are also ignored
3344#
3345
Joe Perchesb4749e92015-07-17 16:24:01 -07003346 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003347 my $msg_type = "LONG_LINE";
3348
3349 # Check the allowed long line types first
3350
3351 # logging functions that end in a string that starts
3352 # before $max_line_length
3353 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3354 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3355 $msg_type = "";
3356
3357 # lines with only strings (w/ possible termination)
3358 # #defines with only strings
3359 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3360 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3361 $msg_type = "";
3362
Joe Perchescc147502017-11-17 15:28:44 -08003363 # More special cases
3364 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3365 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003366 $msg_type = "";
3367
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003368 # URL ($rawline is used in case the URL is in a comment)
3369 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3370 $msg_type = "";
3371
Joe Perches47e0c882015-06-25 15:02:57 -07003372 # Otherwise set the alternate message types
3373
3374 # a comment starts before $max_line_length
3375 } elsif ($line =~ /($;[\s$;]*)$/ &&
3376 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3377 $msg_type = "LONG_LINE_COMMENT"
3378
3379 # a quoted string starts before $max_line_length
3380 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3381 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3382 $msg_type = "LONG_LINE_STRING"
3383 }
3384
3385 if ($msg_type ne "" &&
3386 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003387 my $msg_level = \&WARN;
3388 $msg_level = \&CHK if ($file);
3389 &{$msg_level}($msg_type,
3390 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003391 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003392 }
3393
Andy Whitcroft8905a672007-11-28 16:21:06 -08003394# check for adding lines without a newline.
3395 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003396 WARN("MISSING_EOF_NEWLINE",
3397 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003398 }
3399
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003400# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003401 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003402
3403# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003404# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003405 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3406 $rawline =~ /^\+\s* \s*/) {
3407 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003408 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003409 if (ERROR("CODE_INDENT",
3410 "code indent should use tabs where possible\n" . $herevet) &&
3411 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003412 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003413 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003414 }
3415
Alberto Panizzo08e44362010-03-05 13:43:54 -08003416# check for space before tabs.
3417 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3418 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003419 if (WARN("SPACE_BEFORE_TAB",
3420 "please, no space before tabs\n" . $herevet) &&
3421 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003422 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003423 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003424 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003425 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003426 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003427 }
3428
Joe Perches6a487212018-04-10 16:34:04 -07003429# check for assignments on the start of a line
3430 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3431 CHK("ASSIGNMENT_CONTINUATIONS",
3432 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3433 }
3434
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003435# check for && or || at the start of a line
3436 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3437 CHK("LOGICAL_CONTINUATIONS",
3438 "Logical continuations should be on the previous line\n" . $hereprev);
3439 }
3440
Joe Perchesa91e8992016-05-20 17:04:05 -07003441# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003442 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003443 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003444 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003445 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003446 if (WARN("TABSTOP",
3447 "Statements should start on a tabstop\n" . $herecurr) &&
3448 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003449 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003450 }
3451 }
3452 }
3453
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003454# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003455 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003456 $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 -07003457 $prevline =~ /^\+(\t*)(.*)$/;
3458 my $oldindent = $1;
3459 my $rest = $2;
3460
3461 my $pos = pos_last_openparen($rest);
3462 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003463 $line =~ /^(\+| )([ \t]*)/;
3464 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003465
3466 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003467 "\t" x ($pos / $tabsize) .
3468 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003469 my $goodspaceindent = $oldindent . " " x $pos;
3470
3471 if ($newindent ne $goodtabindent &&
3472 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003473
3474 if (CHK("PARENTHESIS_ALIGNMENT",
3475 "Alignment should match open parenthesis\n" . $hereprev) &&
3476 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003477 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003478 s/^\+[ \t]*/\+$goodtabindent/;
3479 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003480 }
3481 }
3482 }
3483
Joe Perches6ab3a972015-04-16 12:44:05 -07003484# check for space after cast like "(int) foo" or "(struct foo) bar"
3485# avoid checking a few false positives:
3486# "sizeof(<type>)" or "__alignof__(<type>)"
3487# function pointer declarations like "(*foo)(int) = bar;"
3488# structure definitions like "(struct foo) { 0 };"
3489# multiline macros that define functions
3490# known attributes or the __attribute__ keyword
3491 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3492 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003493 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003494 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003495 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003496 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003497 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003498 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003499 }
3500
Joe Perches86406b12015-09-09 15:37:41 -07003501# Block comment styles
3502# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003503 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003504 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003505 $rawline =~ /^\+[ \t]*\*/ &&
Łukasz Stelmachc70735c2020-10-15 20:12:25 -07003506 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
Joe Perches05880602012-10-04 17:13:35 -07003507 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3508 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3509 }
3510
Joe Perches86406b12015-09-09 15:37:41 -07003511# Block comments use * on subsequent lines
3512 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3513 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003514 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003515 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003516 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003517 WARN("BLOCK_COMMENT_STYLE",
3518 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003519 }
3520
Joe Perches86406b12015-09-09 15:37:41 -07003521# Block comments use */ on trailing lines
3522 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003523 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3524 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3525 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003526 WARN("BLOCK_COMMENT_STYLE",
3527 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003528 }
3529
Joe Perches08eb9b82016-10-11 13:51:50 -07003530# Block comment * alignment
3531 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003532 $line =~ /^\+[ \t]*$;/ && #leading comment
3533 $rawline =~ /^\+[ \t]*\*/ && #leading *
3534 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003535 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003536 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3537 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003538 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003539 if (defined($1)) {
3540 $oldindent = expand_tabs($1);
3541 } else {
3542 $prevrawline =~ m@^\+(.*/?)\*@;
3543 $oldindent = expand_tabs($1);
3544 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003545 $rawline =~ m@^\+([ \t]*)\*@;
3546 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003547 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003548 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003549 WARN("BLOCK_COMMENT_STYLE",
3550 "Block comments should align the * on each line\n" . $hereprev);
3551 }
3552 }
3553
Joe Perches7f619192014-08-06 16:10:39 -07003554# check for missing blank lines after struct/union declarations
3555# with exceptions for various attributes and macros
3556 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3557 $line =~ /^\+/ &&
3558 !($line =~ /^\+\s*$/ ||
3559 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3560 $line =~ /^\+\s*MODULE_/i ||
3561 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3562 $line =~ /^\+[a-z_]*init/ ||
3563 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3564 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003565 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003566 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003567 if (CHK("LINE_SPACING",
3568 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3569 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003570 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003571 }
Joe Perches7f619192014-08-06 16:10:39 -07003572 }
3573
Joe Perches365dd4e2014-08-06 16:10:42 -07003574# check for multiple consecutive blank lines
3575 if ($prevline =~ /^[\+ ]\s*$/ &&
3576 $line =~ /^\+\s*$/ &&
3577 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003578 if (CHK("LINE_SPACING",
3579 "Please don't use multiple blank lines\n" . $hereprev) &&
3580 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003581 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003582 }
3583
Joe Perches365dd4e2014-08-06 16:10:42 -07003584 $last_blank_line = $linenr;
3585 }
3586
Joe Perches3b617e32014-04-03 14:49:28 -07003587# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003588 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3589 # actual declarations
3590 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003591 # function pointer declarations
3592 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003593 # foo bar; where foo is some local typedef or #define
3594 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3595 # known declaration macros
3596 $prevline =~ /^\+\s+$declaration_macros/) &&
3597 # for "else if" which can look like "$Ident $Ident"
3598 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3599 # other possible extensions of declaration lines
3600 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3601 # not starting a section or a macro "\" extended line
3602 $prevline =~ /(?:\{\s*|\\)$/) &&
3603 # looks like a declaration
3604 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003605 # function pointer declarations
3606 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003607 # foo bar; where foo is some local typedef or #define
3608 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3609 # known declaration macros
3610 $sline =~ /^\+\s+$declaration_macros/ ||
3611 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003612 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003613 # start or end of block or continuation of declaration
3614 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3615 # bitfield continuation
3616 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3617 # other possible extensions of declaration lines
3618 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3619 # indentation of previous and current line are the same
3620 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003621 if (WARN("LINE_SPACING",
3622 "Missing a blank line after declarations\n" . $hereprev) &&
3623 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003624 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003625 }
Joe Perches3b617e32014-04-03 14:49:28 -07003626 }
3627
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003628# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003629# Exceptions:
3630# 1) within comments
3631# 2) indented preprocessor commands
3632# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003633 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003634 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003635 if (WARN("LEADING_SPACE",
3636 "please, no spaces at the start of a line\n" . $herevet) &&
3637 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003638 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003639 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003640 }
3641
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003642# check we are in a valid C source file if not then ignore this hunk
3643 next if ($realfile !~ /\.(h|c)$/);
3644
Joe Perches5751a242017-11-17 15:28:52 -08003645# check for unusual line ending [ or (
3646 if ($line =~ /^\+.*([\[\(])\s*$/) {
3647 CHK("OPEN_ENDED_LINE",
3648 "Lines should not end with a '$1'\n" . $herecurr);
3649 }
3650
Joe Perches4dbed762017-05-08 15:55:39 -07003651# check if this appears to be the start function declaration, save the name
3652 if ($sline =~ /^\+\{\s*$/ &&
3653 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3654 $context_function = $1;
3655 }
3656
3657# check if this appears to be the end of function declaration
3658 if ($sline =~ /^\+\}\s*$/) {
3659 undef $context_function;
3660 }
3661
Joe Perches032a4c02014-08-06 16:10:29 -07003662# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003663# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003664# if the previous line is a break or return and is indented 1 tab more...
3665 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3666 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003667 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3668 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3669 defined $lines[$linenr] &&
3670 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003671 WARN("UNNECESSARY_ELSE",
3672 "else is not generally useful after a break or return\n" . $hereprev);
3673 }
3674 }
3675
Joe Perchesc00df192014-08-06 16:11:01 -07003676# check indentation of a line with a break;
3677# if the previous line is a goto or return and is indented the same # of tabs
3678 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3679 my $tabs = $1;
3680 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3681 WARN("UNNECESSARY_BREAK",
3682 "break is not useful after a goto or return\n" . $hereprev);
3683 }
3684 }
3685
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003686# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003687 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003688 WARN("CVS_KEYWORD",
3689 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003690 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003691
Joe Perches56e77d72013-02-21 16:44:14 -08003692# check for old HOTPLUG __dev<foo> section markings
3693 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3694 WARN("HOTPLUG_SECTION",
3695 "Using $1 is unnecessary\n" . $herecurr);
3696 }
3697
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003698# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003699 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3700 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003701#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003702 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003703 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003704 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003705 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003706 $stat =~ s/\n./\n /g;
3707 $cond =~ s/\n./\n /g;
3708
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003709#print "linenr<$linenr> <$stat>\n";
3710 # If this statement has no statement boundaries within
3711 # it there is no point in retrying a statement scan
3712 # until we hit end of it.
3713 my $frag = $stat; $frag =~ s/;+\s*$//;
3714 if ($frag !~ /(?:{|;)/) {
3715#print "skip<$line_nr_next>\n";
3716 $suppress_statement = $line_nr_next;
3717 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003718
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003719 # Find the real next line.
3720 $realline_next = $line_nr_next;
3721 if (defined $realline_next &&
3722 (!defined $lines[$realline_next - 1] ||
3723 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3724 $realline_next++;
3725 }
3726
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003727 my $s = $stat;
3728 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003729
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003730 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003731 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003732
3733 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003734 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003735
Andy Whitcroft463f2862009-09-21 17:04:34 -07003736 } elsif ($s =~ /^.\s*else\b/s) {
3737
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003738 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003739 } 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 -07003740 my $type = $1;
3741 $type =~ s/\s+/ /g;
3742 possible($type, "A:" . $s);
3743
Andy Whitcroft8905a672007-11-28 16:21:06 -08003744 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003745 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003746 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003747 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003748
3749 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003750 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003751 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003752 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003753
3754 # Check for any sort of function declaration.
3755 # int foo(something bar, other baz);
3756 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003757 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 -08003758 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003759
Andy Whitcroftcf655042008-03-04 14:28:20 -08003760 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003761 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003762 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003763
Andy Whitcroft8905a672007-11-28 16:21:06 -08003764 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003765 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003766
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003767 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003768 }
3769 }
3770 }
3771
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003772 }
3773
Andy Whitcroft653d4872007-06-23 17:16:34 -07003774#
3775# Checks which may be anchored in the context.
3776#
3777
3778# Check for switch () and associated case and default
3779# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003780 if ($line=~/\bswitch\s*\(.*\)/) {
3781 my $err = '';
3782 my $sep = '';
3783 my @ctx = ctx_block_outer($linenr, $realcnt);
3784 shift(@ctx);
3785 for my $ctx (@ctx) {
3786 my ($clen, $cindent) = line_stats($ctx);
3787 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3788 $indent != $cindent) {
3789 $err .= "$sep$ctx\n";
3790 $sep = '';
3791 } else {
3792 $sep = "[...]\n";
3793 }
3794 }
3795 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003796 ERROR("SWITCH_CASE_INDENT_LEVEL",
3797 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003798 }
3799 }
3800
3801# if/while/etc brace do not go on next line, unless defining a do while loop,
3802# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003803 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 -07003804 my $pre_ctx = "$1$2";
3805
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003806 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003807
3808 if ($line =~ /^\+\t{6,}/) {
3809 WARN("DEEP_INDENTATION",
3810 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3811 }
3812
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003813 my $ctx_cnt = $realcnt - $#ctx - 1;
3814 my $ctx = join("\n", @ctx);
3815
Andy Whitcroft548596d2008-07-23 21:29:01 -07003816 my $ctx_ln = $linenr;
3817 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003818
Andy Whitcroft548596d2008-07-23 21:29:01 -07003819 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3820 defined $lines[$ctx_ln - 1] &&
3821 $lines[$ctx_ln - 1] =~ /^-/)) {
3822 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3823 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003824 $ctx_ln++;
3825 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003826
Andy Whitcroft53210162008-07-23 21:29:03 -07003827 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3828 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003829
Joe Perchesd752fcc2014-08-06 16:11:05 -07003830 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003831 ERROR("OPEN_BRACE",
3832 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003833 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003834 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003835 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3836 $ctx =~ /\)\s*\;\s*$/ &&
3837 defined $lines[$ctx_ln - 1])
3838 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003839 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3840 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003841 WARN("TRAILING_SEMICOLON",
3842 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003843 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003844 }
3845 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003846 }
3847
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003848# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003849 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 -08003850 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3851 ctx_statement_block($linenr, $realcnt, 0)
3852 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003853 my ($s, $c) = ($stat, $cond);
3854
3855 substr($s, 0, length($c), '');
3856
Joe Perches9f5af482015-09-09 15:37:30 -07003857 # remove inline comments
3858 $s =~ s/$;/ /g;
3859 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003860
3861 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003862 my @newlines = ($c =~ /\n/gs);
3863 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003864
Joe Perches9f5af482015-09-09 15:37:30 -07003865 # Make sure we remove the line prefixes as we have
3866 # none on the first line, and are going to readd them
3867 # where necessary.
3868 $s =~ s/\n./\n/gs;
3869 while ($s =~ /\n\s+\\\n/) {
3870 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3871 }
3872
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003873 # We want to check the first line inside the block
3874 # starting at the end of the conditional, so remove:
3875 # 1) any blank line termination
3876 # 2) any opening brace { on end of the line
3877 # 3) any do (...) {
3878 my $continuation = 0;
3879 my $check = 0;
3880 $s =~ s/^.*\bdo\b//;
3881 $s =~ s/^\s*{//;
3882 if ($s =~ s/^\s*\\//) {
3883 $continuation = 1;
3884 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003885 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003886 $check = 1;
3887 $cond_lines++;
3888 }
3889
3890 # Also ignore a loop construct at the end of a
3891 # preprocessor statement.
3892 if (($prevline =~ /^.\s*#\s*define\s/ ||
3893 $prevline =~ /\\\s*$/) && $continuation == 0) {
3894 $check = 0;
3895 }
3896
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003897 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003898 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003899 while ($cond_ptr != $cond_lines) {
3900 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003901
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003902 # If we see an #else/#elif then the code
3903 # is not linear.
3904 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3905 $check = 0;
3906 }
3907
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003908 # Ignore:
3909 # 1) blank lines, they should be at 0,
3910 # 2) preprocessor lines, and
3911 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003912 if ($continuation ||
3913 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003914 $s =~ /^\s*#\s*?/ ||
3915 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003916 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003917 if ($s =~ s/^.*?\n//) {
3918 $cond_lines++;
3919 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003920 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003921 }
3922
3923 my (undef, $sindent) = line_stats("+" . $s);
3924 my $stat_real = raw_line($linenr, $cond_lines);
3925
3926 # Check if either of these lines are modified, else
3927 # this is not this patch's fault.
3928 if (!defined($stat_real) ||
3929 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3930 $check = 0;
3931 }
3932 if (defined($stat_real) && $cond_lines > 1) {
3933 $stat_real = "[...]\n$stat_real";
3934 }
3935
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003936 #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 -07003937
Joe Perches9f5af482015-09-09 15:37:30 -07003938 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07003939 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07003940 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003941 ($sindent == $indent &&
3942 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07003943 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003944 WARN("SUSPECT_CODE_INDENT",
3945 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003946 }
3947 }
3948
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003949 # Track the 'values' across context and added lines.
3950 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003951 my ($curr_values, $curr_vars) =
3952 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003953 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003954 if ($dbg_values) {
3955 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003956 print "$linenr > .$outline\n";
3957 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003958 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003959 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003960 $prev_values = substr($curr_values, -1);
3961
Andy Whitcroft00df3442007-06-08 13:47:06 -07003962#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003963 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003964
Joe Perches99ca38c2020-10-15 20:12:09 -07003965# check for self assignments used to avoid compiler warnings
3966# e.g.: int foo = foo, *bar = NULL;
3967# struct foo bar = *(&(bar));
3968 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
3969 my $var = $1;
3970 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
3971 WARN("SELF_ASSIGNMENT",
3972 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
3973 }
3974 }
3975
Joe Perches11ca40a2016-12-12 16:46:31 -08003976# check for dereferences that span multiple lines
3977 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3978 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3979 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3980 my $ref = $1;
3981 $line =~ /^.\s*($Lval)/;
3982 $ref .= $1;
3983 $ref =~ s/\s//g;
3984 WARN("MULTILINE_DEREFERENCE",
3985 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3986 }
3987
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003988# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003989 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 -07003990 my $type = $1;
3991 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003992 $var = "" if (!defined $var);
3993 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003994 my $sign = $1;
3995 my $pointer = $2;
3996
3997 $pointer = "" if (!defined $pointer);
3998
3999 if (WARN("UNSPECIFIED_INT",
4000 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4001 $fix) {
4002 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07004003 my $comp_pointer = $pointer;
4004 $comp_pointer =~ s/\s//g;
4005 $decl .= $comp_pointer;
4006 $decl = rtrim($decl) if ($var eq "");
4007 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004008 }
4009 }
4010 }
4011
Andy Whitcroft653d4872007-06-23 17:16:34 -07004012# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07004013 if ($dbg_type) {
4014 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004015 ERROR("TEST_TYPE",
4016 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004017 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004018 ERROR("TEST_NOT_TYPE",
4019 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004020 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004021 next;
4022 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004023# TEST: allow direct testing of the attribute matcher.
4024 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004025 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004026 ERROR("TEST_ATTR",
4027 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004028 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004029 ERROR("TEST_NOT_ATTR",
4030 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004031 }
4032 next;
4033 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004034
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004035# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07004036 if ($line =~ /^.\s*{/ &&
4037 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07004038 if (ERROR("OPEN_BRACE",
4039 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004040 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4041 fix_delete_line($fixlinenr - 1, $prevrawline);
4042 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004043 my $fixedline = $prevrawline;
4044 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004045 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004046 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004047 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004048 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004049 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004050 }
4051
Andy Whitcroft653d4872007-06-23 17:16:34 -07004052#
4053# Checks which are anchored on the added line.
4054#
4055
4056# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004057 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07004058 my $path = $1;
4059 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004060 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08004061 "malformed #include filename\n" . $herecurr);
4062 }
4063 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4064 ERROR("UAPI_INCLUDE",
4065 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004066 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004067 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004068
4069# no C99 // comments
4070 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07004071 if (ERROR("C99_COMMENTS",
4072 "do not use C99 // comments\n" . $herecurr) &&
4073 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004074 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07004075 if ($line =~ /\/\/(.*)$/) {
4076 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07004077 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07004078 }
4079 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004080 }
4081 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004082 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004083 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004084
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004085# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4086# the whole statement.
4087#print "APW <$lines[$realline_next - 1]>\n";
4088 if (defined $realline_next &&
4089 exists $lines[$realline_next - 1] &&
4090 !defined $suppress_export{$realline_next} &&
4091 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4092 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004093 # Handle definitions which produce identifiers with
4094 # a prefix:
4095 # XXX(foo);
4096 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004097 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08004098 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004099 $name =~ /^${Ident}_$2/) {
4100#print "FOO C name<$name>\n";
4101 $suppress_export{$realline_next} = 1;
4102
4103 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004104 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07004105 ^.DEFINE_$Ident\(\Q$name\E\)|
4106 ^.DECLARE_$Ident\(\Q$name\E\)|
4107 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004108 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4109 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07004110 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004111#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4112 $suppress_export{$realline_next} = 2;
4113 } else {
4114 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004115 }
4116 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004117 if (!defined $suppress_export{$linenr} &&
4118 $prevline =~ /^.\s*$/ &&
4119 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4120 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4121#print "FOO B <$lines[$linenr - 1]>\n";
4122 $suppress_export{$linenr} = 2;
4123 }
4124 if (defined $suppress_export{$linenr} &&
4125 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004126 WARN("EXPORT_SYMBOL",
4127 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004128 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004129
Joe Eloff5150bda2010-08-09 17:21:00 -07004130# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004131 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004132 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004133 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004134 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004135 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004136 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004137 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004138# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004139 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004140 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004141 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004142 $herecurr) &&
4143 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004144 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004145 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004146 }
4147
Joe Perches18130872014-08-06 16:11:22 -07004148# check for misordered declarations of char/short/int/long with signed/unsigned
4149 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4150 my $tmp = trim($1);
4151 WARN("MISORDERED_TYPE",
4152 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4153 }
4154
Joe Perches809e0822018-08-21 21:58:12 -07004155# check for unnecessary <signed> int declarations of short/long/long long
4156 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4157 my $type = trim($1);
4158 next if ($type !~ /\bint\b/);
4159 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4160 my $new_type = $type;
4161 $new_type =~ s/\b\s*int\s*\b/ /;
4162 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4163 $new_type =~ s/^const\s+//;
4164 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4165 $new_type = "const $new_type" if ($type =~ /^const\b/);
4166 $new_type =~ s/\s+/ /g;
4167 $new_type = trim($new_type);
4168 if (WARN("UNNECESSARY_INT",
4169 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4170 $fix) {
4171 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4172 }
4173 }
4174
Joe Perchescb710ec2010-10-26 14:23:20 -07004175# check for static const char * arrays.
4176 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004177 WARN("STATIC_CONST_CHAR_ARRAY",
4178 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004179 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004180 }
4181
4182# check for initialized const char arrays that should be static const
4183 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4184 if (WARN("STATIC_CONST_CHAR_ARRAY",
4185 "const array should probably be static const\n" . $herecurr) &&
4186 $fix) {
4187 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4188 }
4189 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004190
4191# check for static char foo[] = "bar" declarations.
4192 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004193 WARN("STATIC_CONST_CHAR_ARRAY",
4194 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004195 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004196 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004197
Joe Perchesab7e23f2015-04-16 12:44:22 -07004198# check for const <foo> const where <foo> is not a pointer or array type
4199 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4200 my $found = $1;
4201 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4202 WARN("CONST_CONST",
4203 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4204 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4205 WARN("CONST_CONST",
4206 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4207 }
4208 }
4209
Joe Perches9b0fa602014-04-03 14:49:18 -07004210# check for non-global char *foo[] = {"bar", ...} declarations.
4211 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4212 WARN("STATIC_CONST_CHAR_ARRAY",
4213 "char * array declaration might be better as static const\n" .
4214 $herecurr);
4215 }
4216
Joe Perchesb598b672015-04-16 12:44:36 -07004217# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4218 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4219 my $array = $1;
4220 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4221 my $array_div = $1;
4222 if (WARN("ARRAY_SIZE",
4223 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4224 $fix) {
4225 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4226 }
4227 }
4228 }
4229
Joe Perchesb36190c2014-01-27 17:07:18 -08004230# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004231 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004232 if (ERROR("FUNCTION_WITHOUT_ARGS",
4233 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4234 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004235 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004236 }
4237 }
4238
Andy Whitcroft653d4872007-06-23 17:16:34 -07004239# check for new typedefs, only function parameters and sparse annotations
4240# make sense.
4241 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004242 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004243 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004244 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004245 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004246 WARN("NEW_TYPEDEFS",
4247 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004248 }
4249
4250# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004251 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004252 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4253 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004254 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004255
Andy Whitcroft65863862009-01-06 14:41:21 -08004256 # Should start with a space.
4257 $to =~ s/^(\S)/ $1/;
4258 # Should not end with a space.
4259 $to =~ s/\s+$//;
4260 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004261 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004262 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004263
Joe Perches3705ce52013-07-03 15:05:31 -07004264## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004265 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004266 if (ERROR("POINTER_LOCATION",
4267 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4268 $fix) {
4269 my $sub_from = $ident;
4270 my $sub_to = $ident;
4271 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004272 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004273 s@\Q$sub_from\E@$sub_to@;
4274 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004275 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004276 }
4277 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4278 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004279 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004280
Andy Whitcroft65863862009-01-06 14:41:21 -08004281 # Should start with a space.
4282 $to =~ s/^(\S)/ $1/;
4283 # Should not end with a space.
4284 $to =~ s/\s+$//;
4285 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004286 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004287 }
4288 # Modifiers should have spaces.
4289 $to =~ s/(\b$Modifier$)/$1 /;
4290
Joe Perches3705ce52013-07-03 15:05:31 -07004291## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004292 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004293 if (ERROR("POINTER_LOCATION",
4294 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4295 $fix) {
4296
4297 my $sub_from = $match;
4298 my $sub_to = $match;
4299 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004300 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004301 s@\Q$sub_from\E@$sub_to@;
4302 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004303 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004304 }
4305
Joe Perches9d3e3c72015-09-09 15:37:27 -07004306# avoid BUG() or BUG_ON()
4307 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004308 my $msg_level = \&WARN;
4309 $msg_level = \&CHK if ($file);
4310 &{$msg_level}("AVOID_BUG",
4311 "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 -07004312 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004313
Joe Perches9d3e3c72015-09-09 15:37:27 -07004314# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004315 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004316 WARN("LINUX_VERSION_CODE",
4317 "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 -08004318 }
4319
Joe Perches17441222011-06-15 15:08:17 -07004320# check for uses of printk_ratelimit
4321 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004322 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004323 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004324 }
4325
Joe Percheseeef5732017-11-17 15:28:41 -08004326# printk should use KERN_* levels
4327 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4328 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4329 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004330 }
4331
Joe Perches243f3802012-05-31 16:26:09 -07004332 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4333 my $orig = $1;
4334 my $level = lc($orig);
4335 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004336 my $level2 = $level;
4337 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004338 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004339 "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 -07004340 }
4341
Joe Perchesdc139312013-02-21 16:44:13 -08004342 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4343 my $orig = $1;
4344 my $level = lc($orig);
4345 $level = "warn" if ($level eq "warning");
4346 $level = "dbg" if ($level eq "debug");
4347 WARN("PREFER_DEV_LEVEL",
4348 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4349 }
4350
Nicolas Boichat8020b252020-10-15 20:12:02 -07004351# trace_printk should not be used in production code.
4352 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4353 WARN("TRACE_PRINTK",
4354 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4355 }
4356
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004357# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4358# number of false positives, but assembly files are not checked, so at
4359# least the arch entry code will not trigger this warning.
4360 if ($line =~ /\bENOSYS\b/) {
4361 WARN("ENOSYS",
4362 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4363 }
4364
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004365# ENOTSUPP is not a standard error code and should be avoided in new patches.
4366# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4367# Similarly to ENOSYS warning a small number of false positives is expected.
4368 if (!$file && $line =~ /\bENOTSUPP\b/) {
4369 if (WARN("ENOTSUPP",
4370 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4371 $fix) {
4372 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4373 }
4374 }
4375
Andy Whitcroft653d4872007-06-23 17:16:34 -07004376# function brace can't be on same line, except for #defines of do while,
4377# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004378 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004379 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4380 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4381 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004382 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004383 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004384 $fix) {
4385 fix_delete_line($fixlinenr, $rawline);
4386 my $fixed_line = $rawline;
Dwaipayan Ray06330942020-12-15 20:45:02 -08004387 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
Joe Perches8d182472014-08-06 16:11:12 -07004388 my $line1 = $1;
4389 my $line2 = $2;
4390 fix_insert_line($fixlinenr, ltrim($line1));
4391 fix_insert_line($fixlinenr, "\+{");
4392 if ($line2 !~ /^\s*$/) {
4393 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4394 }
4395 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004396 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004397
Andy Whitcroft8905a672007-11-28 16:21:06 -08004398# open braces for enum, union and struct go on the same line.
4399 if ($line =~ /^.\s*{/ &&
4400 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004401 if (ERROR("OPEN_BRACE",
4402 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4403 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4404 fix_delete_line($fixlinenr - 1, $prevrawline);
4405 fix_delete_line($fixlinenr, $rawline);
4406 my $fixedline = rtrim($prevrawline) . " {";
4407 fix_insert_line($fixlinenr, $fixedline);
4408 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004409 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004410 if ($fixedline !~ /^\+\s*$/) {
4411 fix_insert_line($fixlinenr, $fixedline);
4412 }
4413 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004414 }
4415
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004416# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004417 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4418 if (WARN("SPACING",
4419 "missing space after $1 definition\n" . $herecurr) &&
4420 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004421 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004422 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4423 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004424 }
4425
Joe Perches31070b52014-01-23 15:54:49 -08004426# Function pointer declarations
4427# check spacing between type, funcptr, and args
4428# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004429 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004430 my $declare = $1;
4431 my $pre_pointer_space = $2;
4432 my $post_pointer_space = $3;
4433 my $funcname = $4;
4434 my $post_funcname_space = $5;
4435 my $pre_args_space = $6;
4436
Joe Perches91f72e92014-04-03 14:49:12 -07004437# the $Declare variable will capture all spaces after the type
4438# so check it for a missing trailing missing space but pointer return types
4439# don't need a space so don't warn for those.
4440 my $post_declare_space = "";
4441 if ($declare =~ /(\s+)$/) {
4442 $post_declare_space = $1;
4443 $declare = rtrim($declare);
4444 }
4445 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004446 WARN("SPACING",
4447 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004448 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004449 }
4450
4451# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004452# This test is not currently implemented because these declarations are
4453# equivalent to
4454# int foo(int bar, ...)
4455# and this is form shouldn't/doesn't generate a checkpatch warning.
4456#
4457# elsif ($declare =~ /\s{2,}$/) {
4458# WARN("SPACING",
4459# "Multiple spaces after return type\n" . $herecurr);
4460# }
Joe Perches31070b52014-01-23 15:54:49 -08004461
4462# unnecessary space "type ( *funcptr)(args...)"
4463 if (defined $pre_pointer_space &&
4464 $pre_pointer_space =~ /^\s/) {
4465 WARN("SPACING",
4466 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4467 }
4468
4469# unnecessary space "type (* funcptr)(args...)"
4470 if (defined $post_pointer_space &&
4471 $post_pointer_space =~ /^\s/) {
4472 WARN("SPACING",
4473 "Unnecessary space before function pointer name\n" . $herecurr);
4474 }
4475
4476# unnecessary space "type (*funcptr )(args...)"
4477 if (defined $post_funcname_space &&
4478 $post_funcname_space =~ /^\s/) {
4479 WARN("SPACING",
4480 "Unnecessary space after function pointer name\n" . $herecurr);
4481 }
4482
4483# unnecessary space "type (*funcptr) (args...)"
4484 if (defined $pre_args_space &&
4485 $pre_args_space =~ /^\s/) {
4486 WARN("SPACING",
4487 "Unnecessary space before function pointer arguments\n" . $herecurr);
4488 }
4489
4490 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004491 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004492 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004493 }
4494 }
4495
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004496# check for spacing round square brackets; allowed:
4497# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004498# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4499# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004500 while ($line =~ /(.*?\s)\[/g) {
4501 my ($where, $prefix) = ($-[1], $1);
4502 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004503 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004504 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004505 if (ERROR("BRACKET_SPACE",
4506 "space prohibited before open square bracket '['\n" . $herecurr) &&
4507 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004508 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004509 s/^(\+.*?)\s+\[/$1\[/;
4510 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004511 }
4512 }
4513
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004514# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004515 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004516 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004517 my $ctx_before = substr($line, 0, $-[1]);
4518 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004519
4520 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004521 if ($name =~ /^(?:
4522 if|for|while|switch|return|case|
4523 volatile|__volatile__|
4524 __attribute__|format|__extension__|
4525 asm|__asm__)$/x)
4526 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004527 # cpp #define statements have non-optional spaces, ie
4528 # if there is a space between the name and the open
4529 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004530 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004531
4532 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004533 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004534
4535 # If this whole things ends with a type its most
4536 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004537 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004538
4539 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004540 if (WARN("SPACING",
4541 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4542 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004543 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004544 s/\b$name\s+\(/$name\(/;
4545 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004546 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004547 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004548
Andy Whitcroft653d4872007-06-23 17:16:34 -07004549# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004550 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004551 my $fixed_line = "";
4552 my $line_fixed = 0;
4553
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004554 my $ops = qr{
4555 <<=|>>=|<=|>=|==|!=|
4556 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4557 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004558 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004559 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004560 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004561 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004562
4563## print("element count: <" . $#elements . ">\n");
4564## foreach my $el (@elements) {
4565## print("el: <$el>\n");
4566## }
4567
4568 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004569 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004570
Joe Perches3705ce52013-07-03 15:05:31 -07004571 foreach my $el (@elements) {
4572 push(@fix_elements, substr($rawline, $off, length($el)));
4573 $off += length($el);
4574 }
4575
4576 $off = 0;
4577
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004578 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004579 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004580
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004581 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004582
4583 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4584
4585## print("n: <$n> good: <$good>\n");
4586
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004587 $off += length($elements[$n]);
4588
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004589 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004590 my $ca = substr($opline, 0, $off);
4591 my $cc = '';
4592 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4593 $cc = substr($opline, $off + length($elements[$n + 1]));
4594 }
4595 my $cb = "$ca$;$cc";
4596
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004597 my $a = '';
4598 $a = 'V' if ($elements[$n] ne '');
4599 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004600 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004601 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4602 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004603 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004604
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004605 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004606
4607 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004608 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004609 $c = 'V' if ($elements[$n + 2] ne '');
4610 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004611 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004612 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4613 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004614 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004615 } else {
4616 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004617 }
4618
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004619 my $ctx = "${a}x${c}";
4620
4621 my $at = "(ctx:$ctx)";
4622
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004623 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004624 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004625
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004626 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004627 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004628
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004629 # Get the full operator variant.
4630 my $opv = $op . substr($curr_vars, $off, 1);
4631
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004632 # Ignore operators passed as parameters.
4633 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004634 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004635
Andy Whitcroftcf655042008-03-04 14:28:20 -08004636# # Ignore comments
4637# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004638
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004639 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004640 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004641 if ($ctx !~ /.x[WEBC]/ &&
4642 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004643 if (ERROR("SPACING",
4644 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004645 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004646 $line_fixed = 1;
4647 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004648 }
4649
4650 # // is a comment
4651 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004652
Joe Perchesb00e4812014-04-03 14:49:33 -07004653 # : when part of a bitfield
4654 } elsif ($opv eq ':B') {
4655 # skip the bitfield test for now
4656
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004657 # No spaces for:
4658 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004659 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004660 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004661 if (ERROR("SPACING",
4662 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004663 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004664 if (defined $fix_elements[$n + 2]) {
4665 $fix_elements[$n + 2] =~ s/^\s+//;
4666 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004667 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004668 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004669 }
4670
Joe Perches23810972014-12-10 15:51:32 -08004671 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004672 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004673 my $rtrim_before = 0;
4674 my $space_after = 0;
4675 if ($ctx =~ /Wx./) {
4676 if (ERROR("SPACING",
4677 "space prohibited before that '$op' $at\n" . $hereptr)) {
4678 $line_fixed = 1;
4679 $rtrim_before = 1;
4680 }
4681 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004682 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004683 if (ERROR("SPACING",
4684 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004685 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004686 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004687 $space_after = 1;
4688 }
4689 }
4690 if ($rtrim_before || $space_after) {
4691 if ($rtrim_before) {
4692 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4693 } else {
4694 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4695 }
4696 if ($space_after) {
4697 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004698 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004699 }
4700
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004701 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004702 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004703 #warn "'*' is part of type\n";
4704
4705 # unary operators should have a space before and
4706 # none after. May be left adjacent to another
4707 # unary operator, or a cast
4708 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004709 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004710 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004711 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004712 if (ERROR("SPACING",
4713 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004714 if ($n != $last_after + 2) {
4715 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4716 $line_fixed = 1;
4717 }
Joe Perches3705ce52013-07-03 15:05:31 -07004718 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004719 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004720 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004721 # A unary '*' may be const
4722
4723 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004724 if (ERROR("SPACING",
4725 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004726 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004727 if (defined $fix_elements[$n + 2]) {
4728 $fix_elements[$n + 2] =~ s/^\s+//;
4729 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004730 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004731 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004732 }
4733
4734 # unary ++ and unary -- are allowed no space on one side.
4735 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004736 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004737 if (ERROR("SPACING",
4738 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004739 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004740 $line_fixed = 1;
4741 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004742 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004743 if ($ctx =~ /Wx[BE]/ ||
4744 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004745 if (ERROR("SPACING",
4746 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004747 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004748 $line_fixed = 1;
4749 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004750 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004751 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004752 if (ERROR("SPACING",
4753 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004754 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004755 if (defined $fix_elements[$n + 2]) {
4756 $fix_elements[$n + 2] =~ s/^\s+//;
4757 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004758 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004759 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004760 }
4761
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004762 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004763 } elsif ($op eq '<<' or $op eq '>>' or
4764 $op eq '&' or $op eq '^' or $op eq '|' or
4765 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004766 $op eq '*' or $op eq '/' or
4767 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004768 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004769 if ($check) {
4770 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4771 if (CHK("SPACING",
4772 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4773 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4774 $fix_elements[$n + 2] =~ s/^\s+//;
4775 $line_fixed = 1;
4776 }
4777 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4778 if (CHK("SPACING",
4779 "space preferred before that '$op' $at\n" . $hereptr)) {
4780 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4781 $line_fixed = 1;
4782 }
4783 }
4784 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004785 if (ERROR("SPACING",
4786 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004787 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4788 if (defined $fix_elements[$n + 2]) {
4789 $fix_elements[$n + 2] =~ s/^\s+//;
4790 }
Joe Perches3705ce52013-07-03 15:05:31 -07004791 $line_fixed = 1;
4792 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004793 }
4794
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004795 # A colon needs no spaces before when it is
4796 # terminating a case value or a label.
4797 } elsif ($opv eq ':C' || $opv eq ':L') {
4798 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004799 if (ERROR("SPACING",
4800 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004801 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004802 $line_fixed = 1;
4803 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004804 }
4805
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004806 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004807 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004808 my $ok = 0;
4809
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004810 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004811 if (($op eq '<' &&
4812 $cc =~ /^\S+\@\S+>/) ||
4813 ($op eq '>' &&
4814 $ca =~ /<\S+\@\S+$/))
4815 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07004816 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004817 }
4818
Joe Perchese0df7e12015-04-16 12:44:53 -07004819 # for asm volatile statements
4820 # ignore a colon with another
4821 # colon immediately before or after
4822 if (($op eq ':') &&
4823 ($ca =~ /:$/ || $cc =~ /^:/)) {
4824 $ok = 1;
4825 }
4826
Joe Perches84731622013-11-12 15:10:05 -08004827 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004828 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004829 my $msg_level = \&ERROR;
4830 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004831
Jean Delvare0675a8f2017-09-08 16:16:07 -07004832 if (&{$msg_level}("SPACING",
4833 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004834 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4835 if (defined $fix_elements[$n + 2]) {
4836 $fix_elements[$n + 2] =~ s/^\s+//;
4837 }
Joe Perches3705ce52013-07-03 15:05:31 -07004838 $line_fixed = 1;
4839 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004840 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004841 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004842 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004843
4844## print("n: <$n> GOOD: <$good>\n");
4845
4846 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004847 }
Joe Perches3705ce52013-07-03 15:05:31 -07004848
4849 if (($#elements % 2) == 0) {
4850 $fixed_line = $fixed_line . $fix_elements[$#elements];
4851 }
4852
Joe Perches194f66f2014-08-06 16:11:03 -07004853 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4854 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004855 }
4856
4857
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004858 }
4859
Joe Perches786b6322013-07-03 15:05:32 -07004860# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004861 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004862 if (WARN("SPACING",
4863 "space prohibited before semicolon\n" . $herecurr) &&
4864 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004865 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004866 s/^(\+.*\S)\s+;/$1;/;
4867 }
4868 }
4869
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004870# check for multiple assignments
4871 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004872 CHK("MULTIPLE_ASSIGNMENTS",
4873 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004874 }
4875
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004876## # check for multiple declarations, allowing for a function declaration
4877## # continuation.
4878## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4879## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4880##
4881## # Remove any bracketed sections to ensure we do not
4882## # falsly report the parameters of functions.
4883## my $ln = $line;
4884## while ($ln =~ s/\([^\(\)]*\)//g) {
4885## }
4886## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004887## WARN("MULTIPLE_DECLARATION",
4888## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004889## }
4890## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004891
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004892#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004893 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004894 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004895 if (ERROR("SPACING",
4896 "space required before the open brace '{'\n" . $herecurr) &&
4897 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004898 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004899 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004900 }
4901
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004902## # check for blank lines before declarations
4903## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4904## $prevrawline =~ /^.\s*$/) {
4905## WARN("SPACING",
4906## "No blank lines before declarations\n" . $hereprev);
4907## }
4908##
4909
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004910# closing brace should have a space following it when it has anything
4911# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07004912 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004913 if (ERROR("SPACING",
4914 "space required after that close brace '}'\n" . $herecurr) &&
4915 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004916 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004917 s/}((?!(?:,|;|\)))\S)/} $1/;
4918 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004919 }
4920
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004921# check spacing on square brackets
4922 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004923 if (ERROR("SPACING",
4924 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4925 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004926 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004927 s/\[\s+/\[/;
4928 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004929 }
4930 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004931 if (ERROR("SPACING",
4932 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4933 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004934 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004935 s/\s+\]/\]/;
4936 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004937 }
4938
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004939# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004940 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4941 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004942 if (ERROR("SPACING",
4943 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4944 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004945 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004946 s/\(\s+/\(/;
4947 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004948 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004949 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004950 $line !~ /for\s*\(.*;\s+\)/ &&
4951 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004952 if (ERROR("SPACING",
4953 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4954 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004955 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004956 s/\s+\)/\)/;
4957 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004958 }
4959
Joe Perchese2826fd2014-08-06 16:10:48 -07004960# check unnecessary parentheses around addressof/dereference single $Lvals
4961# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4962
4963 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004964 my $var = $1;
4965 if (CHK("UNNECESSARY_PARENTHESES",
4966 "Unnecessary parentheses around $var\n" . $herecurr) &&
4967 $fix) {
4968 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4969 }
4970 }
4971
4972# check for unnecessary parentheses around function pointer uses
4973# ie: (foo->bar)(); should be foo->bar();
4974# but not "if (foo->bar) (" to avoid some false positives
4975 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4976 my $var = $2;
4977 if (CHK("UNNECESSARY_PARENTHESES",
4978 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4979 $fix) {
4980 my $var2 = deparenthesize($var);
4981 $var2 =~ s/\s//g;
4982 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4983 }
4984 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004985
Joe Perches63b7c732017-09-08 16:16:01 -07004986# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004987# when !drivers/staging or command-line uses --strict
4988 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004989 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004990 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4991 my $if_stat = $1;
4992 my $test = substr($2, 1, -1);
4993 my $herectx;
4994 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4995 my $match = $1;
4996 # avoid parentheses around potential macro args
4997 next if ($match =~ /^\s*\w+\s*$/);
4998 if (!defined($herectx)) {
4999 $herectx = $here . "\n";
5000 my $cnt = statement_rawlines($if_stat);
5001 for (my $n = 0; $n < $cnt; $n++) {
5002 my $rl = raw_line($linenr, $n);
5003 $herectx .= $rl . "\n";
5004 last if $rl =~ /^[ \+].*\{/;
5005 }
5006 }
5007 CHK("UNNECESSARY_PARENTHESES",
5008 "Unnecessary parentheses around '$match'\n" . $herectx);
5009 }
5010 }
5011
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005012#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005013 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005014 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07005015 if (WARN("INDENTED_LABEL",
5016 "labels should not be indented\n" . $herecurr) &&
5017 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005018 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005019 s/^(.)\s+/$1/;
5020 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005021 }
5022
Joe Perches40873ab2020-10-15 20:11:56 -07005023# check if a statement with a comma should be two statements like:
5024# foo = bar(), /* comma should be semicolon */
5025# bar = baz();
5026 if (defined($stat) &&
5027 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5028 my $cnt = statement_rawlines($stat);
5029 my $herectx = get_stat_here($linenr, $cnt, $here);
5030 WARN("SUSPECT_COMMA_SEMICOLON",
5031 "Possible comma where semicolon could be used\n" . $herectx);
5032 }
5033
Joe Perches5b9553a2014-04-03 14:49:21 -07005034# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08005035 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005036 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07005037 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07005038 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5039 my $value = $1;
5040 $value = deparenthesize($value);
5041 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5042 ERROR("RETURN_PARENTHESES",
5043 "return is not a function, parentheses are not required\n" . $herecurr);
5044 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005045 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005046 ERROR("SPACING",
5047 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005048 }
5049 }
Joe Perches507e5142013-11-12 15:10:13 -08005050
Joe Perchesb43ae212014-06-23 13:22:07 -07005051# unnecessary return in a void function
5052# at end-of-function, with the previous line a single leading tab, then return;
5053# and the line before that not a goto label target like "out:"
5054 if ($sline =~ /^[ \+]}\s*$/ &&
5055 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5056 $linenr >= 3 &&
5057 $lines[$linenr - 3] =~ /^[ +]/ &&
5058 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07005059 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07005060 "void function return statements are not generally useful\n" . $hereprev);
5061 }
Joe Perches9819cf22014-06-04 16:12:09 -07005062
Joe Perches189248d2014-01-23 15:54:47 -08005063# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07005064 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08005065 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5066 my $openparens = $1;
5067 my $count = $openparens =~ tr@\(@\(@;
5068 my $msg = "";
5069 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5070 my $comp = $4; #Not $1 because of $LvalOrFunc
5071 $msg = " - maybe == should be = ?" if ($comp eq "==");
5072 WARN("UNNECESSARY_PARENTHESES",
5073 "Unnecessary parentheses$msg\n" . $herecurr);
5074 }
5075 }
5076
Joe Perchesc5595fa2015-09-09 15:37:58 -07005077# comparisons with a constant or upper case identifier on the left
5078# avoid cases like "foo + BAR < baz"
5079# only fix matches surrounded by parentheses to avoid incorrect
5080# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07005081 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005082 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5083 my $lead = $1;
5084 my $const = $2;
5085 my $comp = $3;
5086 my $to = $4;
5087 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07005088 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005089 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5090 WARN("CONSTANT_COMPARISON",
5091 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5092 $fix) {
5093 if ($comp eq "<") {
5094 $newcomp = ">";
5095 } elsif ($comp eq "<=") {
5096 $newcomp = ">=";
5097 } elsif ($comp eq ">") {
5098 $newcomp = "<";
5099 } elsif ($comp eq ">=") {
5100 $newcomp = "<=";
5101 }
5102 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5103 }
5104 }
5105
Joe Perchesf34e4a42015-04-16 12:44:19 -07005106# Return of what appears to be an errno should normally be negative
5107 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005108 my $name = $1;
5109 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07005110 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07005111 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005112 }
5113 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005114
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005115# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07005116 if ($line =~ /\b(if|while|for|switch)\(/) {
5117 if (ERROR("SPACING",
5118 "space required before the open parenthesis '('\n" . $herecurr) &&
5119 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005120 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005121 s/\b(if|while|for|switch)\(/$1 \(/;
5122 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005123 }
5124
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07005125# Check for illegal assignment in if conditional -- and check for trailing
5126# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005127 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08005128 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5129 ctx_statement_block($linenr, $realcnt, 0)
5130 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005131 my ($stat_next) = ctx_statement_block($line_nr_next,
5132 $remain_next, $off_next);
5133 $stat_next =~ s/\n./\n /g;
5134 ##print "stat<$stat> stat_next<$stat_next>\n";
5135
5136 if ($stat_next =~ /^\s*while\b/) {
5137 # If the statement carries leading newlines,
5138 # then count those as offsets.
5139 my ($whitespace) =
5140 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5141 my $offset =
5142 statement_rawlines($whitespace) - 1;
5143
5144 $suppress_whiletrailers{$line_nr_next +
5145 $offset} = 1;
5146 }
5147 }
5148 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005149 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005150 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005151 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005152
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005153 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches65b64b32020-08-11 18:35:10 -07005154 if (ERROR("ASSIGN_IN_IF",
5155 "do not use assignment in if condition\n" . $herecurr) &&
5156 $fix && $perl_version_ok) {
5157 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5158 my $space = $1;
5159 my $not = $2;
5160 my $statement = $3;
5161 my $assigned = $4;
5162 my $test = $8;
5163 my $against = $9;
5164 my $brace = $15;
5165 fix_delete_line($fixlinenr, $rawline);
5166 fix_insert_line($fixlinenr, "$space$statement;");
5167 my $newline = "${space}if (";
5168 $newline .= '!' if defined($not);
5169 $newline .= '(' if (defined $not && defined($test) && defined($against));
5170 $newline .= "$assigned";
5171 $newline .= " $test $against" if (defined($test) && defined($against));
5172 $newline .= ')' if (defined $not && defined($test) && defined($against));
5173 $newline .= ')';
5174 $newline .= " {" if (defined($brace));
5175 fix_insert_line($fixlinenr + 1, $newline);
5176 }
5177 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005178 }
5179
5180 # Find out what is on the end of the line after the
5181 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005182 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005183 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005184 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005185 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5186 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005187 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005188 # Find out how long the conditional actually is.
5189 my @newlines = ($c =~ /\n/gs);
5190 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005191 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005192
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005193 $stat_real = raw_line($linenr, $cond_lines)
5194 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005195 if (defined($stat_real) && $cond_lines > 1) {
5196 $stat_real = "[...]\n$stat_real";
5197 }
5198
Joe Perches000d1cc12011-07-25 17:13:25 -07005199 ERROR("TRAILING_STATEMENTS",
5200 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005201 }
5202 }
5203
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005204# Check for bitwise tests written as boolean
5205 if ($line =~ /
5206 (?:
5207 (?:\[|\(|\&\&|\|\|)
5208 \s*0[xX][0-9]+\s*
5209 (?:\&\&|\|\|)
5210 |
5211 (?:\&\&|\|\|)
5212 \s*0[xX][0-9]+\s*
5213 (?:\&\&|\|\||\)|\])
5214 )/x)
5215 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005216 WARN("HEXADECIMAL_BOOLEAN_TEST",
5217 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005218 }
5219
Andy Whitcroft8905a672007-11-28 16:21:06 -08005220# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005221 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5222 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005223 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005224 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005225 ERROR("TRAILING_STATEMENTS",
5226 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005227 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005228 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005229# if should not continue a brace
5230 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005231 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005232 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005233 $herecurr);
5234 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005235# case and default should not have general statements after them
5236 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5237 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005238 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005239 \s*return\s+
5240 )/xg)
5241 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005242 ERROR("TRAILING_STATEMENTS",
5243 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005244 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005245
5246 # Check for }<nl>else {, these must be at the same
5247 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005248 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5249 $previndent == $indent) {
5250 if (ERROR("ELSE_AFTER_BRACE",
5251 "else should follow close brace '}'\n" . $hereprev) &&
5252 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5253 fix_delete_line($fixlinenr - 1, $prevrawline);
5254 fix_delete_line($fixlinenr, $rawline);
5255 my $fixedline = $prevrawline;
5256 $fixedline =~ s/}\s*$//;
5257 if ($fixedline !~ /^\+\s*$/) {
5258 fix_insert_line($fixlinenr, $fixedline);
5259 }
5260 $fixedline = $rawline;
5261 $fixedline =~ s/^(.\s*)else/$1} else/;
5262 fix_insert_line($fixlinenr, $fixedline);
5263 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005264 }
5265
Joe Perches8b8856f2014-08-06 16:11:14 -07005266 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5267 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005268 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5269
5270 # Find out what is on the end of the line after the
5271 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005272 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005273 $s =~ s/\n.*//g;
5274
5275 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005276 if (ERROR("WHILE_AFTER_BRACE",
5277 "while should follow close brace '}'\n" . $hereprev) &&
5278 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5279 fix_delete_line($fixlinenr - 1, $prevrawline);
5280 fix_delete_line($fixlinenr, $rawline);
5281 my $fixedline = $prevrawline;
5282 my $trailing = $rawline;
5283 $trailing =~ s/^\+//;
5284 $trailing = trim($trailing);
5285 $fixedline =~ s/}\s*$/} $trailing/;
5286 fix_insert_line($fixlinenr, $fixedline);
5287 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005288 }
5289 }
5290
Joe Perches95e2c602013-07-03 15:05:20 -07005291#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005292 while ($line =~ m{($Constant|$Lval)}g) {
5293 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005294
Joe Perches95e2c602013-07-03 15:05:20 -07005295#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005296 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005297 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005298#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005299 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005300#Ignore SI style variants like nS, mV and dB
5301#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5302 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005303#Ignore some three character SI units explicitly, like MiB and KHz
5304 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005305 while ($var =~ m{($Ident)}g) {
5306 my $word = $1;
5307 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005308 if ($check) {
5309 seed_camelcase_includes();
5310 if (!$file && !$camelcase_file_seeded) {
5311 seed_camelcase_file($realfile);
5312 $camelcase_file_seeded = 1;
5313 }
5314 }
Joe Perches7e781f62013-09-11 14:23:55 -07005315 if (!defined $camelcase{$word}) {
5316 $camelcase{$word} = 1;
5317 CHK("CAMELCASE",
5318 "Avoid CamelCase: <$word>\n" . $herecurr);
5319 }
Joe Perches34456862013-07-03 15:05:34 -07005320 }
Joe Perches323c1262012-12-17 16:02:07 -08005321 }
5322 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005323
5324#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005325 if ($line =~ /\#\s*define.*\\\s+$/) {
5326 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5327 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5328 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005329 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005330 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005331 }
5332
Fabian Frederick0e212e02015-04-16 12:44:25 -07005333# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5334# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005335 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005336 my $file = "$1.h";
5337 my $checkfile = "include/linux/$file";
5338 if (-f "$root/$checkfile" &&
5339 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005340 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005341 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005342 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5343 if ($asminclude > 0) {
5344 if ($realfile =~ m{^arch/}) {
5345 CHK("ARCH_INCLUDE_LINUX",
5346 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5347 } else {
5348 WARN("INCLUDE_LINUX",
5349 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5350 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005351 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005352 }
5353 }
5354
Andy Whitcroft653d4872007-06-23 17:16:34 -07005355# multi-statement macros should be enclosed in a do while loop, grab the
5356# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005357# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005358 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5359 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005360 my $ln = $linenr;
5361 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005362 my ($off, $dstat, $dcond, $rest);
5363 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005364 my $has_flow_statement = 0;
5365 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005366 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005367 ctx_statement_block($linenr, $realcnt, 0);
5368 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005369 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005370 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005371
Joe Perches08a28432014-10-13 15:51:55 -07005372 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005373 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005374
Joe Perchesf59b64b2016-10-11 13:52:08 -07005375 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5376 my $define_args = $1;
5377 my $define_stmt = $dstat;
5378 my @def_args = ();
5379
5380 if (defined $define_args && $define_args ne "") {
5381 $define_args = substr($define_args, 1, length($define_args) - 2);
5382 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005383 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005384 @def_args = split(",", $define_args);
5385 }
5386
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005387 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005388 $dstat =~ s/\\\n.//g;
5389 $dstat =~ s/^\s*//s;
5390 $dstat =~ s/\s*$//s;
5391
5392 # Flatten any parentheses and braces
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005393 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5394 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5395 $dstat =~ s/.\[[^\[\]]*\]/1u/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005396 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005397 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005398
Antonio Borneo342d3d22020-04-06 20:11:01 -07005399 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005400 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5401 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005402 {
5403 }
5404
Joe Perches42e15292016-03-15 14:58:01 -07005405 # Make asm volatile uses seem like a generic function
5406 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5407
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005408 my $exceptions = qr{
5409 $Declare|
5410 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005411 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005412 DECLARE_PER_CPU|
5413 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005414 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005415 union|
5416 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005417 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005418 ^\"|\"$|
5419 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005420 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005421 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005422
5423 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005424 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005425 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005426
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005427 if ($dstat ne '' &&
5428 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5429 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005430 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005431 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005432 $dstat !~ /$exceptions/ &&
5433 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005434 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005435 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005436 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005437 $dstat !~ /^for\s*$Constant$/ && # for (...)
5438 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5439 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005440 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005441 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005442 {
Joe Perchese7955562017-05-08 15:55:48 -07005443 if ($dstat =~ /^\s*if\b/) {
5444 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5445 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5446 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005447 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5448 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5449 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005450 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005451 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005452 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005453
5454 }
Joe Perches52076492016-10-11 13:52:14 -07005455
5456 # Make $define_stmt single line, comment-free, etc
5457 my @stmt_array = split('\n', $define_stmt);
5458 my $first = 1;
5459 $define_stmt = "";
5460 foreach my $l (@stmt_array) {
5461 $l =~ s/\\$//;
5462 if ($first) {
5463 $define_stmt = $l;
5464 $first = 0;
5465 } elsif ($l =~ /^[\+ ]/) {
5466 $define_stmt .= substr($l, 1);
5467 }
5468 }
5469 $define_stmt =~ s/$;//g;
5470 $define_stmt =~ s/\s+/ /g;
5471 $define_stmt = trim($define_stmt);
5472
Joe Perchesf59b64b2016-10-11 13:52:08 -07005473# check if any macro arguments are reused (ignore '...' and 'type')
5474 foreach my $arg (@def_args) {
5475 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005476 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005477 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005478 $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 -07005479 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5480 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005481 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005482 if ($use_cnt > 1) {
5483 CHK("MACRO_ARG_REUSE",
5484 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005485 }
5486# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005487 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005488 ((defined($1) && $1 ne ',') ||
5489 (defined($2) && $2 ne ','))) {
5490 CHK("MACRO_ARG_PRECEDENCE",
5491 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005492 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005493 }
Joe Perches5023d342012-12-17 16:01:47 -08005494
Joe Perches08a28432014-10-13 15:51:55 -07005495# check for macros with flow control, but without ## concatenation
5496# ## concatenation is commonly a macro that defines a function so ignore those
5497 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005498 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005499 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005500
Joe Perches08a28432014-10-13 15:51:55 -07005501 WARN("MACRO_WITH_FLOW_CONTROL",
5502 "Macros with flow control statements should be avoided\n" . "$herectx");
5503 }
5504
Joe Perches481eb482012-12-17 16:01:56 -08005505# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005506
5507 } else {
5508 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005509 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5510 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005511 $line =~ /^\+.*\\$/) {
5512 WARN("LINE_CONTINUATIONS",
5513 "Avoid unnecessary line continuations\n" . $herecurr);
5514 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005515 }
5516
Joe Perchesb13edf72012-07-30 14:41:24 -07005517# do {} while (0) macro tests:
5518# single-statement macros do not need to be enclosed in do while (0) loop,
5519# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005520 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005521 $realfile !~ m@/vmlinux.lds.h$@ &&
5522 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5523 my $ln = $linenr;
5524 my $cnt = $realcnt;
5525 my ($off, $dstat, $dcond, $rest);
5526 my $ctx = '';
5527 ($dstat, $dcond, $ln, $cnt, $off) =
5528 ctx_statement_block($linenr, $realcnt, 0);
5529 $ctx = $dstat;
5530
5531 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005532 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005533
5534 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5535 my $stmts = $2;
5536 my $semis = $3;
5537
5538 $ctx =~ s/\n*$//;
5539 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005540 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005541
Joe Perchesac8e97f2012-08-21 16:15:53 -07005542 if (($stmts =~ tr/;/;/) == 1 &&
5543 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005544 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5545 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5546 }
5547 if (defined $semis && $semis ne "") {
5548 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5549 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5550 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005551 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5552 $ctx =~ s/\n*$//;
5553 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005554 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005555
5556 WARN("TRAILING_SEMICOLON",
5557 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005558 }
5559 }
5560
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005561# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005562 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5563 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005564 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005565 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005566 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5567 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005568 my @allowed = ();
5569 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005570 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005571 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005572 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005573 for my $chunk (@chunks) {
5574 my ($cond, $block) = @{$chunk};
5575
Andy Whitcroft773647a2008-03-28 14:15:58 -07005576 # If the condition carries leading newlines, then count those as offsets.
5577 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5578 my $offset = statement_rawlines($whitespace) - 1;
5579
Joe Perchesaad4f612012-03-23 15:02:19 -07005580 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005581 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5582
5583 # We have looked at and allowed this specific line.
5584 $suppress_ifbraces{$ln + $offset} = 1;
5585
5586 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005587 $ln += statement_rawlines($block) - 1;
5588
Andy Whitcroft773647a2008-03-28 14:15:58 -07005589 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005590
5591 $seen++ if ($block =~ /^\s*{/);
5592
Joe Perchesaad4f612012-03-23 15:02:19 -07005593 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005594 if (statement_lines($cond) > 1) {
5595 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005596 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005597 }
5598 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005599 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005600 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005601 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005602 if (statement_block_size($block) > 1) {
5603 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005604 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005605 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005606 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005607 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005608 if ($seen) {
5609 my $sum_allowed = 0;
5610 foreach (@allowed) {
5611 $sum_allowed += $_;
5612 }
5613 if ($sum_allowed == 0) {
5614 WARN("BRACES",
5615 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5616 } elsif ($sum_allowed != $allow &&
5617 $seen != $allow) {
5618 CHK("BRACES",
5619 "braces {} should be used on all arms of this statement\n" . $herectx);
5620 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005621 }
5622 }
5623 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005624 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005625 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005626 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005627
Andy Whitcroftcf655042008-03-04 14:28:20 -08005628 # Check the pre-context.
5629 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5630 #print "APW: ALLOWED: pre<$1>\n";
5631 $allowed = 1;
5632 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005633
5634 my ($level, $endln, @chunks) =
5635 ctx_statement_full($linenr, $realcnt, $-[0]);
5636
Andy Whitcroftcf655042008-03-04 14:28:20 -08005637 # Check the condition.
5638 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005639 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005640 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005641 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005642 }
5643 if (statement_lines($cond) > 1) {
5644 #print "APW: ALLOWED: cond<$cond>\n";
5645 $allowed = 1;
5646 }
5647 if ($block =~/\b(?:if|for|while)\b/) {
5648 #print "APW: ALLOWED: block<$block>\n";
5649 $allowed = 1;
5650 }
5651 if (statement_block_size($block) > 1) {
5652 #print "APW: ALLOWED: lines block<$block>\n";
5653 $allowed = 1;
5654 }
5655 # Check the post-context.
5656 if (defined $chunks[1]) {
5657 my ($cond, $block) = @{$chunks[1]};
5658 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005659 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005660 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005661 if ($block =~ /^\s*\{/) {
5662 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5663 $allowed = 1;
5664 }
5665 }
5666 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005667 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005668 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005669
Joe Perches000d1cc12011-07-25 17:13:25 -07005670 WARN("BRACES",
5671 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005672 }
5673 }
5674
Joe Perchese4c5bab2017-02-24 15:01:41 -08005675# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005676 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5677 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005678 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5679 }
5680
Joe Perches0979ae62012-12-17 16:01:59 -08005681# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005682 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005683 if (CHK("BRACES",
5684 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5685 $fix && $prevrawline =~ /^\+/) {
5686 fix_delete_line($fixlinenr - 1, $prevrawline);
5687 }
Joe Perches0979ae62012-12-17 16:01:59 -08005688 }
Joe Perches77b9a532013-07-03 15:05:29 -07005689 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005690 if (CHK("BRACES",
5691 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5692 $fix) {
5693 fix_delete_line($fixlinenr, $rawline);
5694 }
Joe Perches0979ae62012-12-17 16:01:59 -08005695 }
5696
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005697# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005698 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5699 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005700 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005701 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005702 }
5703
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005704# Check for user-visible strings broken across lines, which breaks the ability
5705# to grep for the string. Make exceptions when the previous string ends in a
5706# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5707# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005708 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005709 $prevline =~ /"\s*$/ &&
5710 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5711 if (WARN("SPLIT_STRING",
5712 "quoted string split across lines\n" . $hereprev) &&
5713 $fix &&
5714 $prevrawline =~ /^\+.*"\s*$/ &&
5715 $last_coalesced_string_linenr != $linenr - 1) {
5716 my $extracted_string = get_quoted_string($line, $rawline);
5717 my $comma_close = "";
5718 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5719 $comma_close = $1;
5720 }
5721
5722 fix_delete_line($fixlinenr - 1, $prevrawline);
5723 fix_delete_line($fixlinenr, $rawline);
5724 my $fixedline = $prevrawline;
5725 $fixedline =~ s/"\s*$//;
5726 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5727 fix_insert_line($fixlinenr - 1, $fixedline);
5728 $fixedline = $rawline;
5729 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5730 if ($fixedline !~ /\+\s*$/) {
5731 fix_insert_line($fixlinenr, $fixedline);
5732 }
5733 $last_coalesced_string_linenr = $linenr;
5734 }
5735 }
5736
5737# check for missing a space in a string concatenation
5738 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5739 WARN('MISSING_SPACE',
5740 "break quoted strings at a space character\n" . $hereprev);
5741 }
5742
Joe Perchese4b7d302017-05-08 15:55:51 -07005743# check for an embedded function name in a string when the function is known
5744# This does not work very well for -f --file checking as it depends on patch
5745# context providing the function name or a single line form for in-file
5746# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005747 if ($line =~ /^\+.*$String/ &&
5748 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005749 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5750 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005751 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005752 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005753 }
5754
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005755# check for spaces before a quoted newline
5756 if ($rawline =~ /^.*\".*\s\\n/) {
5757 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5758 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5759 $fix) {
5760 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5761 }
5762
5763 }
5764
Joe Perchesf17dba42014-10-13 15:51:51 -07005765# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005766 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5767 if (CHK("CONCATENATED_STRING",
5768 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5769 $fix) {
5770 while ($line =~ /($String)/g) {
5771 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5772 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5773 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5774 }
5775 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005776 }
5777
Joe Perches90ad30e2014-12-10 15:51:59 -08005778# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005779 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005780 if (WARN("STRING_FRAGMENTS",
5781 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5782 $fix) {
5783 while ($line =~ /($String)(?=\s*")/g) {
5784 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5785 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5786 }
5787 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005788 }
5789
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005790# check for non-standard and hex prefixed decimal printf formats
5791 my $show_L = 1; #don't show the same defect twice
5792 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005793 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005794 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005795 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005796 # check for %L
5797 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005798 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005799 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5800 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005801 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005802 # check for %Z
5803 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5804 WARN("PRINTF_Z",
5805 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5806 $show_Z = 0;
5807 }
5808 # check for 0x<decimal>
5809 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5810 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005811 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5812 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005813 }
5814
5815# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005816 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005817 WARN("LINE_CONTINUATIONS",
5818 "Avoid line continuations in quoted strings\n" . $herecurr);
5819 }
5820
Andy Whitcroft00df3442007-06-08 13:47:06 -07005821# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005822 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005823 WARN("IF_0",
5824 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5825 }
5826
5827# warn about #if 1
5828 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5829 WARN("IF_1",
5830 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005831 }
5832
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005833# check for needless "if (<foo>) fn(<foo>)" uses
5834 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005835 my $tested = quotemeta($1);
5836 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5837 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5838 my $func = $1;
5839 if (WARN('NEEDLESS_IF',
5840 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5841 $fix) {
5842 my $do_fix = 1;
5843 my $leading_tabs = "";
5844 my $new_leading_tabs = "";
5845 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5846 $leading_tabs = $1;
5847 } else {
5848 $do_fix = 0;
5849 }
5850 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5851 $new_leading_tabs = $1;
5852 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5853 $do_fix = 0;
5854 }
5855 } else {
5856 $do_fix = 0;
5857 }
5858 if ($do_fix) {
5859 fix_delete_line($fixlinenr - 1, $prevrawline);
5860 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5861 }
5862 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005863 }
5864 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005865
Joe Perchesebfdc402014-08-06 16:10:27 -07005866# check for unnecessary "Out of Memory" messages
5867 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5868 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5869 (defined $1 || defined $3) &&
5870 $linenr > 3) {
5871 my $testval = $2;
5872 my $testline = $lines[$linenr - 3];
5873
5874 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5875# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5876
Joe Perchese29a70f2019-03-07 16:28:35 -08005877 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5878 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005879 WARN("OOM_MESSAGE",
5880 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5881 }
5882 }
5883
Joe Perchesf78d98f2014-10-13 15:52:01 -07005884# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005885 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005886 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5887 my $level = $1;
5888 if (WARN("UNNECESSARY_KERN_LEVEL",
5889 "Possible unnecessary $level\n" . $herecurr) &&
5890 $fix) {
5891 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5892 }
5893 }
5894
Joe Perches45c55e92017-02-24 15:01:31 -08005895# check for logging continuations
5896 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5897 WARN("LOGGING_CONTINUATION",
5898 "Avoid logging continuation uses where feasible\n" . $herecurr);
5899 }
5900
Joe Perchesabb08a52014-12-10 15:51:46 -08005901# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005902 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005903 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5904 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5905 WARN("MASK_THEN_SHIFT",
5906 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5907 }
5908
Joe Perchesb75ac612014-12-10 15:52:02 -08005909# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005910 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005911 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5912 my $val = $1;
5913 my $equal = "!";
5914 $equal = "" if ($4 eq "!=");
5915 if (CHK("COMPARISON_TO_NULL",
5916 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5917 $fix) {
5918 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5919 }
5920 }
5921 }
5922
Joe Perches8716de32013-09-11 14:24:05 -07005923# check for bad placement of section $InitAttribute (e.g.: __initdata)
5924 if ($line =~ /(\b$InitAttribute\b)/) {
5925 my $attr = $1;
5926 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5927 my $ptr = $1;
5928 my $var = $2;
5929 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5930 ERROR("MISPLACED_INIT",
5931 "$attr should be placed after $var\n" . $herecurr)) ||
5932 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5933 WARN("MISPLACED_INIT",
5934 "$attr should be placed after $var\n" . $herecurr))) &&
5935 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005936 $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 -07005937 }
5938 }
5939 }
5940
Joe Perchese970b8842013-11-12 15:10:10 -08005941# check for $InitAttributeData (ie: __initdata) with const
5942 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5943 my $attr = $1;
5944 $attr =~ /($InitAttributePrefix)(.*)/;
5945 my $attr_prefix = $1;
5946 my $attr_type = $2;
5947 if (ERROR("INIT_ATTRIBUTE",
5948 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5949 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005950 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005951 s/$InitAttributeData/${attr_prefix}initconst/;
5952 }
5953 }
5954
5955# check for $InitAttributeConst (ie: __initconst) without const
5956 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5957 my $attr = $1;
5958 if (ERROR("INIT_ATTRIBUTE",
5959 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5960 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005961 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005962 /(^\+\s*(?:static\s+))/;
5963 $lead = rtrim($1);
5964 $lead = "$lead " if ($lead !~ /^\+$/);
5965 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005966 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005967 }
5968 }
5969
Joe Perchesc17893c2015-04-16 12:44:42 -07005970# check for __read_mostly with const non-pointer (should just be const)
5971 if ($line =~ /\b__read_mostly\b/ &&
5972 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5973 if (ERROR("CONST_READ_MOSTLY",
5974 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5975 $fix) {
5976 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5977 }
5978 }
5979
Joe Perchesfbdb8132014-04-03 14:49:14 -07005980# don't use __constant_<foo> functions outside of include/uapi/
5981 if ($realfile !~ m@^include/uapi/@ &&
5982 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5983 my $constant_func = $1;
5984 my $func = $constant_func;
5985 $func =~ s/^__constant_//;
5986 if (WARN("CONSTANT_CONVERSION",
5987 "$constant_func should be $func\n" . $herecurr) &&
5988 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005989 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005990 }
5991 }
5992
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005993# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005994 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005995 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005996 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005997 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005998 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005999 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07006000 }
6001 if ($delay > 2000) {
6002 WARN("LONG_UDELAY",
6003 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006004 }
6005 }
6006
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006007# warn about unexpectedly long msleep's
6008 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6009 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006010 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006011 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006012 }
6013 }
6014
Joe Perches36ec1932013-07-03 15:05:25 -07006015# check for comparisons of jiffies
6016 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6017 WARN("JIFFIES_COMPARISON",
6018 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6019 }
6020
Joe Perches9d7a34a2013-07-03 15:05:26 -07006021# check for comparisons of get_jiffies_64()
6022 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6023 WARN("JIFFIES_COMPARISON",
6024 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6025 }
6026
Andy Whitcroft00df3442007-06-08 13:47:06 -07006027# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006028# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07006029# print "#ifdef in C files should be avoided\n";
6030# print "$herecurr";
6031# $clean = 0;
6032# }
6033
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006034# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006035 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07006036 if (ERROR("SPACING",
6037 "exactly one space required after that #$1\n" . $herecurr) &&
6038 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006039 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07006040 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6041 }
6042
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006043 }
6044
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006045# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006046 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6047 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006048 my $which = $1;
6049 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006050 CHK("UNCOMMENTED_DEFINITION",
6051 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006052 }
6053 }
6054# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006055
6056 my $barriers = qr{
6057 mb|
6058 rmb|
Will Deaconad83ec62019-11-07 14:49:00 +00006059 wmb
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006060 }x;
6061 my $barrier_stems = qr{
6062 mb__before_atomic|
6063 mb__after_atomic|
6064 store_release|
6065 load_acquire|
6066 store_mb|
6067 (?:$barriers)
6068 }x;
6069 my $all_barriers = qr{
6070 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02006071 smp_(?:$barrier_stems)|
6072 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006073 }x;
6074
6075 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006076 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08006077 WARN("MEMORY_BARRIER",
6078 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006079 }
6080 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006081
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02006082 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6083
6084 if ($realfile !~ m@^include/asm-generic/@ &&
6085 $realfile !~ m@/barrier\.h$@ &&
6086 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6087 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6088 WARN("MEMORY_BARRIER",
6089 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6090 }
6091
Joe Perchescb426e92015-06-25 15:02:46 -07006092# check for waitqueue_active without a comment.
6093 if ($line =~ /\bwaitqueue_active\s*\(/) {
6094 if (!ctx_has_comment($first_line, $linenr)) {
6095 WARN("WAITQUEUE_ACTIVE",
6096 "waitqueue_active without comment\n" . $herecurr);
6097 }
6098 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006099
Marco Elver5099a722020-04-01 12:17:14 +02006100# check for data_race without a comment.
6101 if ($line =~ /\bdata_race\s*\(/) {
6102 if (!ctx_has_comment($first_line, $linenr)) {
6103 WARN("DATA_RACE",
6104 "data_race without comment\n" . $herecurr);
6105 }
6106 }
6107
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006108# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006109 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006110 CHK("ARCH_DEFINES",
6111 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006112 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006113
Joe Perches596ed452017-07-12 14:37:02 -07006114# check that the storage class is not after a type
6115 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006116 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07006117 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6118 }
6119# Check that the storage class is at the beginning of a declaration
6120 if ($line =~ /\b$Storage\b/ &&
6121 $line !~ /^.\s*$Storage/ &&
6122 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6123 $1 !~ /[\,\)]\s*$/) {
6124 WARN("STORAGE_CLASS",
6125 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07006126 }
6127
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006128# check the location of the inline attribute, that it is between
6129# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006130 if ($line =~ /\b$Type\s+$Inline\b/ ||
6131 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006132 ERROR("INLINE_LOCATION",
6133 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006134 }
6135
Andy Whitcroft8905a672007-11-28 16:21:06 -08006136# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08006137 if ($realfile !~ m@\binclude/uapi/@ &&
6138 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006139 if (WARN("INLINE",
6140 "plain inline is preferred over $1\n" . $herecurr) &&
6141 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006142 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006143
6144 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006145 }
6146
Joe Perches3d130fd2011-01-12 17:00:00 -08006147# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08006148 if ($realfile !~ m@\binclude/uapi/@ &&
6149 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006150 WARN("PREFER_PACKED",
6151 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08006152 }
6153
Joe Perches39b7e282011-07-25 17:13:24 -07006154# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08006155 if ($realfile !~ m@\binclude/uapi/@ &&
6156 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006157 WARN("PREFER_ALIGNED",
6158 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07006159 }
6160
Joe Perches462811d2019-09-25 16:46:44 -07006161# Check for __attribute__ section, prefer __section
6162 if ($realfile !~ m@\binclude/uapi/@ &&
6163 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
6164 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6165 my $new = substr($old, 1, -1);
6166 if (WARN("PREFER_SECTION",
6167 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6168 $fix) {
6169 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6170 }
6171 }
6172
Joe Perches5f14d3b2012-01-10 15:09:52 -08006173# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08006174 if ($realfile !~ m@\binclude/uapi/@ &&
6175 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006176 if (WARN("PREFER_PRINTF",
6177 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6178 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006179 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006180
6181 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08006182 }
6183
Joe Perches6061d942012-03-23 15:02:16 -07006184# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08006185 if ($realfile !~ m@\binclude/uapi/@ &&
6186 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006187 if (WARN("PREFER_SCANF",
6188 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6189 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006190 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006191 }
Joe Perches6061d942012-03-23 15:02:16 -07006192 }
6193
Joe Perches619a9082014-12-10 15:51:35 -08006194# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006195 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006196 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6197 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6198 $line =~ /\b__weak\b/)) {
6199 ERROR("WEAK_DECLARATION",
6200 "Using weak declarations can have unintended link defects\n" . $herecurr);
6201 }
6202
Tomas Winklerfd39f902016-12-12 16:46:34 -08006203# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006204 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006205 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006206 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6207 my $type = $1;
6208 if ($type =~ /\b($typeC99Typedefs)\b/) {
6209 $type = $1;
6210 my $kernel_type = 'u';
6211 $kernel_type = 's' if ($type =~ /^_*[si]/);
6212 $type =~ /(\d+)/;
6213 $kernel_type .= $1;
6214 if (CHK("PREFER_KERNEL_TYPES",
6215 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6216 $fix) {
6217 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6218 }
6219 }
6220 }
6221
Joe Perches938224b2016-01-20 14:59:15 -08006222# check for cast of C90 native int or longer types constants
6223 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6224 my $cast = $1;
6225 my $const = $2;
6226 if (WARN("TYPECAST_INT_CONSTANT",
6227 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6228 $fix) {
6229 my $suffix = "";
6230 my $newconst = $const;
6231 $newconst =~ s/${Int_type}$//;
6232 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6233 if ($cast =~ /\blong\s+long\b/) {
6234 $suffix .= 'LL';
6235 } elsif ($cast =~ /\blong\b/) {
6236 $suffix .= 'L';
6237 }
6238 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6239 }
6240 }
6241
Joe Perches8f53a9b2010-03-05 13:43:48 -08006242# check for sizeof(&)
6243 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006244 WARN("SIZEOF_ADDRESS",
6245 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006246 }
6247
Joe Perches66c80b62012-07-30 14:41:22 -07006248# check for sizeof without parenthesis
6249 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006250 if (WARN("SIZEOF_PARENTHESIS",
6251 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6252 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006253 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006254 }
Joe Perches66c80b62012-07-30 14:41:22 -07006255 }
6256
Joe Perches88982fe2012-12-17 16:02:00 -08006257# check for struct spinlock declarations
6258 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6259 WARN("USE_SPINLOCK_T",
6260 "struct spinlock should be spinlock_t\n" . $herecurr);
6261 }
6262
Joe Perchesa6962d72013-04-29 16:18:13 -07006263# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006264 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006265 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006266 $fmt =~ s/%%//g;
6267 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006268 if (WARN("PREFER_SEQ_PUTS",
6269 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6270 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006271 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006272 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006273 }
6274 }
6275
Joe Perches478b1792018-04-10 16:33:34 -07006276# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006277 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006278 defined $stat &&
6279 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6280 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006281 my $stat_real;
6282
Joe Perches0b523762017-05-08 15:55:36 -07006283 my $lc = $stat =~ tr@\n@@;
6284 $lc = $lc + $linenr;
6285 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006286 my $specifier;
6287 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006288 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006289 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006290 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6291 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006292
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006293 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006294 $specifier = $1;
6295 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006296 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006297 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006298 ($extension eq "f" &&
6299 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006300 $bad_specifier = $specifier;
6301 last;
6302 }
6303 if ($extension eq "x" && !defined($stat_real)) {
6304 if (!defined($stat_real)) {
6305 $stat_real = get_stat_real($linenr, $lc);
6306 }
6307 WARN("VSPRINTF_SPECIFIER_PX",
6308 "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");
6309 }
6310 }
6311 if ($bad_specifier ne "") {
6312 my $stat_real = get_stat_real($linenr, $lc);
6313 my $ext_type = "Invalid";
6314 my $use = "";
6315 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006316 $use = " - use %pS instead";
6317 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6318 }
6319
6320 WARN("VSPRINTF_POINTER_EXTENSION",
6321 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6322 }
Joe Perches0b523762017-05-08 15:55:36 -07006323 }
6324 }
6325
Andy Whitcroft554e1652012-01-10 15:09:57 -08006326# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006327 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006328 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006329 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006330
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006331 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006332 my $ms_val = $7;
6333 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006334
Andy Whitcroft554e1652012-01-10 15:09:57 -08006335 if ($ms_size =~ /^(0x|)0$/i) {
6336 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006337 "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 -08006338 } elsif ($ms_size =~ /^(0x|)1$/i) {
6339 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006340 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6341 }
6342 }
6343
Joe Perches98a9bba2014-01-23 15:54:52 -08006344# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006345# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006346# defined $stat &&
6347# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6348# if (WARN("PREFER_ETHER_ADDR_COPY",
6349# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6350# $fix) {
6351# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6352# }
6353# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006354
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006355# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006356# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006357# defined $stat &&
6358# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6359# WARN("PREFER_ETHER_ADDR_EQUAL",
6360# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6361# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006362
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006363# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6364# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006365# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006366# defined $stat &&
6367# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6368#
6369# my $ms_val = $7;
6370#
6371# if ($ms_val =~ /^(?:0x|)0+$/i) {
6372# if (WARN("PREFER_ETH_ZERO_ADDR",
6373# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6374# $fix) {
6375# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6376# }
6377# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6378# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6379# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6380# $fix) {
6381# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6382# }
6383# }
6384# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006385
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006386# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006387 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006388 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006389 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006390 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006391 my $call = $1;
6392 my $cast1 = deparenthesize($2);
6393 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006394 my $cast2 = deparenthesize($7);
6395 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006396 my $cast;
6397
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006398 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006399 $cast = "$cast1 or $cast2";
6400 } elsif ($cast1 ne "") {
6401 $cast = $cast1;
6402 } else {
6403 $cast = $cast2;
6404 }
6405 WARN("MINMAX",
6406 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006407 }
6408 }
6409
Joe Perches4a273192012-07-30 14:41:20 -07006410# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006411 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006412 defined $stat &&
6413 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6414 my $min = $1;
6415 my $max = $7;
6416 if ($min eq $max) {
6417 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006418 "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 -07006419 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6420 $min > $max) {
6421 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006422 "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 -07006423 }
6424 }
6425
Joe Perches823b7942013-11-12 15:10:15 -08006426# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006427 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006428 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006429 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006430 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6431 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6432 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6433 my $lc = $stat =~ tr@\n@@;
6434 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006435 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006436 WARN("NAKED_SSCANF",
6437 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6438 }
6439
Joe Perchesafc819a2014-06-04 16:12:08 -07006440# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006441 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006442 defined $stat &&
6443 $line =~ /\bsscanf\b/) {
6444 my $lc = $stat =~ tr@\n@@;
6445 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006446 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006447 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6448 my $format = $6;
6449 my $count = $format =~ tr@%@%@;
6450 if ($count == 1 &&
6451 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6452 WARN("SSCANF_TO_KSTRTO",
6453 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6454 }
6455 }
6456 }
6457
Joe Perches70dc8a42013-09-11 14:23:58 -07006458# check for new externs in .h files.
6459 if ($realfile =~ /\.h$/ &&
6460 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006461 if (CHK("AVOID_EXTERNS",
6462 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006463 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006464 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006465 }
6466 }
6467
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006468# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006469 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006470 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006471 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006472 my $function_name = $1;
6473 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006474
6475 my $s = $stat;
6476 if (defined $cond) {
6477 substr($s, 0, length($cond), '');
6478 }
Kees Cookd8b44b52020-06-03 14:18:09 -07006479 if ($s =~ /^\s*;/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006480 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006481 WARN("AVOID_EXTERNS",
6482 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006483 }
6484
6485 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006486 WARN("FUNCTION_ARGUMENTS",
6487 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006488 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006489
6490 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6491 $stat =~ /^.\s*extern\s+/)
6492 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006493 WARN("AVOID_EXTERNS",
6494 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006495 }
6496
Joe Perchesa0ad7592017-07-10 15:52:19 -07006497# check for function declarations that have arguments without identifier names
6498 if (defined $stat &&
Kees Cookd8b44b52020-06-03 14:18:09 -07006499 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6500 $1 ne "void") {
6501 my $args = trim($1);
Joe Perchesca0d8922016-10-11 13:52:16 -07006502 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6503 my $arg = trim($1);
Kees Cookd8b44b52020-06-03 14:18:09 -07006504 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006505 WARN("FUNCTION_ARGUMENTS",
6506 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6507 }
6508 }
6509 }
6510
Joe Perchesa0ad7592017-07-10 15:52:19 -07006511# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006512 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006513 defined $stat &&
6514 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6515 $context_function = $1;
6516
6517# check for multiline function definition with misplaced open brace
6518 my $ok = 0;
6519 my $cnt = statement_rawlines($stat);
6520 my $herectx = $here . "\n";
6521 for (my $n = 0; $n < $cnt; $n++) {
6522 my $rl = raw_line($linenr, $n);
6523 $herectx .= $rl . "\n";
6524 $ok = 1 if ($rl =~ /^[ \+]\{/);
6525 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6526 last if $rl =~ /^[ \+].*\{/;
6527 }
6528 if (!$ok) {
6529 ERROR("OPEN_BRACE",
6530 "open brace '{' following function definitions go on the next line\n" . $herectx);
6531 }
6532 }
6533
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006534# checks for new __setup's
6535 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6536 my $name = $1;
6537
6538 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006539 CHK("UNDOCUMENTED_SETUP",
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07006540 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006541 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006542 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006543
Joe Perchese29a70f2019-03-07 16:28:35 -08006544# check for pointless casting of alloc functions
6545 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006546 WARN("UNNECESSARY_CASTS",
6547 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006548 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006549
Joe Perchesa640d252013-07-03 15:05:21 -07006550# alloc style
6551# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006552 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006553 $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 -07006554 CHK("ALLOC_SIZEOF_STRUCT",
6555 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6556 }
6557
Joe Perches60a55362014-06-04 16:12:07 -07006558# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006559 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006560 defined $stat &&
6561 $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 -07006562 my $oldfunc = $3;
6563 my $a1 = $4;
6564 my $a2 = $10;
6565 my $newfunc = "kmalloc_array";
6566 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006567 my $r1 = $a1;
6568 my $r2 = $a2;
6569 if ($a1 =~ /^sizeof\s*\S/) {
6570 $r1 = $a2;
6571 $r2 = $a1;
6572 }
6573 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6574 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006575 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006576 my $herectx = get_stat_here($linenr, $cnt, $here);
6577
Joe Perches60a55362014-06-04 16:12:07 -07006578 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006579 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6580 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006581 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006582 $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 -07006583 }
6584 }
6585 }
6586
Joe Perches972fdea2013-04-29 16:18:12 -07006587# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006588 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006589 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6590 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006591 WARN("KREALLOC_ARG_REUSE",
6592 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6593 }
6594
Joe Perches5ce59ae2013-02-21 16:44:18 -08006595# check for alloc argument mismatch
6596 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6597 WARN("ALLOC_ARRAY_ARGS",
6598 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6599 }
6600
Joe Perchescaf2a542011-01-12 16:59:56 -08006601# check for multiple semicolons
6602 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006603 if (WARN("ONE_SEMICOLON",
6604 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6605 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006606 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006607 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006608 }
6609
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006610# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6611 if ($realfile !~ m@^include/uapi/@ &&
6612 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006613 my $ull = "";
6614 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6615 if (CHK("BIT_MACRO",
6616 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6617 $fix) {
6618 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6619 }
6620 }
6621
Joe Perches50161262020-08-11 18:35:07 -07006622# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006623 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
Joe Perches50161262020-08-11 18:35:07 -07006624 WARN("IS_ENABLED_CONFIG",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006625 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
Joe Perches50161262020-08-11 18:35:07 -07006626 }
6627
Joe Perches2d632742016-05-20 17:04:00 -07006628# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006629 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 -07006630 my $config = $1;
6631 if (WARN("PREFER_IS_ENABLED",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006632 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
Joe Perches2d632742016-05-20 17:04:00 -07006633 $fix) {
6634 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6635 }
6636 }
6637
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006638# check for /* fallthrough */ like comment, prefer fallthrough;
6639 my @fallthroughs = (
6640 'fallthrough',
6641 '@fallthrough@',
6642 'lint -fallthrough[ \t]*',
6643 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6644 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6645 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6646 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6647 );
6648 if ($raw_comment ne '') {
6649 foreach my $ft (@fallthroughs) {
6650 if ($raw_comment =~ /$ft/) {
6651 my $msg_level = \&WARN;
6652 $msg_level = \&CHK if ($file);
6653 &{$msg_level}("PREFER_FALLTHROUGH",
6654 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6655 last;
6656 }
6657 }
6658 }
6659
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006660# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006661 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006662 defined $stat &&
6663 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006664 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006665 my $herectx = get_stat_here($linenr, $cnt, $here);
6666
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006667 WARN("DEFAULT_NO_BREAK",
6668 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006669 }
6670
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006671# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006672 if ($line =~ /\b__FUNCTION__\b/) {
6673 if (WARN("USE_FUNC",
6674 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6675 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006676 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006677 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006678 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006679
Joe Perches62ec8182015-02-13 14:38:18 -08006680# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6681 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6682 ERROR("DATE_TIME",
6683 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6684 }
6685
Joe Perches2c924882012-03-23 15:02:20 -07006686# check for use of yield()
6687 if ($line =~ /\byield\s*\(\s*\)/) {
6688 WARN("YIELD",
6689 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6690 }
6691
Joe Perches179f8f42013-07-03 15:05:30 -07006692# check for comparisons against true and false
6693 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6694 my $lead = $1;
6695 my $arg = $2;
6696 my $test = $3;
6697 my $otype = $4;
6698 my $trail = $5;
6699 my $op = "!";
6700
6701 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6702
6703 my $type = lc($otype);
6704 if ($type =~ /^(?:true|false)$/) {
6705 if (("$test" eq "==" && "$type" eq "true") ||
6706 ("$test" eq "!=" && "$type" eq "false")) {
6707 $op = "";
6708 }
6709
6710 CHK("BOOL_COMPARISON",
6711 "Using comparison to $otype is error prone\n" . $herecurr);
6712
6713## maybe suggesting a correct construct would better
6714## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6715
6716 }
6717 }
6718
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006719# check for semaphores initialized locked
6720 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006721 WARN("CONSIDER_COMPLETION",
6722 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006723 }
Joe Perches6712d852012-03-23 15:02:20 -07006724
Joe Perches67d0a072011-10-31 17:13:10 -07006725# recommend kstrto* over simple_strto* and strict_strto*
6726 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006727 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006728 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006729 }
Joe Perches6712d852012-03-23 15:02:20 -07006730
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006731# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006732 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006733 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006734 "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 -07006735 }
Joe Perches6712d852012-03-23 15:02:20 -07006736
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006737# check for spin_is_locked(), suggest lockdep instead
6738 if ($line =~ /\bspin_is_locked\(/) {
6739 WARN("USE_LOCKDEP",
6740 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6741 }
6742
Joe Perches9189c7e2018-09-07 15:26:18 -07006743# check for deprecated apis
6744 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6745 my $deprecated_api = $1;
6746 my $new_api = $deprecated_apis{$deprecated_api};
6747 WARN("DEPRECATED_API",
6748 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6749 }
6750
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006751# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006752# and avoid what seem like struct definitions 'struct foo {'
Quentin Monnetced69da12020-08-11 18:35:13 -07006753 if (defined($const_structs) &&
6754 $line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006755 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006756 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006757 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006758 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006759
6760# use of NR_CPUS is usually wrong
6761# ignore definitions of NR_CPUS and usage to define arrays as likely right
6762 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006763 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6764 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006765 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6766 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6767 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006768 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006769 WARN("NR_CPUS",
6770 "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 -07006771 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006772
Joe Perches52ea8502013-11-12 15:10:09 -08006773# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6774 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6775 ERROR("DEFINE_ARCH_HAS",
6776 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6777 }
6778
Joe Perchesacd93622015-02-13 14:38:38 -08006779# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006780 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006781 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6782 WARN("LIKELY_MISUSE",
6783 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6784 }
6785
Denis Efremovde3f1862019-09-25 16:49:25 -07006786# nested likely/unlikely calls
6787 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6788 WARN("LIKELY_MISUSE",
6789 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6790 }
6791
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006792# whine mightly about in_atomic
6793 if ($line =~ /\bin_atomic\s*\(/) {
6794 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006795 ERROR("IN_ATOMIC",
6796 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006797 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006798 WARN("IN_ATOMIC",
6799 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006800 }
6801 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006802
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006803# check for mutex_trylock_recursive usage
6804 if ($line =~ /mutex_trylock_recursive/) {
6805 ERROR("LOCKING",
6806 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6807 }
6808
Peter Zijlstra1704f472010-03-19 01:37:42 +01006809# check for lockdep_set_novalidate_class
6810 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6811 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6812 if ($realfile !~ m@^kernel/lockdep@ &&
6813 $realfile !~ m@^include/linux/lockdep@ &&
6814 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006815 ERROR("LOCKDEP",
6816 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006817 }
6818 }
Dave Jones88f88312011-01-12 16:59:59 -08006819
Joe Perchesb392c642015-04-16 12:44:16 -07006820 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6821 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006822 WARN("EXPORTED_WORLD_WRITABLE",
6823 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006824 }
Joe Perches24358802014-04-03 14:49:13 -07006825
Joe Perches00180462018-02-06 15:38:55 -08006826# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6827# and whether or not function naming is typical and if
6828# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006829 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006830 defined $stat &&
6831 $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*\)/) {
6832 my $var = $1;
6833 my $perms = $2;
6834 my $show = $3;
6835 my $store = $4;
6836 my $octal_perms = perms_to_octal($perms);
6837 if ($show =~ /^${var}_show$/ &&
6838 $store =~ /^${var}_store$/ &&
6839 $octal_perms eq "0644") {
6840 if (WARN("DEVICE_ATTR_RW",
6841 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6842 $fix) {
6843 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6844 }
6845 } elsif ($show =~ /^${var}_show$/ &&
6846 $store =~ /^NULL$/ &&
6847 $octal_perms eq "0444") {
6848 if (WARN("DEVICE_ATTR_RO",
6849 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6850 $fix) {
6851 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6852 }
6853 } elsif ($show =~ /^NULL$/ &&
6854 $store =~ /^${var}_store$/ &&
6855 $octal_perms eq "0200") {
6856 if (WARN("DEVICE_ATTR_WO",
6857 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6858 $fix) {
6859 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6860 }
6861 } elsif ($octal_perms eq "0644" ||
6862 $octal_perms eq "0444" ||
6863 $octal_perms eq "0200") {
6864 my $newshow = "$show";
6865 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6866 my $newstore = $store;
6867 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6868 my $rename = "";
6869 if ($show ne $newshow) {
6870 $rename .= " '$show' to '$newshow'";
6871 }
6872 if ($store ne $newstore) {
6873 $rename .= " '$store' to '$newstore'";
6874 }
6875 WARN("DEVICE_ATTR_FUNCTIONS",
6876 "Consider renaming function(s)$rename\n" . $herecurr);
6877 } else {
6878 WARN("DEVICE_ATTR_PERMS",
6879 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6880 }
6881 }
6882
Joe Perches515a2352014-04-03 14:49:24 -07006883# Mode permission misuses where it seems decimal should be octal
6884# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006885# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6886# specific definition of not visible in sysfs.
6887# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6888# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006889 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006890 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006891 $line =~ /$mode_perms_search/) {
6892 foreach my $entry (@mode_permission_funcs) {
6893 my $func = $entry->[0];
6894 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006895
Joe Perches459cf0a2016-10-11 13:52:19 -07006896 my $lc = $stat =~ tr@\n@@;
6897 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006898 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006899
Joe Perches515a2352014-04-03 14:49:24 -07006900 my $skip_args = "";
6901 if ($arg_pos > 1) {
6902 $arg_pos--;
6903 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6904 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006905 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006906 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006907 my $val = $1;
6908 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006909 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6910 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6911 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006912 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006913 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006914 }
6915 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006916 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006917 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006918 }
Joe Perches24358802014-04-03 14:49:13 -07006919 }
6920 }
6921 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006922
Joe Perches459cf0a2016-10-11 13:52:19 -07006923# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006924 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006925 my $oval = $1;
6926 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006927 if (WARN("SYMBOLIC_PERMS",
6928 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6929 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006930 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006931 }
6932 }
6933
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006934# validate content of MODULE_LICENSE against list from include/linux/module.h
6935 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6936 my $extracted_string = get_quoted_string($line, $rawline);
6937 my $valid_licenses = qr{
6938 GPL|
6939 GPL\ v2|
6940 GPL\ and\ additional\ rights|
6941 Dual\ BSD/GPL|
6942 Dual\ MIT/GPL|
6943 Dual\ MPL/GPL|
6944 Proprietary
6945 }x;
6946 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6947 WARN("MODULE_LICENSE",
6948 "unknown module license " . $extracted_string . "\n" . $herecurr);
6949 }
6950 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07006951
6952# check for sysctl duplicate constants
6953 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6954 WARN("DUPLICATED_SYSCTL_CONST",
6955 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6956 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006957 }
6958
6959 # If we have no input at all, then there is nothing to report on
6960 # so just keep quiet.
6961 if ($#rawlines == -1) {
6962 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006963 }
6964
Andy Whitcroft8905a672007-11-28 16:21:06 -08006965 # In mailback mode only produce a report in the negative, for
6966 # things that appear to be patches.
6967 if ($mailback && ($clean == 1 || !$is_patch)) {
6968 exit(0);
6969 }
6970
6971 # This is not a patch, and we are are in 'no-patch' mode so
6972 # just keep quiet.
6973 if (!$chk_patch && !$is_patch) {
6974 exit(0);
6975 }
6976
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006977 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006978 ERROR("NOT_UNIFIED_DIFF",
6979 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006980 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006981 if ($is_patch && $has_commit_log && $chk_signoff) {
6982 if ($signoff == 0) {
6983 ERROR("MISSING_SIGN_OFF",
6984 "Missing Signed-off-by: line(s)\n");
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07006985 } elsif ($authorsignoff != 1) {
6986 # authorsignoff values:
6987 # 0 -> missing sign off
6988 # 1 -> sign off identical
6989 # 2 -> names and addresses match, comments mismatch
6990 # 3 -> addresses match, names different
6991 # 4 -> names match, addresses different
6992 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
6993
6994 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
6995
6996 if ($authorsignoff == 0) {
6997 ERROR("NO_AUTHOR_SIGN_OFF",
6998 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6999 } elsif ($authorsignoff == 2) {
7000 CHK("FROM_SIGN_OFF_MISMATCH",
7001 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7002 } elsif ($authorsignoff == 3) {
7003 WARN("FROM_SIGN_OFF_MISMATCH",
7004 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7005 } elsif ($authorsignoff == 4) {
7006 WARN("FROM_SIGN_OFF_MISMATCH",
7007 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7008 } elsif ($authorsignoff == 5) {
7009 WARN("FROM_SIGN_OFF_MISMATCH",
7010 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7011 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007012 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007013 }
7014
Andy Whitcroft8905a672007-11-28 16:21:06 -08007015 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007016 if ($summary && !($clean == 1 && $quiet == 1)) {
7017 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08007018 print "total: $cnt_error errors, $cnt_warn warnings, " .
7019 (($check)? "$cnt_chk checks, " : "") .
7020 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07007021 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08007022
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007023 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07007024 # If there were any defects found and not already fixing them
7025 if (!$clean and !$fix) {
7026 print << "EOM"
7027
7028NOTE: For some of the reported defects, checkpatch may be able to
7029 mechanically convert to the typical style using --fix or --fix-inplace.
7030EOM
7031 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007032 # If there were whitespace errors which cleanpatch can fix
7033 # then suggest that.
7034 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07007035 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07007036 print << "EOM"
7037
7038NOTE: Whitespace errors detected.
7039 You may wish to use scripts/cleanpatch or scripts/cleanfile
7040EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007041 }
7042 }
7043
Joe Perchesd752fcc2014-08-06 16:11:05 -07007044 if ($clean == 0 && $fix &&
7045 ("@rawlines" ne "@fixed" ||
7046 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08007047 my $newfile = $filename;
7048 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07007049 my $linecount = 0;
7050 my $f;
7051
Joe Perchesd752fcc2014-08-06 16:11:05 -07007052 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7053
Joe Perches3705ce52013-07-03 15:05:31 -07007054 open($f, '>', $newfile)
7055 or die "$P: Can't open $newfile for write\n";
7056 foreach my $fixed_line (@fixed) {
7057 $linecount++;
7058 if ($file) {
7059 if ($linecount > 3) {
7060 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07007061 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07007062 }
7063 } else {
7064 print $f $fixed_line . "\n";
7065 }
7066 }
7067 close($f);
7068
7069 if (!$quiet) {
7070 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07007071
Joe Perches3705ce52013-07-03 15:05:31 -07007072Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
7073
7074Do _NOT_ trust the results written to this file.
7075Do _NOT_ submit these changes without inspecting them for correctness.
7076
7077This EXPERIMENTAL file is simply a convenience to help rewrite patches.
7078No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07007079EOM
7080 }
7081 }
7082
Joe Perchesd8469f12015-06-25 15:03:00 -07007083 if ($quiet == 0) {
7084 print "\n";
7085 if ($clean == 1) {
7086 print "$vname has no obvious style problems and is ready for submission.\n";
7087 } else {
7088 print "$vname has style problems, please review.\n";
7089 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007090 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007091 return $clean;
7092}