blob: 92949b8c5c7607e1a5a774566fcca91b157051c1 [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Joe Perches882ea1d2018-06-07 17:04:33 -07002# SPDX-License-Identifier: GPL-2.0
3#
Dave Jonesdbf004d2010-01-12 16:59:52 -05004# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07005# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08006# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07007# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Joe Perches882ea1d2018-06-07 17:04:33 -07008# (c) 2010-2018 Joe Perches <joe@perches.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07009
10use strict;
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020011use warnings;
Joe Perchesc707a812013-07-08 16:00:43 -070012use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080013use File::Basename;
14use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070015use Term::ANSIColor qw(:constants);
Geert Uytterhoevencd261492018-08-21 21:57:40 -070016use Encode qw(decode encode);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
18my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080019my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070020
Joe Perches000d1cc12011-07-25 17:13:25 -070021my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070022
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
26my $tree = 1;
27my $chk_signoff = 1;
28my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070029my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080031my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070032my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070034my $git = 0;
Joe Perches0dea9f1e2016-05-20 17:04:19 -070035my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070037my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080038my $summary = 1;
39my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080040my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070041my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070042my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070043my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080044my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070045my $root;
Joe Perches0f7f6352020-10-24 16:59:04 -070046my $gitroot = $ENV{'GIT_DIR'};
47$gitroot = ".git" if !defined($gitroot);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080048my %debug;
Joe Perches34456862013-07-03 15:05:34 -070049my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070050my %use_type = ();
51my @use = ();
52my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070053my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070054my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070055my $configuration_file = ".checkpatch.conf";
Joe Perchesbdc48fa2020-05-29 16:12:21 -070056my $max_line_length = 100;
Dave Hansend62a2012013-09-11 14:23:56 -070057my $ignore_perl_version = 0;
58my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070059my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070060my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070061my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070062my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070063my $conststructsfile = "$D/const_structs.checkpatch";
Quentin Monnetced69da12020-08-11 18:35:13 -070064my $typedefsfile;
John Brooks737c0762017-07-10 15:52:24 -070065my $color = "auto";
Vadim Bendebury98005e82019-03-07 16:28:38 -080066my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
Joe Perchesdbbf8692019-09-25 16:46:52 -070067# git output parsing needs US English output, so first set backtick child process LANGUAGE
68my $git_command ='export LANGUAGE=en_US.UTF-8; git';
Antonio Borneo713a09d2020-04-06 20:11:07 -070069my $tabsize = 8;
Jerome Forissier3e89ad82020-10-15 20:11:49 -070070my ${CONFIG_} = "CONFIG_";
Hannes Eder77f5b102009-09-21 17:04:37 -070071
72sub help {
73 my ($exitcode) = @_;
74
75 print << "EOM";
76Usage: $P [OPTION]... [FILE]...
77Version: $V
78
79Options:
80 -q, --quiet quiet
81 --no-tree run without a kernel tree
82 --no-signoff do not check for 'Signed-off-by' line
83 --patch treat FILE as patchfile (default)
84 --emacs emacs compile window format
85 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070086 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070087 -g, --git treat FILE as a single commit or git revision range
88 single git commit with:
89 <rev>
90 <rev>^
91 <rev>~n
92 multiple git commits with:
93 <rev1>..<rev2>
94 <rev1>...<rev2>
95 <rev>-<count>
96 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070097 -f, --file treat FILE as regular source file
98 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070099 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -0700100 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -0700101 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -0700102 --show-types show the specific message type in the output
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700103 --max-line-length=n set the maximum line length, (default $max_line_length)
104 if exceeded, warn on patches
105 requires --strict for use with --file
Vadim Bendebury56193272014-10-13 15:51:48 -0700106 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700107 --tab-size=n set the number of spaces for tab (default $tabsize)
Hannes Eder77f5b102009-09-21 17:04:37 -0700108 --root=PATH PATH to the kernel tree root
109 --no-summary suppress the per-file summary
110 --mailback only produce a report in case of warnings/errors
111 --summary-file include the filename in summary
112 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
113 'values', 'possible', 'type', and 'attr' (default
114 is all off)
115 --test-only=WORD report only warnings/errors containing WORD
116 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700117 --fix EXPERIMENTAL - may create horrible results
118 If correctable single-line errors exist, create
119 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
120 with potential errors corrected to the preferred
121 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800122 --fix-inplace EXPERIMENTAL - may create horrible results
123 Is the same as --fix, but overwrites the input
124 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700125 --ignore-perl-version override checking of perl version. expect
126 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700127 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700128 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700129 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700130 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700131 --color[=WHEN] Use colors 'always', 'never', or only when output
132 is a terminal ('auto'). Default is 'auto'.
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700133 --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default
134 ${CONFIG_})
Hannes Eder77f5b102009-09-21 17:04:37 -0700135 -h, --help, --version display this help and exit
136
137When FILE is - read standard input.
138EOM
139
140 exit($exitcode);
141}
142
Joe Perches3beb42e2016-05-20 17:04:14 -0700143sub uniq {
144 my %seen;
145 return grep { !$seen{$_}++ } @_;
146}
147
148sub list_types {
149 my ($exitcode) = @_;
150
151 my $count = 0;
152
153 local $/ = undef;
154
155 open(my $script, '<', abs_path($P)) or
156 die "$P: Can't read '$P' $!\n";
157
158 my $text = <$script>;
159 close($script);
160
161 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700162 # Also catch when type or level is passed through a variable
163 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700164 push (@types, $_);
165 }
166 @types = sort(uniq(@types));
167 print("#\tMessage type\n\n");
168 foreach my $type (@types) {
169 print(++$count . "\t" . $type . "\n");
170 }
171
172 exit($exitcode);
173}
174
Joe Perches000d1cc12011-07-25 17:13:25 -0700175my $conf = which_conf($configuration_file);
176if (-f $conf) {
177 my @conf_args;
178 open(my $conffile, '<', "$conf")
179 or warn "$P: Can't find a readable $configuration_file file $!\n";
180
181 while (<$conffile>) {
182 my $line = $_;
183
184 $line =~ s/\s*\n?$//g;
185 $line =~ s/^\s*//g;
186 $line =~ s/\s+/ /g;
187
188 next if ($line =~ m/^\s*#/);
189 next if ($line =~ m/^\s*$/);
190
191 my @words = split(" ", $line);
192 foreach my $word (@words) {
193 last if ($word =~ m/^#/);
194 push (@conf_args, $word);
195 }
196 }
197 close($conffile);
198 unshift(@ARGV, @conf_args) if @conf_args;
199}
200
John Brooks737c0762017-07-10 15:52:24 -0700201# Perl's Getopt::Long allows options to take optional arguments after a space.
202# Prevent --color by itself from consuming other arguments
203foreach (@ARGV) {
204 if ($_ eq "--color" || $_ eq "-color") {
205 $_ = "--color=$color";
206 }
207}
208
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700209GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700210 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700211 'tree!' => \$tree,
212 'signoff!' => \$chk_signoff,
213 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700214 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800215 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700216 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700217 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700218 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700219 'subjective!' => \$check,
220 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700221 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700222 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700223 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700224 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800225 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700226 'min-conf-desc-length=i' => \$min_conf_desc_length,
Antonio Borneo713a09d2020-04-06 20:11:07 -0700227 'tab-size=i' => \$tabsize,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700228 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800229 'summary!' => \$summary,
230 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800231 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700232 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800233 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700234 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800235 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700236 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700237 'codespell!' => \$codespell,
238 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700239 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700240 'color=s' => \$color,
241 'no-color' => \$color, #keep old behaviors of -nocolor
242 'nocolor' => \$color, #keep old behaviors of -nocolor
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700243 'kconfig-prefix=s' => \${CONFIG_},
Hannes Eder77f5b102009-09-21 17:04:37 -0700244 'h|help' => \$help,
245 'version' => \$help
246) or help(1);
247
248help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700249
Joe Perches3beb42e2016-05-20 17:04:14 -0700250list_types(0) if ($list_types);
251
Joe Perches9624b8d2014-01-23 15:54:44 -0800252$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700253$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800254
Joe Perches32f30ca2020-06-04 16:50:40 -0700255die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
256
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700257my $exit = 0;
258
Joe Perches5b579802018-08-21 21:57:33 -0700259my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700260if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700261 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700262 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700263 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700264}
265
Allen Hubbe45107ff2016-08-02 14:04:47 -0700266#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700267if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700268 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700269}
270
John Brooks737c0762017-07-10 15:52:24 -0700271if ($color =~ /^[01]$/) {
272 $color = !$color;
273} elsif ($color =~ /^always$/i) {
274 $color = 1;
275} elsif ($color =~ /^never$/i) {
276 $color = 0;
277} elsif ($color =~ /^auto$/i) {
278 $color = (-t STDOUT);
279} else {
Joe Perches32f30ca2020-06-04 16:50:40 -0700280 die "$P: Invalid color mode: $color\n";
John Brooks737c0762017-07-10 15:52:24 -0700281}
282
Antonio Borneo713a09d2020-04-06 20:11:07 -0700283# skip TAB size 1 to avoid additional checks on $tabsize - 1
Joe Perches32f30ca2020-06-04 16:50:40 -0700284die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
Antonio Borneo713a09d2020-04-06 20:11:07 -0700285
Joe Perches91bfe482013-09-11 14:23:59 -0700286sub hash_save_array_words {
287 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700288
Joe Perches91bfe482013-09-11 14:23:59 -0700289 my @array = split(/,/, join(',', @$arrayRef));
290 foreach my $word (@array) {
291 $word =~ s/\s*\n?$//g;
292 $word =~ s/^\s*//g;
293 $word =~ s/\s+/ /g;
294 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700295
Joe Perches91bfe482013-09-11 14:23:59 -0700296 next if ($word =~ m/^\s*#/);
297 next if ($word =~ m/^\s*$/);
298
299 $hashRef->{$word}++;
300 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700301}
302
Joe Perches91bfe482013-09-11 14:23:59 -0700303sub hash_show_words {
304 my ($hashRef, $prefix) = @_;
305
Joe Perches3c816e42015-06-25 15:03:29 -0700306 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700307 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700308 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700309 print " $word";
310 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700311 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700312 }
313}
314
315hash_save_array_words(\%ignore_type, \@ignore);
316hash_save_array_words(\%use_type, \@use);
317
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800318my $dbg_values = 0;
319my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700320my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700321my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800322for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800323 ## no critic
324 eval "\${dbg_$key} = '$debug{$key}';";
325 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800326}
327
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700328my $rpt_cleaners = 0;
329
Andy Whitcroft8905a672007-11-28 16:21:06 -0800330if ($terse) {
331 $emacs = 1;
332 $quiet++;
333}
334
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700335if ($tree) {
336 if (defined $root) {
337 if (!top_of_kernel_tree($root)) {
338 die "$P: $root: --root does not point at a valid tree\n";
339 }
340 } else {
341 if (top_of_kernel_tree('.')) {
342 $root = '.';
343 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
344 top_of_kernel_tree($1)) {
345 $root = $1;
346 }
347 }
348
349 if (!defined $root) {
350 print "Must be run from the top-level dir. of a kernel tree\n";
351 exit(2);
352 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700353}
354
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700355my $emitted_corrupt = 0;
356
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700357our $Ident = qr{
358 [A-Za-z_][A-Za-z\d_]*
359 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
360 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700361our $Storage = qr{extern|static|asmlinkage};
362our $Sparse = qr{
363 __user|
364 __kernel|
365 __force|
366 __iomem|
367 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800368 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700369 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700370 __refconst|
371 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800372 __rcu|
373 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700374 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800375our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
376our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
377our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
378our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
379our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700380
Wolfram Sang52131292010-03-05 13:43:51 -0800381# Notes to $Attribute:
382# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700383our $Attribute = qr{
384 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700385 __percpu|
386 __nocast|
387 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200388 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700389 __packed__|
390 __packed2__|
391 __naked|
392 __maybe_unused|
393 __always_unused|
394 __noreturn|
395 __used|
396 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800397 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700398 __noclone|
399 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700400 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700401 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700402 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700403 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700404 ____cacheline_aligned|
405 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800406 ____cacheline_internodealigned_in_smp|
407 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700408 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700409our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700410our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700411our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
412our $Lval = qr{$Ident(?:$Member)*};
413
Joe Perches95e2c602013-07-03 15:05:20 -0700414our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
415our $Binary = qr{(?i)0b[01]+$Int_type?};
416our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
417our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700418our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800419our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800420our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
421our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
422our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800423our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700424our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800425our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700426our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700427our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700428our $Operators = qr{
429 <=|>=|==|!=|
430 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700431 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700432 }x;
433
Joe Perches91cb5192014-04-03 14:49:32 -0700434our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
435
Joe Perchesab7e23f2015-04-16 12:44:22 -0700436our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800437our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700438our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700439our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800440our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700441our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800442our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700443our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800444
Joe Perches15662b32011-10-31 17:13:12 -0700445our $NON_ASCII_UTF8 = qr{
446 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700447 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
448 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
449 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
450 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
451 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
452 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
453}x;
454
Joe Perches15662b32011-10-31 17:13:12 -0700455our $UTF8 = qr{
456 [\x09\x0A\x0D\x20-\x7E] # ASCII
457 | $NON_ASCII_UTF8
458}x;
459
Joe Perchese6176fa2015-06-25 15:02:49 -0700460our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800461our $typeOtherOSTypedefs = qr{(?x:
462 u_(?:char|short|int|long) | # bsd
463 u(?:nchar|short|int|long) # sysv
464)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700465our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700466 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700467 atomic_t
468)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700469our $typeTypedefs = qr{(?x:
470 $typeC99Typedefs\b|
471 $typeOtherOSTypedefs\b|
472 $typeKernelTypedefs\b
473)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700474
Joe Perches6d32f7a2015-11-06 16:31:37 -0800475our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
476
Joe Perches691e6692010-03-05 13:43:51 -0800477our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800478 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700479 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches87bd4992017-11-17 15:28:48 -0800480 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700481 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700482 panic|
Joe Perches06668722013-11-12 15:10:07 -0800483 MODULE_[A-Z_]+|
484 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800485)};
486
Joe Perchese29a70f2019-03-07 16:28:35 -0800487our $allocFunctions = qr{(?x:
488 (?:(?:devm_)?
489 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
490 kstrdup(?:_const)? |
491 kmemdup(?:_nul)?) |
Christophe JAILLET461e1562020-04-20 18:13:58 -0700492 (?:\w+)?alloc_skb(?:_ip_align)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800493 # dev_alloc_skb/netdev_alloc_skb, et al
494 dma_alloc_coherent
495)};
496
Joe Perches20112472011-07-25 17:13:23 -0700497our $signature_tags = qr{(?xi:
498 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800499 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700500 Acked-by:|
501 Tested-by:|
502 Reviewed-by:|
503 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700504 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700505 To:|
506 Cc:
507)};
508
Joe Perches18130872014-08-06 16:11:22 -0700509our @typeListMisordered = (
510 qr{char\s+(?:un)?signed},
511 qr{int\s+(?:(?:un)?signed\s+)?short\s},
512 qr{int\s+short(?:\s+(?:un)?signed)},
513 qr{short\s+int(?:\s+(?:un)?signed)},
514 qr{(?:un)?signed\s+int\s+short},
515 qr{short\s+(?:un)?signed},
516 qr{long\s+int\s+(?:un)?signed},
517 qr{int\s+long\s+(?:un)?signed},
518 qr{long\s+(?:un)?signed\s+int},
519 qr{int\s+(?:un)?signed\s+long},
520 qr{int\s+(?:un)?signed},
521 qr{int\s+long\s+long\s+(?:un)?signed},
522 qr{long\s+long\s+int\s+(?:un)?signed},
523 qr{long\s+long\s+(?:un)?signed\s+int},
524 qr{long\s+long\s+(?:un)?signed},
525 qr{long\s+(?:un)?signed},
526);
527
Andy Whitcroft8905a672007-11-28 16:21:06 -0800528our @typeList = (
529 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700530 qr{(?:(?:un)?signed\s+)?char},
531 qr{(?:(?:un)?signed\s+)?short\s+int},
532 qr{(?:(?:un)?signed\s+)?short},
533 qr{(?:(?:un)?signed\s+)?int},
534 qr{(?:(?:un)?signed\s+)?long\s+int},
535 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
536 qr{(?:(?:un)?signed\s+)?long\s+long},
537 qr{(?:(?:un)?signed\s+)?long},
538 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800539 qr{float},
540 qr{double},
541 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800542 qr{struct\s+$Ident},
543 qr{union\s+$Ident},
544 qr{enum\s+$Ident},
545 qr{${Ident}_t},
546 qr{${Ident}_handler},
547 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700548 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800549);
Joe Perches938224b2016-01-20 14:59:15 -0800550
551our $C90_int_types = qr{(?x:
552 long\s+long\s+int\s+(?:un)?signed|
553 long\s+long\s+(?:un)?signed\s+int|
554 long\s+long\s+(?:un)?signed|
555 (?:(?:un)?signed\s+)?long\s+long\s+int|
556 (?:(?:un)?signed\s+)?long\s+long|
557 int\s+long\s+long\s+(?:un)?signed|
558 int\s+(?:(?:un)?signed\s+)?long\s+long|
559
560 long\s+int\s+(?:un)?signed|
561 long\s+(?:un)?signed\s+int|
562 long\s+(?:un)?signed|
563 (?:(?:un)?signed\s+)?long\s+int|
564 (?:(?:un)?signed\s+)?long|
565 int\s+long\s+(?:un)?signed|
566 int\s+(?:(?:un)?signed\s+)?long|
567
568 int\s+(?:un)?signed|
569 (?:(?:un)?signed\s+)?int
570)};
571
Alex Dowad485ff232015-06-25 15:02:52 -0700572our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700573our @typeListWithAttr = (
574 @typeList,
575 qr{struct\s+$InitAttribute\s+$Ident},
576 qr{union\s+$InitAttribute\s+$Ident},
577);
578
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700579our @modifierList = (
580 qr{fastcall},
581);
Alex Dowad485ff232015-06-25 15:02:52 -0700582our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800583
Joe Perches24358802014-04-03 14:49:13 -0700584our @mode_permission_funcs = (
585 ["module_param", 3],
586 ["module_param_(?:array|named|string)", 4],
587 ["module_param_array_named", 5],
588 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
589 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700590 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
591 ["IIO_DEV_ATTR_[A-Z_]+", 1],
592 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
593 ["SENSOR_TEMPLATE(?:_2|)", 3],
594 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700595);
596
Joe Perches1a3dcf22020-08-11 18:35:16 -0700597my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
598
Joe Perches515a2352014-04-03 14:49:24 -0700599#Create a search pattern for all these functions to speed up a loop below
600our $mode_perms_search = "";
601foreach my $entry (@mode_permission_funcs) {
602 $mode_perms_search .= '|' if ($mode_perms_search ne "");
603 $mode_perms_search .= $entry->[0];
604}
Joe Perches00180462018-02-06 15:38:55 -0800605$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700606
Joe Perches9189c7e2018-09-07 15:26:18 -0700607our %deprecated_apis = (
608 "synchronize_rcu_bh" => "synchronize_rcu",
609 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
610 "call_rcu_bh" => "call_rcu",
611 "rcu_barrier_bh" => "rcu_barrier",
612 "synchronize_sched" => "synchronize_rcu",
613 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
614 "call_rcu_sched" => "call_rcu",
615 "rcu_barrier_sched" => "rcu_barrier",
616 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
617 "cond_synchronize_sched" => "cond_synchronize_rcu",
618);
619
620#Create a search pattern for all these strings to speed up a loop below
621our $deprecated_apis_search = "";
622foreach my $entry (keys %deprecated_apis) {
623 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
624 $deprecated_apis_search .= $entry;
625}
626$deprecated_apis_search = "(?:${deprecated_apis_search})";
627
Joe Perchesb392c642015-04-16 12:44:16 -0700628our $mode_perms_world_writable = qr{
629 S_IWUGO |
630 S_IWOTH |
631 S_IRWXUGO |
632 S_IALLUGO |
633 0[0-7][0-7][2367]
634}x;
635
Joe Perchesf90774e2016-10-11 13:51:47 -0700636our %mode_permission_string_types = (
637 "S_IRWXU" => 0700,
638 "S_IRUSR" => 0400,
639 "S_IWUSR" => 0200,
640 "S_IXUSR" => 0100,
641 "S_IRWXG" => 0070,
642 "S_IRGRP" => 0040,
643 "S_IWGRP" => 0020,
644 "S_IXGRP" => 0010,
645 "S_IRWXO" => 0007,
646 "S_IROTH" => 0004,
647 "S_IWOTH" => 0002,
648 "S_IXOTH" => 0001,
649 "S_IRWXUGO" => 0777,
650 "S_IRUGO" => 0444,
651 "S_IWUGO" => 0222,
652 "S_IXUGO" => 0111,
653);
654
655#Create a search pattern for all these strings to speed up a loop below
656our $mode_perms_string_search = "";
657foreach my $entry (keys %mode_permission_string_types) {
658 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
659 $mode_perms_string_search .= $entry;
660}
Joe Perches00180462018-02-06 15:38:55 -0800661our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
662our $multi_mode_perms_string_search = qr{
663 ${single_mode_perms_string_search}
664 (?:\s*\|\s*${single_mode_perms_string_search})*
665}x;
666
667sub perms_to_octal {
668 my ($string) = @_;
669
670 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
671
672 my $val = "";
673 my $oval = "";
674 my $to = 0;
675 my $curpos = 0;
676 my $lastpos = 0;
677 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
678 $curpos = pos($string);
679 my $match = $2;
680 my $omatch = $1;
681 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
682 $lastpos = $curpos;
683 $to |= $mode_permission_string_types{$match};
684 $val .= '\s*\|\s*' if ($val ne "");
685 $val .= $match;
686 $oval .= $omatch;
687 }
688 $oval =~ s/^\s*\|\s*//;
689 $oval =~ s/\s*\|\s*$//;
690 return sprintf("%04o", $to);
691}
Joe Perchesf90774e2016-10-11 13:51:47 -0700692
Wolfram Sang7840a942010-08-09 17:20:57 -0700693our $allowed_asm_includes = qr{(?x:
694 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700695 memory|
696 time|
697 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700698)};
699# memory.h: ARM has a custom one
700
Kees Cook66b47b42014-10-13 15:51:57 -0700701# Load common spelling mistakes and build regular expression list.
702my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700703my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700704
Joe Perches36061e32014-12-10 15:51:43 -0800705if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800706 while (<$spelling>) {
707 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700708
Joe Perches36061e32014-12-10 15:51:43 -0800709 $line =~ s/\s*\n?$//g;
710 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700711
Joe Perches36061e32014-12-10 15:51:43 -0800712 next if ($line =~ m/^\s*#/);
713 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700714
Joe Perches36061e32014-12-10 15:51:43 -0800715 my ($suspect, $fix) = split(/\|\|/, $line);
716
Joe Perches36061e32014-12-10 15:51:43 -0800717 $spelling_fix{$suspect} = $fix;
718 }
719 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800720} else {
721 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700722}
Kees Cook66b47b42014-10-13 15:51:57 -0700723
Joe Perchesebfd7d62015-04-16 12:44:14 -0700724if ($codespell) {
725 if (open(my $spelling, '<', $codespellfile)) {
726 while (<$spelling>) {
727 my $line = $_;
728
729 $line =~ s/\s*\n?$//g;
730 $line =~ s/^\s*//g;
731
732 next if ($line =~ m/^\s*#/);
733 next if ($line =~ m/^\s*$/);
734 next if ($line =~ m/, disabled/i);
735
736 $line =~ s/,.*$//;
737
738 my ($suspect, $fix) = split(/->/, $line);
739
740 $spelling_fix{$suspect} = $fix;
741 }
742 close($spelling);
743 } else {
744 warn "No codespell typos will be found - file '$codespellfile': $!\n";
745 }
746}
747
748$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
749
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700750sub read_words {
751 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700752
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700753 if (open(my $words, '<', $file)) {
754 while (<$words>) {
755 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700756
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700757 $line =~ s/\s*\n?$//g;
758 $line =~ s/^\s*//g;
759
760 next if ($line =~ m/^\s*#/);
761 next if ($line =~ m/^\s*$/);
762 if ($line =~ /\s/) {
763 print("$file: '$line' invalid - ignored\n");
764 next;
765 }
766
Quentin Monnetced69da12020-08-11 18:35:13 -0700767 $$wordsRef .= '|' if (defined $$wordsRef);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700768 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700769 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700770 close($file);
771 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700772 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700773
774 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700775}
776
Quentin Monnetced69da12020-08-11 18:35:13 -0700777my $const_structs;
778if (show_type("CONST_STRUCT")) {
779 read_words(\$const_structs, $conststructsfile)
780 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
781}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700782
Quentin Monnetced69da12020-08-11 18:35:13 -0700783if (defined($typedefsfile)) {
784 my $typeOtherTypedefs;
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700785 read_words(\$typeOtherTypedefs, $typedefsfile)
786 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
Quentin Monnetced69da12020-08-11 18:35:13 -0700787 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700788}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700789
Andy Whitcroft8905a672007-11-28 16:21:06 -0800790sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700791 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
792 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700793 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700794 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700795 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700796 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700797 (?:$typeTypedefs\b)|
798 (?:${all}\b)
799 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800800 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700801 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800802 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800803 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700804 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700805 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800806 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700807 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800808 }x;
Joe Perches18130872014-08-06 16:11:22 -0700809 $NonptrTypeMisordered = qr{
810 (?:$Modifier\s+|const\s+)*
811 (?:
812 (?:${Misordered}\b)
813 )
814 (?:\s+$Modifier|\s+const)*
815 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700816 $NonptrTypeWithAttr = qr{
817 (?:$Modifier\s+|const\s+)*
818 (?:
819 (?:typeof|__typeof__)\s*\([^\)]*\)|
820 (?:$typeTypedefs\b)|
821 (?:${allWithAttr}\b)
822 )
823 (?:\s+$Modifier|\s+const)*
824 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800825 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700826 $NonptrType
Antonio Borneo7b184962020-04-06 20:11:04 -0700827 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700828 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800829 }x;
Joe Perches18130872014-08-06 16:11:22 -0700830 $TypeMisordered = qr{
831 $NonptrTypeMisordered
Antonio Borneo7b184962020-04-06 20:11:04 -0700832 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Perches18130872014-08-06 16:11:22 -0700833 (?:\s+$Inline|\s+$Modifier)*
834 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700835 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700836 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800837}
838build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700839
Joe Perches7d2367a2011-07-25 17:13:22 -0700840our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700841
842# Using $balanced_parens, $LvalOrFunc, or $FuncArg
843# requires at least perl version v5.10.0
844# Any use must be runtime checked with $^V
845
846our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700847our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800848our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700849
Joe Perchesf8422302014-08-06 16:11:31 -0700850our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700851 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700852 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700853 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700854)};
855
Aditya Srivastava8d0325c2020-12-15 20:44:24 -0800856our %allow_repeated_words = (
857 add => '',
858 added => '',
859 bad => '',
860 be => '',
861);
862
Joe Perches7d2367a2011-07-25 17:13:22 -0700863sub deparenthesize {
864 my ($string) = @_;
865 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700866
867 while ($string =~ /^\s*\(.*\)\s*$/) {
868 $string =~ s@^\s*\(\s*@@;
869 $string =~ s@\s*\)\s*$@@;
870 }
871
Joe Perches7d2367a2011-07-25 17:13:22 -0700872 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700873
Joe Perches7d2367a2011-07-25 17:13:22 -0700874 return $string;
875}
876
Joe Perches34456862013-07-03 15:05:34 -0700877sub seed_camelcase_file {
878 my ($file) = @_;
879
880 return if (!(-f $file));
881
882 local $/;
883
884 open(my $include_file, '<', "$file")
885 or warn "$P: Can't read '$file' $!\n";
886 my $text = <$include_file>;
887 close($include_file);
888
889 my @lines = split('\n', $text);
890
891 foreach my $line (@lines) {
892 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
893 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
894 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800895 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
896 $camelcase{$1} = 1;
897 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
Joe Perches34456862013-07-03 15:05:34 -0700898 $camelcase{$1} = 1;
899 }
900 }
901}
902
Joe Perchescd28b112019-12-04 16:52:09 -0800903our %maintained_status = ();
904
Joe Perches85b0ee12016-10-11 13:51:44 -0700905sub is_maintained_obsolete {
906 my ($filename) = @_;
907
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800908 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700909
Joe Perchescd28b112019-12-04 16:52:09 -0800910 if (!exists($maintained_status{$filename})) {
911 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
912 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700913
Joe Perchescd28b112019-12-04 16:52:09 -0800914 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700915}
916
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700917sub is_SPDX_License_valid {
918 my ($license) = @_;
919
Joe Perches0f7f6352020-10-24 16:59:04 -0700920 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700921
Joe Perches56294112018-08-21 21:58:04 -0700922 my $root_path = abs_path($root);
923 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700924 return 0 if ($status ne "");
925 return 1;
926}
927
Joe Perches34456862013-07-03 15:05:34 -0700928my $camelcase_seeded = 0;
929sub seed_camelcase_includes {
930 return if ($camelcase_seeded);
931
932 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700933 my $camelcase_cache = "";
934 my @include_files = ();
935
936 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700937
Joe Perches0f7f6352020-10-24 16:59:04 -0700938 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700939 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -0700940 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700941 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700942 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700943 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700944 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700945 @include_files = split('\n', $files);
946 foreach my $file (@include_files) {
947 my $date = POSIX::strftime("%Y%m%d%H%M",
948 localtime((stat $file)[9]));
949 $last_mod_date = $date if ($last_mod_date < $date);
950 }
951 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700952 }
Joe Perchesc707a812013-07-08 16:00:43 -0700953
954 if ($camelcase_cache ne "" && -f $camelcase_cache) {
955 open(my $camelcase_file, '<', "$camelcase_cache")
956 or warn "$P: Can't read '$camelcase_cache' $!\n";
957 while (<$camelcase_file>) {
958 chomp;
959 $camelcase{$_} = 1;
960 }
961 close($camelcase_file);
962
963 return;
964 }
965
Joe Perches0f7f6352020-10-24 16:59:04 -0700966 if (-e "$gitroot") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700967 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700968 @include_files = split('\n', $files);
969 }
970
Joe Perches34456862013-07-03 15:05:34 -0700971 foreach my $file (@include_files) {
972 seed_camelcase_file($file);
973 }
Joe Perches351b2a12013-07-03 15:05:36 -0700974
Joe Perchesc707a812013-07-08 16:00:43 -0700975 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700976 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700977 open(my $camelcase_file, '>', "$camelcase_cache")
978 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700979 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
980 print $camelcase_file ("$_\n");
981 }
982 close($camelcase_file);
983 }
Joe Perches34456862013-07-03 15:05:34 -0700984}
985
Joe Perchesf5f61322020-10-15 20:12:12 -0700986sub git_is_single_file {
987 my ($filename) = @_;
988
989 return 0 if ((which("git") eq "") || !(-e "$gitroot"));
990
991 my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
992 my $count = $output =~ tr/\n//;
993 return $count eq 1 && $output =~ m{^${filename}$};
994}
995
Joe Perchesd311cd42014-08-06 16:10:57 -0700996sub git_commit_info {
997 my ($commit, $id, $desc) = @_;
998
Joe Perches0f7f6352020-10-24 16:59:04 -0700999 return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
Joe Perchesd311cd42014-08-06 16:10:57 -07001000
Joe Perchesdbbf8692019-09-25 16:46:52 -07001001 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -07001002 $output =~ s/^\s*//gm;
1003 my @lines = split("\n", $output);
1004
Joe Perches0d7835f2015-02-13 14:38:35 -08001005 return ($id, $desc) if ($#lines < 0);
1006
Sean Christopherson5a7f4452019-09-25 16:46:49 -07001007 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -07001008# Maybe one day convert this block of bash into something that returns
1009# all matching commit ids, but it's very slow...
1010#
1011# echo "checking commits $1..."
1012# git rev-list --remotes | grep -i "^$1" |
1013# while read line ; do
1014# git log --format='%H %s' -1 $line |
1015# echo "commit $(cut -c 1-12,41-)"
1016# done
1017 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07001018 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -07001019 } else {
1020 $id = substr($lines[0], 0, 12);
1021 $desc = substr($lines[0], 41);
1022 }
1023
1024 return ($id, $desc);
1025}
1026
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001027$chk_signoff = 0 if ($file);
1028
Andy Whitcroft00df3442007-06-08 13:47:06 -07001029my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001030my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001031my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001032my @fixed_inserted = ();
1033my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001034my $fixlinenr = -1;
1035
Du, Changbin4a593c32016-05-20 17:04:16 -07001036# If input is git commits, extract all commits from the commit expressions.
1037# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
Joe Perches0f7f6352020-10-24 16:59:04 -07001038die "$P: No git repository found\n" if ($git && !-e "$gitroot");
Du, Changbin4a593c32016-05-20 17:04:16 -07001039
1040if ($git) {
1041 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001042 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001043 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001044 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1045 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001046 } elsif ($commit_expr =~ m/\.\./) {
1047 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001048 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001049 $git_range = "-1 $commit_expr";
1050 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001051 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001052 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001053 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1054 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001055 my $sha1 = $1;
1056 my $subject = $2;
1057 unshift(@commits, $sha1);
1058 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001059 }
1060 }
1061 die "$P: no git commits after extraction!\n" if (@commits == 0);
1062 @ARGV = @commits;
1063}
1064
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001065my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001066$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001067for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001068 my $FILE;
Joe Perchesf5f61322020-10-15 20:12:12 -07001069 my $is_git_file = git_is_single_file($filename);
1070 my $oldfile = $file;
1071 $file = 1 if ($is_git_file);
Du, Changbin4a593c32016-05-20 17:04:16 -07001072 if ($git) {
1073 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1074 die "$P: $filename: git format-patch failed - $!\n";
1075 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001076 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001077 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001078 } elsif ($filename eq '-') {
1079 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001080 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001081 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001082 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001083 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001084 if ($filename eq '-') {
1085 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001086 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001087 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001088 } else {
1089 $vname = $filename;
1090 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001091 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001092 chomp;
1093 push(@rawlines, $_);
Geert Uytterhoevenc7f574d2020-06-04 16:50:43 -07001094 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001095 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001096 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001097
1098 if ($#ARGV > 0 && $quiet == 0) {
1099 print '-' x length($vname) . "\n";
1100 print "$vname\n";
1101 print '-' x length($vname) . "\n";
1102 }
1103
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001104 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001105 $exit = 1;
1106 }
1107 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001108 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001109 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001110 @fixed_inserted = ();
1111 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001112 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001113 @modifierListFile = ();
1114 @typeListFile = ();
1115 build_types();
Joe Perchesf5f61322020-10-15 20:12:12 -07001116 $file = $oldfile if ($is_git_file);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001117}
1118
Joe Perchesd8469f12015-06-25 15:03:00 -07001119if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001120 hash_show_words(\%use_type, "Used");
1121 hash_show_words(\%ignore_type, "Ignored");
1122
Joe Perches5b579802018-08-21 21:57:33 -07001123 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001124 print << "EOM"
1125
1126NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001127 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001128EOM
1129 }
1130 if ($exit) {
1131 print << "EOM"
1132
1133NOTE: If any of the errors are false positives, please report
1134 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1135EOM
1136 }
1137}
1138
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001139exit($exit);
1140
1141sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001142 my ($root) = @_;
1143
1144 my @tree_check = (
1145 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1146 "README", "Documentation", "arch", "include", "drivers",
1147 "fs", "init", "ipc", "kernel", "lib", "scripts",
1148 );
1149
1150 foreach my $check (@tree_check) {
1151 if (! -e $root . '/' . $check) {
1152 return 0;
1153 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001154 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001155 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001156}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001157
Joe Perches20112472011-07-25 17:13:23 -07001158sub parse_email {
1159 my ($formatted_email) = @_;
1160
1161 my $name = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001162 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001163 my $address = "";
1164 my $comment = "";
1165
1166 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1167 $name = $1;
1168 $address = $2;
1169 $comment = $3 if defined $3;
1170 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1171 $address = $1;
1172 $comment = $2 if defined $2;
1173 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1174 $address = $1;
1175 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001176 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001177 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001178 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001179 $name =~ s/^\"|\"$//g;
1180 # If there's a name left after stripping spaces and
1181 # leading quotes, and the address doesn't have both
1182 # leading and trailing angle brackets, the address
1183 # is invalid. ie:
1184 # "joe smith joe@smith.com" bad
1185 # "joe smith <joe@smith.com" bad
1186 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1187 $name = "";
1188 $address = "";
1189 $comment = "";
1190 }
1191 }
1192
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001193 $comment = trim($comment);
Joe Perches3705ce52013-07-03 15:05:31 -07001194 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001195 $name =~ s/^\"|\"$//g;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001196 if ($name =~ s/(\s*\([^\)]+\))\s*//) {
Joe Perchesdfa05c22020-04-06 20:10:48 -07001197 $name_comment = trim($1);
1198 }
Joe Perches3705ce52013-07-03 15:05:31 -07001199 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001200 $address =~ s/^\<|\>$//g;
1201
1202 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1203 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1204 $name = "\"$name\"";
1205 }
1206
Joe Perchesdfa05c22020-04-06 20:10:48 -07001207 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001208}
1209
1210sub format_email {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001211 my ($name, $name_comment, $address, $comment) = @_;
Joe Perches20112472011-07-25 17:13:23 -07001212
1213 my $formatted_email;
1214
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001215 $name_comment = trim($name_comment);
1216 $comment = trim($comment);
Joe Perches3705ce52013-07-03 15:05:31 -07001217 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001218 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001219 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001220
1221 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1222 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1223 $name = "\"$name\"";
1224 }
1225
1226 if ("$name" eq "") {
1227 $formatted_email = "$address";
1228 } else {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001229 $formatted_email = "$name$name_comment <$address>";
Joe Perches20112472011-07-25 17:13:23 -07001230 }
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001231 $formatted_email .= "$comment";
Joe Perches20112472011-07-25 17:13:23 -07001232 return $formatted_email;
1233}
1234
Joe Perchesdfa05c22020-04-06 20:10:48 -07001235sub reformat_email {
1236 my ($email) = @_;
1237
1238 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001239 return format_email($email_name, $name_comment, $email_address, $comment);
Joe Perchesdfa05c22020-04-06 20:10:48 -07001240}
1241
1242sub same_email_addresses {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001243 my ($email1, $email2, $match_comment) = @_;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001244
1245 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1246 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1247
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001248 if ($match_comment != 1) {
1249 return $email1_name eq $email2_name &&
1250 $email1_address eq $email2_address;
1251 }
Joe Perchesdfa05c22020-04-06 20:10:48 -07001252 return $email1_name eq $email2_name &&
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001253 $email1_address eq $email2_address &&
1254 $name1_comment eq $name2_comment &&
1255 $comment1 eq $comment2;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001256}
1257
Joe Perchesd311cd42014-08-06 16:10:57 -07001258sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001259 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001260
Joe Perchesbd474ca2014-08-06 16:11:10 -07001261 foreach my $path (split(/:/, $ENV{PATH})) {
1262 if (-e "$path/$bin") {
1263 return "$path/$bin";
1264 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001265 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001266
Joe Perchesbd474ca2014-08-06 16:11:10 -07001267 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001268}
1269
Joe Perches000d1cc12011-07-25 17:13:25 -07001270sub which_conf {
1271 my ($conf) = @_;
1272
1273 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1274 if (-e "$path/$conf") {
1275 return "$path/$conf";
1276 }
1277 }
1278
1279 return "";
1280}
1281
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001282sub expand_tabs {
1283 my ($str) = @_;
1284
1285 my $res = '';
1286 my $n = 0;
1287 for my $c (split(//, $str)) {
1288 if ($c eq "\t") {
1289 $res .= ' ';
1290 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001291 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001292 $res .= ' ';
1293 }
1294 next;
1295 }
1296 $res .= $c;
1297 $n++;
1298 }
1299
1300 return $res;
1301}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001302sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001303 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001304 return $res;
1305}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001306
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001307sub line_stats {
1308 my ($line) = @_;
1309
1310 # Drop the diff line leader and expand tabs
1311 $line =~ s/^.//;
1312 $line = expand_tabs($line);
1313
1314 # Pick the indent from the front of the line.
1315 my ($white) = ($line =~ /^(\s*)/);
1316
1317 return (length($line), length($white));
1318}
1319
Andy Whitcroft773647a2008-03-28 14:15:58 -07001320my $sanitise_quote = '';
1321
1322sub sanitise_line_reset {
1323 my ($in_comment) = @_;
1324
1325 if ($in_comment) {
1326 $sanitise_quote = '*/';
1327 } else {
1328 $sanitise_quote = '';
1329 }
1330}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001331sub sanitise_line {
1332 my ($line) = @_;
1333
1334 my $res = '';
1335 my $l = '';
1336
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001337 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001338 my $off = 0;
1339 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001340
Andy Whitcroft773647a2008-03-28 14:15:58 -07001341 # Always copy over the diff marker.
1342 $res = substr($line, 0, 1);
1343
1344 for ($off = 1; $off < length($line); $off++) {
1345 $c = substr($line, $off, 1);
1346
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001347 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001348 # and end, all to $;.
1349 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1350 $sanitise_quote = '*/';
1351
1352 substr($res, $off, 2, "$;$;");
1353 $off++;
1354 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001355 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001356 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001357 $sanitise_quote = '';
1358 substr($res, $off, 2, "$;$;");
1359 $off++;
1360 next;
1361 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001362 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1363 $sanitise_quote = '//';
1364
1365 substr($res, $off, 2, $sanitise_quote);
1366 $off++;
1367 next;
1368 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001369
1370 # A \ in a string means ignore the next character.
1371 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1372 $c eq "\\") {
1373 substr($res, $off, 2, 'XX');
1374 $off++;
1375 next;
1376 }
1377 # Regular quotes.
1378 if ($c eq "'" || $c eq '"') {
1379 if ($sanitise_quote eq '') {
1380 $sanitise_quote = $c;
1381
1382 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001383 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001384 } elsif ($sanitise_quote eq $c) {
1385 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001386 }
1387 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001388
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001389 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001390 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1391 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001392 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1393 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001394 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1395 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001396 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001397 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001398 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001399 }
1400
Daniel Walker113f04a2009-09-21 17:04:35 -07001401 if ($sanitise_quote eq '//') {
1402 $sanitise_quote = '';
1403 }
1404
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001405 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001406 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001407 my $clean = 'X' x length($1);
1408 $res =~ s@\<.*\>@<$clean>@;
1409
1410 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001411 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001412 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001413 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001414 }
1415
Joe Perchesdadf6802016-08-02 14:04:33 -07001416 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1417 my $match = $1;
1418 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1419 }
1420
Andy Whitcroft00df3442007-06-08 13:47:06 -07001421 return $res;
1422}
1423
Joe Perchesa6962d72013-04-29 16:18:13 -07001424sub get_quoted_string {
1425 my ($line, $rawline) = @_;
1426
Joe Perches478b1792018-04-10 16:33:34 -07001427 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001428 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001429 return substr($rawline, $-[0], $+[0] - $-[0]);
1430}
1431
Andy Whitcroft8905a672007-11-28 16:21:06 -08001432sub ctx_statement_block {
1433 my ($linenr, $remain, $off) = @_;
1434 my $line = $linenr - 1;
1435 my $blk = '';
1436 my $soff = $off;
1437 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001438 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001439
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001440 my $loff = 0;
1441
Andy Whitcroft8905a672007-11-28 16:21:06 -08001442 my $type = '';
1443 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001444 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001445 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001446 my $c;
1447 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001448
1449 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001450 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001451 @stack = (['', 0]) if ($#stack == -1);
1452
Andy Whitcroft773647a2008-03-28 14:15:58 -07001453 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001454 # If we are about to drop off the end, pull in more
1455 # context.
1456 if ($off >= $len) {
1457 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001458 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001459 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001460 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001461 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001462 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001463 $len = length($blk);
1464 $line++;
1465 last;
1466 }
1467 # Bail if there is no further context.
1468 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001469 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001470 last;
1471 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001472 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1473 $level++;
1474 $type = '#';
1475 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001476 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001477 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001478 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001479 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001480
Andy Whitcroft773647a2008-03-28 14:15:58 -07001481 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001482
1483 # Handle nested #if/#else.
1484 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1485 push(@stack, [ $type, $level ]);
1486 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1487 ($type, $level) = @{$stack[$#stack - 1]};
1488 } elsif ($remainder =~ /^#\s*endif\b/) {
1489 ($type, $level) = @{pop(@stack)};
1490 }
1491
Andy Whitcroft8905a672007-11-28 16:21:06 -08001492 # Statement ends at the ';' or a close '}' at the
1493 # outermost level.
1494 if ($level == 0 && $c eq ';') {
1495 last;
1496 }
1497
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001498 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001499 if ($level == 0 && $coff_set == 0 &&
1500 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1501 $remainder =~ /^(else)(?:\s|{)/ &&
1502 $remainder !~ /^else\s+if\b/) {
1503 $coff = $off + length($1) - 1;
1504 $coff_set = 1;
1505 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1506 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001507 }
1508
Andy Whitcroft8905a672007-11-28 16:21:06 -08001509 if (($type eq '' || $type eq '(') && $c eq '(') {
1510 $level++;
1511 $type = '(';
1512 }
1513 if ($type eq '(' && $c eq ')') {
1514 $level--;
1515 $type = ($level != 0)? '(' : '';
1516
1517 if ($level == 0 && $coff < $soff) {
1518 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001519 $coff_set = 1;
1520 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001521 }
1522 }
1523 if (($type eq '' || $type eq '{') && $c eq '{') {
1524 $level++;
1525 $type = '{';
1526 }
1527 if ($type eq '{' && $c eq '}') {
1528 $level--;
1529 $type = ($level != 0)? '{' : '';
1530
1531 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001532 if (substr($blk, $off + 1, 1) eq ';') {
1533 $off++;
1534 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001535 last;
1536 }
1537 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001538 # Preprocessor commands end at the newline unless escaped.
1539 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1540 $level--;
1541 $type = '';
1542 $off++;
1543 last;
1544 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001545 $off++;
1546 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001547 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001548 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001549 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001550 $line++;
1551 $remain--;
1552 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001553
1554 my $statement = substr($blk, $soff, $off - $soff + 1);
1555 my $condition = substr($blk, $soff, $coff - $soff + 1);
1556
1557 #warn "STATEMENT<$statement>\n";
1558 #warn "CONDITION<$condition>\n";
1559
Andy Whitcroft773647a2008-03-28 14:15:58 -07001560 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001561
1562 return ($statement, $condition,
1563 $line, $remain + 1, $off - $loff + 1, $level);
1564}
1565
Andy Whitcroftcf655042008-03-04 14:28:20 -08001566sub statement_lines {
1567 my ($stmt) = @_;
1568
1569 # Strip the diff line prefixes and rip blank lines at start and end.
1570 $stmt =~ s/(^|\n)./$1/g;
1571 $stmt =~ s/^\s*//;
1572 $stmt =~ s/\s*$//;
1573
1574 my @stmt_lines = ($stmt =~ /\n/g);
1575
1576 return $#stmt_lines + 2;
1577}
1578
1579sub statement_rawlines {
1580 my ($stmt) = @_;
1581
1582 my @stmt_lines = ($stmt =~ /\n/g);
1583
1584 return $#stmt_lines + 2;
1585}
1586
1587sub statement_block_size {
1588 my ($stmt) = @_;
1589
1590 $stmt =~ s/(^|\n)./$1/g;
1591 $stmt =~ s/^\s*{//;
1592 $stmt =~ s/}\s*$//;
1593 $stmt =~ s/^\s*//;
1594 $stmt =~ s/\s*$//;
1595
1596 my @stmt_lines = ($stmt =~ /\n/g);
1597 my @stmt_statements = ($stmt =~ /;/g);
1598
1599 my $stmt_lines = $#stmt_lines + 2;
1600 my $stmt_statements = $#stmt_statements + 1;
1601
1602 if ($stmt_lines > $stmt_statements) {
1603 return $stmt_lines;
1604 } else {
1605 return $stmt_statements;
1606 }
1607}
1608
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001609sub ctx_statement_full {
1610 my ($linenr, $remain, $off) = @_;
1611 my ($statement, $condition, $level);
1612
1613 my (@chunks);
1614
Andy Whitcroftcf655042008-03-04 14:28:20 -08001615 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001616 ($statement, $condition, $linenr, $remain, $off, $level) =
1617 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001618 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001619 push(@chunks, [ $condition, $statement ]);
1620 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1621 return ($level, $linenr, @chunks);
1622 }
1623
1624 # Pull in the following conditional/block pairs and see if they
1625 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001626 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001627 ($statement, $condition, $linenr, $remain, $off, $level) =
1628 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001629 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001630 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001631 #print "C: push\n";
1632 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001633 }
1634
1635 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001636}
1637
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001638sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001639 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001640 my $line;
1641 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001642 my $blk = '';
1643 my @o;
1644 my @c;
1645 my @res = ();
1646
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001647 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001648 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001649 for ($line = $start; $remain > 0; $line++) {
1650 next if ($rawlines[$line] =~ /^-/);
1651 $remain--;
1652
1653 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001654
1655 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001656 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001657 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001658 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001659 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001660 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001661 $level = pop(@stack);
1662 }
1663
Andy Whitcroft01464f32010-10-26 14:23:19 -07001664 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001665 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1666 if ($off > 0) {
1667 $off--;
1668 next;
1669 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001670
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001671 if ($c eq $close && $level > 0) {
1672 $level--;
1673 last if ($level == 0);
1674 } elsif ($c eq $open) {
1675 $level++;
1676 }
1677 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001678
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001679 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001680 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001681 }
1682
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001683 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001684 }
1685
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001686 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001687}
1688sub ctx_block_outer {
1689 my ($linenr, $remain) = @_;
1690
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001691 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1692 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001693}
1694sub ctx_block {
1695 my ($linenr, $remain) = @_;
1696
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001697 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1698 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001699}
1700sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001701 my ($linenr, $remain, $off) = @_;
1702
1703 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1704 return @r;
1705}
1706sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001707 my ($linenr, $remain) = @_;
1708
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001709 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001710}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001711sub ctx_statement_level {
1712 my ($linenr, $remain, $off) = @_;
1713
1714 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1715}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001716
1717sub ctx_locate_comment {
1718 my ($first_line, $end_line) = @_;
1719
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001720 # If c99 comment on the current line, or the line before or after
1721 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1722 return $current_comment if (defined $current_comment);
1723 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1724 return $current_comment if (defined $current_comment);
1725 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1726 return $current_comment if (defined $current_comment);
1727
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001728 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001729 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001730 return $current_comment if (defined $current_comment);
1731
1732 # Look through the context and try and figure out if there is a
1733 # comment.
1734 my $in_comment = 0;
1735 $current_comment = '';
1736 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001737 my $line = $rawlines[$linenr - 1];
1738 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001739 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1740 $in_comment = 1;
1741 }
1742 if ($line =~ m@/\*@) {
1743 $in_comment = 1;
1744 }
1745 if (!$in_comment && $current_comment ne '') {
1746 $current_comment = '';
1747 }
1748 $current_comment .= $line . "\n" if ($in_comment);
1749 if ($line =~ m@\*/@) {
1750 $in_comment = 0;
1751 }
1752 }
1753
1754 chomp($current_comment);
1755 return($current_comment);
1756}
1757sub ctx_has_comment {
1758 my ($first_line, $end_line) = @_;
1759 my $cmt = ctx_locate_comment($first_line, $end_line);
1760
Andy Whitcroft00df3442007-06-08 13:47:06 -07001761 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001762 ##print "CMMT: $cmt\n";
1763
1764 return ($cmt ne '');
1765}
1766
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001767sub raw_line {
1768 my ($linenr, $cnt) = @_;
1769
1770 my $offset = $linenr - 1;
1771 $cnt++;
1772
1773 my $line;
1774 while ($cnt) {
1775 $line = $rawlines[$offset++];
1776 next if (defined($line) && $line =~ /^-/);
1777 $cnt--;
1778 }
1779
1780 return $line;
1781}
1782
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001783sub get_stat_real {
1784 my ($linenr, $lc) = @_;
1785
1786 my $stat_real = raw_line($linenr, 0);
1787 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1788 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1789 }
1790
1791 return $stat_real;
1792}
1793
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001794sub get_stat_here {
1795 my ($linenr, $cnt, $here) = @_;
1796
1797 my $herectx = $here . "\n";
1798 for (my $n = 0; $n < $cnt; $n++) {
1799 $herectx .= raw_line($linenr, $n) . "\n";
1800 }
1801
1802 return $herectx;
1803}
1804
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001805sub cat_vet {
1806 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001807 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001808
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001809 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001810 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1811 $res .= $1;
1812 if ($2 ne '') {
1813 $coded = sprintf("^%c", unpack('C', $2) + 64);
1814 $res .= $coded;
1815 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001816 }
1817 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001818
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001819 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001820}
1821
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001822my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001823my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001824my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001825my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001826
1827sub annotate_reset {
1828 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001829 $av_pending = '_';
1830 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001831 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001832}
1833
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001834sub annotate_values {
1835 my ($stream, $type) = @_;
1836
1837 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001838 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001839 my $cur = $stream;
1840
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001841 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001842
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001843 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001844 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001845 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001846 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001847 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001848 print "WS($1)\n" if ($dbg_values > 1);
1849 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001850 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001851 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001852 }
1853
Florian Micklerc023e4732011-01-12 16:59:58 -08001854 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001855 print "CAST($1)\n" if ($dbg_values > 1);
1856 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001857 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001858
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001859 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001860 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001861 $type = 'T';
1862
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001863 } elsif ($cur =~ /^($Modifier)\s*/) {
1864 print "MODIFIER($1)\n" if ($dbg_values > 1);
1865 $type = 'T';
1866
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001867 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001868 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001869 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001870 push(@av_paren_type, $type);
1871 if ($2 ne '') {
1872 $av_pending = 'N';
1873 }
1874 $type = 'E';
1875
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001876 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001877 print "UNDEF($1)\n" if ($dbg_values > 1);
1878 $av_preprocessor = 1;
1879 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001880
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001881 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001882 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001883 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001884
1885 push(@av_paren_type, $type);
1886 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001887 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001888
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001889 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001890 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1891 $av_preprocessor = 1;
1892
1893 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1894
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001895 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001896
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001897 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001898 print "PRE_END($1)\n" if ($dbg_values > 1);
1899
1900 $av_preprocessor = 1;
1901
1902 # Assume all arms of the conditional end as this
1903 # one does, and continue as if the #endif was not here.
1904 pop(@av_paren_type);
1905 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001906 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001907
1908 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001909 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001910
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001911 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1912 print "ATTR($1)\n" if ($dbg_values > 1);
1913 $av_pending = $type;
1914 $type = 'N';
1915
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001916 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001917 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001918 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001919 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001920 }
1921 $type = 'N';
1922
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001923 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001924 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001925 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001926 $type = 'N';
1927
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001928 } elsif ($cur =~/^(case)/o) {
1929 print "CASE($1)\n" if ($dbg_values > 1);
1930 $av_pend_colon = 'C';
1931 $type = 'N';
1932
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001933 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001934 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001935 $type = 'N';
1936
1937 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001938 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001939 push(@av_paren_type, $av_pending);
1940 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001941 $type = 'N';
1942
1943 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001944 my $new_type = pop(@av_paren_type);
1945 if ($new_type ne '_') {
1946 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001947 print "PAREN('$1') -> $type\n"
1948 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001949 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001950 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001951 }
1952
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001953 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001954 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001955 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001956 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001957
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001958 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1959 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001960 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001961 } elsif ($type eq 'E') {
1962 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001963 }
1964 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1965 $type = 'V';
1966
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001967 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001968 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001969 $type = 'V';
1970
1971 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001972 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001973 $type = 'N';
1974
Andy Whitcroftcf655042008-03-04 14:28:20 -08001975 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001976 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001977 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001978 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001979
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001980 } elsif ($cur =~/^(,)/) {
1981 print "COMMA($1)\n" if ($dbg_values > 1);
1982 $type = 'C';
1983
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001984 } elsif ($cur =~ /^(\?)/o) {
1985 print "QUESTION($1)\n" if ($dbg_values > 1);
1986 $type = 'N';
1987
1988 } elsif ($cur =~ /^(:)/o) {
1989 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1990
1991 substr($var, length($res), 1, $av_pend_colon);
1992 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1993 $type = 'E';
1994 } else {
1995 $type = 'N';
1996 }
1997 $av_pend_colon = 'O';
1998
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001999 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002000 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002001 $type = 'N';
2002
Andy Whitcroft0d413862008-10-15 22:02:16 -07002003 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002004 my $variant;
2005
2006 print "OPV($1)\n" if ($dbg_values > 1);
2007 if ($type eq 'V') {
2008 $variant = 'B';
2009 } else {
2010 $variant = 'U';
2011 }
2012
2013 substr($var, length($res), 1, $variant);
2014 $type = 'N';
2015
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002016 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002017 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002018 if ($1 ne '++' && $1 ne '--') {
2019 $type = 'N';
2020 }
2021
2022 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002023 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002024 }
2025 if (defined $1) {
2026 $cur = substr($cur, length($1));
2027 $res .= $type x length($1);
2028 }
2029 }
2030
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002031 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002032}
2033
Andy Whitcroft8905a672007-11-28 16:21:06 -08002034sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002035 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002036 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002037 ^(?:
2038 $Modifier|
2039 $Storage|
2040 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002041 DEFINE_\S+
2042 )$|
2043 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002044 goto|
2045 return|
2046 case|
2047 else|
2048 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002049 do|
2050 \#|
2051 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002052 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002053 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002054 )}x;
2055 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2056 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002057 # Check for modifiers.
2058 $possible =~ s/\s*$Storage\s*//g;
2059 $possible =~ s/\s*$Sparse\s*//g;
2060 if ($possible =~ /^\s*$/) {
2061
2062 } elsif ($possible =~ /\s/) {
2063 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002064 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002065 if ($modifier !~ $notPermitted) {
2066 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002067 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002068 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002069 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002070
2071 } else {
2072 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002073 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002074 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002075 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002076 } else {
2077 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002078 }
2079}
2080
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002081my $prefix = '';
2082
Joe Perches000d1cc12011-07-25 17:13:25 -07002083sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002084 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002085
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002086 $type =~ tr/[a-z]/[A-Z]/;
2087
Joe Perchescbec18a2014-04-03 14:49:19 -07002088 return defined $use_type{$type} if (scalar keys %use_type > 0);
2089
2090 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002091}
2092
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002093sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002094 my ($level, $type, $msg) = @_;
2095
2096 if (!show_type($type) ||
2097 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002098 return 0;
2099 }
Joe Perches57230292015-06-25 15:03:03 -07002100 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002101 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002102 if ($level eq 'ERROR') {
2103 $output .= RED;
2104 } elsif ($level eq 'WARNING') {
2105 $output .= YELLOW;
2106 } else {
2107 $output .= GREEN;
2108 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002109 }
Joe Perches57230292015-06-25 15:03:03 -07002110 $output .= $prefix . $level . ':';
2111 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002112 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002113 $output .= "$type:";
2114 }
John Brooks737c0762017-07-10 15:52:24 -07002115 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002116 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002117
2118 if ($showfile) {
2119 my @lines = split("\n", $output, -1);
2120 splice(@lines, 1, 1);
2121 $output = join("\n", @lines);
2122 }
Joe Perches57230292015-06-25 15:03:03 -07002123 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002124
Joe Perches57230292015-06-25 15:03:03 -07002125 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002126
2127 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002128}
Joe Perchescbec18a2014-04-03 14:49:19 -07002129
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002130sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002131 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002132}
Joe Perches000d1cc12011-07-25 17:13:25 -07002133
Joe Perchesd752fcc2014-08-06 16:11:05 -07002134sub fixup_current_range {
2135 my ($lineRef, $offset, $length) = @_;
2136
2137 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2138 my $o = $1;
2139 my $l = $2;
2140 my $no = $o + $offset;
2141 my $nl = $l + $length;
2142 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2143 }
2144}
2145
2146sub fix_inserted_deleted_lines {
2147 my ($linesRef, $insertedRef, $deletedRef) = @_;
2148
2149 my $range_last_linenr = 0;
2150 my $delta_offset = 0;
2151
2152 my $old_linenr = 0;
2153 my $new_linenr = 0;
2154
2155 my $next_insert = 0;
2156 my $next_delete = 0;
2157
2158 my @lines = ();
2159
2160 my $inserted = @{$insertedRef}[$next_insert++];
2161 my $deleted = @{$deletedRef}[$next_delete++];
2162
2163 foreach my $old_line (@{$linesRef}) {
2164 my $save_line = 1;
2165 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002166 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002167 $delta_offset = 0;
2168 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2169 $range_last_linenr = $new_linenr;
2170 fixup_current_range(\$line, $delta_offset, 0);
2171 }
2172
2173 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2174 $deleted = @{$deletedRef}[$next_delete++];
2175 $save_line = 0;
2176 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2177 }
2178
2179 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2180 push(@lines, ${$inserted}{'LINE'});
2181 $inserted = @{$insertedRef}[$next_insert++];
2182 $new_linenr++;
2183 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2184 }
2185
2186 if ($save_line) {
2187 push(@lines, $line);
2188 $new_linenr++;
2189 }
2190
2191 $old_linenr++;
2192 }
2193
2194 return @lines;
2195}
2196
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002197sub fix_insert_line {
2198 my ($linenr, $line) = @_;
2199
2200 my $inserted = {
2201 LINENR => $linenr,
2202 LINE => $line,
2203 };
2204 push(@fixed_inserted, $inserted);
2205}
2206
2207sub fix_delete_line {
2208 my ($linenr, $line) = @_;
2209
2210 my $deleted = {
2211 LINENR => $linenr,
2212 LINE => $line,
2213 };
2214
2215 push(@fixed_deleted, $deleted);
2216}
2217
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002218sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002219 my ($type, $msg) = @_;
2220
2221 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002222 our $clean = 0;
2223 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002224 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002225 }
Joe Perches3705ce52013-07-03 15:05:31 -07002226 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002227}
2228sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002229 my ($type, $msg) = @_;
2230
2231 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002232 our $clean = 0;
2233 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002234 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002235 }
Joe Perches3705ce52013-07-03 15:05:31 -07002236 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002237}
2238sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002239 my ($type, $msg) = @_;
2240
2241 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002242 our $clean = 0;
2243 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002244 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002245 }
Joe Perches3705ce52013-07-03 15:05:31 -07002246 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002247}
2248
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002249sub check_absolute_file {
2250 my ($absolute, $herecurr) = @_;
2251 my $file = $absolute;
2252
2253 ##print "absolute<$absolute>\n";
2254
2255 # See if any suffix of this path is a path within the tree.
2256 while ($file =~ s@^[^/]*/@@) {
2257 if (-f "$root/$file") {
2258 ##print "file<$file>\n";
2259 last;
2260 }
2261 }
2262 if (! -f _) {
2263 return 0;
2264 }
2265
2266 # It is, so see if the prefix is acceptable.
2267 my $prefix = $absolute;
2268 substr($prefix, -length($file)) = '';
2269
2270 ##print "prefix<$prefix>\n";
2271 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002272 WARN("USE_RELATIVE_PATH",
2273 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002274 }
2275}
2276
Joe Perches3705ce52013-07-03 15:05:31 -07002277sub trim {
2278 my ($string) = @_;
2279
Joe Perchesb34c6482013-09-11 14:24:01 -07002280 $string =~ s/^\s+|\s+$//g;
2281
2282 return $string;
2283}
2284
2285sub ltrim {
2286 my ($string) = @_;
2287
2288 $string =~ s/^\s+//;
2289
2290 return $string;
2291}
2292
2293sub rtrim {
2294 my ($string) = @_;
2295
2296 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002297
2298 return $string;
2299}
2300
Joe Perches52ea8502013-11-12 15:10:09 -08002301sub string_find_replace {
2302 my ($string, $find, $replace) = @_;
2303
2304 $string =~ s/$find/$replace/g;
2305
2306 return $string;
2307}
2308
Joe Perches3705ce52013-07-03 15:05:31 -07002309sub tabify {
2310 my ($leading) = @_;
2311
Antonio Borneo713a09d2020-04-06 20:11:07 -07002312 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002313 my $max_spaces_before_tab = $source_indent - 1;
2314 my $spaces_to_tab = " " x $source_indent;
2315
2316 #convert leading spaces to tabs
2317 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2318 #Remove spaces before a tab
2319 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2320
2321 return "$leading";
2322}
2323
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002324sub pos_last_openparen {
2325 my ($line) = @_;
2326
2327 my $pos = 0;
2328
2329 my $opens = $line =~ tr/\(/\(/;
2330 my $closes = $line =~ tr/\)/\)/;
2331
2332 my $last_openparen = 0;
2333
2334 if (($opens == 0) || ($closes >= $opens)) {
2335 return -1;
2336 }
2337
2338 my $len = length($line);
2339
2340 for ($pos = 0; $pos < $len; $pos++) {
2341 my $string = substr($line, $pos);
2342 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2343 $pos += length($1) - 1;
2344 } elsif (substr($line, $pos, 1) eq '(') {
2345 $last_openparen = $pos;
2346 } elsif (index($string, '(') == -1) {
2347 last;
2348 }
2349 }
2350
Joe Perches91cb5192014-04-03 14:49:32 -07002351 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002352}
2353
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002354sub get_raw_comment {
2355 my ($line, $rawline) = @_;
2356 my $comment = '';
2357
2358 for my $i (0 .. (length($line) - 1)) {
2359 if (substr($line, $i, 1) eq "$;") {
2360 $comment .= substr($rawline, $i, 1);
2361 }
2362 }
2363
2364 return $comment;
2365}
2366
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002367sub process {
2368 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002369
2370 my $linenr=0;
2371 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002372 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002373 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002374 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002375
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002376 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002377 my $indent;
2378 my $previndent=0;
2379 my $stashindent=0;
2380
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002381 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002382 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002383 my $author = '';
2384 my $authorsignoff = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002385 my $author_sob = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002386 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002387 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002388 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002389 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002390 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002391 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002392 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002393 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002394 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002395 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002396 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002397 my $non_utf8_charset = 0;
2398
Joe Perches365dd4e2014-08-06 16:10:42 -07002399 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002400 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002401
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002402 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002403 our $cnt_lines = 0;
2404 our $cnt_error = 0;
2405 our $cnt_warn = 0;
2406 our $cnt_chk = 0;
2407
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002408 # Trace the real file/line as we go.
2409 my $realfile = '';
2410 my $realline = 0;
2411 my $realcnt = 0;
2412 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002413 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002414 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002415 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002416 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002417 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002418
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002419 my $prev_values = 'E';
2420
2421 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002422 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002423 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002424 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002425 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002426
Joe Perches7e51f192013-09-11 14:23:57 -07002427 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002428
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002429 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002430 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002431 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002432 my @setup_docs = ();
2433 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002434
Joe Perchesd8b07712013-11-12 15:10:06 -08002435 my $camelcase_file_seeded = 0;
2436
Rob Herring9f3a8992018-04-10 16:33:13 -07002437 my $checklicenseline = 1;
2438
Andy Whitcroft773647a2008-03-28 14:15:58 -07002439 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002440 my $line;
2441 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002442 $linenr++;
2443 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002444
Joe Perches3705ce52013-07-03 15:05:31 -07002445 push(@fixed, $rawline) if ($fix);
2446
Andy Whitcroft773647a2008-03-28 14:15:58 -07002447 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002448 $setup_docs = 0;
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07002449 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002450 $setup_docs = 1;
2451 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002452 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002453 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002454 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002455 $realline=$1-1;
2456 if (defined $2) {
2457 $realcnt=$3+1;
2458 } else {
2459 $realcnt=1+1;
2460 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002461 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002462
2463 # Guestimate if this is a continuing comment. Run
2464 # the context looking for a comment "edge". If this
2465 # edge is a close comment then we must be in a comment
2466 # at context start.
2467 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002468 my $cnt = $realcnt;
2469 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2470 next if (defined $rawlines[$ln - 1] &&
2471 $rawlines[$ln - 1] =~ /^-/);
2472 $cnt--;
2473 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002474 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002475 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2476 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2477 ($edge) = $1;
2478 last;
2479 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002480 }
2481 if (defined $edge && $edge eq '*/') {
2482 $in_comment = 1;
2483 }
2484
2485 # Guestimate if this is a continuing comment. If this
2486 # is the start of a diff block and this line starts
2487 # ' *' then it is very likely a comment.
2488 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002489 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002490 {
2491 $in_comment = 1;
2492 }
2493
2494 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2495 sanitise_line_reset($in_comment);
2496
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002497 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002498 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002499 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002500 $line = sanitise_line($rawline);
2501 }
2502 push(@lines, $line);
2503
2504 if ($realcnt > 1) {
2505 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2506 } else {
2507 $realcnt = 0;
2508 }
2509
2510 #print "==>$rawline\n";
2511 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002512
2513 if ($setup_docs && $line =~ /^\+/) {
2514 push(@setup_docs, $line);
2515 }
2516 }
2517
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002518 $prefix = '';
2519
Andy Whitcroft773647a2008-03-28 14:15:58 -07002520 $realcnt = 0;
2521 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002522 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002523 foreach my $line (@lines) {
2524 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002525 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002526 my $sline = $line; #copy of $line
2527 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002528
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002529 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002530 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002531
Joe Perches12c253a2018-06-07 17:10:58 -07002532# check if it's a mode change, rename or start of a patch
2533 if (!$in_commit_log &&
2534 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2535 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2536 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2537 $is_patch = 1;
2538 }
2539
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002540#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002541 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002542 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2543 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002544 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002545 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002546 $realline=$1-1;
2547 if (defined $2) {
2548 $realcnt=$3+1;
2549 } else {
2550 $realcnt=1+1;
2551 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002552 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002553 $prev_values = 'E';
2554
Andy Whitcroft773647a2008-03-28 14:15:58 -07002555 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002556 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002557 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002558 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002559 if ($context =~ /\b(\w+)\s*\(/) {
2560 $context_function = $1;
2561 } else {
2562 undef $context_function;
2563 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002564 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002565
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002566# track the line number as we move through the hunk, note that
2567# new versions of GNU diff omit the leading space on completely
2568# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002569 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002570 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002571 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002572
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002573 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002574 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002575
2576 # Track the previous line.
2577 ($prevline, $stashline) = ($stashline, $line);
2578 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002579 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2580
Andy Whitcroft773647a2008-03-28 14:15:58 -07002581 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002582
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002583 } elsif ($realcnt == 1) {
2584 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002585 }
2586
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002587 my $hunk_line = ($realcnt != 0);
2588
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002589 $here = "#$linenr: " if (!$file);
2590 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002591
Joe Perches2ac73b42014-06-04 16:12:05 -07002592 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002593 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002594 if ($line =~ /^diff --git.*?(\S+)$/) {
2595 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002596 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002597 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002598 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002599 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002600 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002601 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002602 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002603
2604 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002605 if (!$file && $tree && $p1_prefix ne '' &&
2606 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002607 WARN("PATCH_PREFIX",
2608 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002609 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002610
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002611 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002612 ERROR("MODIFIED_INCLUDE_ASM",
2613 "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 -07002614 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002615 $found_file = 1;
2616 }
2617
Joe Perches34d88152015-06-25 15:03:05 -07002618#make up the handle for any error we report on this line
2619 if ($showfile) {
2620 $prefix = "$realfile:$realline: "
2621 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002622 if ($file) {
2623 $prefix = "$filename:$realline: ";
2624 } else {
2625 $prefix = "$filename:$linenr: ";
2626 }
Joe Perches34d88152015-06-25 15:03:05 -07002627 }
2628
Joe Perches2ac73b42014-06-04 16:12:05 -07002629 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002630 if (is_maintained_obsolete($realfile)) {
2631 WARN("OBSOLETE",
2632 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2633 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002634 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002635 $check = 1;
2636 } else {
2637 $check = $check_orig;
2638 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002639 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002640
2641 if ($realfile !~ /^MAINTAINERS/) {
2642 my $last_binding_patch = $is_binding_patch;
2643
2644 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2645
2646 if (($last_binding_patch != -1) &&
2647 ($last_binding_patch ^ $is_binding_patch)) {
2648 WARN("DT_SPLIT_BINDING_PATCH",
Mauro Carvalho Chehab858e6842020-04-15 16:45:25 +02002649 "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 -07002650 }
2651 }
2652
Andy Whitcroft773647a2008-03-28 14:15:58 -07002653 next;
2654 }
2655
Randy Dunlap389834b2007-06-08 13:47:03 -07002656 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002657
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002658 my $hereline = "$here\n$rawline\n";
2659 my $herecurr = "$here\n$rawline\n";
2660 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002661
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002662 $cnt_lines++ if ($realcnt != 0);
2663
Joe Perches490b2922018-08-21 21:58:01 -07002664# Verify the existence of a commit log if appropriate
2665# 2 is used because a $signature is counted in $commit_log_lines
2666 if ($in_commit_log) {
2667 if ($line !~ /^\s*$/) {
2668 $commit_log_lines++; #could be a $signature
2669 }
2670 } elsif ($has_commit_log && $commit_log_lines < 2) {
2671 WARN("COMMIT_MESSAGE",
2672 "Missing commit description - Add an appropriate one\n");
2673 $commit_log_lines = 2; #warn only once
2674 }
2675
Joe Perchese518e9a2015-06-25 15:03:27 -07002676# Check if the commit log has what seems like a diff which can confuse patch
2677 if ($in_commit_log && !$commit_log_has_diff &&
Mrinal Pandey13e45412020-09-04 16:35:52 -07002678 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2679 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
Joe Perchese518e9a2015-06-25 15:03:27 -07002680 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2681 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2682 ERROR("DIFF_IN_COMMIT_MSG",
2683 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2684 $commit_log_has_diff = 1;
2685 }
2686
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002687# Check for incorrect file permissions
2688 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2689 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002690 if ($realfile !~ m@scripts/@ &&
2691 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002692 ERROR("EXECUTE_PERMISSIONS",
2693 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002694 }
2695 }
2696
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002697# Check the patch for a From:
2698 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2699 $author = $1;
Dwaipayan Raye7f929f2020-10-15 20:12:15 -07002700 my $curline = $linenr;
2701 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2702 $author .= $1;
2703 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002704 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2705 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002706 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002707 }
2708
Joe Perches20112472011-07-25 17:13:23 -07002709# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002710 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002711 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002712 $in_commit_log = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002713 if ($author ne '' && $authorsignoff != 1) {
2714 if (same_email_addresses($1, $author, 1)) {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002715 $authorsignoff = 1;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002716 } else {
2717 my $ctx = $1;
2718 my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
2719 my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
2720
2721 if ($email_address eq $author_address && $email_name eq $author_name) {
2722 $author_sob = $ctx;
2723 $authorsignoff = 2;
2724 } elsif ($email_address eq $author_address) {
2725 $author_sob = $ctx;
2726 $authorsignoff = 3;
2727 } elsif ($email_name eq $author_name) {
2728 $author_sob = $ctx;
2729 $authorsignoff = 4;
2730
2731 my $address1 = $email_address;
2732 my $address2 = $author_address;
2733
2734 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
2735 $address1 = "$1$2";
2736 }
2737 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
2738 $address2 = "$1$2";
2739 }
2740 if ($address1 eq $address2) {
2741 $authorsignoff = 5;
2742 }
2743 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002744 }
2745 }
Joe Perches20112472011-07-25 17:13:23 -07002746 }
2747
Joe Perches44d303e2020-04-06 20:11:10 -07002748# Check for patch separator
2749 if ($line =~ /^---$/) {
2750 $has_patch_separator = 1;
2751 $in_commit_log = 0;
2752 }
2753
Joe Perchese0d975b2014-12-10 15:51:49 -08002754# Check if MAINTAINERS is being updated. If so, there's probably no need to
2755# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2756 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2757 $reported_maintainer_file = 1;
2758 }
2759
Joe Perches20112472011-07-25 17:13:23 -07002760# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002761 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002762 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002763 my $space_before = $1;
2764 my $sign_off = $2;
2765 my $space_after = $3;
2766 my $email = $4;
2767 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2768
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002769 if ($sign_off !~ /$signature_tags/) {
2770 WARN("BAD_SIGN_OFF",
2771 "Non-standard signature: $sign_off\n" . $herecurr);
2772 }
Joe Perches20112472011-07-25 17:13:23 -07002773 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002774 if (WARN("BAD_SIGN_OFF",
2775 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2776 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002777 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002778 "$ucfirst_sign_off $email";
2779 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002780 }
Joe Perches20112472011-07-25 17:13:23 -07002781 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002782 if (WARN("BAD_SIGN_OFF",
2783 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2784 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002785 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002786 "$ucfirst_sign_off $email";
2787 }
2788
Joe Perches20112472011-07-25 17:13:23 -07002789 }
2790 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002791 if (WARN("BAD_SIGN_OFF",
2792 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2793 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002794 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002795 "$ucfirst_sign_off $email";
2796 }
Joe Perches20112472011-07-25 17:13:23 -07002797 }
2798
Joe Perchesdfa05c22020-04-06 20:10:48 -07002799 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002800 my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
Joe Perches20112472011-07-25 17:13:23 -07002801 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002802 ERROR("BAD_SIGN_OFF",
2803 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002804 } else {
2805 my $dequoted = $suggested_email;
2806 $dequoted =~ s/^"//;
2807 $dequoted =~ s/" </ </;
2808 # Don't force email to have quotes
2809 # Allow just an angle bracketed address
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002810 if (!same_email_addresses($email, $suggested_email, 0)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002811 WARN("BAD_SIGN_OFF",
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002812 "email address '$email' might be better as '$suggested_email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002813 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002814 }
Joe Perches7e51f192013-09-11 14:23:57 -07002815
2816# Check for duplicate signatures
2817 my $sig_nospace = $line;
2818 $sig_nospace =~ s/\s//g;
2819 $sig_nospace = lc($sig_nospace);
2820 if (defined $signatures{$sig_nospace}) {
2821 WARN("BAD_SIGN_OFF",
2822 "Duplicate signature\n" . $herecurr);
2823 } else {
2824 $signatures{$sig_nospace} = 1;
2825 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002826
2827# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2828 if ($sign_off =~ /^co-developed-by:$/i) {
2829 if ($email eq $author) {
2830 WARN("BAD_SIGN_OFF",
2831 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2832 }
2833 if (!defined $lines[$linenr]) {
2834 WARN("BAD_SIGN_OFF",
2835 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2836 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2837 WARN("BAD_SIGN_OFF",
2838 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2839 } elsif ($1 ne $email) {
2840 WARN("BAD_SIGN_OFF",
2841 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2842 }
2843 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002844 }
2845
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002846# Check email subject for common tools that don't need to be mentioned
2847 if ($in_header_lines &&
2848 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2849 WARN("EMAIL_SUBJECT",
2850 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2851 }
2852
Joe Perches44d303e2020-04-06 20:11:10 -07002853# Check for Gerrit Change-Ids not in any patch context
2854 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002855 ERROR("GERRIT_CHANGE_ID",
Joe Perches44d303e2020-04-06 20:11:10 -07002856 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002857 }
2858
Joe Perches369c8dd2015-11-06 16:31:34 -08002859# Check if the commit log is in a possible stack dump
2860 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2861 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2862 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2863 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07002864 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2865 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2866 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2867 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08002868 $commit_log_possible_stack_dump = 1;
2869 }
2870
Joe Perches2a076f42015-04-16 12:44:28 -07002871# Check for line lengths > 75 in commit log, warn once
2872 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002873 length($line) > 75 &&
2874 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2875 # file delta changes
2876 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2877 # filename then :
2878 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2879 # A Fixes: or Link: line
2880 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002881 WARN("COMMIT_LOG_LONG_LINE",
2882 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2883 $commit_log_long_line = 1;
2884 }
2885
Joe Perchesbf4daf12015-09-09 15:37:50 -07002886# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002887 if ($in_commit_log && $commit_log_possible_stack_dump &&
2888 $line =~ /^\s*$/) {
2889 $commit_log_possible_stack_dump = 0;
2890 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002891
Joe Perches0d7835f2015-02-13 14:38:35 -08002892# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002893 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07002894 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002895 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002896 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002897 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002898 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2899 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002900 my $init_char = "c";
2901 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002902 my $short = 1;
2903 my $long = 0;
2904 my $case = 1;
2905 my $space = 1;
2906 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002907 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002908 my $id = '0123456789ab';
2909 my $orig_desc = "commit description";
2910 my $description = "";
2911
Joe Perchesfe043ea2015-09-09 15:37:25 -07002912 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2913 $init_char = $1;
2914 $orig_commit = lc($2);
2915 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2916 $orig_commit = lc($1);
2917 }
2918
Joe Perches0d7835f2015-02-13 14:38:35 -08002919 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2920 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2921 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2922 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2923 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2924 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002925 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002926 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2927 defined $rawlines[$linenr] &&
2928 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2929 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002930 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002931 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2932 defined $rawlines[$linenr] &&
2933 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2934 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2935 $orig_desc = $1;
2936 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2937 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002938 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002939 }
2940
2941 ($id, $description) = git_commit_info($orig_commit,
2942 $id, $orig_desc);
2943
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002944 if (defined($id) &&
2945 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002946 ERROR("GIT_COMMIT_ID",
2947 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2948 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002949 }
2950
Joe Perches13f19372014-08-06 16:10:59 -07002951# Check for added, moved or deleted files
2952 if (!$reported_maintainer_file && !$in_commit_log &&
2953 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2954 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2955 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2956 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002957 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002958 $reported_maintainer_file = 1;
2959 WARN("FILE_PATH_CHANGES",
2960 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2961 }
2962
Rob Herringe400edb2019-09-12 15:18:20 +01002963# Check for adding new DT bindings not in schema format
2964 if (!$in_commit_log &&
2965 ($line =~ /^new file mode\s*\d+\s*$/) &&
2966 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2967 WARN("DT_SCHEMA_BINDING_PATCH",
2968 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2969 }
2970
Andy Whitcroft00df3442007-06-08 13:47:06 -07002971# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002972 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002973 ERROR("CORRUPTED_PATCH",
2974 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002975 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002976 }
2977
2978# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2979 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002980 $rawline !~ m/^$UTF8*$/) {
2981 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2982
2983 my $blank = copy_spacing($rawline);
2984 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2985 my $hereptr = "$hereline$ptr\n";
2986
Joe Perches34d99212011-07-25 17:13:26 -07002987 CHK("INVALID_UTF8",
2988 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002989 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002990
Joe Perches15662b32011-10-31 17:13:12 -07002991# Check if it's the start of a commit log
2992# (not a header line and we haven't seen the patch filename)
2993 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002994 !($rawline =~ /^\s+(?:\S|$)/ ||
2995 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002996 $in_header_lines = 0;
2997 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002998 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002999 }
3000
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003001# Check if there is UTF-8 in a commit log when a mail header has explicitly
3002# declined it, i.e defined some charset where it is missing.
3003 if ($in_header_lines &&
3004 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3005 $1 !~ /utf-8/i) {
3006 $non_utf8_charset = 1;
3007 }
3008
3009 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07003010 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003011 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07003012 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3013 }
3014
Joe Perchesd6430f72016-12-12 16:46:28 -08003015# Check for absolute kernel paths in commit message
3016 if ($tree && $in_commit_log) {
3017 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3018 my $file = $1;
3019
3020 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3021 check_absolute_file($1, $herecurr)) {
3022 #
3023 } else {
3024 check_absolute_file($file, $herecurr);
3025 }
3026 }
3027 }
3028
Kees Cook66b47b42014-10-13 15:51:57 -07003029# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07003030 if (defined($misspellings) &&
3031 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07003032 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07003033 my $typo = $1;
3034 my $typo_fix = $spelling_fix{lc($typo)};
3035 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3036 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07003037 my $msg_level = \&WARN;
3038 $msg_level = \&CHK if ($file);
3039 if (&{$msg_level}("TYPO_SPELLING",
3040 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07003041 $fix) {
3042 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3043 }
3044 }
3045 }
3046
Matteo Crocea8dd86b2019-09-25 16:46:38 -07003047# check for invalid commit id
3048 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3049 my $id;
3050 my $description;
3051 ($id, $description) = git_commit_info($2, undef, undef);
3052 if (!defined($id)) {
3053 WARN("UNKNOWN_COMMIT_ID",
3054 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3055 }
3056 }
3057
Joe Perches310cd062020-10-15 20:11:52 -07003058# check for repeated words separated by a single space
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003059# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3060 if (($rawline =~ /^\+/ || $in_commit_log) &&
3061 $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003062 pos($rawline) = 1 if (!$in_commit_log);
Joe Perches310cd062020-10-15 20:11:52 -07003063 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3064
3065 my $first = $1;
3066 my $second = $2;
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003067 my $start_pos = $-[1];
3068 my $end_pos = $+[2];
Joe Perches310cd062020-10-15 20:11:52 -07003069 if ($first =~ /(?:struct|union|enum)/) {
3070 pos($rawline) += length($first) + length($second) + 1;
3071 next;
3072 }
3073
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003074 next if (lc($first) ne lc($second));
Joe Perches310cd062020-10-15 20:11:52 -07003075 next if ($first eq 'long');
3076
Dwaipayan Ray1db81a62020-12-15 20:44:20 -08003077 # check for character before and after the word matches
3078 my $start_char = '';
3079 my $end_char = '';
3080 $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
3081 $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3082
3083 next if ($start_char =~ /^\S$/);
3084 next if (index(" \t.,;?!", $end_char) == -1);
3085
Aditya Srivastava8d0325c2020-12-15 20:44:24 -08003086 # avoid repeating hex occurrences like 'ff ff fe 09 ...'
3087 if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3088 next if (!exists($allow_repeated_words{lc($first)}));
3089 }
3090
Joe Perches310cd062020-10-15 20:11:52 -07003091 if (WARN("REPEATED_WORD",
3092 "Possible repeated word: '$first'\n" . $herecurr) &&
3093 $fix) {
3094 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3095 }
3096 }
3097
3098 # if it's a repeated word on consecutive lines in a comment block
3099 if ($prevline =~ /$;+\s*$/ &&
3100 $prevrawline =~ /($word_pattern)\s*$/) {
3101 my $last_word = $1;
3102 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3103 if (WARN("REPEATED_WORD",
3104 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3105 $fix) {
3106 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3107 }
3108 }
3109 }
3110 }
3111
Andy Whitcroft306708542008-10-15 22:02:28 -07003112# ignore non-hunk lines and lines being removed
3113 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003114
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003115#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003116 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003117 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07003118 if (ERROR("DOS_LINE_ENDINGS",
3119 "DOS line endings\n" . $herevet) &&
3120 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003121 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003122 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003123 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3124 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003125 if (ERROR("TRAILING_WHITESPACE",
3126 "trailing whitespace\n" . $herevet) &&
3127 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003128 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003129 }
3130
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003131 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003132 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07003133
Josh Triplett4783f892013-11-12 15:10:12 -08003134# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003135 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003136 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003137 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3138 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003139 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003140 my $msg_level = \&ERROR;
3141 $msg_level = \&CHK if ($file);
3142 &{$msg_level}("FSF_MAILING_ADDRESS",
3143 "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 -08003144 }
3145
Andi Kleen33549572010-05-24 14:33:29 -07003146# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003147# Only applies when adding the entry originally, after that we do not have
3148# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003149 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003150 # 'choice' is usually the last thing on the line (though
3151 # Kconfig supports named choices), so use a word boundary
3152 # (\b) rather than a whitespace character (\s)
3153 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003154 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003155 my $cnt = $realcnt;
3156 my $ln = $linenr + 1;
3157 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003158 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003159 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003160 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003161 $f = $lines[$ln - 1];
3162 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3163 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003164
3165 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003166 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003167
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003168 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003169 $is_start = 1;
Masahiro Yamada22a4ac02020-06-17 12:02:20 +09003170 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003171 $length = -1;
3172 }
3173
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003174 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003175 $f =~ s/#.*//;
3176 $f =~ s/^\s+//;
3177 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003178
3179 # This only checks context lines in the patch
3180 # and so hopefully shouldn't trigger false
3181 # positives, even though some of these are
3182 # common words in help texts
3183 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3184 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003185 $is_end = 1;
3186 last;
3187 }
Andi Kleen33549572010-05-24 14:33:29 -07003188 $length++;
3189 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003190 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3191 WARN("CONFIG_DESCRIPTION",
3192 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3193 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003194 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003195 }
3196
Joe Perches7ccf41a2020-06-04 16:50:33 -07003197# check MAINTAINERS entries
3198 if ($realfile =~ /^MAINTAINERS$/) {
3199# check MAINTAINERS entries for the right form
3200 if ($rawline =~ /^\+[A-Z]:/ &&
3201 $rawline !~ /^\+[A-Z]:\t\S/) {
3202 if (WARN("MAINTAINERS_STYLE",
3203 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3204 $fix) {
3205 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3206 }
3207 }
3208# check MAINTAINERS entries for the right ordering too
3209 my $preferred_order = 'MRLSWQBCPTFXNK';
3210 if ($rawline =~ /^\+[A-Z]:/ &&
3211 $prevrawline =~ /^[\+ ][A-Z]:/) {
3212 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3213 my $cur = $1;
3214 my $curval = $2;
3215 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3216 my $prev = $1;
3217 my $prevval = $2;
3218 my $curindex = index($preferred_order, $cur);
3219 my $previndex = index($preferred_order, $prev);
3220 if ($curindex < 0) {
3221 WARN("MAINTAINERS_STYLE",
3222 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3223 } else {
3224 if ($previndex >= 0 && $curindex < $previndex) {
3225 WARN("MAINTAINERS_STYLE",
3226 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3227 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3228 ($prev eq 'X' && $cur eq 'X')) &&
3229 ($prevval cmp $curval) > 0) {
3230 WARN("MAINTAINERS_STYLE",
3231 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3232 }
3233 }
Joe Perches628f91a2017-07-10 15:52:07 -07003234 }
3235 }
3236
Christoph Jaeger327953e2015-02-13 14:38:29 -08003237# discourage the use of boolean for type definition attributes of Kconfig options
3238 if ($realfile =~ /Kconfig/ &&
3239 $line =~ /^\+\s*\bboolean\b/) {
3240 WARN("CONFIG_TYPE_BOOLEAN",
3241 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3242 }
3243
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003244 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3245 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3246 my $flag = $1;
3247 my $replacement = {
3248 'EXTRA_AFLAGS' => 'asflags-y',
3249 'EXTRA_CFLAGS' => 'ccflags-y',
3250 'EXTRA_CPPFLAGS' => 'cppflags-y',
3251 'EXTRA_LDFLAGS' => 'ldflags-y',
3252 };
3253
3254 WARN("DEPRECATED_VARIABLE",
3255 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3256 }
3257
Rob Herringbff5da42014-01-23 15:54:51 -08003258# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003259 if (defined $root &&
3260 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3261 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3262
Rob Herringbff5da42014-01-23 15:54:51 -08003263 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3264
Florian Vaussardcc933192014-04-03 14:49:27 -07003265 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003266 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003267
Rob Herringbff5da42014-01-23 15:54:51 -08003268 foreach my $compat (@compats) {
3269 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003270 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3271 my $compat3 = $compat;
3272 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3273 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003274 if ( $? >> 8 ) {
3275 WARN("UNDOCUMENTED_DT_STRING",
3276 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3277 }
3278
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003279 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3280 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003281 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003282 if ( $? >> 8 ) {
3283 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003284 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003285 }
3286 }
3287 }
3288
Rob Herring9f3a8992018-04-10 16:33:13 -07003289# check for using SPDX license tag at beginning of files
3290 if ($realline == $checklicenseline) {
3291 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3292 $checklicenseline = 2;
3293 } elsif ($rawline =~ /^\+/) {
3294 my $comment = "";
3295 if ($realfile =~ /\.(h|s|S)$/) {
3296 $comment = '/*';
3297 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3298 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003299 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003300 $comment = '#';
3301 } elsif ($realfile =~ /\.rst$/) {
3302 $comment = '..';
3303 }
3304
Joe Perchesfdf13692019-03-07 16:28:32 -08003305# check SPDX comment style for .[chsS] files
3306 if ($realfile =~ /\.[chsS]$/ &&
3307 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003308 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003309 WARN("SPDX_LICENSE_TAG",
3310 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3311 }
3312
Rob Herring9f3a8992018-04-10 16:33:13 -07003313 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003314 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3315 WARN("SPDX_LICENSE_TAG",
3316 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003317 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003318 my $spdx_license = $1;
3319 if (!is_SPDX_License_valid($spdx_license)) {
3320 WARN("SPDX_LICENSE_TAG",
3321 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3322 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003323 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3324 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3325 my $msg_level = \&WARN;
3326 $msg_level = \&CHK if ($file);
3327 if (&{$msg_level}("SPDX_LICENSE_TAG",
3328
3329 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3330 $fix) {
3331 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3332 }
3333 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003334 }
3335 }
3336 }
3337
Joe Perchesa0154cd2020-10-15 20:12:19 -07003338# check for embedded filenames
3339 if ($rawline =~ /^\+.*\Q$realfile\E/) {
3340 WARN("EMBEDDED_FILENAME",
3341 "It's generally not useful to have the filename in the file\n" . $herecurr);
3342 }
3343
Andy Whitcroft5368df202008-10-15 22:02:27 -07003344# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003345 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003346
Joe Perchesa8da38a2019-03-07 16:28:42 -08003347# check for using SPDX-License-Identifier on the wrong line number
3348 if ($realline != $checklicenseline &&
3349 $rawline =~ /\bSPDX-License-Identifier:/ &&
3350 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3351 WARN("SPDX_LICENSE_TAG",
3352 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3353 }
3354
Joe Perches47e0c882015-06-25 15:02:57 -07003355# line length limit (with some exclusions)
3356#
3357# There are a few types of lines that may extend beyond $max_line_length:
3358# logging functions like pr_info that end in a string
3359# lines with a single string
3360# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003361# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003362#
3363# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003364# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003365# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3366# LONG_LINE all other lines longer than $max_line_length
3367#
3368# if LONG_LINE is ignored, the other 2 types are also ignored
3369#
3370
Joe Perchesb4749e92015-07-17 16:24:01 -07003371 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003372 my $msg_type = "LONG_LINE";
3373
3374 # Check the allowed long line types first
3375
3376 # logging functions that end in a string that starts
3377 # before $max_line_length
3378 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3379 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3380 $msg_type = "";
3381
3382 # lines with only strings (w/ possible termination)
3383 # #defines with only strings
3384 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3385 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3386 $msg_type = "";
3387
Joe Perchescc147502017-11-17 15:28:44 -08003388 # More special cases
3389 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3390 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003391 $msg_type = "";
3392
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003393 # URL ($rawline is used in case the URL is in a comment)
3394 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3395 $msg_type = "";
3396
Joe Perches47e0c882015-06-25 15:02:57 -07003397 # Otherwise set the alternate message types
3398
3399 # a comment starts before $max_line_length
3400 } elsif ($line =~ /($;[\s$;]*)$/ &&
3401 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3402 $msg_type = "LONG_LINE_COMMENT"
3403
3404 # a quoted string starts before $max_line_length
3405 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3406 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3407 $msg_type = "LONG_LINE_STRING"
3408 }
3409
3410 if ($msg_type ne "" &&
3411 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003412 my $msg_level = \&WARN;
3413 $msg_level = \&CHK if ($file);
3414 &{$msg_level}($msg_type,
3415 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003416 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003417 }
3418
Andy Whitcroft8905a672007-11-28 16:21:06 -08003419# check for adding lines without a newline.
3420 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003421 WARN("MISSING_EOF_NEWLINE",
3422 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003423 }
3424
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003425# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003426 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003427
3428# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003429# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003430 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3431 $rawline =~ /^\+\s* \s*/) {
3432 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003433 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003434 if (ERROR("CODE_INDENT",
3435 "code indent should use tabs where possible\n" . $herevet) &&
3436 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003437 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003438 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003439 }
3440
Alberto Panizzo08e44362010-03-05 13:43:54 -08003441# check for space before tabs.
3442 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3443 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003444 if (WARN("SPACE_BEFORE_TAB",
3445 "please, no space before tabs\n" . $herevet) &&
3446 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003447 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003448 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003449 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003450 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003451 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003452 }
3453
Joe Perches6a487212018-04-10 16:34:04 -07003454# check for assignments on the start of a line
3455 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3456 CHK("ASSIGNMENT_CONTINUATIONS",
3457 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3458 }
3459
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003460# check for && or || at the start of a line
3461 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3462 CHK("LOGICAL_CONTINUATIONS",
3463 "Logical continuations should be on the previous line\n" . $hereprev);
3464 }
3465
Joe Perchesa91e8992016-05-20 17:04:05 -07003466# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003467 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003468 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003469 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003470 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003471 if (WARN("TABSTOP",
3472 "Statements should start on a tabstop\n" . $herecurr) &&
3473 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003474 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003475 }
3476 }
3477 }
3478
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003479# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003480 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003481 $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 -07003482 $prevline =~ /^\+(\t*)(.*)$/;
3483 my $oldindent = $1;
3484 my $rest = $2;
3485
3486 my $pos = pos_last_openparen($rest);
3487 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003488 $line =~ /^(\+| )([ \t]*)/;
3489 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003490
3491 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003492 "\t" x ($pos / $tabsize) .
3493 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003494 my $goodspaceindent = $oldindent . " " x $pos;
3495
3496 if ($newindent ne $goodtabindent &&
3497 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003498
3499 if (CHK("PARENTHESIS_ALIGNMENT",
3500 "Alignment should match open parenthesis\n" . $hereprev) &&
3501 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003502 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003503 s/^\+[ \t]*/\+$goodtabindent/;
3504 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003505 }
3506 }
3507 }
3508
Joe Perches6ab3a972015-04-16 12:44:05 -07003509# check for space after cast like "(int) foo" or "(struct foo) bar"
3510# avoid checking a few false positives:
3511# "sizeof(<type>)" or "__alignof__(<type>)"
3512# function pointer declarations like "(*foo)(int) = bar;"
3513# structure definitions like "(struct foo) { 0 };"
3514# multiline macros that define functions
3515# known attributes or the __attribute__ keyword
3516 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3517 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003518 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003519 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003520 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003521 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003522 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003523 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003524 }
3525
Joe Perches86406b12015-09-09 15:37:41 -07003526# Block comment styles
3527# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003528 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003529 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003530 $rawline =~ /^\+[ \t]*\*/ &&
Łukasz Stelmachc70735c2020-10-15 20:12:25 -07003531 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
Joe Perches05880602012-10-04 17:13:35 -07003532 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3533 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3534 }
3535
Joe Perches86406b12015-09-09 15:37:41 -07003536# Block comments use * on subsequent lines
3537 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3538 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003539 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003540 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003541 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003542 WARN("BLOCK_COMMENT_STYLE",
3543 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003544 }
3545
Joe Perches86406b12015-09-09 15:37:41 -07003546# Block comments use */ on trailing lines
3547 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003548 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3549 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3550 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003551 WARN("BLOCK_COMMENT_STYLE",
3552 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003553 }
3554
Joe Perches08eb9b82016-10-11 13:51:50 -07003555# Block comment * alignment
3556 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003557 $line =~ /^\+[ \t]*$;/ && #leading comment
3558 $rawline =~ /^\+[ \t]*\*/ && #leading *
3559 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003560 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003561 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3562 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003563 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003564 if (defined($1)) {
3565 $oldindent = expand_tabs($1);
3566 } else {
3567 $prevrawline =~ m@^\+(.*/?)\*@;
3568 $oldindent = expand_tabs($1);
3569 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003570 $rawline =~ m@^\+([ \t]*)\*@;
3571 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003572 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003573 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003574 WARN("BLOCK_COMMENT_STYLE",
3575 "Block comments should align the * on each line\n" . $hereprev);
3576 }
3577 }
3578
Joe Perches7f619192014-08-06 16:10:39 -07003579# check for missing blank lines after struct/union declarations
3580# with exceptions for various attributes and macros
3581 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3582 $line =~ /^\+/ &&
3583 !($line =~ /^\+\s*$/ ||
3584 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3585 $line =~ /^\+\s*MODULE_/i ||
3586 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3587 $line =~ /^\+[a-z_]*init/ ||
3588 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3589 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003590 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003591 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003592 if (CHK("LINE_SPACING",
3593 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3594 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003595 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003596 }
Joe Perches7f619192014-08-06 16:10:39 -07003597 }
3598
Joe Perches365dd4e2014-08-06 16:10:42 -07003599# check for multiple consecutive blank lines
3600 if ($prevline =~ /^[\+ ]\s*$/ &&
3601 $line =~ /^\+\s*$/ &&
3602 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003603 if (CHK("LINE_SPACING",
3604 "Please don't use multiple blank lines\n" . $hereprev) &&
3605 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003606 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003607 }
3608
Joe Perches365dd4e2014-08-06 16:10:42 -07003609 $last_blank_line = $linenr;
3610 }
3611
Joe Perches3b617e32014-04-03 14:49:28 -07003612# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003613 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3614 # actual declarations
3615 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003616 # function pointer declarations
3617 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003618 # foo bar; where foo is some local typedef or #define
3619 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3620 # known declaration macros
3621 $prevline =~ /^\+\s+$declaration_macros/) &&
3622 # for "else if" which can look like "$Ident $Ident"
3623 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3624 # other possible extensions of declaration lines
3625 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3626 # not starting a section or a macro "\" extended line
3627 $prevline =~ /(?:\{\s*|\\)$/) &&
3628 # looks like a declaration
3629 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003630 # function pointer declarations
3631 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003632 # foo bar; where foo is some local typedef or #define
3633 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3634 # known declaration macros
3635 $sline =~ /^\+\s+$declaration_macros/ ||
3636 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003637 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003638 # start or end of block or continuation of declaration
3639 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3640 # bitfield continuation
3641 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3642 # other possible extensions of declaration lines
3643 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3644 # indentation of previous and current line are the same
3645 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003646 if (WARN("LINE_SPACING",
3647 "Missing a blank line after declarations\n" . $hereprev) &&
3648 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003649 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003650 }
Joe Perches3b617e32014-04-03 14:49:28 -07003651 }
3652
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003653# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003654# Exceptions:
3655# 1) within comments
3656# 2) indented preprocessor commands
3657# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003658 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003659 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003660 if (WARN("LEADING_SPACE",
3661 "please, no spaces at the start of a line\n" . $herevet) &&
3662 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003663 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003664 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003665 }
3666
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003667# check we are in a valid C source file if not then ignore this hunk
3668 next if ($realfile !~ /\.(h|c)$/);
3669
Joe Perches5751a242017-11-17 15:28:52 -08003670# check for unusual line ending [ or (
3671 if ($line =~ /^\+.*([\[\(])\s*$/) {
3672 CHK("OPEN_ENDED_LINE",
3673 "Lines should not end with a '$1'\n" . $herecurr);
3674 }
3675
Joe Perches4dbed762017-05-08 15:55:39 -07003676# check if this appears to be the start function declaration, save the name
3677 if ($sline =~ /^\+\{\s*$/ &&
3678 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3679 $context_function = $1;
3680 }
3681
3682# check if this appears to be the end of function declaration
3683 if ($sline =~ /^\+\}\s*$/) {
3684 undef $context_function;
3685 }
3686
Joe Perches032a4c02014-08-06 16:10:29 -07003687# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003688# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003689# if the previous line is a break or return and is indented 1 tab more...
3690 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3691 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003692 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3693 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3694 defined $lines[$linenr] &&
3695 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003696 WARN("UNNECESSARY_ELSE",
3697 "else is not generally useful after a break or return\n" . $hereprev);
3698 }
3699 }
3700
Joe Perchesc00df192014-08-06 16:11:01 -07003701# check indentation of a line with a break;
Joe Perchesdc58bc52020-12-15 20:44:33 -08003702# if the previous line is a goto, return or break
3703# and is indented the same # of tabs
Joe Perchesc00df192014-08-06 16:11:01 -07003704 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3705 my $tabs = $1;
Joe Perchesdc58bc52020-12-15 20:44:33 -08003706 if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
3707 if (WARN("UNNECESSARY_BREAK",
3708 "break is not useful after a $1\n" . $hereprev) &&
3709 $fix) {
3710 fix_delete_line($fixlinenr, $rawline);
3711 }
Joe Perchesc00df192014-08-06 16:11:01 -07003712 }
3713 }
3714
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003715# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003716 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003717 WARN("CVS_KEYWORD",
3718 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003719 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003720
Joe Perches56e77d72013-02-21 16:44:14 -08003721# check for old HOTPLUG __dev<foo> section markings
3722 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3723 WARN("HOTPLUG_SECTION",
3724 "Using $1 is unnecessary\n" . $herecurr);
3725 }
3726
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003727# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003728 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3729 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003730#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003731 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003732 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003733 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003734 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003735 $stat =~ s/\n./\n /g;
3736 $cond =~ s/\n./\n /g;
3737
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003738#print "linenr<$linenr> <$stat>\n";
3739 # If this statement has no statement boundaries within
3740 # it there is no point in retrying a statement scan
3741 # until we hit end of it.
3742 my $frag = $stat; $frag =~ s/;+\s*$//;
3743 if ($frag !~ /(?:{|;)/) {
3744#print "skip<$line_nr_next>\n";
3745 $suppress_statement = $line_nr_next;
3746 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003747
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003748 # Find the real next line.
3749 $realline_next = $line_nr_next;
3750 if (defined $realline_next &&
3751 (!defined $lines[$realline_next - 1] ||
3752 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3753 $realline_next++;
3754 }
3755
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003756 my $s = $stat;
3757 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003758
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003759 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003760 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003761
3762 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003763 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003764
Andy Whitcroft463f2862009-09-21 17:04:34 -07003765 } elsif ($s =~ /^.\s*else\b/s) {
3766
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003767 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003768 } 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 -07003769 my $type = $1;
3770 $type =~ s/\s+/ /g;
3771 possible($type, "A:" . $s);
3772
Andy Whitcroft8905a672007-11-28 16:21:06 -08003773 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003774 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003775 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003776 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003777
3778 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003779 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003780 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003781 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003782
3783 # Check for any sort of function declaration.
3784 # int foo(something bar, other baz);
3785 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003786 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 -08003787 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003788
Andy Whitcroftcf655042008-03-04 14:28:20 -08003789 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003790 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003791 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003792
Andy Whitcroft8905a672007-11-28 16:21:06 -08003793 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003794 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003795
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003796 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003797 }
3798 }
3799 }
3800
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003801 }
3802
Andy Whitcroft653d4872007-06-23 17:16:34 -07003803#
3804# Checks which may be anchored in the context.
3805#
3806
3807# Check for switch () and associated case and default
3808# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003809 if ($line=~/\bswitch\s*\(.*\)/) {
3810 my $err = '';
3811 my $sep = '';
3812 my @ctx = ctx_block_outer($linenr, $realcnt);
3813 shift(@ctx);
3814 for my $ctx (@ctx) {
3815 my ($clen, $cindent) = line_stats($ctx);
3816 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3817 $indent != $cindent) {
3818 $err .= "$sep$ctx\n";
3819 $sep = '';
3820 } else {
3821 $sep = "[...]\n";
3822 }
3823 }
3824 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003825 ERROR("SWITCH_CASE_INDENT_LEVEL",
3826 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003827 }
3828 }
3829
3830# if/while/etc brace do not go on next line, unless defining a do while loop,
3831# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003832 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 -07003833 my $pre_ctx = "$1$2";
3834
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003835 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003836
3837 if ($line =~ /^\+\t{6,}/) {
3838 WARN("DEEP_INDENTATION",
3839 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3840 }
3841
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003842 my $ctx_cnt = $realcnt - $#ctx - 1;
3843 my $ctx = join("\n", @ctx);
3844
Andy Whitcroft548596d2008-07-23 21:29:01 -07003845 my $ctx_ln = $linenr;
3846 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003847
Andy Whitcroft548596d2008-07-23 21:29:01 -07003848 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3849 defined $lines[$ctx_ln - 1] &&
3850 $lines[$ctx_ln - 1] =~ /^-/)) {
3851 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3852 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003853 $ctx_ln++;
3854 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003855
Andy Whitcroft53210162008-07-23 21:29:03 -07003856 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3857 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003858
Joe Perchesd752fcc2014-08-06 16:11:05 -07003859 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003860 ERROR("OPEN_BRACE",
3861 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003862 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003863 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003864 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3865 $ctx =~ /\)\s*\;\s*$/ &&
3866 defined $lines[$ctx_ln - 1])
3867 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003868 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3869 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003870 WARN("TRAILING_SEMICOLON",
3871 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003872 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003873 }
3874 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003875 }
3876
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003877# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003878 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 -08003879 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3880 ctx_statement_block($linenr, $realcnt, 0)
3881 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003882 my ($s, $c) = ($stat, $cond);
3883
3884 substr($s, 0, length($c), '');
3885
Joe Perches9f5af482015-09-09 15:37:30 -07003886 # remove inline comments
3887 $s =~ s/$;/ /g;
3888 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003889
3890 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003891 my @newlines = ($c =~ /\n/gs);
3892 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003893
Joe Perches9f5af482015-09-09 15:37:30 -07003894 # Make sure we remove the line prefixes as we have
3895 # none on the first line, and are going to readd them
3896 # where necessary.
3897 $s =~ s/\n./\n/gs;
3898 while ($s =~ /\n\s+\\\n/) {
3899 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3900 }
3901
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003902 # We want to check the first line inside the block
3903 # starting at the end of the conditional, so remove:
3904 # 1) any blank line termination
3905 # 2) any opening brace { on end of the line
3906 # 3) any do (...) {
3907 my $continuation = 0;
3908 my $check = 0;
3909 $s =~ s/^.*\bdo\b//;
3910 $s =~ s/^\s*{//;
3911 if ($s =~ s/^\s*\\//) {
3912 $continuation = 1;
3913 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003914 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003915 $check = 1;
3916 $cond_lines++;
3917 }
3918
3919 # Also ignore a loop construct at the end of a
3920 # preprocessor statement.
3921 if (($prevline =~ /^.\s*#\s*define\s/ ||
3922 $prevline =~ /\\\s*$/) && $continuation == 0) {
3923 $check = 0;
3924 }
3925
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003926 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003927 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003928 while ($cond_ptr != $cond_lines) {
3929 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003930
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003931 # If we see an #else/#elif then the code
3932 # is not linear.
3933 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3934 $check = 0;
3935 }
3936
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003937 # Ignore:
3938 # 1) blank lines, they should be at 0,
3939 # 2) preprocessor lines, and
3940 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003941 if ($continuation ||
3942 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003943 $s =~ /^\s*#\s*?/ ||
3944 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003945 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003946 if ($s =~ s/^.*?\n//) {
3947 $cond_lines++;
3948 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003949 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003950 }
3951
3952 my (undef, $sindent) = line_stats("+" . $s);
3953 my $stat_real = raw_line($linenr, $cond_lines);
3954
3955 # Check if either of these lines are modified, else
3956 # this is not this patch's fault.
3957 if (!defined($stat_real) ||
3958 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3959 $check = 0;
3960 }
3961 if (defined($stat_real) && $cond_lines > 1) {
3962 $stat_real = "[...]\n$stat_real";
3963 }
3964
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003965 #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 -07003966
Joe Perches9f5af482015-09-09 15:37:30 -07003967 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07003968 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07003969 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003970 ($sindent == $indent &&
3971 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07003972 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003973 WARN("SUSPECT_CODE_INDENT",
3974 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003975 }
3976 }
3977
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003978 # Track the 'values' across context and added lines.
3979 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003980 my ($curr_values, $curr_vars) =
3981 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003982 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003983 if ($dbg_values) {
3984 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003985 print "$linenr > .$outline\n";
3986 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003987 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003988 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003989 $prev_values = substr($curr_values, -1);
3990
Andy Whitcroft00df3442007-06-08 13:47:06 -07003991#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003992 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003993
Joe Perches99ca38c2020-10-15 20:12:09 -07003994# check for self assignments used to avoid compiler warnings
3995# e.g.: int foo = foo, *bar = NULL;
3996# struct foo bar = *(&(bar));
3997 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
3998 my $var = $1;
3999 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
4000 WARN("SELF_ASSIGNMENT",
4001 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
4002 }
4003 }
4004
Joe Perches11ca40a2016-12-12 16:46:31 -08004005# check for dereferences that span multiple lines
4006 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
4007 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
4008 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
4009 my $ref = $1;
4010 $line =~ /^.\s*($Lval)/;
4011 $ref .= $1;
4012 $ref =~ s/\s//g;
4013 WARN("MULTILINE_DEREFERENCE",
4014 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
4015 }
4016
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004017# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07004018 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 -07004019 my $type = $1;
4020 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07004021 $var = "" if (!defined $var);
4022 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004023 my $sign = $1;
4024 my $pointer = $2;
4025
4026 $pointer = "" if (!defined $pointer);
4027
4028 if (WARN("UNSPECIFIED_INT",
4029 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4030 $fix) {
4031 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07004032 my $comp_pointer = $pointer;
4033 $comp_pointer =~ s/\s//g;
4034 $decl .= $comp_pointer;
4035 $decl = rtrim($decl) if ($var eq "");
4036 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004037 }
4038 }
4039 }
4040
Andy Whitcroft653d4872007-06-23 17:16:34 -07004041# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07004042 if ($dbg_type) {
4043 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004044 ERROR("TEST_TYPE",
4045 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004046 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004047 ERROR("TEST_NOT_TYPE",
4048 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004049 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004050 next;
4051 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004052# TEST: allow direct testing of the attribute matcher.
4053 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004054 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004055 ERROR("TEST_ATTR",
4056 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004057 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004058 ERROR("TEST_NOT_ATTR",
4059 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004060 }
4061 next;
4062 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004063
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004064# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07004065 if ($line =~ /^.\s*{/ &&
4066 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07004067 if (ERROR("OPEN_BRACE",
4068 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004069 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4070 fix_delete_line($fixlinenr - 1, $prevrawline);
4071 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004072 my $fixedline = $prevrawline;
4073 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004074 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004075 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004076 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004077 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004078 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004079 }
4080
Andy Whitcroft653d4872007-06-23 17:16:34 -07004081#
4082# Checks which are anchored on the added line.
4083#
4084
4085# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004086 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07004087 my $path = $1;
4088 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004089 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08004090 "malformed #include filename\n" . $herecurr);
4091 }
4092 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4093 ERROR("UAPI_INCLUDE",
4094 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004095 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004096 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004097
4098# no C99 // comments
4099 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07004100 if (ERROR("C99_COMMENTS",
4101 "do not use C99 // comments\n" . $herecurr) &&
4102 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004103 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07004104 if ($line =~ /\/\/(.*)$/) {
4105 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07004106 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07004107 }
4108 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004109 }
4110 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004111 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004112 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004113
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004114# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4115# the whole statement.
4116#print "APW <$lines[$realline_next - 1]>\n";
4117 if (defined $realline_next &&
4118 exists $lines[$realline_next - 1] &&
4119 !defined $suppress_export{$realline_next} &&
4120 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4121 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004122 # Handle definitions which produce identifiers with
4123 # a prefix:
4124 # XXX(foo);
4125 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004126 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08004127 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004128 $name =~ /^${Ident}_$2/) {
4129#print "FOO C name<$name>\n";
4130 $suppress_export{$realline_next} = 1;
4131
4132 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004133 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07004134 ^.DEFINE_$Ident\(\Q$name\E\)|
4135 ^.DECLARE_$Ident\(\Q$name\E\)|
4136 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004137 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4138 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07004139 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004140#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4141 $suppress_export{$realline_next} = 2;
4142 } else {
4143 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004144 }
4145 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004146 if (!defined $suppress_export{$linenr} &&
4147 $prevline =~ /^.\s*$/ &&
4148 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4149 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4150#print "FOO B <$lines[$linenr - 1]>\n";
4151 $suppress_export{$linenr} = 2;
4152 }
4153 if (defined $suppress_export{$linenr} &&
4154 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004155 WARN("EXPORT_SYMBOL",
4156 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004157 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004158
Joe Eloff5150bda2010-08-09 17:21:00 -07004159# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004160 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004161 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004162 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004163 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004164 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004165 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004166 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004167# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004168 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004169 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004170 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004171 $herecurr) &&
4172 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004173 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004174 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004175 }
4176
Joe Perches18130872014-08-06 16:11:22 -07004177# check for misordered declarations of char/short/int/long with signed/unsigned
4178 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4179 my $tmp = trim($1);
4180 WARN("MISORDERED_TYPE",
4181 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4182 }
4183
Joe Perches809e0822018-08-21 21:58:12 -07004184# check for unnecessary <signed> int declarations of short/long/long long
4185 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4186 my $type = trim($1);
4187 next if ($type !~ /\bint\b/);
4188 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4189 my $new_type = $type;
4190 $new_type =~ s/\b\s*int\s*\b/ /;
4191 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4192 $new_type =~ s/^const\s+//;
4193 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4194 $new_type = "const $new_type" if ($type =~ /^const\b/);
4195 $new_type =~ s/\s+/ /g;
4196 $new_type = trim($new_type);
4197 if (WARN("UNNECESSARY_INT",
4198 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4199 $fix) {
4200 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4201 }
4202 }
4203
Joe Perchescb710ec2010-10-26 14:23:20 -07004204# check for static const char * arrays.
4205 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004206 WARN("STATIC_CONST_CHAR_ARRAY",
4207 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004208 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004209 }
4210
4211# check for initialized const char arrays that should be static const
4212 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4213 if (WARN("STATIC_CONST_CHAR_ARRAY",
4214 "const array should probably be static const\n" . $herecurr) &&
4215 $fix) {
4216 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4217 }
4218 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004219
4220# check for static char foo[] = "bar" declarations.
4221 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004222 WARN("STATIC_CONST_CHAR_ARRAY",
4223 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004224 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004225 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004226
Joe Perchesab7e23f2015-04-16 12:44:22 -07004227# check for const <foo> const where <foo> is not a pointer or array type
4228 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4229 my $found = $1;
4230 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4231 WARN("CONST_CONST",
4232 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4233 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4234 WARN("CONST_CONST",
4235 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4236 }
4237 }
4238
Joe Perches73169762020-12-15 20:44:30 -08004239# check for const static or static <non ptr type> const declarations
4240# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
4241 if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
4242 $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
4243 if (WARN("STATIC_CONST",
4244 "Move const after static - use 'static const $1'\n" . $herecurr) &&
4245 $fix) {
4246 $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
4247 $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
4248 }
4249 }
4250
Joe Perches9b0fa602014-04-03 14:49:18 -07004251# check for non-global char *foo[] = {"bar", ...} declarations.
4252 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4253 WARN("STATIC_CONST_CHAR_ARRAY",
4254 "char * array declaration might be better as static const\n" .
4255 $herecurr);
4256 }
4257
Joe Perchesb598b672015-04-16 12:44:36 -07004258# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4259 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4260 my $array = $1;
4261 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4262 my $array_div = $1;
4263 if (WARN("ARRAY_SIZE",
4264 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4265 $fix) {
4266 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4267 }
4268 }
4269 }
4270
Joe Perchesb36190c2014-01-27 17:07:18 -08004271# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004272 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004273 if (ERROR("FUNCTION_WITHOUT_ARGS",
4274 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4275 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004276 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004277 }
4278 }
4279
Andy Whitcroft653d4872007-06-23 17:16:34 -07004280# check for new typedefs, only function parameters and sparse annotations
4281# make sense.
4282 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004283 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004284 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004285 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004286 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004287 WARN("NEW_TYPEDEFS",
4288 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004289 }
4290
4291# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004292 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004293 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4294 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004295 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004296
Andy Whitcroft65863862009-01-06 14:41:21 -08004297 # Should start with a space.
4298 $to =~ s/^(\S)/ $1/;
4299 # Should not end with a space.
4300 $to =~ s/\s+$//;
4301 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004302 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004303 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004304
Joe Perches3705ce52013-07-03 15:05:31 -07004305## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004306 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004307 if (ERROR("POINTER_LOCATION",
4308 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4309 $fix) {
4310 my $sub_from = $ident;
4311 my $sub_to = $ident;
4312 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004313 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004314 s@\Q$sub_from\E@$sub_to@;
4315 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004316 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004317 }
4318 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4319 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004320 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004321
Andy Whitcroft65863862009-01-06 14:41:21 -08004322 # Should start with a space.
4323 $to =~ s/^(\S)/ $1/;
4324 # Should not end with a space.
4325 $to =~ s/\s+$//;
4326 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004327 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004328 }
4329 # Modifiers should have spaces.
4330 $to =~ s/(\b$Modifier$)/$1 /;
4331
Joe Perches3705ce52013-07-03 15:05:31 -07004332## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004333 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004334 if (ERROR("POINTER_LOCATION",
4335 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4336 $fix) {
4337
4338 my $sub_from = $match;
4339 my $sub_to = $match;
4340 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004341 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004342 s@\Q$sub_from\E@$sub_to@;
4343 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004344 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004345 }
4346
Joe Perches9d3e3c72015-09-09 15:37:27 -07004347# avoid BUG() or BUG_ON()
4348 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004349 my $msg_level = \&WARN;
4350 $msg_level = \&CHK if ($file);
4351 &{$msg_level}("AVOID_BUG",
4352 "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 -07004353 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004354
Joe Perches9d3e3c72015-09-09 15:37:27 -07004355# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004356 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004357 WARN("LINUX_VERSION_CODE",
4358 "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 -08004359 }
4360
Joe Perches17441222011-06-15 15:08:17 -07004361# check for uses of printk_ratelimit
4362 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004363 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004364 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004365 }
4366
Joe Percheseeef5732017-11-17 15:28:41 -08004367# printk should use KERN_* levels
4368 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4369 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4370 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004371 }
4372
Joe Perches243f3802012-05-31 16:26:09 -07004373 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4374 my $orig = $1;
4375 my $level = lc($orig);
4376 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004377 my $level2 = $level;
4378 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004379 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004380 "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 -07004381 }
4382
Joe Perchesdc139312013-02-21 16:44:13 -08004383 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4384 my $orig = $1;
4385 my $level = lc($orig);
4386 $level = "warn" if ($level eq "warning");
4387 $level = "dbg" if ($level eq "debug");
4388 WARN("PREFER_DEV_LEVEL",
4389 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4390 }
4391
Nicolas Boichat8020b252020-10-15 20:12:02 -07004392# trace_printk should not be used in production code.
4393 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4394 WARN("TRACE_PRINTK",
4395 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4396 }
4397
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004398# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4399# number of false positives, but assembly files are not checked, so at
4400# least the arch entry code will not trigger this warning.
4401 if ($line =~ /\bENOSYS\b/) {
4402 WARN("ENOSYS",
4403 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4404 }
4405
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004406# ENOTSUPP is not a standard error code and should be avoided in new patches.
4407# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4408# Similarly to ENOSYS warning a small number of false positives is expected.
4409 if (!$file && $line =~ /\bENOTSUPP\b/) {
4410 if (WARN("ENOTSUPP",
4411 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4412 $fix) {
4413 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4414 }
4415 }
4416
Andy Whitcroft653d4872007-06-23 17:16:34 -07004417# function brace can't be on same line, except for #defines of do while,
4418# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004419 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004420 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4421 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4422 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004423 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004424 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004425 $fix) {
4426 fix_delete_line($fixlinenr, $rawline);
4427 my $fixed_line = $rawline;
4428 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4429 my $line1 = $1;
4430 my $line2 = $2;
4431 fix_insert_line($fixlinenr, ltrim($line1));
4432 fix_insert_line($fixlinenr, "\+{");
4433 if ($line2 !~ /^\s*$/) {
4434 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4435 }
4436 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004437 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004438
Andy Whitcroft8905a672007-11-28 16:21:06 -08004439# open braces for enum, union and struct go on the same line.
4440 if ($line =~ /^.\s*{/ &&
4441 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004442 if (ERROR("OPEN_BRACE",
4443 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4444 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4445 fix_delete_line($fixlinenr - 1, $prevrawline);
4446 fix_delete_line($fixlinenr, $rawline);
4447 my $fixedline = rtrim($prevrawline) . " {";
4448 fix_insert_line($fixlinenr, $fixedline);
4449 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004450 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004451 if ($fixedline !~ /^\+\s*$/) {
4452 fix_insert_line($fixlinenr, $fixedline);
4453 }
4454 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004455 }
4456
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004457# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004458 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4459 if (WARN("SPACING",
4460 "missing space after $1 definition\n" . $herecurr) &&
4461 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004462 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004463 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4464 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004465 }
4466
Joe Perches31070b52014-01-23 15:54:49 -08004467# Function pointer declarations
4468# check spacing between type, funcptr, and args
4469# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004470 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004471 my $declare = $1;
4472 my $pre_pointer_space = $2;
4473 my $post_pointer_space = $3;
4474 my $funcname = $4;
4475 my $post_funcname_space = $5;
4476 my $pre_args_space = $6;
4477
Joe Perches91f72e92014-04-03 14:49:12 -07004478# the $Declare variable will capture all spaces after the type
4479# so check it for a missing trailing missing space but pointer return types
4480# don't need a space so don't warn for those.
4481 my $post_declare_space = "";
4482 if ($declare =~ /(\s+)$/) {
4483 $post_declare_space = $1;
4484 $declare = rtrim($declare);
4485 }
4486 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004487 WARN("SPACING",
4488 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004489 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004490 }
4491
4492# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004493# This test is not currently implemented because these declarations are
4494# equivalent to
4495# int foo(int bar, ...)
4496# and this is form shouldn't/doesn't generate a checkpatch warning.
4497#
4498# elsif ($declare =~ /\s{2,}$/) {
4499# WARN("SPACING",
4500# "Multiple spaces after return type\n" . $herecurr);
4501# }
Joe Perches31070b52014-01-23 15:54:49 -08004502
4503# unnecessary space "type ( *funcptr)(args...)"
4504 if (defined $pre_pointer_space &&
4505 $pre_pointer_space =~ /^\s/) {
4506 WARN("SPACING",
4507 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4508 }
4509
4510# unnecessary space "type (* funcptr)(args...)"
4511 if (defined $post_pointer_space &&
4512 $post_pointer_space =~ /^\s/) {
4513 WARN("SPACING",
4514 "Unnecessary space before function pointer name\n" . $herecurr);
4515 }
4516
4517# unnecessary space "type (*funcptr )(args...)"
4518 if (defined $post_funcname_space &&
4519 $post_funcname_space =~ /^\s/) {
4520 WARN("SPACING",
4521 "Unnecessary space after function pointer name\n" . $herecurr);
4522 }
4523
4524# unnecessary space "type (*funcptr) (args...)"
4525 if (defined $pre_args_space &&
4526 $pre_args_space =~ /^\s/) {
4527 WARN("SPACING",
4528 "Unnecessary space before function pointer arguments\n" . $herecurr);
4529 }
4530
4531 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004532 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004533 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004534 }
4535 }
4536
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004537# check for spacing round square brackets; allowed:
4538# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004539# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4540# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004541 while ($line =~ /(.*?\s)\[/g) {
4542 my ($where, $prefix) = ($-[1], $1);
4543 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004544 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004545 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004546 if (ERROR("BRACKET_SPACE",
4547 "space prohibited before open square bracket '['\n" . $herecurr) &&
4548 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004549 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004550 s/^(\+.*?)\s+\[/$1\[/;
4551 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004552 }
4553 }
4554
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004555# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004556 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004557 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004558 my $ctx_before = substr($line, 0, $-[1]);
4559 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004560
4561 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004562 if ($name =~ /^(?:
4563 if|for|while|switch|return|case|
4564 volatile|__volatile__|
4565 __attribute__|format|__extension__|
4566 asm|__asm__)$/x)
4567 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004568 # cpp #define statements have non-optional spaces, ie
4569 # if there is a space between the name and the open
4570 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004571 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004572
4573 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004574 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004575
4576 # If this whole things ends with a type its most
4577 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004578 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004579
4580 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004581 if (WARN("SPACING",
4582 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4583 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004584 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004585 s/\b$name\s+\(/$name\(/;
4586 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004587 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004588 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004589
Andy Whitcroft653d4872007-06-23 17:16:34 -07004590# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004591 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004592 my $fixed_line = "";
4593 my $line_fixed = 0;
4594
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004595 my $ops = qr{
4596 <<=|>>=|<=|>=|==|!=|
4597 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4598 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004599 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004600 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004601 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004602 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004603
4604## print("element count: <" . $#elements . ">\n");
4605## foreach my $el (@elements) {
4606## print("el: <$el>\n");
4607## }
4608
4609 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004610 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004611
Joe Perches3705ce52013-07-03 15:05:31 -07004612 foreach my $el (@elements) {
4613 push(@fix_elements, substr($rawline, $off, length($el)));
4614 $off += length($el);
4615 }
4616
4617 $off = 0;
4618
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004619 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004620 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004621
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004622 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004623
4624 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4625
4626## print("n: <$n> good: <$good>\n");
4627
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004628 $off += length($elements[$n]);
4629
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004630 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004631 my $ca = substr($opline, 0, $off);
4632 my $cc = '';
4633 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4634 $cc = substr($opline, $off + length($elements[$n + 1]));
4635 }
4636 my $cb = "$ca$;$cc";
4637
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004638 my $a = '';
4639 $a = 'V' if ($elements[$n] ne '');
4640 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004641 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004642 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4643 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004644 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004645
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004646 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004647
4648 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004649 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004650 $c = 'V' if ($elements[$n + 2] ne '');
4651 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004652 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004653 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4654 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004655 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004656 } else {
4657 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004658 }
4659
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004660 my $ctx = "${a}x${c}";
4661
4662 my $at = "(ctx:$ctx)";
4663
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004664 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004665 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004666
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004667 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004668 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004669
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004670 # Get the full operator variant.
4671 my $opv = $op . substr($curr_vars, $off, 1);
4672
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004673 # Ignore operators passed as parameters.
4674 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004675 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004676
Andy Whitcroftcf655042008-03-04 14:28:20 -08004677# # Ignore comments
4678# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004679
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004680 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004681 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004682 if ($ctx !~ /.x[WEBC]/ &&
4683 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004684 if (ERROR("SPACING",
4685 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004686 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004687 $line_fixed = 1;
4688 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004689 }
4690
4691 # // is a comment
4692 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004693
Joe Perchesb00e4812014-04-03 14:49:33 -07004694 # : when part of a bitfield
4695 } elsif ($opv eq ':B') {
4696 # skip the bitfield test for now
4697
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004698 # No spaces for:
4699 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004700 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004701 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004702 if (ERROR("SPACING",
4703 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004704 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004705 if (defined $fix_elements[$n + 2]) {
4706 $fix_elements[$n + 2] =~ s/^\s+//;
4707 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004708 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004709 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004710 }
4711
Joe Perches23810972014-12-10 15:51:32 -08004712 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004713 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004714 my $rtrim_before = 0;
4715 my $space_after = 0;
4716 if ($ctx =~ /Wx./) {
4717 if (ERROR("SPACING",
4718 "space prohibited before that '$op' $at\n" . $hereptr)) {
4719 $line_fixed = 1;
4720 $rtrim_before = 1;
4721 }
4722 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004723 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004724 if (ERROR("SPACING",
4725 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004726 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004727 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004728 $space_after = 1;
4729 }
4730 }
4731 if ($rtrim_before || $space_after) {
4732 if ($rtrim_before) {
4733 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4734 } else {
4735 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4736 }
4737 if ($space_after) {
4738 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004739 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004740 }
4741
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004742 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004743 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004744 #warn "'*' is part of type\n";
4745
4746 # unary operators should have a space before and
4747 # none after. May be left adjacent to another
4748 # unary operator, or a cast
4749 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004750 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004751 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004752 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004753 if (ERROR("SPACING",
4754 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004755 if ($n != $last_after + 2) {
4756 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4757 $line_fixed = 1;
4758 }
Joe Perches3705ce52013-07-03 15:05:31 -07004759 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004760 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004761 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004762 # A unary '*' may be const
4763
4764 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004765 if (ERROR("SPACING",
4766 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004767 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004768 if (defined $fix_elements[$n + 2]) {
4769 $fix_elements[$n + 2] =~ s/^\s+//;
4770 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004771 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004772 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004773 }
4774
4775 # unary ++ and unary -- are allowed no space on one side.
4776 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004777 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004778 if (ERROR("SPACING",
4779 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004780 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004781 $line_fixed = 1;
4782 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004783 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004784 if ($ctx =~ /Wx[BE]/ ||
4785 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004786 if (ERROR("SPACING",
4787 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004788 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004789 $line_fixed = 1;
4790 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004791 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004792 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004793 if (ERROR("SPACING",
4794 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004795 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004796 if (defined $fix_elements[$n + 2]) {
4797 $fix_elements[$n + 2] =~ s/^\s+//;
4798 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004799 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004800 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004801 }
4802
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004803 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004804 } elsif ($op eq '<<' or $op eq '>>' or
4805 $op eq '&' or $op eq '^' or $op eq '|' or
4806 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004807 $op eq '*' or $op eq '/' or
4808 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004809 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004810 if ($check) {
4811 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4812 if (CHK("SPACING",
4813 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4814 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4815 $fix_elements[$n + 2] =~ s/^\s+//;
4816 $line_fixed = 1;
4817 }
4818 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4819 if (CHK("SPACING",
4820 "space preferred before that '$op' $at\n" . $hereptr)) {
4821 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4822 $line_fixed = 1;
4823 }
4824 }
4825 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004826 if (ERROR("SPACING",
4827 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004828 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4829 if (defined $fix_elements[$n + 2]) {
4830 $fix_elements[$n + 2] =~ s/^\s+//;
4831 }
Joe Perches3705ce52013-07-03 15:05:31 -07004832 $line_fixed = 1;
4833 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004834 }
4835
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004836 # A colon needs no spaces before when it is
4837 # terminating a case value or a label.
4838 } elsif ($opv eq ':C' || $opv eq ':L') {
4839 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004840 if (ERROR("SPACING",
4841 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004842 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004843 $line_fixed = 1;
4844 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004845 }
4846
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004847 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004848 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004849 my $ok = 0;
4850
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004851 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004852 if (($op eq '<' &&
4853 $cc =~ /^\S+\@\S+>/) ||
4854 ($op eq '>' &&
4855 $ca =~ /<\S+\@\S+$/))
4856 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07004857 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004858 }
4859
Joe Perchese0df7e12015-04-16 12:44:53 -07004860 # for asm volatile statements
4861 # ignore a colon with another
4862 # colon immediately before or after
4863 if (($op eq ':') &&
4864 ($ca =~ /:$/ || $cc =~ /^:/)) {
4865 $ok = 1;
4866 }
4867
Joe Perches84731622013-11-12 15:10:05 -08004868 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004869 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004870 my $msg_level = \&ERROR;
4871 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004872
Jean Delvare0675a8f2017-09-08 16:16:07 -07004873 if (&{$msg_level}("SPACING",
4874 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004875 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4876 if (defined $fix_elements[$n + 2]) {
4877 $fix_elements[$n + 2] =~ s/^\s+//;
4878 }
Joe Perches3705ce52013-07-03 15:05:31 -07004879 $line_fixed = 1;
4880 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004881 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004882 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004883 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004884
4885## print("n: <$n> GOOD: <$good>\n");
4886
4887 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004888 }
Joe Perches3705ce52013-07-03 15:05:31 -07004889
4890 if (($#elements % 2) == 0) {
4891 $fixed_line = $fixed_line . $fix_elements[$#elements];
4892 }
4893
Joe Perches194f66f2014-08-06 16:11:03 -07004894 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4895 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004896 }
4897
4898
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004899 }
4900
Joe Perches786b6322013-07-03 15:05:32 -07004901# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004902 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004903 if (WARN("SPACING",
4904 "space prohibited before semicolon\n" . $herecurr) &&
4905 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004906 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004907 s/^(\+.*\S)\s+;/$1;/;
4908 }
4909 }
4910
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004911# check for multiple assignments
4912 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004913 CHK("MULTIPLE_ASSIGNMENTS",
4914 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004915 }
4916
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004917## # check for multiple declarations, allowing for a function declaration
4918## # continuation.
4919## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4920## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4921##
4922## # Remove any bracketed sections to ensure we do not
4923## # falsly report the parameters of functions.
4924## my $ln = $line;
4925## while ($ln =~ s/\([^\(\)]*\)//g) {
4926## }
4927## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004928## WARN("MULTIPLE_DECLARATION",
4929## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004930## }
4931## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004932
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004933#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004934 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004935 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004936 if (ERROR("SPACING",
4937 "space required before the open brace '{'\n" . $herecurr) &&
4938 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004939 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004940 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004941 }
4942
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004943## # check for blank lines before declarations
4944## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4945## $prevrawline =~ /^.\s*$/) {
4946## WARN("SPACING",
4947## "No blank lines before declarations\n" . $hereprev);
4948## }
4949##
4950
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004951# closing brace should have a space following it when it has anything
4952# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07004953 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004954 if (ERROR("SPACING",
4955 "space required after that close brace '}'\n" . $herecurr) &&
4956 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004957 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004958 s/}((?!(?:,|;|\)))\S)/} $1/;
4959 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004960 }
4961
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004962# check spacing on square brackets
4963 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004964 if (ERROR("SPACING",
4965 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4966 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004967 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004968 s/\[\s+/\[/;
4969 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004970 }
4971 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004972 if (ERROR("SPACING",
4973 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4974 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004975 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004976 s/\s+\]/\]/;
4977 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004978 }
4979
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004980# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004981 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4982 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004983 if (ERROR("SPACING",
4984 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4985 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004986 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004987 s/\(\s+/\(/;
4988 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004989 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004990 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004991 $line !~ /for\s*\(.*;\s+\)/ &&
4992 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004993 if (ERROR("SPACING",
4994 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4995 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004996 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004997 s/\s+\)/\)/;
4998 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004999 }
5000
Joe Perchese2826fd2014-08-06 16:10:48 -07005001# check unnecessary parentheses around addressof/dereference single $Lvals
5002# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5003
5004 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08005005 my $var = $1;
5006 if (CHK("UNNECESSARY_PARENTHESES",
5007 "Unnecessary parentheses around $var\n" . $herecurr) &&
5008 $fix) {
5009 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5010 }
5011 }
5012
5013# check for unnecessary parentheses around function pointer uses
5014# ie: (foo->bar)(); should be foo->bar();
5015# but not "if (foo->bar) (" to avoid some false positives
5016 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5017 my $var = $2;
5018 if (CHK("UNNECESSARY_PARENTHESES",
5019 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5020 $fix) {
5021 my $var2 = deparenthesize($var);
5022 $var2 =~ s/\s//g;
5023 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5024 }
5025 }
Joe Perchese2826fd2014-08-06 16:10:48 -07005026
Joe Perches63b7c732017-09-08 16:16:01 -07005027# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08005028# when !drivers/staging or command-line uses --strict
5029 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07005030 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07005031 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
5032 my $if_stat = $1;
5033 my $test = substr($2, 1, -1);
5034 my $herectx;
5035 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
5036 my $match = $1;
5037 # avoid parentheses around potential macro args
5038 next if ($match =~ /^\s*\w+\s*$/);
5039 if (!defined($herectx)) {
5040 $herectx = $here . "\n";
5041 my $cnt = statement_rawlines($if_stat);
5042 for (my $n = 0; $n < $cnt; $n++) {
5043 my $rl = raw_line($linenr, $n);
5044 $herectx .= $rl . "\n";
5045 last if $rl =~ /^[ \+].*\{/;
5046 }
5047 }
5048 CHK("UNNECESSARY_PARENTHESES",
5049 "Unnecessary parentheses around '$match'\n" . $herectx);
5050 }
5051 }
5052
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005053#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005054 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005055 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07005056 if (WARN("INDENTED_LABEL",
5057 "labels should not be indented\n" . $herecurr) &&
5058 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005059 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005060 s/^(.)\s+/$1/;
5061 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005062 }
5063
Joe Perches40873ab2020-10-15 20:11:56 -07005064# check if a statement with a comma should be two statements like:
5065# foo = bar(), /* comma should be semicolon */
5066# bar = baz();
5067 if (defined($stat) &&
5068 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5069 my $cnt = statement_rawlines($stat);
5070 my $herectx = get_stat_here($linenr, $cnt, $here);
5071 WARN("SUSPECT_COMMA_SEMICOLON",
5072 "Possible comma where semicolon could be used\n" . $herectx);
5073 }
5074
Joe Perches5b9553a2014-04-03 14:49:21 -07005075# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08005076 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005077 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07005078 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07005079 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5080 my $value = $1;
5081 $value = deparenthesize($value);
5082 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5083 ERROR("RETURN_PARENTHESES",
5084 "return is not a function, parentheses are not required\n" . $herecurr);
5085 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005086 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005087 ERROR("SPACING",
5088 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005089 }
5090 }
Joe Perches507e5142013-11-12 15:10:13 -08005091
Joe Perchesb43ae212014-06-23 13:22:07 -07005092# unnecessary return in a void function
5093# at end-of-function, with the previous line a single leading tab, then return;
5094# and the line before that not a goto label target like "out:"
5095 if ($sline =~ /^[ \+]}\s*$/ &&
5096 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5097 $linenr >= 3 &&
5098 $lines[$linenr - 3] =~ /^[ +]/ &&
5099 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07005100 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07005101 "void function return statements are not generally useful\n" . $hereprev);
5102 }
Joe Perches9819cf22014-06-04 16:12:09 -07005103
Joe Perches189248d2014-01-23 15:54:47 -08005104# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07005105 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08005106 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5107 my $openparens = $1;
5108 my $count = $openparens =~ tr@\(@\(@;
5109 my $msg = "";
5110 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5111 my $comp = $4; #Not $1 because of $LvalOrFunc
5112 $msg = " - maybe == should be = ?" if ($comp eq "==");
5113 WARN("UNNECESSARY_PARENTHESES",
5114 "Unnecessary parentheses$msg\n" . $herecurr);
5115 }
5116 }
5117
Joe Perchesc5595fa2015-09-09 15:37:58 -07005118# comparisons with a constant or upper case identifier on the left
5119# avoid cases like "foo + BAR < baz"
5120# only fix matches surrounded by parentheses to avoid incorrect
5121# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07005122 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005123 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5124 my $lead = $1;
5125 my $const = $2;
5126 my $comp = $3;
5127 my $to = $4;
5128 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07005129 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005130 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5131 WARN("CONSTANT_COMPARISON",
5132 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5133 $fix) {
5134 if ($comp eq "<") {
5135 $newcomp = ">";
5136 } elsif ($comp eq "<=") {
5137 $newcomp = ">=";
5138 } elsif ($comp eq ">") {
5139 $newcomp = "<";
5140 } elsif ($comp eq ">=") {
5141 $newcomp = "<=";
5142 }
5143 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5144 }
5145 }
5146
Joe Perchesf34e4a42015-04-16 12:44:19 -07005147# Return of what appears to be an errno should normally be negative
5148 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005149 my $name = $1;
5150 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07005151 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07005152 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005153 }
5154 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005155
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005156# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07005157 if ($line =~ /\b(if|while|for|switch)\(/) {
5158 if (ERROR("SPACING",
5159 "space required before the open parenthesis '('\n" . $herecurr) &&
5160 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005161 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005162 s/\b(if|while|for|switch)\(/$1 \(/;
5163 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005164 }
5165
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07005166# Check for illegal assignment in if conditional -- and check for trailing
5167# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005168 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08005169 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5170 ctx_statement_block($linenr, $realcnt, 0)
5171 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005172 my ($stat_next) = ctx_statement_block($line_nr_next,
5173 $remain_next, $off_next);
5174 $stat_next =~ s/\n./\n /g;
5175 ##print "stat<$stat> stat_next<$stat_next>\n";
5176
5177 if ($stat_next =~ /^\s*while\b/) {
5178 # If the statement carries leading newlines,
5179 # then count those as offsets.
5180 my ($whitespace) =
5181 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5182 my $offset =
5183 statement_rawlines($whitespace) - 1;
5184
5185 $suppress_whiletrailers{$line_nr_next +
5186 $offset} = 1;
5187 }
5188 }
5189 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005190 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005191 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005192 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005193
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005194 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches65b64b32020-08-11 18:35:10 -07005195 if (ERROR("ASSIGN_IN_IF",
5196 "do not use assignment in if condition\n" . $herecurr) &&
5197 $fix && $perl_version_ok) {
5198 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5199 my $space = $1;
5200 my $not = $2;
5201 my $statement = $3;
5202 my $assigned = $4;
5203 my $test = $8;
5204 my $against = $9;
5205 my $brace = $15;
5206 fix_delete_line($fixlinenr, $rawline);
5207 fix_insert_line($fixlinenr, "$space$statement;");
5208 my $newline = "${space}if (";
5209 $newline .= '!' if defined($not);
5210 $newline .= '(' if (defined $not && defined($test) && defined($against));
5211 $newline .= "$assigned";
5212 $newline .= " $test $against" if (defined($test) && defined($against));
5213 $newline .= ')' if (defined $not && defined($test) && defined($against));
5214 $newline .= ')';
5215 $newline .= " {" if (defined($brace));
5216 fix_insert_line($fixlinenr + 1, $newline);
5217 }
5218 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005219 }
5220
5221 # Find out what is on the end of the line after the
5222 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005223 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005224 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005225 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005226 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5227 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005228 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005229 # Find out how long the conditional actually is.
5230 my @newlines = ($c =~ /\n/gs);
5231 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005232 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005233
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005234 $stat_real = raw_line($linenr, $cond_lines)
5235 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005236 if (defined($stat_real) && $cond_lines > 1) {
5237 $stat_real = "[...]\n$stat_real";
5238 }
5239
Joe Perches000d1cc12011-07-25 17:13:25 -07005240 ERROR("TRAILING_STATEMENTS",
5241 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005242 }
5243 }
5244
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005245# Check for bitwise tests written as boolean
5246 if ($line =~ /
5247 (?:
5248 (?:\[|\(|\&\&|\|\|)
5249 \s*0[xX][0-9]+\s*
5250 (?:\&\&|\|\|)
5251 |
5252 (?:\&\&|\|\|)
5253 \s*0[xX][0-9]+\s*
5254 (?:\&\&|\|\||\)|\])
5255 )/x)
5256 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005257 WARN("HEXADECIMAL_BOOLEAN_TEST",
5258 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005259 }
5260
Andy Whitcroft8905a672007-11-28 16:21:06 -08005261# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005262 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5263 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005264 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005265 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005266 ERROR("TRAILING_STATEMENTS",
5267 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005268 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005269 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005270# if should not continue a brace
5271 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005272 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005273 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005274 $herecurr);
5275 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005276# case and default should not have general statements after them
5277 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5278 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005279 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005280 \s*return\s+
5281 )/xg)
5282 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005283 ERROR("TRAILING_STATEMENTS",
5284 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005285 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005286
5287 # Check for }<nl>else {, these must be at the same
5288 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005289 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5290 $previndent == $indent) {
5291 if (ERROR("ELSE_AFTER_BRACE",
5292 "else should follow close brace '}'\n" . $hereprev) &&
5293 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5294 fix_delete_line($fixlinenr - 1, $prevrawline);
5295 fix_delete_line($fixlinenr, $rawline);
5296 my $fixedline = $prevrawline;
5297 $fixedline =~ s/}\s*$//;
5298 if ($fixedline !~ /^\+\s*$/) {
5299 fix_insert_line($fixlinenr, $fixedline);
5300 }
5301 $fixedline = $rawline;
5302 $fixedline =~ s/^(.\s*)else/$1} else/;
5303 fix_insert_line($fixlinenr, $fixedline);
5304 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005305 }
5306
Joe Perches8b8856f2014-08-06 16:11:14 -07005307 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5308 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005309 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5310
5311 # Find out what is on the end of the line after the
5312 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005313 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005314 $s =~ s/\n.*//g;
5315
5316 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005317 if (ERROR("WHILE_AFTER_BRACE",
5318 "while should follow close brace '}'\n" . $hereprev) &&
5319 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5320 fix_delete_line($fixlinenr - 1, $prevrawline);
5321 fix_delete_line($fixlinenr, $rawline);
5322 my $fixedline = $prevrawline;
5323 my $trailing = $rawline;
5324 $trailing =~ s/^\+//;
5325 $trailing = trim($trailing);
5326 $fixedline =~ s/}\s*$/} $trailing/;
5327 fix_insert_line($fixlinenr, $fixedline);
5328 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005329 }
5330 }
5331
Joe Perches95e2c602013-07-03 15:05:20 -07005332#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005333 while ($line =~ m{($Constant|$Lval)}g) {
5334 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005335
Joe Perches95e2c602013-07-03 15:05:20 -07005336#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005337 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005338 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Łukasz Stelmach4104a202020-12-15 20:44:27 -08005339#Ignore some autogenerated defines and enum values
5340 $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005341#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005342 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005343#Ignore SI style variants like nS, mV and dB
5344#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5345 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005346#Ignore some three character SI units explicitly, like MiB and KHz
5347 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005348 while ($var =~ m{($Ident)}g) {
5349 my $word = $1;
5350 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005351 if ($check) {
5352 seed_camelcase_includes();
5353 if (!$file && !$camelcase_file_seeded) {
5354 seed_camelcase_file($realfile);
5355 $camelcase_file_seeded = 1;
5356 }
5357 }
Joe Perches7e781f62013-09-11 14:23:55 -07005358 if (!defined $camelcase{$word}) {
5359 $camelcase{$word} = 1;
5360 CHK("CAMELCASE",
5361 "Avoid CamelCase: <$word>\n" . $herecurr);
5362 }
Joe Perches34456862013-07-03 15:05:34 -07005363 }
Joe Perches323c1262012-12-17 16:02:07 -08005364 }
5365 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005366
5367#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005368 if ($line =~ /\#\s*define.*\\\s+$/) {
5369 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5370 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5371 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005372 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005373 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005374 }
5375
Fabian Frederick0e212e02015-04-16 12:44:25 -07005376# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5377# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005378 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005379 my $file = "$1.h";
5380 my $checkfile = "include/linux/$file";
5381 if (-f "$root/$checkfile" &&
5382 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005383 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005384 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005385 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5386 if ($asminclude > 0) {
5387 if ($realfile =~ m{^arch/}) {
5388 CHK("ARCH_INCLUDE_LINUX",
5389 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5390 } else {
5391 WARN("INCLUDE_LINUX",
5392 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5393 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005394 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005395 }
5396 }
5397
Andy Whitcroft653d4872007-06-23 17:16:34 -07005398# multi-statement macros should be enclosed in a do while loop, grab the
5399# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005400# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07005401 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5402 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005403 my $ln = $linenr;
5404 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005405 my ($off, $dstat, $dcond, $rest);
5406 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005407 my $has_flow_statement = 0;
5408 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005409 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005410 ctx_statement_block($linenr, $realcnt, 0);
5411 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005412 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005413 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005414
Joe Perches08a28432014-10-13 15:51:55 -07005415 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005416 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005417
Joe Perchesf59b64b2016-10-11 13:52:08 -07005418 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5419 my $define_args = $1;
5420 my $define_stmt = $dstat;
5421 my @def_args = ();
5422
5423 if (defined $define_args && $define_args ne "") {
5424 $define_args = substr($define_args, 1, length($define_args) - 2);
5425 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005426 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005427 @def_args = split(",", $define_args);
5428 }
5429
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005430 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005431 $dstat =~ s/\\\n.//g;
5432 $dstat =~ s/^\s*//s;
5433 $dstat =~ s/\s*$//s;
5434
5435 # Flatten any parentheses and braces
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005436 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5437 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5438 $dstat =~ s/.\[[^\[\]]*\]/1u/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005439 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005440 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005441
Antonio Borneo342d3d22020-04-06 20:11:01 -07005442 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005443 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5444 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005445 {
5446 }
5447
Joe Perches42e15292016-03-15 14:58:01 -07005448 # Make asm volatile uses seem like a generic function
5449 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5450
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005451 my $exceptions = qr{
5452 $Declare|
5453 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005454 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005455 DECLARE_PER_CPU|
5456 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005457 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005458 union|
5459 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005460 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005461 ^\"|\"$|
5462 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005463 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005464 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005465
5466 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005467 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005468 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005469
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005470 if ($dstat ne '' &&
5471 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5472 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005473 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005474 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005475 $dstat !~ /$exceptions/ &&
5476 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005477 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005478 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005479 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005480 $dstat !~ /^for\s*$Constant$/ && # for (...)
5481 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5482 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005483 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005484 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005485 {
Joe Perchese7955562017-05-08 15:55:48 -07005486 if ($dstat =~ /^\s*if\b/) {
5487 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5488 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5489 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005490 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5491 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5492 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005493 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005494 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005495 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005496
5497 }
Joe Perches52076492016-10-11 13:52:14 -07005498
5499 # Make $define_stmt single line, comment-free, etc
5500 my @stmt_array = split('\n', $define_stmt);
5501 my $first = 1;
5502 $define_stmt = "";
5503 foreach my $l (@stmt_array) {
5504 $l =~ s/\\$//;
5505 if ($first) {
5506 $define_stmt = $l;
5507 $first = 0;
5508 } elsif ($l =~ /^[\+ ]/) {
5509 $define_stmt .= substr($l, 1);
5510 }
5511 }
5512 $define_stmt =~ s/$;//g;
5513 $define_stmt =~ s/\s+/ /g;
5514 $define_stmt = trim($define_stmt);
5515
Joe Perchesf59b64b2016-10-11 13:52:08 -07005516# check if any macro arguments are reused (ignore '...' and 'type')
5517 foreach my $arg (@def_args) {
5518 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005519 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005520 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005521 $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 -07005522 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5523 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005524 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005525 if ($use_cnt > 1) {
5526 CHK("MACRO_ARG_REUSE",
5527 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005528 }
5529# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005530 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005531 ((defined($1) && $1 ne ',') ||
5532 (defined($2) && $2 ne ','))) {
5533 CHK("MACRO_ARG_PRECEDENCE",
5534 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005535 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005536 }
Joe Perches5023d342012-12-17 16:01:47 -08005537
Joe Perches08a28432014-10-13 15:51:55 -07005538# check for macros with flow control, but without ## concatenation
5539# ## concatenation is commonly a macro that defines a function so ignore those
5540 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005541 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005542 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005543
Joe Perches08a28432014-10-13 15:51:55 -07005544 WARN("MACRO_WITH_FLOW_CONTROL",
5545 "Macros with flow control statements should be avoided\n" . "$herectx");
5546 }
5547
Joe Perches481eb482012-12-17 16:01:56 -08005548# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005549
5550 } else {
5551 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005552 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5553 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005554 $line =~ /^\+.*\\$/) {
5555 WARN("LINE_CONTINUATIONS",
5556 "Avoid unnecessary line continuations\n" . $herecurr);
5557 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005558 }
5559
Joe Perchesb13edf72012-07-30 14:41:24 -07005560# do {} while (0) macro tests:
5561# single-statement macros do not need to be enclosed in do while (0) loop,
5562# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005563 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005564 $realfile !~ m@/vmlinux.lds.h$@ &&
5565 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5566 my $ln = $linenr;
5567 my $cnt = $realcnt;
5568 my ($off, $dstat, $dcond, $rest);
5569 my $ctx = '';
5570 ($dstat, $dcond, $ln, $cnt, $off) =
5571 ctx_statement_block($linenr, $realcnt, 0);
5572 $ctx = $dstat;
5573
5574 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005575 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005576
5577 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5578 my $stmts = $2;
5579 my $semis = $3;
5580
5581 $ctx =~ s/\n*$//;
5582 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005583 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005584
Joe Perchesac8e97f2012-08-21 16:15:53 -07005585 if (($stmts =~ tr/;/;/) == 1 &&
5586 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005587 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5588 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5589 }
5590 if (defined $semis && $semis ne "") {
5591 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5592 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5593 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005594 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5595 $ctx =~ s/\n*$//;
5596 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005597 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005598
5599 WARN("TRAILING_SEMICOLON",
5600 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005601 }
5602 }
5603
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005604# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005605 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5606 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005607 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005608 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005609 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5610 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005611 my @allowed = ();
5612 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005613 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005614 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005615 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005616 for my $chunk (@chunks) {
5617 my ($cond, $block) = @{$chunk};
5618
Andy Whitcroft773647a2008-03-28 14:15:58 -07005619 # If the condition carries leading newlines, then count those as offsets.
5620 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5621 my $offset = statement_rawlines($whitespace) - 1;
5622
Joe Perchesaad4f612012-03-23 15:02:19 -07005623 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005624 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5625
5626 # We have looked at and allowed this specific line.
5627 $suppress_ifbraces{$ln + $offset} = 1;
5628
5629 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005630 $ln += statement_rawlines($block) - 1;
5631
Andy Whitcroft773647a2008-03-28 14:15:58 -07005632 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005633
5634 $seen++ if ($block =~ /^\s*{/);
5635
Joe Perchesaad4f612012-03-23 15:02:19 -07005636 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005637 if (statement_lines($cond) > 1) {
5638 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005639 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005640 }
5641 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005642 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005643 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005644 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005645 if (statement_block_size($block) > 1) {
5646 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005647 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005648 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005649 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005650 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005651 if ($seen) {
5652 my $sum_allowed = 0;
5653 foreach (@allowed) {
5654 $sum_allowed += $_;
5655 }
5656 if ($sum_allowed == 0) {
5657 WARN("BRACES",
5658 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5659 } elsif ($sum_allowed != $allow &&
5660 $seen != $allow) {
5661 CHK("BRACES",
5662 "braces {} should be used on all arms of this statement\n" . $herectx);
5663 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005664 }
5665 }
5666 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005667 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005668 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005669 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005670
Andy Whitcroftcf655042008-03-04 14:28:20 -08005671 # Check the pre-context.
5672 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5673 #print "APW: ALLOWED: pre<$1>\n";
5674 $allowed = 1;
5675 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005676
5677 my ($level, $endln, @chunks) =
5678 ctx_statement_full($linenr, $realcnt, $-[0]);
5679
Andy Whitcroftcf655042008-03-04 14:28:20 -08005680 # Check the condition.
5681 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005682 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005683 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005684 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005685 }
5686 if (statement_lines($cond) > 1) {
5687 #print "APW: ALLOWED: cond<$cond>\n";
5688 $allowed = 1;
5689 }
5690 if ($block =~/\b(?:if|for|while)\b/) {
5691 #print "APW: ALLOWED: block<$block>\n";
5692 $allowed = 1;
5693 }
5694 if (statement_block_size($block) > 1) {
5695 #print "APW: ALLOWED: lines block<$block>\n";
5696 $allowed = 1;
5697 }
5698 # Check the post-context.
5699 if (defined $chunks[1]) {
5700 my ($cond, $block) = @{$chunks[1]};
5701 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005702 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005703 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005704 if ($block =~ /^\s*\{/) {
5705 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5706 $allowed = 1;
5707 }
5708 }
5709 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005710 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005711 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005712
Joe Perches000d1cc12011-07-25 17:13:25 -07005713 WARN("BRACES",
5714 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005715 }
5716 }
5717
Joe Perchese4c5bab2017-02-24 15:01:41 -08005718# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005719 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5720 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005721 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5722 }
5723
Joe Perches0979ae62012-12-17 16:01:59 -08005724# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005725 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005726 if (CHK("BRACES",
5727 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5728 $fix && $prevrawline =~ /^\+/) {
5729 fix_delete_line($fixlinenr - 1, $prevrawline);
5730 }
Joe Perches0979ae62012-12-17 16:01:59 -08005731 }
Joe Perches77b9a532013-07-03 15:05:29 -07005732 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005733 if (CHK("BRACES",
5734 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5735 $fix) {
5736 fix_delete_line($fixlinenr, $rawline);
5737 }
Joe Perches0979ae62012-12-17 16:01:59 -08005738 }
5739
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005740# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005741 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5742 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005743 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005744 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005745 }
5746
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005747# Check for user-visible strings broken across lines, which breaks the ability
5748# to grep for the string. Make exceptions when the previous string ends in a
5749# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5750# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005751 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005752 $prevline =~ /"\s*$/ &&
5753 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5754 if (WARN("SPLIT_STRING",
5755 "quoted string split across lines\n" . $hereprev) &&
5756 $fix &&
5757 $prevrawline =~ /^\+.*"\s*$/ &&
5758 $last_coalesced_string_linenr != $linenr - 1) {
5759 my $extracted_string = get_quoted_string($line, $rawline);
5760 my $comma_close = "";
5761 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5762 $comma_close = $1;
5763 }
5764
5765 fix_delete_line($fixlinenr - 1, $prevrawline);
5766 fix_delete_line($fixlinenr, $rawline);
5767 my $fixedline = $prevrawline;
5768 $fixedline =~ s/"\s*$//;
5769 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5770 fix_insert_line($fixlinenr - 1, $fixedline);
5771 $fixedline = $rawline;
5772 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5773 if ($fixedline !~ /\+\s*$/) {
5774 fix_insert_line($fixlinenr, $fixedline);
5775 }
5776 $last_coalesced_string_linenr = $linenr;
5777 }
5778 }
5779
5780# check for missing a space in a string concatenation
5781 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5782 WARN('MISSING_SPACE',
5783 "break quoted strings at a space character\n" . $hereprev);
5784 }
5785
Joe Perchese4b7d302017-05-08 15:55:51 -07005786# check for an embedded function name in a string when the function is known
5787# This does not work very well for -f --file checking as it depends on patch
5788# context providing the function name or a single line form for in-file
5789# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005790 if ($line =~ /^\+.*$String/ &&
5791 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005792 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5793 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005794 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005795 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005796 }
5797
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005798# check for spaces before a quoted newline
5799 if ($rawline =~ /^.*\".*\s\\n/) {
5800 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5801 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5802 $fix) {
5803 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5804 }
5805
5806 }
5807
Joe Perchesf17dba42014-10-13 15:51:51 -07005808# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005809 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5810 if (CHK("CONCATENATED_STRING",
5811 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5812 $fix) {
5813 while ($line =~ /($String)/g) {
5814 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5815 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5816 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5817 }
5818 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005819 }
5820
Joe Perches90ad30e2014-12-10 15:51:59 -08005821# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005822 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005823 if (WARN("STRING_FRAGMENTS",
5824 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5825 $fix) {
5826 while ($line =~ /($String)(?=\s*")/g) {
5827 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5828 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5829 }
5830 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005831 }
5832
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005833# check for non-standard and hex prefixed decimal printf formats
5834 my $show_L = 1; #don't show the same defect twice
5835 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005836 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005837 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005838 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005839 # check for %L
5840 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005841 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005842 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5843 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005844 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005845 # check for %Z
5846 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5847 WARN("PRINTF_Z",
5848 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5849 $show_Z = 0;
5850 }
5851 # check for 0x<decimal>
5852 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5853 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005854 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5855 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005856 }
5857
5858# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005859 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005860 WARN("LINE_CONTINUATIONS",
5861 "Avoid line continuations in quoted strings\n" . $herecurr);
5862 }
5863
Andy Whitcroft00df3442007-06-08 13:47:06 -07005864# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005865 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005866 WARN("IF_0",
5867 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5868 }
5869
5870# warn about #if 1
5871 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5872 WARN("IF_1",
5873 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005874 }
5875
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005876# check for needless "if (<foo>) fn(<foo>)" uses
5877 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005878 my $tested = quotemeta($1);
5879 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5880 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5881 my $func = $1;
5882 if (WARN('NEEDLESS_IF',
5883 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5884 $fix) {
5885 my $do_fix = 1;
5886 my $leading_tabs = "";
5887 my $new_leading_tabs = "";
5888 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5889 $leading_tabs = $1;
5890 } else {
5891 $do_fix = 0;
5892 }
5893 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5894 $new_leading_tabs = $1;
5895 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5896 $do_fix = 0;
5897 }
5898 } else {
5899 $do_fix = 0;
5900 }
5901 if ($do_fix) {
5902 fix_delete_line($fixlinenr - 1, $prevrawline);
5903 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5904 }
5905 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005906 }
5907 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005908
Joe Perchesebfdc402014-08-06 16:10:27 -07005909# check for unnecessary "Out of Memory" messages
5910 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5911 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5912 (defined $1 || defined $3) &&
5913 $linenr > 3) {
5914 my $testval = $2;
5915 my $testline = $lines[$linenr - 3];
5916
5917 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5918# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5919
Joe Perchese29a70f2019-03-07 16:28:35 -08005920 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5921 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005922 WARN("OOM_MESSAGE",
5923 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5924 }
5925 }
5926
Joe Perchesf78d98f2014-10-13 15:52:01 -07005927# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005928 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005929 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5930 my $level = $1;
5931 if (WARN("UNNECESSARY_KERN_LEVEL",
5932 "Possible unnecessary $level\n" . $herecurr) &&
5933 $fix) {
5934 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5935 }
5936 }
5937
Joe Perches45c55e92017-02-24 15:01:31 -08005938# check for logging continuations
5939 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5940 WARN("LOGGING_CONTINUATION",
5941 "Avoid logging continuation uses where feasible\n" . $herecurr);
5942 }
5943
Joe Perchesabb08a52014-12-10 15:51:46 -08005944# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005945 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005946 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5947 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5948 WARN("MASK_THEN_SHIFT",
5949 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5950 }
5951
Joe Perchesb75ac612014-12-10 15:52:02 -08005952# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005953 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005954 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5955 my $val = $1;
5956 my $equal = "!";
5957 $equal = "" if ($4 eq "!=");
5958 if (CHK("COMPARISON_TO_NULL",
5959 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5960 $fix) {
5961 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5962 }
5963 }
5964 }
5965
Joe Perches8716de32013-09-11 14:24:05 -07005966# check for bad placement of section $InitAttribute (e.g.: __initdata)
5967 if ($line =~ /(\b$InitAttribute\b)/) {
5968 my $attr = $1;
5969 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5970 my $ptr = $1;
5971 my $var = $2;
5972 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5973 ERROR("MISPLACED_INIT",
5974 "$attr should be placed after $var\n" . $herecurr)) ||
5975 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5976 WARN("MISPLACED_INIT",
5977 "$attr should be placed after $var\n" . $herecurr))) &&
5978 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005979 $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 -07005980 }
5981 }
5982 }
5983
Joe Perchese970b8842013-11-12 15:10:10 -08005984# check for $InitAttributeData (ie: __initdata) with const
5985 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5986 my $attr = $1;
5987 $attr =~ /($InitAttributePrefix)(.*)/;
5988 my $attr_prefix = $1;
5989 my $attr_type = $2;
5990 if (ERROR("INIT_ATTRIBUTE",
5991 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5992 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005993 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005994 s/$InitAttributeData/${attr_prefix}initconst/;
5995 }
5996 }
5997
5998# check for $InitAttributeConst (ie: __initconst) without const
5999 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6000 my $attr = $1;
6001 if (ERROR("INIT_ATTRIBUTE",
6002 "Use of $attr requires a separate use of const\n" . $herecurr) &&
6003 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006004 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08006005 /(^\+\s*(?:static\s+))/;
6006 $lead = rtrim($1);
6007 $lead = "$lead " if ($lead !~ /^\+$/);
6008 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07006009 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08006010 }
6011 }
6012
Joe Perchesc17893c2015-04-16 12:44:42 -07006013# check for __read_mostly with const non-pointer (should just be const)
6014 if ($line =~ /\b__read_mostly\b/ &&
6015 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6016 if (ERROR("CONST_READ_MOSTLY",
6017 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6018 $fix) {
6019 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6020 }
6021 }
6022
Joe Perchesfbdb8132014-04-03 14:49:14 -07006023# don't use __constant_<foo> functions outside of include/uapi/
6024 if ($realfile !~ m@^include/uapi/@ &&
6025 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6026 my $constant_func = $1;
6027 my $func = $constant_func;
6028 $func =~ s/^__constant_//;
6029 if (WARN("CONSTANT_CONVERSION",
6030 "$constant_func should be $func\n" . $herecurr) &&
6031 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006032 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07006033 }
6034 }
6035
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006036# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08006037 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07006038 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006039 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07006040 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006041 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006042 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07006043 }
6044 if ($delay > 2000) {
6045 WARN("LONG_UDELAY",
6046 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006047 }
6048 }
6049
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006050# warn about unexpectedly long msleep's
6051 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6052 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006053 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006054 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006055 }
6056 }
6057
Joe Perches36ec1932013-07-03 15:05:25 -07006058# check for comparisons of jiffies
6059 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6060 WARN("JIFFIES_COMPARISON",
6061 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6062 }
6063
Joe Perches9d7a34a2013-07-03 15:05:26 -07006064# check for comparisons of get_jiffies_64()
6065 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6066 WARN("JIFFIES_COMPARISON",
6067 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6068 }
6069
Andy Whitcroft00df3442007-06-08 13:47:06 -07006070# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006071# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07006072# print "#ifdef in C files should be avoided\n";
6073# print "$herecurr";
6074# $clean = 0;
6075# }
6076
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006077# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006078 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07006079 if (ERROR("SPACING",
6080 "exactly one space required after that #$1\n" . $herecurr) &&
6081 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006082 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07006083 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6084 }
6085
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006086 }
6087
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006088# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006089 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6090 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006091 my $which = $1;
6092 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006093 CHK("UNCOMMENTED_DEFINITION",
6094 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006095 }
6096 }
6097# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006098
6099 my $barriers = qr{
6100 mb|
6101 rmb|
Will Deaconad83ec62019-11-07 14:49:00 +00006102 wmb
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006103 }x;
6104 my $barrier_stems = qr{
6105 mb__before_atomic|
6106 mb__after_atomic|
6107 store_release|
6108 load_acquire|
6109 store_mb|
6110 (?:$barriers)
6111 }x;
6112 my $all_barriers = qr{
6113 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02006114 smp_(?:$barrier_stems)|
6115 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006116 }x;
6117
6118 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006119 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08006120 WARN("MEMORY_BARRIER",
6121 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006122 }
6123 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006124
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02006125 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6126
6127 if ($realfile !~ m@^include/asm-generic/@ &&
6128 $realfile !~ m@/barrier\.h$@ &&
6129 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6130 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6131 WARN("MEMORY_BARRIER",
6132 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6133 }
6134
Joe Perchescb426e92015-06-25 15:02:46 -07006135# check for waitqueue_active without a comment.
6136 if ($line =~ /\bwaitqueue_active\s*\(/) {
6137 if (!ctx_has_comment($first_line, $linenr)) {
6138 WARN("WAITQUEUE_ACTIVE",
6139 "waitqueue_active without comment\n" . $herecurr);
6140 }
6141 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006142
Marco Elver5099a722020-04-01 12:17:14 +02006143# check for data_race without a comment.
6144 if ($line =~ /\bdata_race\s*\(/) {
6145 if (!ctx_has_comment($first_line, $linenr)) {
6146 WARN("DATA_RACE",
6147 "data_race without comment\n" . $herecurr);
6148 }
6149 }
6150
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006151# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006152 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006153 CHK("ARCH_DEFINES",
6154 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006155 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006156
Joe Perches596ed452017-07-12 14:37:02 -07006157# check that the storage class is not after a type
6158 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006159 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07006160 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6161 }
6162# Check that the storage class is at the beginning of a declaration
6163 if ($line =~ /\b$Storage\b/ &&
6164 $line !~ /^.\s*$Storage/ &&
6165 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6166 $1 !~ /[\,\)]\s*$/) {
6167 WARN("STORAGE_CLASS",
6168 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07006169 }
6170
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006171# check the location of the inline attribute, that it is between
6172# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006173 if ($line =~ /\b$Type\s+$Inline\b/ ||
6174 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006175 ERROR("INLINE_LOCATION",
6176 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006177 }
6178
Andy Whitcroft8905a672007-11-28 16:21:06 -08006179# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08006180 if ($realfile !~ m@\binclude/uapi/@ &&
6181 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006182 if (WARN("INLINE",
6183 "plain inline is preferred over $1\n" . $herecurr) &&
6184 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006185 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006186
6187 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006188 }
6189
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006190# Check for compiler attributes
Joe Perches2b7ab452013-11-12 15:10:14 -08006191 if ($realfile !~ m@\binclude/uapi/@ &&
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006192 $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
6193 my $attr = $1;
6194 $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
Joe Perches3d130fd2011-01-12 17:00:00 -08006195
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006196 my %attr_list = (
6197 "aligned" => "__aligned",
6198 "always_inline" => "__always_inline",
6199 "assume_aligned" => "__assume_aligned",
6200 "cold" => "__cold",
6201 "const" => "__attribute_const__",
6202 "copy" => "__copy",
6203 "designated_init" => "__designated_init",
6204 "externally_visible" => "__visible",
6205 "format" => "printf|scanf",
6206 "gnu_inline" => "__gnu_inline",
6207 "malloc" => "__malloc",
6208 "mode" => "__mode",
6209 "no_caller_saved_registers" => "__no_caller_saved_registers",
6210 "noclone" => "__noclone",
6211 "noinline" => "noinline",
6212 "nonstring" => "__nonstring",
6213 "noreturn" => "__noreturn",
6214 "packed" => "__packed",
6215 "pure" => "__pure",
6216 "used" => "__used"
6217 );
Joe Perches39b7e282011-07-25 17:13:24 -07006218
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006219 my @conv_array = ();
6220 my $conv_possible = 1;
6221
6222 while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
6223 my $curr_attr = $1;
6224 my $params = '';
6225 $params = $2 if defined($2);
6226 $curr_attr =~ s/^[\s_]+|[\s_]+$//g;
6227
6228 if (exists($attr_list{$curr_attr})) {
6229 if ($curr_attr eq "format" && $params) {
6230 $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
6231 push(@conv_array, "__$1\($2");
6232 } else {
6233 my $new = $attr_list{$curr_attr};
6234 push(@conv_array, "$new$params");
6235 }
6236 } else {
6237 $conv_possible = 0;
6238 last;
6239 }
Joe Perches462811d2019-09-25 16:46:44 -07006240 }
Joe Perches462811d2019-09-25 16:46:44 -07006241
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006242 if (scalar @conv_array > 0 && $conv_possible == 1) {
6243 my $replace = join(' ', @conv_array);
6244 if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6245 "$replace is preferred over __attribute__(($attr))\n" . $herecurr) &&
6246 $fix) {
6247 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*\Q$attr\E\s*\)\s*\)/$replace/;
6248 $fixed[$fixlinenr] =~ s/\}\Q$replace\E/} $replace/;
6249 }
Joe Perchesd5e616f2013-09-11 14:23:54 -07006250 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08006251
Dwaipayan Ray7ebe1d12020-12-15 20:44:36 -08006252 # Check for __attribute__ section, prefer __section
6253 if ($attr =~ /^_*section_*\s*\(\s*("[^"]*")/) {
6254 my $old = substr($attr, $-[1], $+[1] - $-[1]);
6255 my $new = substr($old, 1, -1);
6256 if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6257 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6258 $fix) {
6259 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6260 }
6261 }
6262
6263 # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
6264 if ($attr =~ /^_*unused/) {
6265 WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6266 "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
Joe Perchesd5e616f2013-09-11 14:23:54 -07006267 }
Joe Perches6061d942012-03-23 15:02:16 -07006268 }
6269
Joe Perches619a9082014-12-10 15:51:35 -08006270# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006271 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006272 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6273 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6274 $line =~ /\b__weak\b/)) {
6275 ERROR("WEAK_DECLARATION",
6276 "Using weak declarations can have unintended link defects\n" . $herecurr);
6277 }
6278
Tomas Winklerfd39f902016-12-12 16:46:34 -08006279# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006280 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006281 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006282 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6283 my $type = $1;
6284 if ($type =~ /\b($typeC99Typedefs)\b/) {
6285 $type = $1;
6286 my $kernel_type = 'u';
6287 $kernel_type = 's' if ($type =~ /^_*[si]/);
6288 $type =~ /(\d+)/;
6289 $kernel_type .= $1;
6290 if (CHK("PREFER_KERNEL_TYPES",
6291 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6292 $fix) {
6293 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6294 }
6295 }
6296 }
6297
Joe Perches938224b2016-01-20 14:59:15 -08006298# check for cast of C90 native int or longer types constants
6299 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6300 my $cast = $1;
6301 my $const = $2;
6302 if (WARN("TYPECAST_INT_CONSTANT",
6303 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6304 $fix) {
6305 my $suffix = "";
6306 my $newconst = $const;
6307 $newconst =~ s/${Int_type}$//;
6308 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6309 if ($cast =~ /\blong\s+long\b/) {
6310 $suffix .= 'LL';
6311 } elsif ($cast =~ /\blong\b/) {
6312 $suffix .= 'L';
6313 }
6314 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6315 }
6316 }
6317
Joe Perches8f53a9b2010-03-05 13:43:48 -08006318# check for sizeof(&)
6319 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006320 WARN("SIZEOF_ADDRESS",
6321 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006322 }
6323
Joe Perches66c80b62012-07-30 14:41:22 -07006324# check for sizeof without parenthesis
6325 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006326 if (WARN("SIZEOF_PARENTHESIS",
6327 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6328 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006329 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006330 }
Joe Perches66c80b62012-07-30 14:41:22 -07006331 }
6332
Joe Perches88982fe2012-12-17 16:02:00 -08006333# check for struct spinlock declarations
6334 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6335 WARN("USE_SPINLOCK_T",
6336 "struct spinlock should be spinlock_t\n" . $herecurr);
6337 }
6338
Joe Perchesa6962d72013-04-29 16:18:13 -07006339# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006340 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006341 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006342 $fmt =~ s/%%//g;
6343 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006344 if (WARN("PREFER_SEQ_PUTS",
6345 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6346 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006347 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006348 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006349 }
6350 }
6351
Joe Perches478b1792018-04-10 16:33:34 -07006352# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006353 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006354 defined $stat &&
6355 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6356 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006357 my $stat_real;
6358
Joe Perches0b523762017-05-08 15:55:36 -07006359 my $lc = $stat =~ tr@\n@@;
6360 $lc = $lc + $linenr;
6361 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006362 my $specifier;
6363 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006364 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006365 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006366 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6367 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006368
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006369 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006370 $specifier = $1;
6371 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006372 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006373 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006374 ($extension eq "f" &&
6375 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006376 $bad_specifier = $specifier;
6377 last;
6378 }
6379 if ($extension eq "x" && !defined($stat_real)) {
6380 if (!defined($stat_real)) {
6381 $stat_real = get_stat_real($linenr, $lc);
6382 }
6383 WARN("VSPRINTF_SPECIFIER_PX",
6384 "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");
6385 }
6386 }
6387 if ($bad_specifier ne "") {
6388 my $stat_real = get_stat_real($linenr, $lc);
6389 my $ext_type = "Invalid";
6390 my $use = "";
6391 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006392 $use = " - use %pS instead";
6393 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6394 }
6395
6396 WARN("VSPRINTF_POINTER_EXTENSION",
6397 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6398 }
Joe Perches0b523762017-05-08 15:55:36 -07006399 }
6400 }
6401
Andy Whitcroft554e1652012-01-10 15:09:57 -08006402# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006403 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006404 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006405 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006406
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006407 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006408 my $ms_val = $7;
6409 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006410
Andy Whitcroft554e1652012-01-10 15:09:57 -08006411 if ($ms_size =~ /^(0x|)0$/i) {
6412 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006413 "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 -08006414 } elsif ($ms_size =~ /^(0x|)1$/i) {
6415 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006416 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6417 }
6418 }
6419
Joe Perches98a9bba2014-01-23 15:54:52 -08006420# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006421# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006422# defined $stat &&
6423# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6424# if (WARN("PREFER_ETHER_ADDR_COPY",
6425# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6426# $fix) {
6427# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6428# }
6429# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006430
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006431# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006432# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006433# defined $stat &&
6434# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6435# WARN("PREFER_ETHER_ADDR_EQUAL",
6436# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6437# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006438
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006439# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6440# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006441# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006442# defined $stat &&
6443# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6444#
6445# my $ms_val = $7;
6446#
6447# if ($ms_val =~ /^(?:0x|)0+$/i) {
6448# if (WARN("PREFER_ETH_ZERO_ADDR",
6449# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6450# $fix) {
6451# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6452# }
6453# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6454# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6455# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6456# $fix) {
6457# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6458# }
6459# }
6460# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006461
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006462# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006463 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006464 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006465 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006466 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006467 my $call = $1;
6468 my $cast1 = deparenthesize($2);
6469 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006470 my $cast2 = deparenthesize($7);
6471 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006472 my $cast;
6473
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006474 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006475 $cast = "$cast1 or $cast2";
6476 } elsif ($cast1 ne "") {
6477 $cast = $cast1;
6478 } else {
6479 $cast = $cast2;
6480 }
6481 WARN("MINMAX",
6482 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006483 }
6484 }
6485
Joe Perches4a273192012-07-30 14:41:20 -07006486# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006487 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006488 defined $stat &&
6489 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6490 my $min = $1;
6491 my $max = $7;
6492 if ($min eq $max) {
6493 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006494 "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 -07006495 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6496 $min > $max) {
6497 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006498 "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 -07006499 }
6500 }
6501
Joe Perches823b7942013-11-12 15:10:15 -08006502# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006503 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006504 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006505 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006506 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6507 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6508 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6509 my $lc = $stat =~ tr@\n@@;
6510 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006511 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006512 WARN("NAKED_SSCANF",
6513 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6514 }
6515
Joe Perchesafc819a2014-06-04 16:12:08 -07006516# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006517 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006518 defined $stat &&
6519 $line =~ /\bsscanf\b/) {
6520 my $lc = $stat =~ tr@\n@@;
6521 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006522 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006523 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6524 my $format = $6;
6525 my $count = $format =~ tr@%@%@;
6526 if ($count == 1 &&
6527 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6528 WARN("SSCANF_TO_KSTRTO",
6529 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6530 }
6531 }
6532 }
6533
Joe Perches70dc8a42013-09-11 14:23:58 -07006534# check for new externs in .h files.
6535 if ($realfile =~ /\.h$/ &&
6536 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006537 if (CHK("AVOID_EXTERNS",
6538 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006539 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006540 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006541 }
6542 }
6543
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006544# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006545 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006546 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006547 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006548 my $function_name = $1;
6549 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006550
6551 my $s = $stat;
6552 if (defined $cond) {
6553 substr($s, 0, length($cond), '');
6554 }
Kees Cookd8b44b52020-06-03 14:18:09 -07006555 if ($s =~ /^\s*;/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006556 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006557 WARN("AVOID_EXTERNS",
6558 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006559 }
6560
6561 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006562 WARN("FUNCTION_ARGUMENTS",
6563 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006564 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006565
6566 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6567 $stat =~ /^.\s*extern\s+/)
6568 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006569 WARN("AVOID_EXTERNS",
6570 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006571 }
6572
Joe Perchesa0ad7592017-07-10 15:52:19 -07006573# check for function declarations that have arguments without identifier names
6574 if (defined $stat &&
Kees Cookd8b44b52020-06-03 14:18:09 -07006575 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6576 $1 ne "void") {
6577 my $args = trim($1);
Joe Perchesca0d8922016-10-11 13:52:16 -07006578 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6579 my $arg = trim($1);
Kees Cookd8b44b52020-06-03 14:18:09 -07006580 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006581 WARN("FUNCTION_ARGUMENTS",
6582 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6583 }
6584 }
6585 }
6586
Joe Perchesa0ad7592017-07-10 15:52:19 -07006587# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006588 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006589 defined $stat &&
6590 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6591 $context_function = $1;
6592
6593# check for multiline function definition with misplaced open brace
6594 my $ok = 0;
6595 my $cnt = statement_rawlines($stat);
6596 my $herectx = $here . "\n";
6597 for (my $n = 0; $n < $cnt; $n++) {
6598 my $rl = raw_line($linenr, $n);
6599 $herectx .= $rl . "\n";
6600 $ok = 1 if ($rl =~ /^[ \+]\{/);
6601 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6602 last if $rl =~ /^[ \+].*\{/;
6603 }
6604 if (!$ok) {
6605 ERROR("OPEN_BRACE",
6606 "open brace '{' following function definitions go on the next line\n" . $herectx);
6607 }
6608 }
6609
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006610# checks for new __setup's
6611 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6612 my $name = $1;
6613
6614 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006615 CHK("UNDOCUMENTED_SETUP",
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07006616 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006617 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006618 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006619
Joe Perchese29a70f2019-03-07 16:28:35 -08006620# check for pointless casting of alloc functions
6621 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006622 WARN("UNNECESSARY_CASTS",
6623 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006624 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006625
Joe Perchesa640d252013-07-03 15:05:21 -07006626# alloc style
6627# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006628 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006629 $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 -07006630 CHK("ALLOC_SIZEOF_STRUCT",
6631 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6632 }
6633
Joe Perches60a55362014-06-04 16:12:07 -07006634# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006635 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006636 defined $stat &&
6637 $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 -07006638 my $oldfunc = $3;
6639 my $a1 = $4;
6640 my $a2 = $10;
6641 my $newfunc = "kmalloc_array";
6642 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006643 my $r1 = $a1;
6644 my $r2 = $a2;
6645 if ($a1 =~ /^sizeof\s*\S/) {
6646 $r1 = $a2;
6647 $r2 = $a1;
6648 }
6649 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6650 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006651 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006652 my $herectx = get_stat_here($linenr, $cnt, $here);
6653
Joe Perches60a55362014-06-04 16:12:07 -07006654 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006655 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6656 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006657 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006658 $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 -07006659 }
6660 }
6661 }
6662
Joe Perches972fdea2013-04-29 16:18:12 -07006663# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006664 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006665 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6666 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006667 WARN("KREALLOC_ARG_REUSE",
6668 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6669 }
6670
Joe Perches5ce59ae2013-02-21 16:44:18 -08006671# check for alloc argument mismatch
6672 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6673 WARN("ALLOC_ARRAY_ARGS",
6674 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6675 }
6676
Joe Perchescaf2a542011-01-12 16:59:56 -08006677# check for multiple semicolons
6678 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006679 if (WARN("ONE_SEMICOLON",
6680 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6681 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006682 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006683 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006684 }
6685
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006686# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6687 if ($realfile !~ m@^include/uapi/@ &&
6688 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006689 my $ull = "";
6690 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6691 if (CHK("BIT_MACRO",
6692 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6693 $fix) {
6694 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6695 }
6696 }
6697
Joe Perches50161262020-08-11 18:35:07 -07006698# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006699 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
Joe Perches50161262020-08-11 18:35:07 -07006700 WARN("IS_ENABLED_CONFIG",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006701 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
Joe Perches50161262020-08-11 18:35:07 -07006702 }
6703
Joe Perches2d632742016-05-20 17:04:00 -07006704# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006705 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 -07006706 my $config = $1;
6707 if (WARN("PREFER_IS_ENABLED",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006708 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
Joe Perches2d632742016-05-20 17:04:00 -07006709 $fix) {
6710 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6711 }
6712 }
6713
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006714# check for /* fallthrough */ like comment, prefer fallthrough;
6715 my @fallthroughs = (
6716 'fallthrough',
6717 '@fallthrough@',
6718 'lint -fallthrough[ \t]*',
6719 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6720 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6721 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6722 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6723 );
6724 if ($raw_comment ne '') {
6725 foreach my $ft (@fallthroughs) {
6726 if ($raw_comment =~ /$ft/) {
6727 my $msg_level = \&WARN;
6728 $msg_level = \&CHK if ($file);
6729 &{$msg_level}("PREFER_FALLTHROUGH",
6730 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6731 last;
6732 }
6733 }
6734 }
6735
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006736# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006737 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006738 defined $stat &&
6739 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006740 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006741 my $herectx = get_stat_here($linenr, $cnt, $here);
6742
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006743 WARN("DEFAULT_NO_BREAK",
6744 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006745 }
6746
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006747# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006748 if ($line =~ /\b__FUNCTION__\b/) {
6749 if (WARN("USE_FUNC",
6750 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6751 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006752 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006753 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006754 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006755
Joe Perches62ec8182015-02-13 14:38:18 -08006756# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6757 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6758 ERROR("DATE_TIME",
6759 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6760 }
6761
Joe Perches2c924882012-03-23 15:02:20 -07006762# check for use of yield()
6763 if ($line =~ /\byield\s*\(\s*\)/) {
6764 WARN("YIELD",
6765 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6766 }
6767
Joe Perches179f8f42013-07-03 15:05:30 -07006768# check for comparisons against true and false
6769 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6770 my $lead = $1;
6771 my $arg = $2;
6772 my $test = $3;
6773 my $otype = $4;
6774 my $trail = $5;
6775 my $op = "!";
6776
6777 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6778
6779 my $type = lc($otype);
6780 if ($type =~ /^(?:true|false)$/) {
6781 if (("$test" eq "==" && "$type" eq "true") ||
6782 ("$test" eq "!=" && "$type" eq "false")) {
6783 $op = "";
6784 }
6785
6786 CHK("BOOL_COMPARISON",
6787 "Using comparison to $otype is error prone\n" . $herecurr);
6788
6789## maybe suggesting a correct construct would better
6790## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6791
6792 }
6793 }
6794
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006795# check for semaphores initialized locked
6796 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006797 WARN("CONSIDER_COMPLETION",
6798 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006799 }
Joe Perches6712d852012-03-23 15:02:20 -07006800
Joe Perches67d0a072011-10-31 17:13:10 -07006801# recommend kstrto* over simple_strto* and strict_strto*
6802 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006803 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006804 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006805 }
Joe Perches6712d852012-03-23 15:02:20 -07006806
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006807# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006808 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006809 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006810 "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 -07006811 }
Joe Perches6712d852012-03-23 15:02:20 -07006812
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006813# check for spin_is_locked(), suggest lockdep instead
6814 if ($line =~ /\bspin_is_locked\(/) {
6815 WARN("USE_LOCKDEP",
6816 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6817 }
6818
Joe Perches9189c7e2018-09-07 15:26:18 -07006819# check for deprecated apis
6820 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6821 my $deprecated_api = $1;
6822 my $new_api = $deprecated_apis{$deprecated_api};
6823 WARN("DEPRECATED_API",
6824 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6825 }
6826
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006827# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006828# and avoid what seem like struct definitions 'struct foo {'
Quentin Monnetced69da12020-08-11 18:35:13 -07006829 if (defined($const_structs) &&
6830 $line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006831 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006832 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006833 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006834 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006835
6836# use of NR_CPUS is usually wrong
6837# ignore definitions of NR_CPUS and usage to define arrays as likely right
6838 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006839 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6840 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006841 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6842 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6843 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006844 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006845 WARN("NR_CPUS",
6846 "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 -07006847 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006848
Joe Perches52ea8502013-11-12 15:10:09 -08006849# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6850 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6851 ERROR("DEFINE_ARCH_HAS",
6852 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6853 }
6854
Joe Perchesacd93622015-02-13 14:38:38 -08006855# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006856 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006857 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6858 WARN("LIKELY_MISUSE",
6859 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6860 }
6861
Denis Efremovde3f1862019-09-25 16:49:25 -07006862# nested likely/unlikely calls
6863 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6864 WARN("LIKELY_MISUSE",
6865 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6866 }
6867
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006868# whine mightly about in_atomic
6869 if ($line =~ /\bin_atomic\s*\(/) {
6870 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006871 ERROR("IN_ATOMIC",
6872 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006873 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006874 WARN("IN_ATOMIC",
6875 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006876 }
6877 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006878
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006879# check for mutex_trylock_recursive usage
6880 if ($line =~ /mutex_trylock_recursive/) {
6881 ERROR("LOCKING",
6882 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6883 }
6884
Peter Zijlstra1704f472010-03-19 01:37:42 +01006885# check for lockdep_set_novalidate_class
6886 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6887 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6888 if ($realfile !~ m@^kernel/lockdep@ &&
6889 $realfile !~ m@^include/linux/lockdep@ &&
6890 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006891 ERROR("LOCKDEP",
6892 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006893 }
6894 }
Dave Jones88f88312011-01-12 16:59:59 -08006895
Joe Perchesb392c642015-04-16 12:44:16 -07006896 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6897 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006898 WARN("EXPORTED_WORLD_WRITABLE",
6899 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006900 }
Joe Perches24358802014-04-03 14:49:13 -07006901
Joe Perches00180462018-02-06 15:38:55 -08006902# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6903# and whether or not function naming is typical and if
6904# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006905 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006906 defined $stat &&
6907 $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*\)/) {
6908 my $var = $1;
6909 my $perms = $2;
6910 my $show = $3;
6911 my $store = $4;
6912 my $octal_perms = perms_to_octal($perms);
6913 if ($show =~ /^${var}_show$/ &&
6914 $store =~ /^${var}_store$/ &&
6915 $octal_perms eq "0644") {
6916 if (WARN("DEVICE_ATTR_RW",
6917 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6918 $fix) {
6919 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6920 }
6921 } elsif ($show =~ /^${var}_show$/ &&
6922 $store =~ /^NULL$/ &&
6923 $octal_perms eq "0444") {
6924 if (WARN("DEVICE_ATTR_RO",
6925 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6926 $fix) {
6927 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6928 }
6929 } elsif ($show =~ /^NULL$/ &&
6930 $store =~ /^${var}_store$/ &&
6931 $octal_perms eq "0200") {
6932 if (WARN("DEVICE_ATTR_WO",
6933 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6934 $fix) {
6935 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6936 }
6937 } elsif ($octal_perms eq "0644" ||
6938 $octal_perms eq "0444" ||
6939 $octal_perms eq "0200") {
6940 my $newshow = "$show";
6941 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6942 my $newstore = $store;
6943 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6944 my $rename = "";
6945 if ($show ne $newshow) {
6946 $rename .= " '$show' to '$newshow'";
6947 }
6948 if ($store ne $newstore) {
6949 $rename .= " '$store' to '$newstore'";
6950 }
6951 WARN("DEVICE_ATTR_FUNCTIONS",
6952 "Consider renaming function(s)$rename\n" . $herecurr);
6953 } else {
6954 WARN("DEVICE_ATTR_PERMS",
6955 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6956 }
6957 }
6958
Joe Perches515a2352014-04-03 14:49:24 -07006959# Mode permission misuses where it seems decimal should be octal
6960# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006961# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6962# specific definition of not visible in sysfs.
6963# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6964# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006965 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006966 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006967 $line =~ /$mode_perms_search/) {
6968 foreach my $entry (@mode_permission_funcs) {
6969 my $func = $entry->[0];
6970 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006971
Joe Perches459cf0a2016-10-11 13:52:19 -07006972 my $lc = $stat =~ tr@\n@@;
6973 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006974 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006975
Joe Perches515a2352014-04-03 14:49:24 -07006976 my $skip_args = "";
6977 if ($arg_pos > 1) {
6978 $arg_pos--;
6979 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6980 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006981 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006982 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006983 my $val = $1;
6984 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006985 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6986 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6987 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006988 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006989 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006990 }
6991 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006992 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006993 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006994 }
Joe Perches24358802014-04-03 14:49:13 -07006995 }
6996 }
6997 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006998
Joe Perches459cf0a2016-10-11 13:52:19 -07006999# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07007000 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08007001 my $oval = $1;
7002 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07007003 if (WARN("SYMBOLIC_PERMS",
7004 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7005 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08007006 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07007007 }
7008 }
7009
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07007010# validate content of MODULE_LICENSE against list from include/linux/module.h
7011 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
7012 my $extracted_string = get_quoted_string($line, $rawline);
7013 my $valid_licenses = qr{
7014 GPL|
7015 GPL\ v2|
7016 GPL\ and\ additional\ rights|
7017 Dual\ BSD/GPL|
7018 Dual\ MIT/GPL|
7019 Dual\ MPL/GPL|
7020 Proprietary
7021 }x;
7022 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
7023 WARN("MODULE_LICENSE",
7024 "unknown module license " . $extracted_string . "\n" . $herecurr);
7025 }
7026 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07007027
7028# check for sysctl duplicate constants
7029 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
7030 WARN("DUPLICATED_SYSCTL_CONST",
7031 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
7032 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007033 }
7034
7035 # If we have no input at all, then there is nothing to report on
7036 # so just keep quiet.
7037 if ($#rawlines == -1) {
7038 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007039 }
7040
Andy Whitcroft8905a672007-11-28 16:21:06 -08007041 # In mailback mode only produce a report in the negative, for
7042 # things that appear to be patches.
7043 if ($mailback && ($clean == 1 || !$is_patch)) {
7044 exit(0);
7045 }
7046
7047 # This is not a patch, and we are are in 'no-patch' mode so
7048 # just keep quiet.
7049 if (!$chk_patch && !$is_patch) {
7050 exit(0);
7051 }
7052
Stafford Hornea08ffbe2017-10-03 16:16:51 -07007053 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07007054 ERROR("NOT_UNIFIED_DIFF",
7055 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007056 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007057 if ($is_patch && $has_commit_log && $chk_signoff) {
7058 if ($signoff == 0) {
7059 ERROR("MISSING_SIGN_OFF",
7060 "Missing Signed-off-by: line(s)\n");
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07007061 } elsif ($authorsignoff != 1) {
7062 # authorsignoff values:
7063 # 0 -> missing sign off
7064 # 1 -> sign off identical
7065 # 2 -> names and addresses match, comments mismatch
7066 # 3 -> addresses match, names different
7067 # 4 -> names match, addresses different
7068 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
7069
7070 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
7071
7072 if ($authorsignoff == 0) {
7073 ERROR("NO_AUTHOR_SIGN_OFF",
7074 "Missing Signed-off-by: line by nominal patch author '$author'\n");
7075 } elsif ($authorsignoff == 2) {
7076 CHK("FROM_SIGN_OFF_MISMATCH",
7077 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7078 } elsif ($authorsignoff == 3) {
7079 WARN("FROM_SIGN_OFF_MISMATCH",
7080 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7081 } elsif ($authorsignoff == 4) {
7082 WARN("FROM_SIGN_OFF_MISMATCH",
7083 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7084 } elsif ($authorsignoff == 5) {
7085 WARN("FROM_SIGN_OFF_MISMATCH",
7086 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7087 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007088 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007089 }
7090
Andy Whitcroft8905a672007-11-28 16:21:06 -08007091 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007092 if ($summary && !($clean == 1 && $quiet == 1)) {
7093 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08007094 print "total: $cnt_error errors, $cnt_warn warnings, " .
7095 (($check)? "$cnt_chk checks, " : "") .
7096 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07007097 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08007098
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007099 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07007100 # If there were any defects found and not already fixing them
7101 if (!$clean and !$fix) {
7102 print << "EOM"
7103
7104NOTE: For some of the reported defects, checkpatch may be able to
7105 mechanically convert to the typical style using --fix or --fix-inplace.
7106EOM
7107 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007108 # If there were whitespace errors which cleanpatch can fix
7109 # then suggest that.
7110 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07007111 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07007112 print << "EOM"
7113
7114NOTE: Whitespace errors detected.
7115 You may wish to use scripts/cleanpatch or scripts/cleanfile
7116EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007117 }
7118 }
7119
Joe Perchesd752fcc2014-08-06 16:11:05 -07007120 if ($clean == 0 && $fix &&
7121 ("@rawlines" ne "@fixed" ||
7122 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08007123 my $newfile = $filename;
7124 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07007125 my $linecount = 0;
7126 my $f;
7127
Joe Perchesd752fcc2014-08-06 16:11:05 -07007128 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7129
Joe Perches3705ce52013-07-03 15:05:31 -07007130 open($f, '>', $newfile)
7131 or die "$P: Can't open $newfile for write\n";
7132 foreach my $fixed_line (@fixed) {
7133 $linecount++;
7134 if ($file) {
7135 if ($linecount > 3) {
7136 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07007137 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07007138 }
7139 } else {
7140 print $f $fixed_line . "\n";
7141 }
7142 }
7143 close($f);
7144
7145 if (!$quiet) {
7146 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07007147
Joe Perches3705ce52013-07-03 15:05:31 -07007148Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
7149
7150Do _NOT_ trust the results written to this file.
7151Do _NOT_ submit these changes without inspecting them for correctness.
7152
7153This EXPERIMENTAL file is simply a convenience to help rewrite patches.
7154No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07007155EOM
7156 }
7157 }
7158
Joe Perchesd8469f12015-06-25 15:03:00 -07007159 if ($quiet == 0) {
7160 print "\n";
7161 if ($clean == 1) {
7162 print "$vname has no obvious style problems and is ready for submission.\n";
7163 } else {
7164 print "$vname has style problems, please review.\n";
7165 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007166 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007167 return $clean;
7168}