blob: 10d75da049474d20b0d1fac5c95cc22dd9ed8597 [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;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080046my %debug;
Joe Perches34456862013-07-03 15:05:34 -070047my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070048my %use_type = ();
49my @use = ();
50my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070051my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070052my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070053my $configuration_file = ".checkpatch.conf";
Joe Perchesbdc48fa2020-05-29 16:12:21 -070054my $max_line_length = 100;
Dave Hansend62a2012013-09-11 14:23:56 -070055my $ignore_perl_version = 0;
56my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070057my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070058my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070059my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070060my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070061my $conststructsfile = "$D/const_structs.checkpatch";
Jerome Forissier75ad8c52017-05-08 15:56:00 -070062my $typedefsfile = "";
John Brooks737c0762017-07-10 15:52:24 -070063my $color = "auto";
Vadim Bendebury98005e82019-03-07 16:28:38 -080064my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
Joe Perchesdbbf8692019-09-25 16:46:52 -070065# git output parsing needs US English output, so first set backtick child process LANGUAGE
66my $git_command ='export LANGUAGE=en_US.UTF-8; git';
Antonio Borneo713a09d2020-04-06 20:11:07 -070067my $tabsize = 8;
Hannes Eder77f5b102009-09-21 17:04:37 -070068
69sub help {
70 my ($exitcode) = @_;
71
72 print << "EOM";
73Usage: $P [OPTION]... [FILE]...
74Version: $V
75
76Options:
77 -q, --quiet quiet
78 --no-tree run without a kernel tree
79 --no-signoff do not check for 'Signed-off-by' line
80 --patch treat FILE as patchfile (default)
81 --emacs emacs compile window format
82 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070083 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070084 -g, --git treat FILE as a single commit or git revision range
85 single git commit with:
86 <rev>
87 <rev>^
88 <rev>~n
89 multiple git commits with:
90 <rev1>..<rev2>
91 <rev1>...<rev2>
92 <rev>-<count>
93 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070094 -f, --file treat FILE as regular source file
95 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070096 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070097 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070098 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070099 --show-types show the specific message type in the output
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700100 --max-line-length=n set the maximum line length, (default $max_line_length)
101 if exceeded, warn on patches
102 requires --strict for use with --file
Vadim Bendebury56193272014-10-13 15:51:48 -0700103 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700104 --tab-size=n set the number of spaces for tab (default $tabsize)
Hannes Eder77f5b102009-09-21 17:04:37 -0700105 --root=PATH PATH to the kernel tree root
106 --no-summary suppress the per-file summary
107 --mailback only produce a report in case of warnings/errors
108 --summary-file include the filename in summary
109 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
110 'values', 'possible', 'type', and 'attr' (default
111 is all off)
112 --test-only=WORD report only warnings/errors containing WORD
113 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700114 --fix EXPERIMENTAL - may create horrible results
115 If correctable single-line errors exist, create
116 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
117 with potential errors corrected to the preferred
118 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800119 --fix-inplace EXPERIMENTAL - may create horrible results
120 Is the same as --fix, but overwrites the input
121 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700122 --ignore-perl-version override checking of perl version. expect
123 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700124 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700125 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700126 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700127 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700128 --color[=WHEN] Use colors 'always', 'never', or only when output
129 is a terminal ('auto'). Default is 'auto'.
Hannes Eder77f5b102009-09-21 17:04:37 -0700130 -h, --help, --version display this help and exit
131
132When FILE is - read standard input.
133EOM
134
135 exit($exitcode);
136}
137
Joe Perches3beb42e2016-05-20 17:04:14 -0700138sub uniq {
139 my %seen;
140 return grep { !$seen{$_}++ } @_;
141}
142
143sub list_types {
144 my ($exitcode) = @_;
145
146 my $count = 0;
147
148 local $/ = undef;
149
150 open(my $script, '<', abs_path($P)) or
151 die "$P: Can't read '$P' $!\n";
152
153 my $text = <$script>;
154 close($script);
155
156 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700157 # Also catch when type or level is passed through a variable
158 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700159 push (@types, $_);
160 }
161 @types = sort(uniq(@types));
162 print("#\tMessage type\n\n");
163 foreach my $type (@types) {
164 print(++$count . "\t" . $type . "\n");
165 }
166
167 exit($exitcode);
168}
169
Joe Perches000d1cc12011-07-25 17:13:25 -0700170my $conf = which_conf($configuration_file);
171if (-f $conf) {
172 my @conf_args;
173 open(my $conffile, '<', "$conf")
174 or warn "$P: Can't find a readable $configuration_file file $!\n";
175
176 while (<$conffile>) {
177 my $line = $_;
178
179 $line =~ s/\s*\n?$//g;
180 $line =~ s/^\s*//g;
181 $line =~ s/\s+/ /g;
182
183 next if ($line =~ m/^\s*#/);
184 next if ($line =~ m/^\s*$/);
185
186 my @words = split(" ", $line);
187 foreach my $word (@words) {
188 last if ($word =~ m/^#/);
189 push (@conf_args, $word);
190 }
191 }
192 close($conffile);
193 unshift(@ARGV, @conf_args) if @conf_args;
194}
195
John Brooks737c0762017-07-10 15:52:24 -0700196# Perl's Getopt::Long allows options to take optional arguments after a space.
197# Prevent --color by itself from consuming other arguments
198foreach (@ARGV) {
199 if ($_ eq "--color" || $_ eq "-color") {
200 $_ = "--color=$color";
201 }
202}
203
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700204GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700205 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700206 'tree!' => \$tree,
207 'signoff!' => \$chk_signoff,
208 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700209 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800210 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700211 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700212 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700213 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700214 'subjective!' => \$check,
215 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700216 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700217 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700218 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700219 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800220 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700221 'min-conf-desc-length=i' => \$min_conf_desc_length,
Antonio Borneo713a09d2020-04-06 20:11:07 -0700222 'tab-size=i' => \$tabsize,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700223 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800224 'summary!' => \$summary,
225 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800226 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700227 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800228 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700229 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800230 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700231 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700232 'codespell!' => \$codespell,
233 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700234 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700235 'color=s' => \$color,
236 'no-color' => \$color, #keep old behaviors of -nocolor
237 'nocolor' => \$color, #keep old behaviors of -nocolor
Hannes Eder77f5b102009-09-21 17:04:37 -0700238 'h|help' => \$help,
239 'version' => \$help
240) or help(1);
241
242help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700243
Joe Perches3beb42e2016-05-20 17:04:14 -0700244list_types(0) if ($list_types);
245
Joe Perches9624b8d2014-01-23 15:54:44 -0800246$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700247$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800248
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700249my $exit = 0;
250
Joe Perches5b579802018-08-21 21:57:33 -0700251my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700252if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700253 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700254 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700255 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700256}
257
Allen Hubbe45107ff2016-08-02 14:04:47 -0700258#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700259if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700260 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700261}
262
John Brooks737c0762017-07-10 15:52:24 -0700263if ($color =~ /^[01]$/) {
264 $color = !$color;
265} elsif ($color =~ /^always$/i) {
266 $color = 1;
267} elsif ($color =~ /^never$/i) {
268 $color = 0;
269} elsif ($color =~ /^auto$/i) {
270 $color = (-t STDOUT);
271} else {
272 die "Invalid color mode: $color\n";
273}
274
Antonio Borneo713a09d2020-04-06 20:11:07 -0700275# skip TAB size 1 to avoid additional checks on $tabsize - 1
276die "Invalid TAB size: $tabsize\n" if ($tabsize < 2);
277
Joe Perches91bfe482013-09-11 14:23:59 -0700278sub hash_save_array_words {
279 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700280
Joe Perches91bfe482013-09-11 14:23:59 -0700281 my @array = split(/,/, join(',', @$arrayRef));
282 foreach my $word (@array) {
283 $word =~ s/\s*\n?$//g;
284 $word =~ s/^\s*//g;
285 $word =~ s/\s+/ /g;
286 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700287
Joe Perches91bfe482013-09-11 14:23:59 -0700288 next if ($word =~ m/^\s*#/);
289 next if ($word =~ m/^\s*$/);
290
291 $hashRef->{$word}++;
292 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700293}
294
Joe Perches91bfe482013-09-11 14:23:59 -0700295sub hash_show_words {
296 my ($hashRef, $prefix) = @_;
297
Joe Perches3c816e42015-06-25 15:03:29 -0700298 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700299 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700300 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700301 print " $word";
302 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700303 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700304 }
305}
306
307hash_save_array_words(\%ignore_type, \@ignore);
308hash_save_array_words(\%use_type, \@use);
309
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800310my $dbg_values = 0;
311my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700312my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700313my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800314for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800315 ## no critic
316 eval "\${dbg_$key} = '$debug{$key}';";
317 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800318}
319
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700320my $rpt_cleaners = 0;
321
Andy Whitcroft8905a672007-11-28 16:21:06 -0800322if ($terse) {
323 $emacs = 1;
324 $quiet++;
325}
326
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700327if ($tree) {
328 if (defined $root) {
329 if (!top_of_kernel_tree($root)) {
330 die "$P: $root: --root does not point at a valid tree\n";
331 }
332 } else {
333 if (top_of_kernel_tree('.')) {
334 $root = '.';
335 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
336 top_of_kernel_tree($1)) {
337 $root = $1;
338 }
339 }
340
341 if (!defined $root) {
342 print "Must be run from the top-level dir. of a kernel tree\n";
343 exit(2);
344 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700345}
346
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700347my $emitted_corrupt = 0;
348
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700349our $Ident = qr{
350 [A-Za-z_][A-Za-z\d_]*
351 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
352 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700353our $Storage = qr{extern|static|asmlinkage};
354our $Sparse = qr{
355 __user|
356 __kernel|
357 __force|
358 __iomem|
359 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800360 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700361 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700362 __refconst|
363 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800364 __rcu|
365 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700366 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800367our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
368our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
369our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
370our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
371our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700372
Wolfram Sang52131292010-03-05 13:43:51 -0800373# Notes to $Attribute:
374# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700375our $Attribute = qr{
376 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700377 __percpu|
378 __nocast|
379 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200380 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700381 __packed__|
382 __packed2__|
383 __naked|
384 __maybe_unused|
385 __always_unused|
386 __noreturn|
387 __used|
388 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800389 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700390 __noclone|
391 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700392 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700393 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700394 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700395 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700396 ____cacheline_aligned|
397 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800398 ____cacheline_internodealigned_in_smp|
399 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700400 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700401our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700402our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700403our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
404our $Lval = qr{$Ident(?:$Member)*};
405
Joe Perches95e2c602013-07-03 15:05:20 -0700406our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
407our $Binary = qr{(?i)0b[01]+$Int_type?};
408our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
409our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700410our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800411our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800412our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
413our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
414our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800415our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700416our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800417our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700418our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700419our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700420our $Operators = qr{
421 <=|>=|==|!=|
422 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700423 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700424 }x;
425
Joe Perches91cb5192014-04-03 14:49:32 -0700426our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
427
Joe Perchesab7e23f2015-04-16 12:44:22 -0700428our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800429our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700430our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700431our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800432our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700433our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800434our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700435our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800436
Joe Perches15662b32011-10-31 17:13:12 -0700437our $NON_ASCII_UTF8 = qr{
438 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700439 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
440 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
441 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
442 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
443 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
444 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
445}x;
446
Joe Perches15662b32011-10-31 17:13:12 -0700447our $UTF8 = qr{
448 [\x09\x0A\x0D\x20-\x7E] # ASCII
449 | $NON_ASCII_UTF8
450}x;
451
Joe Perchese6176fa2015-06-25 15:02:49 -0700452our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800453our $typeOtherOSTypedefs = qr{(?x:
454 u_(?:char|short|int|long) | # bsd
455 u(?:nchar|short|int|long) # sysv
456)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700457our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700458 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700459 atomic_t
460)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700461our $typeTypedefs = qr{(?x:
462 $typeC99Typedefs\b|
463 $typeOtherOSTypedefs\b|
464 $typeKernelTypedefs\b
465)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700466
Joe Perches6d32f7a2015-11-06 16:31:37 -0800467our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
468
Joe Perches691e6692010-03-05 13:43:51 -0800469our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800470 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700471 (?:[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 -0800472 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700473 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700474 panic|
Joe Perches06668722013-11-12 15:10:07 -0800475 MODULE_[A-Z_]+|
476 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800477)};
478
Joe Perchese29a70f2019-03-07 16:28:35 -0800479our $allocFunctions = qr{(?x:
480 (?:(?:devm_)?
481 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
482 kstrdup(?:_const)? |
483 kmemdup(?:_nul)?) |
Christophe JAILLET461e1562020-04-20 18:13:58 -0700484 (?:\w+)?alloc_skb(?:_ip_align)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800485 # dev_alloc_skb/netdev_alloc_skb, et al
486 dma_alloc_coherent
487)};
488
Joe Perches20112472011-07-25 17:13:23 -0700489our $signature_tags = qr{(?xi:
490 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800491 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700492 Acked-by:|
493 Tested-by:|
494 Reviewed-by:|
495 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700496 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700497 To:|
498 Cc:
499)};
500
Joe Perches18130872014-08-06 16:11:22 -0700501our @typeListMisordered = (
502 qr{char\s+(?:un)?signed},
503 qr{int\s+(?:(?:un)?signed\s+)?short\s},
504 qr{int\s+short(?:\s+(?:un)?signed)},
505 qr{short\s+int(?:\s+(?:un)?signed)},
506 qr{(?:un)?signed\s+int\s+short},
507 qr{short\s+(?:un)?signed},
508 qr{long\s+int\s+(?:un)?signed},
509 qr{int\s+long\s+(?:un)?signed},
510 qr{long\s+(?:un)?signed\s+int},
511 qr{int\s+(?:un)?signed\s+long},
512 qr{int\s+(?:un)?signed},
513 qr{int\s+long\s+long\s+(?:un)?signed},
514 qr{long\s+long\s+int\s+(?:un)?signed},
515 qr{long\s+long\s+(?:un)?signed\s+int},
516 qr{long\s+long\s+(?:un)?signed},
517 qr{long\s+(?:un)?signed},
518);
519
Andy Whitcroft8905a672007-11-28 16:21:06 -0800520our @typeList = (
521 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700522 qr{(?:(?:un)?signed\s+)?char},
523 qr{(?:(?:un)?signed\s+)?short\s+int},
524 qr{(?:(?:un)?signed\s+)?short},
525 qr{(?:(?:un)?signed\s+)?int},
526 qr{(?:(?:un)?signed\s+)?long\s+int},
527 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
528 qr{(?:(?:un)?signed\s+)?long\s+long},
529 qr{(?:(?:un)?signed\s+)?long},
530 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800531 qr{float},
532 qr{double},
533 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800534 qr{struct\s+$Ident},
535 qr{union\s+$Ident},
536 qr{enum\s+$Ident},
537 qr{${Ident}_t},
538 qr{${Ident}_handler},
539 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700540 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800541);
Joe Perches938224b2016-01-20 14:59:15 -0800542
543our $C90_int_types = qr{(?x:
544 long\s+long\s+int\s+(?:un)?signed|
545 long\s+long\s+(?:un)?signed\s+int|
546 long\s+long\s+(?:un)?signed|
547 (?:(?:un)?signed\s+)?long\s+long\s+int|
548 (?:(?:un)?signed\s+)?long\s+long|
549 int\s+long\s+long\s+(?:un)?signed|
550 int\s+(?:(?:un)?signed\s+)?long\s+long|
551
552 long\s+int\s+(?:un)?signed|
553 long\s+(?:un)?signed\s+int|
554 long\s+(?:un)?signed|
555 (?:(?:un)?signed\s+)?long\s+int|
556 (?:(?:un)?signed\s+)?long|
557 int\s+long\s+(?:un)?signed|
558 int\s+(?:(?:un)?signed\s+)?long|
559
560 int\s+(?:un)?signed|
561 (?:(?:un)?signed\s+)?int
562)};
563
Alex Dowad485ff232015-06-25 15:02:52 -0700564our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700565our @typeListWithAttr = (
566 @typeList,
567 qr{struct\s+$InitAttribute\s+$Ident},
568 qr{union\s+$InitAttribute\s+$Ident},
569);
570
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700571our @modifierList = (
572 qr{fastcall},
573);
Alex Dowad485ff232015-06-25 15:02:52 -0700574our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800575
Joe Perches24358802014-04-03 14:49:13 -0700576our @mode_permission_funcs = (
577 ["module_param", 3],
578 ["module_param_(?:array|named|string)", 4],
579 ["module_param_array_named", 5],
580 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
581 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700582 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
583 ["IIO_DEV_ATTR_[A-Z_]+", 1],
584 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
585 ["SENSOR_TEMPLATE(?:_2|)", 3],
586 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700587);
588
Joe Perches515a2352014-04-03 14:49:24 -0700589#Create a search pattern for all these functions to speed up a loop below
590our $mode_perms_search = "";
591foreach my $entry (@mode_permission_funcs) {
592 $mode_perms_search .= '|' if ($mode_perms_search ne "");
593 $mode_perms_search .= $entry->[0];
594}
Joe Perches00180462018-02-06 15:38:55 -0800595$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700596
Joe Perches9189c7e2018-09-07 15:26:18 -0700597our %deprecated_apis = (
598 "synchronize_rcu_bh" => "synchronize_rcu",
599 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
600 "call_rcu_bh" => "call_rcu",
601 "rcu_barrier_bh" => "rcu_barrier",
602 "synchronize_sched" => "synchronize_rcu",
603 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
604 "call_rcu_sched" => "call_rcu",
605 "rcu_barrier_sched" => "rcu_barrier",
606 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
607 "cond_synchronize_sched" => "cond_synchronize_rcu",
608);
609
610#Create a search pattern for all these strings to speed up a loop below
611our $deprecated_apis_search = "";
612foreach my $entry (keys %deprecated_apis) {
613 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
614 $deprecated_apis_search .= $entry;
615}
616$deprecated_apis_search = "(?:${deprecated_apis_search})";
617
Joe Perchesb392c642015-04-16 12:44:16 -0700618our $mode_perms_world_writable = qr{
619 S_IWUGO |
620 S_IWOTH |
621 S_IRWXUGO |
622 S_IALLUGO |
623 0[0-7][0-7][2367]
624}x;
625
Joe Perchesf90774e2016-10-11 13:51:47 -0700626our %mode_permission_string_types = (
627 "S_IRWXU" => 0700,
628 "S_IRUSR" => 0400,
629 "S_IWUSR" => 0200,
630 "S_IXUSR" => 0100,
631 "S_IRWXG" => 0070,
632 "S_IRGRP" => 0040,
633 "S_IWGRP" => 0020,
634 "S_IXGRP" => 0010,
635 "S_IRWXO" => 0007,
636 "S_IROTH" => 0004,
637 "S_IWOTH" => 0002,
638 "S_IXOTH" => 0001,
639 "S_IRWXUGO" => 0777,
640 "S_IRUGO" => 0444,
641 "S_IWUGO" => 0222,
642 "S_IXUGO" => 0111,
643);
644
645#Create a search pattern for all these strings to speed up a loop below
646our $mode_perms_string_search = "";
647foreach my $entry (keys %mode_permission_string_types) {
648 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
649 $mode_perms_string_search .= $entry;
650}
Joe Perches00180462018-02-06 15:38:55 -0800651our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
652our $multi_mode_perms_string_search = qr{
653 ${single_mode_perms_string_search}
654 (?:\s*\|\s*${single_mode_perms_string_search})*
655}x;
656
657sub perms_to_octal {
658 my ($string) = @_;
659
660 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
661
662 my $val = "";
663 my $oval = "";
664 my $to = 0;
665 my $curpos = 0;
666 my $lastpos = 0;
667 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
668 $curpos = pos($string);
669 my $match = $2;
670 my $omatch = $1;
671 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
672 $lastpos = $curpos;
673 $to |= $mode_permission_string_types{$match};
674 $val .= '\s*\|\s*' if ($val ne "");
675 $val .= $match;
676 $oval .= $omatch;
677 }
678 $oval =~ s/^\s*\|\s*//;
679 $oval =~ s/\s*\|\s*$//;
680 return sprintf("%04o", $to);
681}
Joe Perchesf90774e2016-10-11 13:51:47 -0700682
Wolfram Sang7840a942010-08-09 17:20:57 -0700683our $allowed_asm_includes = qr{(?x:
684 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700685 memory|
686 time|
687 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700688)};
689# memory.h: ARM has a custom one
690
Kees Cook66b47b42014-10-13 15:51:57 -0700691# Load common spelling mistakes and build regular expression list.
692my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700693my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700694
Joe Perches36061e32014-12-10 15:51:43 -0800695if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800696 while (<$spelling>) {
697 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700698
Joe Perches36061e32014-12-10 15:51:43 -0800699 $line =~ s/\s*\n?$//g;
700 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700701
Joe Perches36061e32014-12-10 15:51:43 -0800702 next if ($line =~ m/^\s*#/);
703 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700704
Joe Perches36061e32014-12-10 15:51:43 -0800705 my ($suspect, $fix) = split(/\|\|/, $line);
706
Joe Perches36061e32014-12-10 15:51:43 -0800707 $spelling_fix{$suspect} = $fix;
708 }
709 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800710} else {
711 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700712}
Kees Cook66b47b42014-10-13 15:51:57 -0700713
Joe Perchesebfd7d62015-04-16 12:44:14 -0700714if ($codespell) {
715 if (open(my $spelling, '<', $codespellfile)) {
716 while (<$spelling>) {
717 my $line = $_;
718
719 $line =~ s/\s*\n?$//g;
720 $line =~ s/^\s*//g;
721
722 next if ($line =~ m/^\s*#/);
723 next if ($line =~ m/^\s*$/);
724 next if ($line =~ m/, disabled/i);
725
726 $line =~ s/,.*$//;
727
728 my ($suspect, $fix) = split(/->/, $line);
729
730 $spelling_fix{$suspect} = $fix;
731 }
732 close($spelling);
733 } else {
734 warn "No codespell typos will be found - file '$codespellfile': $!\n";
735 }
736}
737
738$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
739
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700740sub read_words {
741 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700742
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700743 if (open(my $words, '<', $file)) {
744 while (<$words>) {
745 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700746
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700747 $line =~ s/\s*\n?$//g;
748 $line =~ s/^\s*//g;
749
750 next if ($line =~ m/^\s*#/);
751 next if ($line =~ m/^\s*$/);
752 if ($line =~ /\s/) {
753 print("$file: '$line' invalid - ignored\n");
754 next;
755 }
756
757 $$wordsRef .= '|' if ($$wordsRef ne "");
758 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700759 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700760 close($file);
761 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700762 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700763
764 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700765}
766
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700767my $const_structs = "";
768read_words(\$const_structs, $conststructsfile)
769 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
770
771my $typeOtherTypedefs = "";
772if (length($typedefsfile)) {
773 read_words(\$typeOtherTypedefs, $typedefsfile)
774 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
775}
776$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
777
Andy Whitcroft8905a672007-11-28 16:21:06 -0800778sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700779 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
780 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700781 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700782 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700783 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700784 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700785 (?:$typeTypedefs\b)|
786 (?:${all}\b)
787 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800788 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700789 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800790 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800791 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700792 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700793 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800794 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700795 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800796 }x;
Joe Perches18130872014-08-06 16:11:22 -0700797 $NonptrTypeMisordered = qr{
798 (?:$Modifier\s+|const\s+)*
799 (?:
800 (?:${Misordered}\b)
801 )
802 (?:\s+$Modifier|\s+const)*
803 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700804 $NonptrTypeWithAttr = qr{
805 (?:$Modifier\s+|const\s+)*
806 (?:
807 (?:typeof|__typeof__)\s*\([^\)]*\)|
808 (?:$typeTypedefs\b)|
809 (?:${allWithAttr}\b)
810 )
811 (?:\s+$Modifier|\s+const)*
812 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800813 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700814 $NonptrType
Antonio Borneo7b184962020-04-06 20:11:04 -0700815 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700816 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800817 }x;
Joe Perches18130872014-08-06 16:11:22 -0700818 $TypeMisordered = qr{
819 $NonptrTypeMisordered
Antonio Borneo7b184962020-04-06 20:11:04 -0700820 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Perches18130872014-08-06 16:11:22 -0700821 (?:\s+$Inline|\s+$Modifier)*
822 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700823 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700824 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800825}
826build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700827
Joe Perches7d2367a2011-07-25 17:13:22 -0700828our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700829
830# Using $balanced_parens, $LvalOrFunc, or $FuncArg
831# requires at least perl version v5.10.0
832# Any use must be runtime checked with $^V
833
834our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700835our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800836our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700837
Joe Perchesf8422302014-08-06 16:11:31 -0700838our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700839 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700840 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700841 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
842 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700843)};
844
Joe Perches7d2367a2011-07-25 17:13:22 -0700845sub deparenthesize {
846 my ($string) = @_;
847 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700848
849 while ($string =~ /^\s*\(.*\)\s*$/) {
850 $string =~ s@^\s*\(\s*@@;
851 $string =~ s@\s*\)\s*$@@;
852 }
853
Joe Perches7d2367a2011-07-25 17:13:22 -0700854 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700855
Joe Perches7d2367a2011-07-25 17:13:22 -0700856 return $string;
857}
858
Joe Perches34456862013-07-03 15:05:34 -0700859sub seed_camelcase_file {
860 my ($file) = @_;
861
862 return if (!(-f $file));
863
864 local $/;
865
866 open(my $include_file, '<', "$file")
867 or warn "$P: Can't read '$file' $!\n";
868 my $text = <$include_file>;
869 close($include_file);
870
871 my @lines = split('\n', $text);
872
873 foreach my $line (@lines) {
874 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
875 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
876 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800877 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
878 $camelcase{$1} = 1;
879 } 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 -0700880 $camelcase{$1} = 1;
881 }
882 }
883}
884
Joe Perchescd28b112019-12-04 16:52:09 -0800885our %maintained_status = ();
886
Joe Perches85b0ee12016-10-11 13:51:44 -0700887sub is_maintained_obsolete {
888 my ($filename) = @_;
889
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800890 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700891
Joe Perchescd28b112019-12-04 16:52:09 -0800892 if (!exists($maintained_status{$filename})) {
893 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
894 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700895
Joe Perchescd28b112019-12-04 16:52:09 -0800896 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700897}
898
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700899sub is_SPDX_License_valid {
900 my ($license) = @_;
901
Joe Perches56294112018-08-21 21:58:04 -0700902 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700903
Joe Perches56294112018-08-21 21:58:04 -0700904 my $root_path = abs_path($root);
905 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700906 return 0 if ($status ne "");
907 return 1;
908}
909
Joe Perches34456862013-07-03 15:05:34 -0700910my $camelcase_seeded = 0;
911sub seed_camelcase_includes {
912 return if ($camelcase_seeded);
913
914 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700915 my $camelcase_cache = "";
916 my @include_files = ();
917
918 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700919
Richard Genoud3645e322014-02-10 14:25:32 -0800920 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700921 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -0700922 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700923 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700924 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700925 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700926 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700927 @include_files = split('\n', $files);
928 foreach my $file (@include_files) {
929 my $date = POSIX::strftime("%Y%m%d%H%M",
930 localtime((stat $file)[9]));
931 $last_mod_date = $date if ($last_mod_date < $date);
932 }
933 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700934 }
Joe Perchesc707a812013-07-08 16:00:43 -0700935
936 if ($camelcase_cache ne "" && -f $camelcase_cache) {
937 open(my $camelcase_file, '<', "$camelcase_cache")
938 or warn "$P: Can't read '$camelcase_cache' $!\n";
939 while (<$camelcase_file>) {
940 chomp;
941 $camelcase{$_} = 1;
942 }
943 close($camelcase_file);
944
945 return;
946 }
947
Richard Genoud3645e322014-02-10 14:25:32 -0800948 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700949 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700950 @include_files = split('\n', $files);
951 }
952
Joe Perches34456862013-07-03 15:05:34 -0700953 foreach my $file (@include_files) {
954 seed_camelcase_file($file);
955 }
Joe Perches351b2a12013-07-03 15:05:36 -0700956
Joe Perchesc707a812013-07-08 16:00:43 -0700957 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700958 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700959 open(my $camelcase_file, '>', "$camelcase_cache")
960 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700961 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
962 print $camelcase_file ("$_\n");
963 }
964 close($camelcase_file);
965 }
Joe Perches34456862013-07-03 15:05:34 -0700966}
967
Joe Perchesd311cd42014-08-06 16:10:57 -0700968sub git_commit_info {
969 my ($commit, $id, $desc) = @_;
970
971 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
972
Joe Perchesdbbf8692019-09-25 16:46:52 -0700973 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -0700974 $output =~ s/^\s*//gm;
975 my @lines = split("\n", $output);
976
Joe Perches0d7835f2015-02-13 14:38:35 -0800977 return ($id, $desc) if ($#lines < 0);
978
Sean Christopherson5a7f4452019-09-25 16:46:49 -0700979 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -0700980# Maybe one day convert this block of bash into something that returns
981# all matching commit ids, but it's very slow...
982#
983# echo "checking commits $1..."
984# git rev-list --remotes | grep -i "^$1" |
985# while read line ; do
986# git log --format='%H %s' -1 $line |
987# echo "commit $(cut -c 1-12,41-)"
988# done
989 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700990 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700991 } else {
992 $id = substr($lines[0], 0, 12);
993 $desc = substr($lines[0], 41);
994 }
995
996 return ($id, $desc);
997}
998
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700999$chk_signoff = 0 if ($file);
1000
Andy Whitcroft00df3442007-06-08 13:47:06 -07001001my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001002my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001003my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001004my @fixed_inserted = ();
1005my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001006my $fixlinenr = -1;
1007
Du, Changbin4a593c32016-05-20 17:04:16 -07001008# If input is git commits, extract all commits from the commit expressions.
1009# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1010die "$P: No git repository found\n" if ($git && !-e ".git");
1011
1012if ($git) {
1013 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001014 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001015 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001016 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1017 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001018 } elsif ($commit_expr =~ m/\.\./) {
1019 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001020 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001021 $git_range = "-1 $commit_expr";
1022 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001023 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001024 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001025 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1026 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001027 my $sha1 = $1;
1028 my $subject = $2;
1029 unshift(@commits, $sha1);
1030 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001031 }
1032 }
1033 die "$P: no git commits after extraction!\n" if (@commits == 0);
1034 @ARGV = @commits;
1035}
1036
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001037my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001038$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001039for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001040 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -07001041 if ($git) {
1042 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1043 die "$P: $filename: git format-patch failed - $!\n";
1044 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001045 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001046 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001047 } elsif ($filename eq '-') {
1048 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001049 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001050 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001051 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001052 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001053 if ($filename eq '-') {
1054 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001055 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001056 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001057 } else {
1058 $vname = $filename;
1059 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001060 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001061 chomp;
1062 push(@rawlines, $_);
1063 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001064 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001065
1066 if ($#ARGV > 0 && $quiet == 0) {
1067 print '-' x length($vname) . "\n";
1068 print "$vname\n";
1069 print '-' x length($vname) . "\n";
1070 }
1071
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001072 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001073 $exit = 1;
1074 }
1075 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001076 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001077 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001078 @fixed_inserted = ();
1079 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001080 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001081 @modifierListFile = ();
1082 @typeListFile = ();
1083 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001084}
1085
Joe Perchesd8469f12015-06-25 15:03:00 -07001086if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001087 hash_show_words(\%use_type, "Used");
1088 hash_show_words(\%ignore_type, "Ignored");
1089
Joe Perches5b579802018-08-21 21:57:33 -07001090 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001091 print << "EOM"
1092
1093NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001094 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001095EOM
1096 }
1097 if ($exit) {
1098 print << "EOM"
1099
1100NOTE: If any of the errors are false positives, please report
1101 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1102EOM
1103 }
1104}
1105
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001106exit($exit);
1107
1108sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001109 my ($root) = @_;
1110
1111 my @tree_check = (
1112 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1113 "README", "Documentation", "arch", "include", "drivers",
1114 "fs", "init", "ipc", "kernel", "lib", "scripts",
1115 );
1116
1117 foreach my $check (@tree_check) {
1118 if (! -e $root . '/' . $check) {
1119 return 0;
1120 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001121 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001122 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001123}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001124
Joe Perches20112472011-07-25 17:13:23 -07001125sub parse_email {
1126 my ($formatted_email) = @_;
1127
1128 my $name = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001129 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001130 my $address = "";
1131 my $comment = "";
1132
1133 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1134 $name = $1;
1135 $address = $2;
1136 $comment = $3 if defined $3;
1137 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1138 $address = $1;
1139 $comment = $2 if defined $2;
1140 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1141 $address = $1;
1142 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001143 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001144 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001145 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001146 $name =~ s/^\"|\"$//g;
1147 # If there's a name left after stripping spaces and
1148 # leading quotes, and the address doesn't have both
1149 # leading and trailing angle brackets, the address
1150 # is invalid. ie:
1151 # "joe smith joe@smith.com" bad
1152 # "joe smith <joe@smith.com" bad
1153 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1154 $name = "";
1155 $address = "";
1156 $comment = "";
1157 }
1158 }
1159
Joe Perches3705ce52013-07-03 15:05:31 -07001160 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001161 $name =~ s/^\"|\"$//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001162 $name =~ s/(\s*\([^\)]+\))\s*//;
1163 if (defined($1)) {
1164 $name_comment = trim($1);
1165 }
Joe Perches3705ce52013-07-03 15:05:31 -07001166 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001167 $address =~ s/^\<|\>$//g;
1168
1169 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1170 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1171 $name = "\"$name\"";
1172 }
1173
Joe Perchesdfa05c22020-04-06 20:10:48 -07001174 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001175}
1176
1177sub format_email {
1178 my ($name, $address) = @_;
1179
1180 my $formatted_email;
1181
Joe Perches3705ce52013-07-03 15:05:31 -07001182 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001183 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001184 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001185
1186 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1187 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1188 $name = "\"$name\"";
1189 }
1190
1191 if ("$name" eq "") {
1192 $formatted_email = "$address";
1193 } else {
1194 $formatted_email = "$name <$address>";
1195 }
1196
1197 return $formatted_email;
1198}
1199
Joe Perchesdfa05c22020-04-06 20:10:48 -07001200sub reformat_email {
1201 my ($email) = @_;
1202
1203 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1204 return format_email($email_name, $email_address);
1205}
1206
1207sub same_email_addresses {
1208 my ($email1, $email2) = @_;
1209
1210 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1211 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1212
1213 return $email1_name eq $email2_name &&
1214 $email1_address eq $email2_address;
1215}
1216
Joe Perchesd311cd42014-08-06 16:10:57 -07001217sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001218 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001219
Joe Perchesbd474ca2014-08-06 16:11:10 -07001220 foreach my $path (split(/:/, $ENV{PATH})) {
1221 if (-e "$path/$bin") {
1222 return "$path/$bin";
1223 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001224 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001225
Joe Perchesbd474ca2014-08-06 16:11:10 -07001226 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001227}
1228
Joe Perches000d1cc12011-07-25 17:13:25 -07001229sub which_conf {
1230 my ($conf) = @_;
1231
1232 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1233 if (-e "$path/$conf") {
1234 return "$path/$conf";
1235 }
1236 }
1237
1238 return "";
1239}
1240
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001241sub expand_tabs {
1242 my ($str) = @_;
1243
1244 my $res = '';
1245 my $n = 0;
1246 for my $c (split(//, $str)) {
1247 if ($c eq "\t") {
1248 $res .= ' ';
1249 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001250 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001251 $res .= ' ';
1252 }
1253 next;
1254 }
1255 $res .= $c;
1256 $n++;
1257 }
1258
1259 return $res;
1260}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001261sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001262 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001263 return $res;
1264}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001265
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001266sub line_stats {
1267 my ($line) = @_;
1268
1269 # Drop the diff line leader and expand tabs
1270 $line =~ s/^.//;
1271 $line = expand_tabs($line);
1272
1273 # Pick the indent from the front of the line.
1274 my ($white) = ($line =~ /^(\s*)/);
1275
1276 return (length($line), length($white));
1277}
1278
Andy Whitcroft773647a2008-03-28 14:15:58 -07001279my $sanitise_quote = '';
1280
1281sub sanitise_line_reset {
1282 my ($in_comment) = @_;
1283
1284 if ($in_comment) {
1285 $sanitise_quote = '*/';
1286 } else {
1287 $sanitise_quote = '';
1288 }
1289}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001290sub sanitise_line {
1291 my ($line) = @_;
1292
1293 my $res = '';
1294 my $l = '';
1295
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001296 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001297 my $off = 0;
1298 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001299
Andy Whitcroft773647a2008-03-28 14:15:58 -07001300 # Always copy over the diff marker.
1301 $res = substr($line, 0, 1);
1302
1303 for ($off = 1; $off < length($line); $off++) {
1304 $c = substr($line, $off, 1);
1305
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001306 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001307 # and end, all to $;.
1308 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1309 $sanitise_quote = '*/';
1310
1311 substr($res, $off, 2, "$;$;");
1312 $off++;
1313 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001314 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001315 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001316 $sanitise_quote = '';
1317 substr($res, $off, 2, "$;$;");
1318 $off++;
1319 next;
1320 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001321 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1322 $sanitise_quote = '//';
1323
1324 substr($res, $off, 2, $sanitise_quote);
1325 $off++;
1326 next;
1327 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001328
1329 # A \ in a string means ignore the next character.
1330 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1331 $c eq "\\") {
1332 substr($res, $off, 2, 'XX');
1333 $off++;
1334 next;
1335 }
1336 # Regular quotes.
1337 if ($c eq "'" || $c eq '"') {
1338 if ($sanitise_quote eq '') {
1339 $sanitise_quote = $c;
1340
1341 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001342 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001343 } elsif ($sanitise_quote eq $c) {
1344 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001345 }
1346 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001347
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001348 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001349 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1350 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001351 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1352 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001353 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1354 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001355 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001356 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001357 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001358 }
1359
Daniel Walker113f04a2009-09-21 17:04:35 -07001360 if ($sanitise_quote eq '//') {
1361 $sanitise_quote = '';
1362 }
1363
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001364 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001365 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001366 my $clean = 'X' x length($1);
1367 $res =~ s@\<.*\>@<$clean>@;
1368
1369 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001370 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001371 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001372 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001373 }
1374
Joe Perchesdadf6802016-08-02 14:04:33 -07001375 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1376 my $match = $1;
1377 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1378 }
1379
Andy Whitcroft00df3442007-06-08 13:47:06 -07001380 return $res;
1381}
1382
Joe Perchesa6962d72013-04-29 16:18:13 -07001383sub get_quoted_string {
1384 my ($line, $rawline) = @_;
1385
Joe Perches478b1792018-04-10 16:33:34 -07001386 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001387 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001388 return substr($rawline, $-[0], $+[0] - $-[0]);
1389}
1390
Andy Whitcroft8905a672007-11-28 16:21:06 -08001391sub ctx_statement_block {
1392 my ($linenr, $remain, $off) = @_;
1393 my $line = $linenr - 1;
1394 my $blk = '';
1395 my $soff = $off;
1396 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001397 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001398
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001399 my $loff = 0;
1400
Andy Whitcroft8905a672007-11-28 16:21:06 -08001401 my $type = '';
1402 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001403 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001404 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001405 my $c;
1406 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001407
1408 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001409 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001410 @stack = (['', 0]) if ($#stack == -1);
1411
Andy Whitcroft773647a2008-03-28 14:15:58 -07001412 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001413 # If we are about to drop off the end, pull in more
1414 # context.
1415 if ($off >= $len) {
1416 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001417 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001418 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001419 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001420 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001421 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001422 $len = length($blk);
1423 $line++;
1424 last;
1425 }
1426 # Bail if there is no further context.
1427 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001428 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001429 last;
1430 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001431 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1432 $level++;
1433 $type = '#';
1434 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001435 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001436 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001437 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001438 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001439
Andy Whitcroft773647a2008-03-28 14:15:58 -07001440 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001441
1442 # Handle nested #if/#else.
1443 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1444 push(@stack, [ $type, $level ]);
1445 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1446 ($type, $level) = @{$stack[$#stack - 1]};
1447 } elsif ($remainder =~ /^#\s*endif\b/) {
1448 ($type, $level) = @{pop(@stack)};
1449 }
1450
Andy Whitcroft8905a672007-11-28 16:21:06 -08001451 # Statement ends at the ';' or a close '}' at the
1452 # outermost level.
1453 if ($level == 0 && $c eq ';') {
1454 last;
1455 }
1456
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001457 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001458 if ($level == 0 && $coff_set == 0 &&
1459 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1460 $remainder =~ /^(else)(?:\s|{)/ &&
1461 $remainder !~ /^else\s+if\b/) {
1462 $coff = $off + length($1) - 1;
1463 $coff_set = 1;
1464 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1465 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001466 }
1467
Andy Whitcroft8905a672007-11-28 16:21:06 -08001468 if (($type eq '' || $type eq '(') && $c eq '(') {
1469 $level++;
1470 $type = '(';
1471 }
1472 if ($type eq '(' && $c eq ')') {
1473 $level--;
1474 $type = ($level != 0)? '(' : '';
1475
1476 if ($level == 0 && $coff < $soff) {
1477 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001478 $coff_set = 1;
1479 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001480 }
1481 }
1482 if (($type eq '' || $type eq '{') && $c eq '{') {
1483 $level++;
1484 $type = '{';
1485 }
1486 if ($type eq '{' && $c eq '}') {
1487 $level--;
1488 $type = ($level != 0)? '{' : '';
1489
1490 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001491 if (substr($blk, $off + 1, 1) eq ';') {
1492 $off++;
1493 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001494 last;
1495 }
1496 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001497 # Preprocessor commands end at the newline unless escaped.
1498 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1499 $level--;
1500 $type = '';
1501 $off++;
1502 last;
1503 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001504 $off++;
1505 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001506 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001507 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001508 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001509 $line++;
1510 $remain--;
1511 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001512
1513 my $statement = substr($blk, $soff, $off - $soff + 1);
1514 my $condition = substr($blk, $soff, $coff - $soff + 1);
1515
1516 #warn "STATEMENT<$statement>\n";
1517 #warn "CONDITION<$condition>\n";
1518
Andy Whitcroft773647a2008-03-28 14:15:58 -07001519 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001520
1521 return ($statement, $condition,
1522 $line, $remain + 1, $off - $loff + 1, $level);
1523}
1524
Andy Whitcroftcf655042008-03-04 14:28:20 -08001525sub statement_lines {
1526 my ($stmt) = @_;
1527
1528 # Strip the diff line prefixes and rip blank lines at start and end.
1529 $stmt =~ s/(^|\n)./$1/g;
1530 $stmt =~ s/^\s*//;
1531 $stmt =~ s/\s*$//;
1532
1533 my @stmt_lines = ($stmt =~ /\n/g);
1534
1535 return $#stmt_lines + 2;
1536}
1537
1538sub statement_rawlines {
1539 my ($stmt) = @_;
1540
1541 my @stmt_lines = ($stmt =~ /\n/g);
1542
1543 return $#stmt_lines + 2;
1544}
1545
1546sub statement_block_size {
1547 my ($stmt) = @_;
1548
1549 $stmt =~ s/(^|\n)./$1/g;
1550 $stmt =~ s/^\s*{//;
1551 $stmt =~ s/}\s*$//;
1552 $stmt =~ s/^\s*//;
1553 $stmt =~ s/\s*$//;
1554
1555 my @stmt_lines = ($stmt =~ /\n/g);
1556 my @stmt_statements = ($stmt =~ /;/g);
1557
1558 my $stmt_lines = $#stmt_lines + 2;
1559 my $stmt_statements = $#stmt_statements + 1;
1560
1561 if ($stmt_lines > $stmt_statements) {
1562 return $stmt_lines;
1563 } else {
1564 return $stmt_statements;
1565 }
1566}
1567
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001568sub ctx_statement_full {
1569 my ($linenr, $remain, $off) = @_;
1570 my ($statement, $condition, $level);
1571
1572 my (@chunks);
1573
Andy Whitcroftcf655042008-03-04 14:28:20 -08001574 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001575 ($statement, $condition, $linenr, $remain, $off, $level) =
1576 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001577 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001578 push(@chunks, [ $condition, $statement ]);
1579 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1580 return ($level, $linenr, @chunks);
1581 }
1582
1583 # Pull in the following conditional/block pairs and see if they
1584 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001585 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001586 ($statement, $condition, $linenr, $remain, $off, $level) =
1587 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001588 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001589 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001590 #print "C: push\n";
1591 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001592 }
1593
1594 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001595}
1596
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001597sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001598 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001599 my $line;
1600 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001601 my $blk = '';
1602 my @o;
1603 my @c;
1604 my @res = ();
1605
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001606 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001607 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001608 for ($line = $start; $remain > 0; $line++) {
1609 next if ($rawlines[$line] =~ /^-/);
1610 $remain--;
1611
1612 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001613
1614 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001615 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001616 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001617 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001618 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001619 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001620 $level = pop(@stack);
1621 }
1622
Andy Whitcroft01464f32010-10-26 14:23:19 -07001623 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001624 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1625 if ($off > 0) {
1626 $off--;
1627 next;
1628 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001629
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001630 if ($c eq $close && $level > 0) {
1631 $level--;
1632 last if ($level == 0);
1633 } elsif ($c eq $open) {
1634 $level++;
1635 }
1636 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001637
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001638 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001639 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001640 }
1641
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001642 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001643 }
1644
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001645 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001646}
1647sub ctx_block_outer {
1648 my ($linenr, $remain) = @_;
1649
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001650 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1651 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001652}
1653sub ctx_block {
1654 my ($linenr, $remain) = @_;
1655
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001656 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1657 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001658}
1659sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001660 my ($linenr, $remain, $off) = @_;
1661
1662 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1663 return @r;
1664}
1665sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001666 my ($linenr, $remain) = @_;
1667
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001668 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001669}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001670sub ctx_statement_level {
1671 my ($linenr, $remain, $off) = @_;
1672
1673 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1674}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001675
1676sub ctx_locate_comment {
1677 my ($first_line, $end_line) = @_;
1678
1679 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001680 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001681 return $current_comment if (defined $current_comment);
1682
1683 # Look through the context and try and figure out if there is a
1684 # comment.
1685 my $in_comment = 0;
1686 $current_comment = '';
1687 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001688 my $line = $rawlines[$linenr - 1];
1689 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001690 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1691 $in_comment = 1;
1692 }
1693 if ($line =~ m@/\*@) {
1694 $in_comment = 1;
1695 }
1696 if (!$in_comment && $current_comment ne '') {
1697 $current_comment = '';
1698 }
1699 $current_comment .= $line . "\n" if ($in_comment);
1700 if ($line =~ m@\*/@) {
1701 $in_comment = 0;
1702 }
1703 }
1704
1705 chomp($current_comment);
1706 return($current_comment);
1707}
1708sub ctx_has_comment {
1709 my ($first_line, $end_line) = @_;
1710 my $cmt = ctx_locate_comment($first_line, $end_line);
1711
Andy Whitcroft00df3442007-06-08 13:47:06 -07001712 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001713 ##print "CMMT: $cmt\n";
1714
1715 return ($cmt ne '');
1716}
1717
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001718sub raw_line {
1719 my ($linenr, $cnt) = @_;
1720
1721 my $offset = $linenr - 1;
1722 $cnt++;
1723
1724 my $line;
1725 while ($cnt) {
1726 $line = $rawlines[$offset++];
1727 next if (defined($line) && $line =~ /^-/);
1728 $cnt--;
1729 }
1730
1731 return $line;
1732}
1733
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001734sub get_stat_real {
1735 my ($linenr, $lc) = @_;
1736
1737 my $stat_real = raw_line($linenr, 0);
1738 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1739 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1740 }
1741
1742 return $stat_real;
1743}
1744
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001745sub get_stat_here {
1746 my ($linenr, $cnt, $here) = @_;
1747
1748 my $herectx = $here . "\n";
1749 for (my $n = 0; $n < $cnt; $n++) {
1750 $herectx .= raw_line($linenr, $n) . "\n";
1751 }
1752
1753 return $herectx;
1754}
1755
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001756sub cat_vet {
1757 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001758 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001759
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001760 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001761 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1762 $res .= $1;
1763 if ($2 ne '') {
1764 $coded = sprintf("^%c", unpack('C', $2) + 64);
1765 $res .= $coded;
1766 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001767 }
1768 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001769
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001770 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001771}
1772
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001773my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001774my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001775my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001776my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001777
1778sub annotate_reset {
1779 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001780 $av_pending = '_';
1781 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001782 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001783}
1784
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001785sub annotate_values {
1786 my ($stream, $type) = @_;
1787
1788 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001789 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001790 my $cur = $stream;
1791
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001792 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001793
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001794 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001795 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001796 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001797 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001798 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001799 print "WS($1)\n" if ($dbg_values > 1);
1800 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001801 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001802 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001803 }
1804
Florian Micklerc023e4732011-01-12 16:59:58 -08001805 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001806 print "CAST($1)\n" if ($dbg_values > 1);
1807 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001808 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001809
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001810 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001811 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001812 $type = 'T';
1813
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001814 } elsif ($cur =~ /^($Modifier)\s*/) {
1815 print "MODIFIER($1)\n" if ($dbg_values > 1);
1816 $type = 'T';
1817
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001818 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001819 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001820 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001821 push(@av_paren_type, $type);
1822 if ($2 ne '') {
1823 $av_pending = 'N';
1824 }
1825 $type = 'E';
1826
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001827 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001828 print "UNDEF($1)\n" if ($dbg_values > 1);
1829 $av_preprocessor = 1;
1830 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001831
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001832 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001833 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001834 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001835
1836 push(@av_paren_type, $type);
1837 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001838 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001839
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001840 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001841 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1842 $av_preprocessor = 1;
1843
1844 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1845
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001846 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001847
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001848 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001849 print "PRE_END($1)\n" if ($dbg_values > 1);
1850
1851 $av_preprocessor = 1;
1852
1853 # Assume all arms of the conditional end as this
1854 # one does, and continue as if the #endif was not here.
1855 pop(@av_paren_type);
1856 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001857 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001858
1859 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001860 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001861
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001862 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1863 print "ATTR($1)\n" if ($dbg_values > 1);
1864 $av_pending = $type;
1865 $type = 'N';
1866
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001867 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001868 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001869 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001870 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001871 }
1872 $type = 'N';
1873
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001874 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001875 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001876 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001877 $type = 'N';
1878
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001879 } elsif ($cur =~/^(case)/o) {
1880 print "CASE($1)\n" if ($dbg_values > 1);
1881 $av_pend_colon = 'C';
1882 $type = 'N';
1883
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001884 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001885 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001886 $type = 'N';
1887
1888 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001889 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001890 push(@av_paren_type, $av_pending);
1891 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001892 $type = 'N';
1893
1894 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001895 my $new_type = pop(@av_paren_type);
1896 if ($new_type ne '_') {
1897 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001898 print "PAREN('$1') -> $type\n"
1899 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001900 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001901 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001902 }
1903
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001904 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001905 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001906 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001907 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001908
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001909 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1910 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001911 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001912 } elsif ($type eq 'E') {
1913 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001914 }
1915 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1916 $type = 'V';
1917
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001918 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001919 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001920 $type = 'V';
1921
1922 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001923 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001924 $type = 'N';
1925
Andy Whitcroftcf655042008-03-04 14:28:20 -08001926 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001927 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001928 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001929 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001930
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001931 } elsif ($cur =~/^(,)/) {
1932 print "COMMA($1)\n" if ($dbg_values > 1);
1933 $type = 'C';
1934
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001935 } elsif ($cur =~ /^(\?)/o) {
1936 print "QUESTION($1)\n" if ($dbg_values > 1);
1937 $type = 'N';
1938
1939 } elsif ($cur =~ /^(:)/o) {
1940 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1941
1942 substr($var, length($res), 1, $av_pend_colon);
1943 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1944 $type = 'E';
1945 } else {
1946 $type = 'N';
1947 }
1948 $av_pend_colon = 'O';
1949
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001950 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001951 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001952 $type = 'N';
1953
Andy Whitcroft0d413862008-10-15 22:02:16 -07001954 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001955 my $variant;
1956
1957 print "OPV($1)\n" if ($dbg_values > 1);
1958 if ($type eq 'V') {
1959 $variant = 'B';
1960 } else {
1961 $variant = 'U';
1962 }
1963
1964 substr($var, length($res), 1, $variant);
1965 $type = 'N';
1966
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001967 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001968 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001969 if ($1 ne '++' && $1 ne '--') {
1970 $type = 'N';
1971 }
1972
1973 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001974 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001975 }
1976 if (defined $1) {
1977 $cur = substr($cur, length($1));
1978 $res .= $type x length($1);
1979 }
1980 }
1981
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001982 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001983}
1984
Andy Whitcroft8905a672007-11-28 16:21:06 -08001985sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001986 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001987 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001988 ^(?:
1989 $Modifier|
1990 $Storage|
1991 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001992 DEFINE_\S+
1993 )$|
1994 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001995 goto|
1996 return|
1997 case|
1998 else|
1999 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002000 do|
2001 \#|
2002 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002003 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002004 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002005 )}x;
2006 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2007 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002008 # Check for modifiers.
2009 $possible =~ s/\s*$Storage\s*//g;
2010 $possible =~ s/\s*$Sparse\s*//g;
2011 if ($possible =~ /^\s*$/) {
2012
2013 } elsif ($possible =~ /\s/) {
2014 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002015 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002016 if ($modifier !~ $notPermitted) {
2017 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002018 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002019 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002020 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002021
2022 } else {
2023 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002024 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002025 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002026 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002027 } else {
2028 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002029 }
2030}
2031
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002032my $prefix = '';
2033
Joe Perches000d1cc12011-07-25 17:13:25 -07002034sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002035 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002036
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002037 $type =~ tr/[a-z]/[A-Z]/;
2038
Joe Perchescbec18a2014-04-03 14:49:19 -07002039 return defined $use_type{$type} if (scalar keys %use_type > 0);
2040
2041 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002042}
2043
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002044sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002045 my ($level, $type, $msg) = @_;
2046
2047 if (!show_type($type) ||
2048 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002049 return 0;
2050 }
Joe Perches57230292015-06-25 15:03:03 -07002051 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002052 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002053 if ($level eq 'ERROR') {
2054 $output .= RED;
2055 } elsif ($level eq 'WARNING') {
2056 $output .= YELLOW;
2057 } else {
2058 $output .= GREEN;
2059 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002060 }
Joe Perches57230292015-06-25 15:03:03 -07002061 $output .= $prefix . $level . ':';
2062 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002063 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002064 $output .= "$type:";
2065 }
John Brooks737c0762017-07-10 15:52:24 -07002066 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002067 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002068
2069 if ($showfile) {
2070 my @lines = split("\n", $output, -1);
2071 splice(@lines, 1, 1);
2072 $output = join("\n", @lines);
2073 }
Joe Perches57230292015-06-25 15:03:03 -07002074 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002075
Joe Perches57230292015-06-25 15:03:03 -07002076 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002077
2078 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002079}
Joe Perchescbec18a2014-04-03 14:49:19 -07002080
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002081sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002082 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002083}
Joe Perches000d1cc12011-07-25 17:13:25 -07002084
Joe Perchesd752fcc2014-08-06 16:11:05 -07002085sub fixup_current_range {
2086 my ($lineRef, $offset, $length) = @_;
2087
2088 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2089 my $o = $1;
2090 my $l = $2;
2091 my $no = $o + $offset;
2092 my $nl = $l + $length;
2093 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2094 }
2095}
2096
2097sub fix_inserted_deleted_lines {
2098 my ($linesRef, $insertedRef, $deletedRef) = @_;
2099
2100 my $range_last_linenr = 0;
2101 my $delta_offset = 0;
2102
2103 my $old_linenr = 0;
2104 my $new_linenr = 0;
2105
2106 my $next_insert = 0;
2107 my $next_delete = 0;
2108
2109 my @lines = ();
2110
2111 my $inserted = @{$insertedRef}[$next_insert++];
2112 my $deleted = @{$deletedRef}[$next_delete++];
2113
2114 foreach my $old_line (@{$linesRef}) {
2115 my $save_line = 1;
2116 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002117 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002118 $delta_offset = 0;
2119 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2120 $range_last_linenr = $new_linenr;
2121 fixup_current_range(\$line, $delta_offset, 0);
2122 }
2123
2124 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2125 $deleted = @{$deletedRef}[$next_delete++];
2126 $save_line = 0;
2127 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2128 }
2129
2130 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2131 push(@lines, ${$inserted}{'LINE'});
2132 $inserted = @{$insertedRef}[$next_insert++];
2133 $new_linenr++;
2134 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2135 }
2136
2137 if ($save_line) {
2138 push(@lines, $line);
2139 $new_linenr++;
2140 }
2141
2142 $old_linenr++;
2143 }
2144
2145 return @lines;
2146}
2147
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002148sub fix_insert_line {
2149 my ($linenr, $line) = @_;
2150
2151 my $inserted = {
2152 LINENR => $linenr,
2153 LINE => $line,
2154 };
2155 push(@fixed_inserted, $inserted);
2156}
2157
2158sub fix_delete_line {
2159 my ($linenr, $line) = @_;
2160
2161 my $deleted = {
2162 LINENR => $linenr,
2163 LINE => $line,
2164 };
2165
2166 push(@fixed_deleted, $deleted);
2167}
2168
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002169sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002170 my ($type, $msg) = @_;
2171
2172 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002173 our $clean = 0;
2174 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002175 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002176 }
Joe Perches3705ce52013-07-03 15:05:31 -07002177 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002178}
2179sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002180 my ($type, $msg) = @_;
2181
2182 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002183 our $clean = 0;
2184 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002185 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002186 }
Joe Perches3705ce52013-07-03 15:05:31 -07002187 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002188}
2189sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002190 my ($type, $msg) = @_;
2191
2192 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002193 our $clean = 0;
2194 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002195 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002196 }
Joe Perches3705ce52013-07-03 15:05:31 -07002197 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002198}
2199
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002200sub check_absolute_file {
2201 my ($absolute, $herecurr) = @_;
2202 my $file = $absolute;
2203
2204 ##print "absolute<$absolute>\n";
2205
2206 # See if any suffix of this path is a path within the tree.
2207 while ($file =~ s@^[^/]*/@@) {
2208 if (-f "$root/$file") {
2209 ##print "file<$file>\n";
2210 last;
2211 }
2212 }
2213 if (! -f _) {
2214 return 0;
2215 }
2216
2217 # It is, so see if the prefix is acceptable.
2218 my $prefix = $absolute;
2219 substr($prefix, -length($file)) = '';
2220
2221 ##print "prefix<$prefix>\n";
2222 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002223 WARN("USE_RELATIVE_PATH",
2224 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002225 }
2226}
2227
Joe Perches3705ce52013-07-03 15:05:31 -07002228sub trim {
2229 my ($string) = @_;
2230
Joe Perchesb34c6482013-09-11 14:24:01 -07002231 $string =~ s/^\s+|\s+$//g;
2232
2233 return $string;
2234}
2235
2236sub ltrim {
2237 my ($string) = @_;
2238
2239 $string =~ s/^\s+//;
2240
2241 return $string;
2242}
2243
2244sub rtrim {
2245 my ($string) = @_;
2246
2247 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002248
2249 return $string;
2250}
2251
Joe Perches52ea8502013-11-12 15:10:09 -08002252sub string_find_replace {
2253 my ($string, $find, $replace) = @_;
2254
2255 $string =~ s/$find/$replace/g;
2256
2257 return $string;
2258}
2259
Joe Perches3705ce52013-07-03 15:05:31 -07002260sub tabify {
2261 my ($leading) = @_;
2262
Antonio Borneo713a09d2020-04-06 20:11:07 -07002263 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002264 my $max_spaces_before_tab = $source_indent - 1;
2265 my $spaces_to_tab = " " x $source_indent;
2266
2267 #convert leading spaces to tabs
2268 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2269 #Remove spaces before a tab
2270 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2271
2272 return "$leading";
2273}
2274
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002275sub pos_last_openparen {
2276 my ($line) = @_;
2277
2278 my $pos = 0;
2279
2280 my $opens = $line =~ tr/\(/\(/;
2281 my $closes = $line =~ tr/\)/\)/;
2282
2283 my $last_openparen = 0;
2284
2285 if (($opens == 0) || ($closes >= $opens)) {
2286 return -1;
2287 }
2288
2289 my $len = length($line);
2290
2291 for ($pos = 0; $pos < $len; $pos++) {
2292 my $string = substr($line, $pos);
2293 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2294 $pos += length($1) - 1;
2295 } elsif (substr($line, $pos, 1) eq '(') {
2296 $last_openparen = $pos;
2297 } elsif (index($string, '(') == -1) {
2298 last;
2299 }
2300 }
2301
Joe Perches91cb5192014-04-03 14:49:32 -07002302 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002303}
2304
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002305sub get_raw_comment {
2306 my ($line, $rawline) = @_;
2307 my $comment = '';
2308
2309 for my $i (0 .. (length($line) - 1)) {
2310 if (substr($line, $i, 1) eq "$;") {
2311 $comment .= substr($rawline, $i, 1);
2312 }
2313 }
2314
2315 return $comment;
2316}
2317
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002318sub process {
2319 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002320
2321 my $linenr=0;
2322 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002323 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002324 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002325 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002326
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002327 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002328 my $indent;
2329 my $previndent=0;
2330 my $stashindent=0;
2331
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002332 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002333 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002334 my $author = '';
2335 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002336 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002337 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002338 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002339 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002340 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002341 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002342 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002343 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002344 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002345 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002346 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002347 my $non_utf8_charset = 0;
2348
Joe Perches365dd4e2014-08-06 16:10:42 -07002349 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002350 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002351
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002352 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002353 our $cnt_lines = 0;
2354 our $cnt_error = 0;
2355 our $cnt_warn = 0;
2356 our $cnt_chk = 0;
2357
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002358 # Trace the real file/line as we go.
2359 my $realfile = '';
2360 my $realline = 0;
2361 my $realcnt = 0;
2362 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002363 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002364 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002365 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002366 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002367 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002368
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002369 my $prev_values = 'E';
2370
2371 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002372 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002373 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002374 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002375 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002376
Joe Perches7e51f192013-09-11 14:23:57 -07002377 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002378
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002379 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002380 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002381 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002382 my @setup_docs = ();
2383 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002384
Joe Perchesd8b07712013-11-12 15:10:06 -08002385 my $camelcase_file_seeded = 0;
2386
Rob Herring9f3a8992018-04-10 16:33:13 -07002387 my $checklicenseline = 1;
2388
Andy Whitcroft773647a2008-03-28 14:15:58 -07002389 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002390 my $line;
2391 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002392 $linenr++;
2393 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002394
Joe Perches3705ce52013-07-03 15:05:31 -07002395 push(@fixed, $rawline) if ($fix);
2396
Andy Whitcroft773647a2008-03-28 14:15:58 -07002397 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002398 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002399 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002400 $setup_docs = 1;
2401 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002402 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002403 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002404 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002405 $realline=$1-1;
2406 if (defined $2) {
2407 $realcnt=$3+1;
2408 } else {
2409 $realcnt=1+1;
2410 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002411 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002412
2413 # Guestimate if this is a continuing comment. Run
2414 # the context looking for a comment "edge". If this
2415 # edge is a close comment then we must be in a comment
2416 # at context start.
2417 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002418 my $cnt = $realcnt;
2419 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2420 next if (defined $rawlines[$ln - 1] &&
2421 $rawlines[$ln - 1] =~ /^-/);
2422 $cnt--;
2423 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002424 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002425 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2426 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2427 ($edge) = $1;
2428 last;
2429 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002430 }
2431 if (defined $edge && $edge eq '*/') {
2432 $in_comment = 1;
2433 }
2434
2435 # Guestimate if this is a continuing comment. If this
2436 # is the start of a diff block and this line starts
2437 # ' *' then it is very likely a comment.
2438 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002439 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002440 {
2441 $in_comment = 1;
2442 }
2443
2444 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2445 sanitise_line_reset($in_comment);
2446
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002447 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002448 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002449 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002450 $line = sanitise_line($rawline);
2451 }
2452 push(@lines, $line);
2453
2454 if ($realcnt > 1) {
2455 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2456 } else {
2457 $realcnt = 0;
2458 }
2459
2460 #print "==>$rawline\n";
2461 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002462
2463 if ($setup_docs && $line =~ /^\+/) {
2464 push(@setup_docs, $line);
2465 }
2466 }
2467
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002468 $prefix = '';
2469
Andy Whitcroft773647a2008-03-28 14:15:58 -07002470 $realcnt = 0;
2471 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002472 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002473 foreach my $line (@lines) {
2474 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002475 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002476 my $sline = $line; #copy of $line
2477 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002478
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002479 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002480 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002481
Joe Perches12c253a2018-06-07 17:10:58 -07002482# check if it's a mode change, rename or start of a patch
2483 if (!$in_commit_log &&
2484 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2485 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2486 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2487 $is_patch = 1;
2488 }
2489
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002490#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002491 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002492 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2493 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002494 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002495 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002496 $realline=$1-1;
2497 if (defined $2) {
2498 $realcnt=$3+1;
2499 } else {
2500 $realcnt=1+1;
2501 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002502 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002503 $prev_values = 'E';
2504
Andy Whitcroft773647a2008-03-28 14:15:58 -07002505 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002506 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002507 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002508 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002509 if ($context =~ /\b(\w+)\s*\(/) {
2510 $context_function = $1;
2511 } else {
2512 undef $context_function;
2513 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002514 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002515
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002516# track the line number as we move through the hunk, note that
2517# new versions of GNU diff omit the leading space on completely
2518# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002519 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002520 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002521 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002522
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002523 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002524 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002525
2526 # Track the previous line.
2527 ($prevline, $stashline) = ($stashline, $line);
2528 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002529 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2530
Andy Whitcroft773647a2008-03-28 14:15:58 -07002531 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002532
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002533 } elsif ($realcnt == 1) {
2534 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002535 }
2536
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002537 my $hunk_line = ($realcnt != 0);
2538
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002539 $here = "#$linenr: " if (!$file);
2540 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002541
Joe Perches2ac73b42014-06-04 16:12:05 -07002542 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002543 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002544 if ($line =~ /^diff --git.*?(\S+)$/) {
2545 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002546 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002547 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002548 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002549 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002550 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002551 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002552 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002553
2554 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002555 if (!$file && $tree && $p1_prefix ne '' &&
2556 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002557 WARN("PATCH_PREFIX",
2558 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002559 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002560
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002561 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002562 ERROR("MODIFIED_INCLUDE_ASM",
2563 "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 -07002564 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002565 $found_file = 1;
2566 }
2567
Joe Perches34d88152015-06-25 15:03:05 -07002568#make up the handle for any error we report on this line
2569 if ($showfile) {
2570 $prefix = "$realfile:$realline: "
2571 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002572 if ($file) {
2573 $prefix = "$filename:$realline: ";
2574 } else {
2575 $prefix = "$filename:$linenr: ";
2576 }
Joe Perches34d88152015-06-25 15:03:05 -07002577 }
2578
Joe Perches2ac73b42014-06-04 16:12:05 -07002579 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002580 if (is_maintained_obsolete($realfile)) {
2581 WARN("OBSOLETE",
2582 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2583 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002584 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002585 $check = 1;
2586 } else {
2587 $check = $check_orig;
2588 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002589 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002590
2591 if ($realfile !~ /^MAINTAINERS/) {
2592 my $last_binding_patch = $is_binding_patch;
2593
2594 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2595
2596 if (($last_binding_patch != -1) &&
2597 ($last_binding_patch ^ $is_binding_patch)) {
2598 WARN("DT_SPLIT_BINDING_PATCH",
2599 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2600 }
2601 }
2602
Andy Whitcroft773647a2008-03-28 14:15:58 -07002603 next;
2604 }
2605
Randy Dunlap389834b2007-06-08 13:47:03 -07002606 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002607
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002608 my $hereline = "$here\n$rawline\n";
2609 my $herecurr = "$here\n$rawline\n";
2610 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002611
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002612 $cnt_lines++ if ($realcnt != 0);
2613
Joe Perches490b2922018-08-21 21:58:01 -07002614# Verify the existence of a commit log if appropriate
2615# 2 is used because a $signature is counted in $commit_log_lines
2616 if ($in_commit_log) {
2617 if ($line !~ /^\s*$/) {
2618 $commit_log_lines++; #could be a $signature
2619 }
2620 } elsif ($has_commit_log && $commit_log_lines < 2) {
2621 WARN("COMMIT_MESSAGE",
2622 "Missing commit description - Add an appropriate one\n");
2623 $commit_log_lines = 2; #warn only once
2624 }
2625
Joe Perchese518e9a2015-06-25 15:03:27 -07002626# Check if the commit log has what seems like a diff which can confuse patch
2627 if ($in_commit_log && !$commit_log_has_diff &&
2628 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2629 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2630 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2631 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2632 ERROR("DIFF_IN_COMMIT_MSG",
2633 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2634 $commit_log_has_diff = 1;
2635 }
2636
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002637# Check for incorrect file permissions
2638 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2639 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002640 if ($realfile !~ m@scripts/@ &&
2641 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002642 ERROR("EXECUTE_PERMISSIONS",
2643 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002644 }
2645 }
2646
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002647# Check the patch for a From:
2648 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2649 $author = $1;
2650 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2651 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002652 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002653 }
2654
Joe Perches20112472011-07-25 17:13:23 -07002655# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002656 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002657 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002658 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002659 if ($author ne '') {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002660 if (same_email_addresses($1, $author)) {
2661 $authorsignoff = 1;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002662 }
2663 }
Joe Perches20112472011-07-25 17:13:23 -07002664 }
2665
Joe Perches44d303e2020-04-06 20:11:10 -07002666# Check for patch separator
2667 if ($line =~ /^---$/) {
2668 $has_patch_separator = 1;
2669 $in_commit_log = 0;
2670 }
2671
Joe Perchese0d975b2014-12-10 15:51:49 -08002672# Check if MAINTAINERS is being updated. If so, there's probably no need to
2673# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2674 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2675 $reported_maintainer_file = 1;
2676 }
2677
Joe Perches20112472011-07-25 17:13:23 -07002678# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002679 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002680 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002681 my $space_before = $1;
2682 my $sign_off = $2;
2683 my $space_after = $3;
2684 my $email = $4;
2685 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2686
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002687 if ($sign_off !~ /$signature_tags/) {
2688 WARN("BAD_SIGN_OFF",
2689 "Non-standard signature: $sign_off\n" . $herecurr);
2690 }
Joe Perches20112472011-07-25 17:13:23 -07002691 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002692 if (WARN("BAD_SIGN_OFF",
2693 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2694 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002695 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002696 "$ucfirst_sign_off $email";
2697 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002698 }
Joe Perches20112472011-07-25 17:13:23 -07002699 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002700 if (WARN("BAD_SIGN_OFF",
2701 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2702 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002703 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002704 "$ucfirst_sign_off $email";
2705 }
2706
Joe Perches20112472011-07-25 17:13:23 -07002707 }
2708 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002709 if (WARN("BAD_SIGN_OFF",
2710 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2711 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002712 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002713 "$ucfirst_sign_off $email";
2714 }
Joe Perches20112472011-07-25 17:13:23 -07002715 }
2716
Joe Perchesdfa05c22020-04-06 20:10:48 -07002717 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Joe Perches20112472011-07-25 17:13:23 -07002718 my $suggested_email = format_email(($email_name, $email_address));
2719 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002720 ERROR("BAD_SIGN_OFF",
2721 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002722 } else {
2723 my $dequoted = $suggested_email;
2724 $dequoted =~ s/^"//;
2725 $dequoted =~ s/" </ </;
2726 # Don't force email to have quotes
2727 # Allow just an angle bracketed address
Joe Perchesdfa05c22020-04-06 20:10:48 -07002728 if (!same_email_addresses($email, $suggested_email)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002729 WARN("BAD_SIGN_OFF",
2730 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002731 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002732 }
Joe Perches7e51f192013-09-11 14:23:57 -07002733
2734# Check for duplicate signatures
2735 my $sig_nospace = $line;
2736 $sig_nospace =~ s/\s//g;
2737 $sig_nospace = lc($sig_nospace);
2738 if (defined $signatures{$sig_nospace}) {
2739 WARN("BAD_SIGN_OFF",
2740 "Duplicate signature\n" . $herecurr);
2741 } else {
2742 $signatures{$sig_nospace} = 1;
2743 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002744
2745# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2746 if ($sign_off =~ /^co-developed-by:$/i) {
2747 if ($email eq $author) {
2748 WARN("BAD_SIGN_OFF",
2749 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2750 }
2751 if (!defined $lines[$linenr]) {
2752 WARN("BAD_SIGN_OFF",
2753 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2754 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2755 WARN("BAD_SIGN_OFF",
2756 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2757 } elsif ($1 ne $email) {
2758 WARN("BAD_SIGN_OFF",
2759 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2760 }
2761 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002762 }
2763
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002764# Check email subject for common tools that don't need to be mentioned
2765 if ($in_header_lines &&
2766 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2767 WARN("EMAIL_SUBJECT",
2768 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2769 }
2770
Joe Perches44d303e2020-04-06 20:11:10 -07002771# Check for Gerrit Change-Ids not in any patch context
2772 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002773 ERROR("GERRIT_CHANGE_ID",
Joe Perches44d303e2020-04-06 20:11:10 -07002774 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002775 }
2776
Joe Perches369c8dd2015-11-06 16:31:34 -08002777# Check if the commit log is in a possible stack dump
2778 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2779 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2780 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2781 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07002782 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2783 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2784 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2785 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08002786 $commit_log_possible_stack_dump = 1;
2787 }
2788
Joe Perches2a076f42015-04-16 12:44:28 -07002789# Check for line lengths > 75 in commit log, warn once
2790 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002791 length($line) > 75 &&
2792 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2793 # file delta changes
2794 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2795 # filename then :
2796 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2797 # A Fixes: or Link: line
2798 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002799 WARN("COMMIT_LOG_LONG_LINE",
2800 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2801 $commit_log_long_line = 1;
2802 }
2803
Joe Perchesbf4daf12015-09-09 15:37:50 -07002804# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002805 if ($in_commit_log && $commit_log_possible_stack_dump &&
2806 $line =~ /^\s*$/) {
2807 $commit_log_possible_stack_dump = 0;
2808 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002809
Joe Perches0d7835f2015-02-13 14:38:35 -08002810# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002811 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07002812 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002813 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002814 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002815 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002816 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2817 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002818 my $init_char = "c";
2819 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002820 my $short = 1;
2821 my $long = 0;
2822 my $case = 1;
2823 my $space = 1;
2824 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002825 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002826 my $id = '0123456789ab';
2827 my $orig_desc = "commit description";
2828 my $description = "";
2829
Joe Perchesfe043ea2015-09-09 15:37:25 -07002830 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2831 $init_char = $1;
2832 $orig_commit = lc($2);
2833 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2834 $orig_commit = lc($1);
2835 }
2836
Joe Perches0d7835f2015-02-13 14:38:35 -08002837 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2838 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2839 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2840 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2841 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2842 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002843 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002844 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2845 defined $rawlines[$linenr] &&
2846 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2847 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002848 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002849 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2850 defined $rawlines[$linenr] &&
2851 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2852 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2853 $orig_desc = $1;
2854 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2855 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002856 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002857 }
2858
2859 ($id, $description) = git_commit_info($orig_commit,
2860 $id, $orig_desc);
2861
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002862 if (defined($id) &&
2863 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002864 ERROR("GIT_COMMIT_ID",
2865 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2866 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002867 }
2868
Joe Perches13f19372014-08-06 16:10:59 -07002869# Check for added, moved or deleted files
2870 if (!$reported_maintainer_file && !$in_commit_log &&
2871 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2872 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2873 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2874 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002875 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002876 $reported_maintainer_file = 1;
2877 WARN("FILE_PATH_CHANGES",
2878 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2879 }
2880
Rob Herringe400edb2019-09-12 15:18:20 +01002881# Check for adding new DT bindings not in schema format
2882 if (!$in_commit_log &&
2883 ($line =~ /^new file mode\s*\d+\s*$/) &&
2884 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2885 WARN("DT_SCHEMA_BINDING_PATCH",
2886 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2887 }
2888
Andy Whitcroft00df3442007-06-08 13:47:06 -07002889# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002890 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002891 ERROR("CORRUPTED_PATCH",
2892 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002893 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002894 }
2895
2896# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2897 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002898 $rawline !~ m/^$UTF8*$/) {
2899 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2900
2901 my $blank = copy_spacing($rawline);
2902 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2903 my $hereptr = "$hereline$ptr\n";
2904
Joe Perches34d99212011-07-25 17:13:26 -07002905 CHK("INVALID_UTF8",
2906 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002907 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002908
Joe Perches15662b32011-10-31 17:13:12 -07002909# Check if it's the start of a commit log
2910# (not a header line and we haven't seen the patch filename)
2911 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002912 !($rawline =~ /^\s+(?:\S|$)/ ||
2913 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002914 $in_header_lines = 0;
2915 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002916 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002917 }
2918
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002919# Check if there is UTF-8 in a commit log when a mail header has explicitly
2920# declined it, i.e defined some charset where it is missing.
2921 if ($in_header_lines &&
2922 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2923 $1 !~ /utf-8/i) {
2924 $non_utf8_charset = 1;
2925 }
2926
2927 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002928 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002929 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002930 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2931 }
2932
Joe Perchesd6430f72016-12-12 16:46:28 -08002933# Check for absolute kernel paths in commit message
2934 if ($tree && $in_commit_log) {
2935 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2936 my $file = $1;
2937
2938 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2939 check_absolute_file($1, $herecurr)) {
2940 #
2941 } else {
2942 check_absolute_file($file, $herecurr);
2943 }
2944 }
2945 }
2946
Kees Cook66b47b42014-10-13 15:51:57 -07002947# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002948 if (defined($misspellings) &&
2949 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002950 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002951 my $typo = $1;
2952 my $typo_fix = $spelling_fix{lc($typo)};
2953 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2954 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002955 my $msg_level = \&WARN;
2956 $msg_level = \&CHK if ($file);
2957 if (&{$msg_level}("TYPO_SPELLING",
2958 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002959 $fix) {
2960 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2961 }
2962 }
2963 }
2964
Matteo Crocea8dd86b2019-09-25 16:46:38 -07002965# check for invalid commit id
2966 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2967 my $id;
2968 my $description;
2969 ($id, $description) = git_commit_info($2, undef, undef);
2970 if (!defined($id)) {
2971 WARN("UNKNOWN_COMMIT_ID",
2972 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2973 }
2974 }
2975
Andy Whitcroft306708542008-10-15 22:02:28 -07002976# ignore non-hunk lines and lines being removed
2977 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002978
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002979#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002980 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002981 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002982 if (ERROR("DOS_LINE_ENDINGS",
2983 "DOS line endings\n" . $herevet) &&
2984 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002985 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002986 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002987 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2988 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002989 if (ERROR("TRAILING_WHITESPACE",
2990 "trailing whitespace\n" . $herevet) &&
2991 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002992 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002993 }
2994
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002995 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002996 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002997
Josh Triplett4783f892013-11-12 15:10:12 -08002998# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002999 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003000 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003001 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3002 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003003 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003004 my $msg_level = \&ERROR;
3005 $msg_level = \&CHK if ($file);
3006 &{$msg_level}("FSF_MAILING_ADDRESS",
3007 "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 -08003008 }
3009
Andi Kleen33549572010-05-24 14:33:29 -07003010# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003011# Only applies when adding the entry originally, after that we do not have
3012# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003013 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003014 # 'choice' is usually the last thing on the line (though
3015 # Kconfig supports named choices), so use a word boundary
3016 # (\b) rather than a whitespace character (\s)
3017 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003018 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003019 my $cnt = $realcnt;
3020 my $ln = $linenr + 1;
3021 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003022 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003023 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003024 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003025 $f = $lines[$ln - 1];
3026 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3027 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003028
3029 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003030 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003031
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003032 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003033 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01003034 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
3035 if ($lines[$ln - 1] =~ "---help---") {
3036 WARN("CONFIG_DESCRIPTION",
3037 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
3038 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003039 $length = -1;
3040 }
3041
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003042 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003043 $f =~ s/#.*//;
3044 $f =~ s/^\s+//;
3045 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003046
3047 # This only checks context lines in the patch
3048 # and so hopefully shouldn't trigger false
3049 # positives, even though some of these are
3050 # common words in help texts
3051 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3052 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003053 $is_end = 1;
3054 last;
3055 }
Andi Kleen33549572010-05-24 14:33:29 -07003056 $length++;
3057 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003058 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3059 WARN("CONFIG_DESCRIPTION",
3060 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3061 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003062 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003063 }
3064
Joe Perches7ccf41a2020-06-04 16:50:33 -07003065# check MAINTAINERS entries
3066 if ($realfile =~ /^MAINTAINERS$/) {
3067# check MAINTAINERS entries for the right form
3068 if ($rawline =~ /^\+[A-Z]:/ &&
3069 $rawline !~ /^\+[A-Z]:\t\S/) {
3070 if (WARN("MAINTAINERS_STYLE",
3071 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3072 $fix) {
3073 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3074 }
3075 }
3076# check MAINTAINERS entries for the right ordering too
3077 my $preferred_order = 'MRLSWQBCPTFXNK';
3078 if ($rawline =~ /^\+[A-Z]:/ &&
3079 $prevrawline =~ /^[\+ ][A-Z]:/) {
3080 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3081 my $cur = $1;
3082 my $curval = $2;
3083 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3084 my $prev = $1;
3085 my $prevval = $2;
3086 my $curindex = index($preferred_order, $cur);
3087 my $previndex = index($preferred_order, $prev);
3088 if ($curindex < 0) {
3089 WARN("MAINTAINERS_STYLE",
3090 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3091 } else {
3092 if ($previndex >= 0 && $curindex < $previndex) {
3093 WARN("MAINTAINERS_STYLE",
3094 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3095 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3096 ($prev eq 'X' && $cur eq 'X')) &&
3097 ($prevval cmp $curval) > 0) {
3098 WARN("MAINTAINERS_STYLE",
3099 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3100 }
3101 }
Joe Perches628f91a2017-07-10 15:52:07 -07003102 }
3103 }
3104
Christoph Jaeger327953e2015-02-13 14:38:29 -08003105# discourage the use of boolean for type definition attributes of Kconfig options
3106 if ($realfile =~ /Kconfig/ &&
3107 $line =~ /^\+\s*\bboolean\b/) {
3108 WARN("CONFIG_TYPE_BOOLEAN",
3109 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3110 }
3111
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003112 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3113 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3114 my $flag = $1;
3115 my $replacement = {
3116 'EXTRA_AFLAGS' => 'asflags-y',
3117 'EXTRA_CFLAGS' => 'ccflags-y',
3118 'EXTRA_CPPFLAGS' => 'cppflags-y',
3119 'EXTRA_LDFLAGS' => 'ldflags-y',
3120 };
3121
3122 WARN("DEPRECATED_VARIABLE",
3123 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3124 }
3125
Rob Herringbff5da42014-01-23 15:54:51 -08003126# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003127 if (defined $root &&
3128 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3129 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3130
Rob Herringbff5da42014-01-23 15:54:51 -08003131 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3132
Florian Vaussardcc933192014-04-03 14:49:27 -07003133 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003134 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003135
Rob Herringbff5da42014-01-23 15:54:51 -08003136 foreach my $compat (@compats) {
3137 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003138 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3139 my $compat3 = $compat;
3140 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3141 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003142 if ( $? >> 8 ) {
3143 WARN("UNDOCUMENTED_DT_STRING",
3144 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3145 }
3146
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003147 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3148 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003149 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003150 if ( $? >> 8 ) {
3151 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003152 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003153 }
3154 }
3155 }
3156
Rob Herring9f3a8992018-04-10 16:33:13 -07003157# check for using SPDX license tag at beginning of files
3158 if ($realline == $checklicenseline) {
3159 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3160 $checklicenseline = 2;
3161 } elsif ($rawline =~ /^\+/) {
3162 my $comment = "";
3163 if ($realfile =~ /\.(h|s|S)$/) {
3164 $comment = '/*';
3165 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3166 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003167 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003168 $comment = '#';
3169 } elsif ($realfile =~ /\.rst$/) {
3170 $comment = '..';
3171 }
3172
Joe Perchesfdf13692019-03-07 16:28:32 -08003173# check SPDX comment style for .[chsS] files
3174 if ($realfile =~ /\.[chsS]$/ &&
3175 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003176 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003177 WARN("SPDX_LICENSE_TAG",
3178 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3179 }
3180
Rob Herring9f3a8992018-04-10 16:33:13 -07003181 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003182 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3183 WARN("SPDX_LICENSE_TAG",
3184 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003185 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003186 my $spdx_license = $1;
3187 if (!is_SPDX_License_valid($spdx_license)) {
3188 WARN("SPDX_LICENSE_TAG",
3189 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3190 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003191 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3192 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3193 my $msg_level = \&WARN;
3194 $msg_level = \&CHK if ($file);
3195 if (&{$msg_level}("SPDX_LICENSE_TAG",
3196
3197 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3198 $fix) {
3199 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3200 }
3201 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003202 }
3203 }
3204 }
3205
Andy Whitcroft5368df202008-10-15 22:02:27 -07003206# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003207 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003208
Joe Perchesa8da38a2019-03-07 16:28:42 -08003209# check for using SPDX-License-Identifier on the wrong line number
3210 if ($realline != $checklicenseline &&
3211 $rawline =~ /\bSPDX-License-Identifier:/ &&
3212 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3213 WARN("SPDX_LICENSE_TAG",
3214 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3215 }
3216
Joe Perches47e0c882015-06-25 15:02:57 -07003217# line length limit (with some exclusions)
3218#
3219# There are a few types of lines that may extend beyond $max_line_length:
3220# logging functions like pr_info that end in a string
3221# lines with a single string
3222# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003223# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003224#
3225# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003226# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003227# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3228# LONG_LINE all other lines longer than $max_line_length
3229#
3230# if LONG_LINE is ignored, the other 2 types are also ignored
3231#
3232
Joe Perchesb4749e92015-07-17 16:24:01 -07003233 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003234 my $msg_type = "LONG_LINE";
3235
3236 # Check the allowed long line types first
3237
3238 # logging functions that end in a string that starts
3239 # before $max_line_length
3240 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3241 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3242 $msg_type = "";
3243
3244 # lines with only strings (w/ possible termination)
3245 # #defines with only strings
3246 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3247 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3248 $msg_type = "";
3249
Joe Perchescc147502017-11-17 15:28:44 -08003250 # More special cases
3251 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3252 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003253 $msg_type = "";
3254
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003255 # URL ($rawline is used in case the URL is in a comment)
3256 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3257 $msg_type = "";
3258
Joe Perches47e0c882015-06-25 15:02:57 -07003259 # Otherwise set the alternate message types
3260
3261 # a comment starts before $max_line_length
3262 } elsif ($line =~ /($;[\s$;]*)$/ &&
3263 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3264 $msg_type = "LONG_LINE_COMMENT"
3265
3266 # a quoted string starts before $max_line_length
3267 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3268 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3269 $msg_type = "LONG_LINE_STRING"
3270 }
3271
3272 if ($msg_type ne "" &&
3273 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003274 my $msg_level = \&WARN;
3275 $msg_level = \&CHK if ($file);
3276 &{$msg_level}($msg_type,
3277 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003278 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003279 }
3280
Andy Whitcroft8905a672007-11-28 16:21:06 -08003281# check for adding lines without a newline.
3282 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003283 WARN("MISSING_EOF_NEWLINE",
3284 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003285 }
3286
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003287# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003288 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003289
3290# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003291# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003292 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3293 $rawline =~ /^\+\s* \s*/) {
3294 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003295 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003296 if (ERROR("CODE_INDENT",
3297 "code indent should use tabs where possible\n" . $herevet) &&
3298 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003299 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003300 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003301 }
3302
Alberto Panizzo08e44362010-03-05 13:43:54 -08003303# check for space before tabs.
3304 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3305 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003306 if (WARN("SPACE_BEFORE_TAB",
3307 "please, no space before tabs\n" . $herevet) &&
3308 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003309 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003310 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003311 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003312 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003313 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003314 }
3315
Joe Perches6a487212018-04-10 16:34:04 -07003316# check for assignments on the start of a line
3317 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3318 CHK("ASSIGNMENT_CONTINUATIONS",
3319 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3320 }
3321
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003322# check for && or || at the start of a line
3323 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3324 CHK("LOGICAL_CONTINUATIONS",
3325 "Logical continuations should be on the previous line\n" . $hereprev);
3326 }
3327
Joe Perchesa91e8992016-05-20 17:04:05 -07003328# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003329 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003330 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003331 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003332 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003333 if (WARN("TABSTOP",
3334 "Statements should start on a tabstop\n" . $herecurr) &&
3335 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003336 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003337 }
3338 }
3339 }
3340
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003341# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003342 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003343 $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 -07003344 $prevline =~ /^\+(\t*)(.*)$/;
3345 my $oldindent = $1;
3346 my $rest = $2;
3347
3348 my $pos = pos_last_openparen($rest);
3349 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003350 $line =~ /^(\+| )([ \t]*)/;
3351 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003352
3353 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003354 "\t" x ($pos / $tabsize) .
3355 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003356 my $goodspaceindent = $oldindent . " " x $pos;
3357
3358 if ($newindent ne $goodtabindent &&
3359 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003360
3361 if (CHK("PARENTHESIS_ALIGNMENT",
3362 "Alignment should match open parenthesis\n" . $hereprev) &&
3363 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003364 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003365 s/^\+[ \t]*/\+$goodtabindent/;
3366 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003367 }
3368 }
3369 }
3370
Joe Perches6ab3a972015-04-16 12:44:05 -07003371# check for space after cast like "(int) foo" or "(struct foo) bar"
3372# avoid checking a few false positives:
3373# "sizeof(<type>)" or "__alignof__(<type>)"
3374# function pointer declarations like "(*foo)(int) = bar;"
3375# structure definitions like "(struct foo) { 0 };"
3376# multiline macros that define functions
3377# known attributes or the __attribute__ keyword
3378 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3379 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003380 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003381 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003382 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003383 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003384 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003385 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003386 }
3387
Joe Perches86406b12015-09-09 15:37:41 -07003388# Block comment styles
3389# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003390 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003391 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003392 $rawline =~ /^\+[ \t]*\*/ &&
3393 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003394 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3395 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3396 }
3397
Joe Perches86406b12015-09-09 15:37:41 -07003398# Block comments use * on subsequent lines
3399 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3400 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003401 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003402 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003403 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003404 WARN("BLOCK_COMMENT_STYLE",
3405 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003406 }
3407
Joe Perches86406b12015-09-09 15:37:41 -07003408# Block comments use */ on trailing lines
3409 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003410 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3411 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3412 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003413 WARN("BLOCK_COMMENT_STYLE",
3414 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003415 }
3416
Joe Perches08eb9b82016-10-11 13:51:50 -07003417# Block comment * alignment
3418 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003419 $line =~ /^\+[ \t]*$;/ && #leading comment
3420 $rawline =~ /^\+[ \t]*\*/ && #leading *
3421 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003422 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003423 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3424 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003425 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003426 if (defined($1)) {
3427 $oldindent = expand_tabs($1);
3428 } else {
3429 $prevrawline =~ m@^\+(.*/?)\*@;
3430 $oldindent = expand_tabs($1);
3431 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003432 $rawline =~ m@^\+([ \t]*)\*@;
3433 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003434 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003435 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003436 WARN("BLOCK_COMMENT_STYLE",
3437 "Block comments should align the * on each line\n" . $hereprev);
3438 }
3439 }
3440
Joe Perches7f619192014-08-06 16:10:39 -07003441# check for missing blank lines after struct/union declarations
3442# with exceptions for various attributes and macros
3443 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3444 $line =~ /^\+/ &&
3445 !($line =~ /^\+\s*$/ ||
3446 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3447 $line =~ /^\+\s*MODULE_/i ||
3448 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3449 $line =~ /^\+[a-z_]*init/ ||
3450 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3451 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003452 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003453 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003454 if (CHK("LINE_SPACING",
3455 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3456 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003457 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003458 }
Joe Perches7f619192014-08-06 16:10:39 -07003459 }
3460
Joe Perches365dd4e2014-08-06 16:10:42 -07003461# check for multiple consecutive blank lines
3462 if ($prevline =~ /^[\+ ]\s*$/ &&
3463 $line =~ /^\+\s*$/ &&
3464 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003465 if (CHK("LINE_SPACING",
3466 "Please don't use multiple blank lines\n" . $hereprev) &&
3467 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003468 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003469 }
3470
Joe Perches365dd4e2014-08-06 16:10:42 -07003471 $last_blank_line = $linenr;
3472 }
3473
Joe Perches3b617e32014-04-03 14:49:28 -07003474# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003475 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3476 # actual declarations
3477 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003478 # function pointer declarations
3479 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003480 # foo bar; where foo is some local typedef or #define
3481 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3482 # known declaration macros
3483 $prevline =~ /^\+\s+$declaration_macros/) &&
3484 # for "else if" which can look like "$Ident $Ident"
3485 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3486 # other possible extensions of declaration lines
3487 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3488 # not starting a section or a macro "\" extended line
3489 $prevline =~ /(?:\{\s*|\\)$/) &&
3490 # looks like a declaration
3491 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003492 # function pointer declarations
3493 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003494 # foo bar; where foo is some local typedef or #define
3495 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3496 # known declaration macros
3497 $sline =~ /^\+\s+$declaration_macros/ ||
3498 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003499 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003500 # start or end of block or continuation of declaration
3501 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3502 # bitfield continuation
3503 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3504 # other possible extensions of declaration lines
3505 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3506 # indentation of previous and current line are the same
3507 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003508 if (WARN("LINE_SPACING",
3509 "Missing a blank line after declarations\n" . $hereprev) &&
3510 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003511 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003512 }
Joe Perches3b617e32014-04-03 14:49:28 -07003513 }
3514
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003515# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003516# Exceptions:
3517# 1) within comments
3518# 2) indented preprocessor commands
3519# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003520 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003521 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003522 if (WARN("LEADING_SPACE",
3523 "please, no spaces at the start of a line\n" . $herevet) &&
3524 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003525 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003526 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003527 }
3528
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003529# check we are in a valid C source file if not then ignore this hunk
3530 next if ($realfile !~ /\.(h|c)$/);
3531
Joe Perches5751a242017-11-17 15:28:52 -08003532# check for unusual line ending [ or (
3533 if ($line =~ /^\+.*([\[\(])\s*$/) {
3534 CHK("OPEN_ENDED_LINE",
3535 "Lines should not end with a '$1'\n" . $herecurr);
3536 }
3537
Joe Perches4dbed762017-05-08 15:55:39 -07003538# check if this appears to be the start function declaration, save the name
3539 if ($sline =~ /^\+\{\s*$/ &&
3540 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3541 $context_function = $1;
3542 }
3543
3544# check if this appears to be the end of function declaration
3545 if ($sline =~ /^\+\}\s*$/) {
3546 undef $context_function;
3547 }
3548
Joe Perches032a4c02014-08-06 16:10:29 -07003549# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003550# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003551# if the previous line is a break or return and is indented 1 tab more...
3552 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3553 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003554 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3555 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3556 defined $lines[$linenr] &&
3557 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003558 WARN("UNNECESSARY_ELSE",
3559 "else is not generally useful after a break or return\n" . $hereprev);
3560 }
3561 }
3562
Joe Perchesc00df192014-08-06 16:11:01 -07003563# check indentation of a line with a break;
3564# if the previous line is a goto or return and is indented the same # of tabs
3565 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3566 my $tabs = $1;
3567 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3568 WARN("UNNECESSARY_BREAK",
3569 "break is not useful after a goto or return\n" . $hereprev);
3570 }
3571 }
3572
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003573# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003574 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003575 WARN("CVS_KEYWORD",
3576 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003577 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003578
Joe Perches56e77d72013-02-21 16:44:14 -08003579# check for old HOTPLUG __dev<foo> section markings
3580 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3581 WARN("HOTPLUG_SECTION",
3582 "Using $1 is unnecessary\n" . $herecurr);
3583 }
3584
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003585# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003586 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3587 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003588#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003589 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003590 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003591 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003592 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003593 $stat =~ s/\n./\n /g;
3594 $cond =~ s/\n./\n /g;
3595
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003596#print "linenr<$linenr> <$stat>\n";
3597 # If this statement has no statement boundaries within
3598 # it there is no point in retrying a statement scan
3599 # until we hit end of it.
3600 my $frag = $stat; $frag =~ s/;+\s*$//;
3601 if ($frag !~ /(?:{|;)/) {
3602#print "skip<$line_nr_next>\n";
3603 $suppress_statement = $line_nr_next;
3604 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003605
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003606 # Find the real next line.
3607 $realline_next = $line_nr_next;
3608 if (defined $realline_next &&
3609 (!defined $lines[$realline_next - 1] ||
3610 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3611 $realline_next++;
3612 }
3613
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003614 my $s = $stat;
3615 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003616
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003617 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003618 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003619
3620 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003621 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003622
Andy Whitcroft463f2862009-09-21 17:04:34 -07003623 } elsif ($s =~ /^.\s*else\b/s) {
3624
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003625 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003626 } 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 -07003627 my $type = $1;
3628 $type =~ s/\s+/ /g;
3629 possible($type, "A:" . $s);
3630
Andy Whitcroft8905a672007-11-28 16:21:06 -08003631 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003632 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003633 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003634 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003635
3636 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003637 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003638 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003639 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003640
3641 # Check for any sort of function declaration.
3642 # int foo(something bar, other baz);
3643 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003644 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 -08003645 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003646
Andy Whitcroftcf655042008-03-04 14:28:20 -08003647 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003648 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003649 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003650
Andy Whitcroft8905a672007-11-28 16:21:06 -08003651 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003652 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003653
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003654 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003655 }
3656 }
3657 }
3658
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003659 }
3660
Andy Whitcroft653d4872007-06-23 17:16:34 -07003661#
3662# Checks which may be anchored in the context.
3663#
3664
3665# Check for switch () and associated case and default
3666# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003667 if ($line=~/\bswitch\s*\(.*\)/) {
3668 my $err = '';
3669 my $sep = '';
3670 my @ctx = ctx_block_outer($linenr, $realcnt);
3671 shift(@ctx);
3672 for my $ctx (@ctx) {
3673 my ($clen, $cindent) = line_stats($ctx);
3674 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3675 $indent != $cindent) {
3676 $err .= "$sep$ctx\n";
3677 $sep = '';
3678 } else {
3679 $sep = "[...]\n";
3680 }
3681 }
3682 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003683 ERROR("SWITCH_CASE_INDENT_LEVEL",
3684 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003685 }
3686 }
3687
3688# if/while/etc brace do not go on next line, unless defining a do while loop,
3689# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003690 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 -07003691 my $pre_ctx = "$1$2";
3692
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003693 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003694
3695 if ($line =~ /^\+\t{6,}/) {
3696 WARN("DEEP_INDENTATION",
3697 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3698 }
3699
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003700 my $ctx_cnt = $realcnt - $#ctx - 1;
3701 my $ctx = join("\n", @ctx);
3702
Andy Whitcroft548596d2008-07-23 21:29:01 -07003703 my $ctx_ln = $linenr;
3704 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003705
Andy Whitcroft548596d2008-07-23 21:29:01 -07003706 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3707 defined $lines[$ctx_ln - 1] &&
3708 $lines[$ctx_ln - 1] =~ /^-/)) {
3709 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3710 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003711 $ctx_ln++;
3712 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003713
Andy Whitcroft53210162008-07-23 21:29:03 -07003714 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3715 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003716
Joe Perchesd752fcc2014-08-06 16:11:05 -07003717 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003718 ERROR("OPEN_BRACE",
3719 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003720 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003721 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003722 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3723 $ctx =~ /\)\s*\;\s*$/ &&
3724 defined $lines[$ctx_ln - 1])
3725 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003726 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3727 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003728 WARN("TRAILING_SEMICOLON",
3729 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003730 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003731 }
3732 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003733 }
3734
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003735# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003736 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 -08003737 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3738 ctx_statement_block($linenr, $realcnt, 0)
3739 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003740 my ($s, $c) = ($stat, $cond);
3741
3742 substr($s, 0, length($c), '');
3743
Joe Perches9f5af482015-09-09 15:37:30 -07003744 # remove inline comments
3745 $s =~ s/$;/ /g;
3746 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003747
3748 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003749 my @newlines = ($c =~ /\n/gs);
3750 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003751
Joe Perches9f5af482015-09-09 15:37:30 -07003752 # Make sure we remove the line prefixes as we have
3753 # none on the first line, and are going to readd them
3754 # where necessary.
3755 $s =~ s/\n./\n/gs;
3756 while ($s =~ /\n\s+\\\n/) {
3757 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3758 }
3759
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003760 # We want to check the first line inside the block
3761 # starting at the end of the conditional, so remove:
3762 # 1) any blank line termination
3763 # 2) any opening brace { on end of the line
3764 # 3) any do (...) {
3765 my $continuation = 0;
3766 my $check = 0;
3767 $s =~ s/^.*\bdo\b//;
3768 $s =~ s/^\s*{//;
3769 if ($s =~ s/^\s*\\//) {
3770 $continuation = 1;
3771 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003772 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003773 $check = 1;
3774 $cond_lines++;
3775 }
3776
3777 # Also ignore a loop construct at the end of a
3778 # preprocessor statement.
3779 if (($prevline =~ /^.\s*#\s*define\s/ ||
3780 $prevline =~ /\\\s*$/) && $continuation == 0) {
3781 $check = 0;
3782 }
3783
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003784 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003785 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003786 while ($cond_ptr != $cond_lines) {
3787 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003788
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003789 # If we see an #else/#elif then the code
3790 # is not linear.
3791 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3792 $check = 0;
3793 }
3794
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003795 # Ignore:
3796 # 1) blank lines, they should be at 0,
3797 # 2) preprocessor lines, and
3798 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003799 if ($continuation ||
3800 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003801 $s =~ /^\s*#\s*?/ ||
3802 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003803 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003804 if ($s =~ s/^.*?\n//) {
3805 $cond_lines++;
3806 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003807 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003808 }
3809
3810 my (undef, $sindent) = line_stats("+" . $s);
3811 my $stat_real = raw_line($linenr, $cond_lines);
3812
3813 # Check if either of these lines are modified, else
3814 # this is not this patch's fault.
3815 if (!defined($stat_real) ||
3816 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3817 $check = 0;
3818 }
3819 if (defined($stat_real) && $cond_lines > 1) {
3820 $stat_real = "[...]\n$stat_real";
3821 }
3822
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003823 #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 -07003824
Joe Perches9f5af482015-09-09 15:37:30 -07003825 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07003826 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07003827 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003828 ($sindent == $indent &&
3829 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07003830 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003831 WARN("SUSPECT_CODE_INDENT",
3832 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003833 }
3834 }
3835
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003836 # Track the 'values' across context and added lines.
3837 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003838 my ($curr_values, $curr_vars) =
3839 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003840 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003841 if ($dbg_values) {
3842 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003843 print "$linenr > .$outline\n";
3844 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003845 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003846 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003847 $prev_values = substr($curr_values, -1);
3848
Andy Whitcroft00df3442007-06-08 13:47:06 -07003849#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003850 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003851
Joe Perches11ca40a2016-12-12 16:46:31 -08003852# check for dereferences that span multiple lines
3853 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3854 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3855 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3856 my $ref = $1;
3857 $line =~ /^.\s*($Lval)/;
3858 $ref .= $1;
3859 $ref =~ s/\s//g;
3860 WARN("MULTILINE_DEREFERENCE",
3861 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3862 }
3863
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003864# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003865 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 -07003866 my $type = $1;
3867 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003868 $var = "" if (!defined $var);
3869 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003870 my $sign = $1;
3871 my $pointer = $2;
3872
3873 $pointer = "" if (!defined $pointer);
3874
3875 if (WARN("UNSPECIFIED_INT",
3876 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3877 $fix) {
3878 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003879 my $comp_pointer = $pointer;
3880 $comp_pointer =~ s/\s//g;
3881 $decl .= $comp_pointer;
3882 $decl = rtrim($decl) if ($var eq "");
3883 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003884 }
3885 }
3886 }
3887
Andy Whitcroft653d4872007-06-23 17:16:34 -07003888# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003889 if ($dbg_type) {
3890 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003891 ERROR("TEST_TYPE",
3892 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003893 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003894 ERROR("TEST_NOT_TYPE",
3895 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003896 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003897 next;
3898 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003899# TEST: allow direct testing of the attribute matcher.
3900 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003901 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003902 ERROR("TEST_ATTR",
3903 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003904 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003905 ERROR("TEST_NOT_ATTR",
3906 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003907 }
3908 next;
3909 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003910
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003911# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003912 if ($line =~ /^.\s*{/ &&
3913 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003914 if (ERROR("OPEN_BRACE",
3915 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003916 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3917 fix_delete_line($fixlinenr - 1, $prevrawline);
3918 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003919 my $fixedline = $prevrawline;
3920 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003921 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003922 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003923 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003924 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003925 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003926 }
3927
Andy Whitcroft653d4872007-06-23 17:16:34 -07003928#
3929# Checks which are anchored on the added line.
3930#
3931
3932# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003933 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003934 my $path = $1;
3935 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003936 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003937 "malformed #include filename\n" . $herecurr);
3938 }
3939 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3940 ERROR("UAPI_INCLUDE",
3941 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003942 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003943 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003944
3945# no C99 // comments
3946 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003947 if (ERROR("C99_COMMENTS",
3948 "do not use C99 // comments\n" . $herecurr) &&
3949 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003950 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003951 if ($line =~ /\/\/(.*)$/) {
3952 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003953 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003954 }
3955 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003956 }
3957 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003958 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003959 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003960
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003961# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3962# the whole statement.
3963#print "APW <$lines[$realline_next - 1]>\n";
3964 if (defined $realline_next &&
3965 exists $lines[$realline_next - 1] &&
3966 !defined $suppress_export{$realline_next} &&
3967 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3968 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003969 # Handle definitions which produce identifiers with
3970 # a prefix:
3971 # XXX(foo);
3972 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003973 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003974 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003975 $name =~ /^${Ident}_$2/) {
3976#print "FOO C name<$name>\n";
3977 $suppress_export{$realline_next} = 1;
3978
3979 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003980 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003981 ^.DEFINE_$Ident\(\Q$name\E\)|
3982 ^.DECLARE_$Ident\(\Q$name\E\)|
3983 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003984 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3985 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003986 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003987#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3988 $suppress_export{$realline_next} = 2;
3989 } else {
3990 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003991 }
3992 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003993 if (!defined $suppress_export{$linenr} &&
3994 $prevline =~ /^.\s*$/ &&
3995 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3996 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3997#print "FOO B <$lines[$linenr - 1]>\n";
3998 $suppress_export{$linenr} = 2;
3999 }
4000 if (defined $suppress_export{$linenr} &&
4001 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004002 WARN("EXPORT_SYMBOL",
4003 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004004 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004005
Joe Eloff5150bda2010-08-09 17:21:00 -07004006# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004007 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004008 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004009 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004010 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004011 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004012 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004013 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004014# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004015 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004016 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004017 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004018 $herecurr) &&
4019 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004020 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004021 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004022 }
4023
Joe Perches18130872014-08-06 16:11:22 -07004024# check for misordered declarations of char/short/int/long with signed/unsigned
4025 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4026 my $tmp = trim($1);
4027 WARN("MISORDERED_TYPE",
4028 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4029 }
4030
Joe Perches809e0822018-08-21 21:58:12 -07004031# check for unnecessary <signed> int declarations of short/long/long long
4032 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4033 my $type = trim($1);
4034 next if ($type !~ /\bint\b/);
4035 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4036 my $new_type = $type;
4037 $new_type =~ s/\b\s*int\s*\b/ /;
4038 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4039 $new_type =~ s/^const\s+//;
4040 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4041 $new_type = "const $new_type" if ($type =~ /^const\b/);
4042 $new_type =~ s/\s+/ /g;
4043 $new_type = trim($new_type);
4044 if (WARN("UNNECESSARY_INT",
4045 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4046 $fix) {
4047 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4048 }
4049 }
4050
Joe Perchescb710ec2010-10-26 14:23:20 -07004051# check for static const char * arrays.
4052 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004053 WARN("STATIC_CONST_CHAR_ARRAY",
4054 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004055 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004056 }
4057
4058# check for initialized const char arrays that should be static const
4059 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4060 if (WARN("STATIC_CONST_CHAR_ARRAY",
4061 "const array should probably be static const\n" . $herecurr) &&
4062 $fix) {
4063 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4064 }
4065 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004066
4067# check for static char foo[] = "bar" declarations.
4068 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004069 WARN("STATIC_CONST_CHAR_ARRAY",
4070 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004071 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004072 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004073
Joe Perchesab7e23f2015-04-16 12:44:22 -07004074# check for const <foo> const where <foo> is not a pointer or array type
4075 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4076 my $found = $1;
4077 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4078 WARN("CONST_CONST",
4079 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4080 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4081 WARN("CONST_CONST",
4082 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4083 }
4084 }
4085
Joe Perches9b0fa602014-04-03 14:49:18 -07004086# check for non-global char *foo[] = {"bar", ...} declarations.
4087 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4088 WARN("STATIC_CONST_CHAR_ARRAY",
4089 "char * array declaration might be better as static const\n" .
4090 $herecurr);
4091 }
4092
Joe Perchesb598b672015-04-16 12:44:36 -07004093# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4094 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4095 my $array = $1;
4096 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4097 my $array_div = $1;
4098 if (WARN("ARRAY_SIZE",
4099 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4100 $fix) {
4101 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4102 }
4103 }
4104 }
4105
Joe Perchesb36190c2014-01-27 17:07:18 -08004106# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004107 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004108 if (ERROR("FUNCTION_WITHOUT_ARGS",
4109 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4110 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004111 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004112 }
4113 }
4114
Andy Whitcroft653d4872007-06-23 17:16:34 -07004115# check for new typedefs, only function parameters and sparse annotations
4116# make sense.
4117 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004118 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004119 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004120 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004121 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004122 WARN("NEW_TYPEDEFS",
4123 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004124 }
4125
4126# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004127 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004128 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4129 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004130 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004131
Andy Whitcroft65863862009-01-06 14:41:21 -08004132 # Should start with a space.
4133 $to =~ s/^(\S)/ $1/;
4134 # Should not end with a space.
4135 $to =~ s/\s+$//;
4136 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004137 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004138 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004139
Joe Perches3705ce52013-07-03 15:05:31 -07004140## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004141 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004142 if (ERROR("POINTER_LOCATION",
4143 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4144 $fix) {
4145 my $sub_from = $ident;
4146 my $sub_to = $ident;
4147 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004148 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004149 s@\Q$sub_from\E@$sub_to@;
4150 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004151 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004152 }
4153 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4154 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004155 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004156
Andy Whitcroft65863862009-01-06 14:41:21 -08004157 # Should start with a space.
4158 $to =~ s/^(\S)/ $1/;
4159 # Should not end with a space.
4160 $to =~ s/\s+$//;
4161 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004162 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004163 }
4164 # Modifiers should have spaces.
4165 $to =~ s/(\b$Modifier$)/$1 /;
4166
Joe Perches3705ce52013-07-03 15:05:31 -07004167## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004168 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004169 if (ERROR("POINTER_LOCATION",
4170 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4171 $fix) {
4172
4173 my $sub_from = $match;
4174 my $sub_to = $match;
4175 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004176 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004177 s@\Q$sub_from\E@$sub_to@;
4178 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004179 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004180 }
4181
Joe Perches9d3e3c72015-09-09 15:37:27 -07004182# avoid BUG() or BUG_ON()
4183 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004184 my $msg_level = \&WARN;
4185 $msg_level = \&CHK if ($file);
4186 &{$msg_level}("AVOID_BUG",
4187 "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 -07004188 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004189
Joe Perches9d3e3c72015-09-09 15:37:27 -07004190# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004191 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004192 WARN("LINUX_VERSION_CODE",
4193 "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 -08004194 }
4195
Joe Perches17441222011-06-15 15:08:17 -07004196# check for uses of printk_ratelimit
4197 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004198 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004199 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004200 }
4201
Joe Percheseeef5732017-11-17 15:28:41 -08004202# printk should use KERN_* levels
4203 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4204 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4205 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004206 }
4207
Joe Perches243f3802012-05-31 16:26:09 -07004208 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4209 my $orig = $1;
4210 my $level = lc($orig);
4211 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004212 my $level2 = $level;
4213 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004214 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004215 "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 -07004216 }
4217
Joe Perchesdc139312013-02-21 16:44:13 -08004218 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4219 my $orig = $1;
4220 my $level = lc($orig);
4221 $level = "warn" if ($level eq "warning");
4222 $level = "dbg" if ($level eq "debug");
4223 WARN("PREFER_DEV_LEVEL",
4224 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4225 }
4226
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004227# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4228# number of false positives, but assembly files are not checked, so at
4229# least the arch entry code will not trigger this warning.
4230 if ($line =~ /\bENOSYS\b/) {
4231 WARN("ENOSYS",
4232 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4233 }
4234
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004235# ENOTSUPP is not a standard error code and should be avoided in new patches.
4236# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4237# Similarly to ENOSYS warning a small number of false positives is expected.
4238 if (!$file && $line =~ /\bENOTSUPP\b/) {
4239 if (WARN("ENOTSUPP",
4240 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4241 $fix) {
4242 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4243 }
4244 }
4245
Andy Whitcroft653d4872007-06-23 17:16:34 -07004246# function brace can't be on same line, except for #defines of do while,
4247# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004248 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004249 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4250 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4251 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004252 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004253 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004254 $fix) {
4255 fix_delete_line($fixlinenr, $rawline);
4256 my $fixed_line = $rawline;
4257 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4258 my $line1 = $1;
4259 my $line2 = $2;
4260 fix_insert_line($fixlinenr, ltrim($line1));
4261 fix_insert_line($fixlinenr, "\+{");
4262 if ($line2 !~ /^\s*$/) {
4263 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4264 }
4265 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004266 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004267
Andy Whitcroft8905a672007-11-28 16:21:06 -08004268# open braces for enum, union and struct go on the same line.
4269 if ($line =~ /^.\s*{/ &&
4270 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004271 if (ERROR("OPEN_BRACE",
4272 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4273 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4274 fix_delete_line($fixlinenr - 1, $prevrawline);
4275 fix_delete_line($fixlinenr, $rawline);
4276 my $fixedline = rtrim($prevrawline) . " {";
4277 fix_insert_line($fixlinenr, $fixedline);
4278 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004279 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004280 if ($fixedline !~ /^\+\s*$/) {
4281 fix_insert_line($fixlinenr, $fixedline);
4282 }
4283 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004284 }
4285
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004286# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004287 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4288 if (WARN("SPACING",
4289 "missing space after $1 definition\n" . $herecurr) &&
4290 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004291 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004292 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4293 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004294 }
4295
Joe Perches31070b52014-01-23 15:54:49 -08004296# Function pointer declarations
4297# check spacing between type, funcptr, and args
4298# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004299 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004300 my $declare = $1;
4301 my $pre_pointer_space = $2;
4302 my $post_pointer_space = $3;
4303 my $funcname = $4;
4304 my $post_funcname_space = $5;
4305 my $pre_args_space = $6;
4306
Joe Perches91f72e92014-04-03 14:49:12 -07004307# the $Declare variable will capture all spaces after the type
4308# so check it for a missing trailing missing space but pointer return types
4309# don't need a space so don't warn for those.
4310 my $post_declare_space = "";
4311 if ($declare =~ /(\s+)$/) {
4312 $post_declare_space = $1;
4313 $declare = rtrim($declare);
4314 }
4315 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004316 WARN("SPACING",
4317 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004318 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004319 }
4320
4321# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004322# This test is not currently implemented because these declarations are
4323# equivalent to
4324# int foo(int bar, ...)
4325# and this is form shouldn't/doesn't generate a checkpatch warning.
4326#
4327# elsif ($declare =~ /\s{2,}$/) {
4328# WARN("SPACING",
4329# "Multiple spaces after return type\n" . $herecurr);
4330# }
Joe Perches31070b52014-01-23 15:54:49 -08004331
4332# unnecessary space "type ( *funcptr)(args...)"
4333 if (defined $pre_pointer_space &&
4334 $pre_pointer_space =~ /^\s/) {
4335 WARN("SPACING",
4336 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4337 }
4338
4339# unnecessary space "type (* funcptr)(args...)"
4340 if (defined $post_pointer_space &&
4341 $post_pointer_space =~ /^\s/) {
4342 WARN("SPACING",
4343 "Unnecessary space before function pointer name\n" . $herecurr);
4344 }
4345
4346# unnecessary space "type (*funcptr )(args...)"
4347 if (defined $post_funcname_space &&
4348 $post_funcname_space =~ /^\s/) {
4349 WARN("SPACING",
4350 "Unnecessary space after function pointer name\n" . $herecurr);
4351 }
4352
4353# unnecessary space "type (*funcptr) (args...)"
4354 if (defined $pre_args_space &&
4355 $pre_args_space =~ /^\s/) {
4356 WARN("SPACING",
4357 "Unnecessary space before function pointer arguments\n" . $herecurr);
4358 }
4359
4360 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004361 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004362 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004363 }
4364 }
4365
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004366# check for spacing round square brackets; allowed:
4367# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004368# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4369# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004370 while ($line =~ /(.*?\s)\[/g) {
4371 my ($where, $prefix) = ($-[1], $1);
4372 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004373 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004374 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004375 if (ERROR("BRACKET_SPACE",
4376 "space prohibited before open square bracket '['\n" . $herecurr) &&
4377 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004378 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004379 s/^(\+.*?)\s+\[/$1\[/;
4380 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004381 }
4382 }
4383
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004384# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004385 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004386 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004387 my $ctx_before = substr($line, 0, $-[1]);
4388 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004389
4390 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004391 if ($name =~ /^(?:
4392 if|for|while|switch|return|case|
4393 volatile|__volatile__|
4394 __attribute__|format|__extension__|
4395 asm|__asm__)$/x)
4396 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004397 # cpp #define statements have non-optional spaces, ie
4398 # if there is a space between the name and the open
4399 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004400 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004401
4402 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004403 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004404
4405 # If this whole things ends with a type its most
4406 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004407 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004408
4409 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004410 if (WARN("SPACING",
4411 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4412 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004413 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004414 s/\b$name\s+\(/$name\(/;
4415 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004416 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004417 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004418
Andy Whitcroft653d4872007-06-23 17:16:34 -07004419# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004420 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004421 my $fixed_line = "";
4422 my $line_fixed = 0;
4423
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004424 my $ops = qr{
4425 <<=|>>=|<=|>=|==|!=|
4426 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4427 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004428 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004429 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004430 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004431 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004432
4433## print("element count: <" . $#elements . ">\n");
4434## foreach my $el (@elements) {
4435## print("el: <$el>\n");
4436## }
4437
4438 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004439 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004440
Joe Perches3705ce52013-07-03 15:05:31 -07004441 foreach my $el (@elements) {
4442 push(@fix_elements, substr($rawline, $off, length($el)));
4443 $off += length($el);
4444 }
4445
4446 $off = 0;
4447
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004448 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004449 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004450
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004451 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004452
4453 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4454
4455## print("n: <$n> good: <$good>\n");
4456
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004457 $off += length($elements[$n]);
4458
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004459 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004460 my $ca = substr($opline, 0, $off);
4461 my $cc = '';
4462 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4463 $cc = substr($opline, $off + length($elements[$n + 1]));
4464 }
4465 my $cb = "$ca$;$cc";
4466
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004467 my $a = '';
4468 $a = 'V' if ($elements[$n] ne '');
4469 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004470 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004471 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4472 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004473 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004474
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004475 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004476
4477 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004478 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004479 $c = 'V' if ($elements[$n + 2] ne '');
4480 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004481 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004482 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4483 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004484 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004485 } else {
4486 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004487 }
4488
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004489 my $ctx = "${a}x${c}";
4490
4491 my $at = "(ctx:$ctx)";
4492
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004493 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004494 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004495
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004496 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004497 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004498
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004499 # Get the full operator variant.
4500 my $opv = $op . substr($curr_vars, $off, 1);
4501
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004502 # Ignore operators passed as parameters.
4503 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004504 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004505
Andy Whitcroftcf655042008-03-04 14:28:20 -08004506# # Ignore comments
4507# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004508
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004509 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004510 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004511 if ($ctx !~ /.x[WEBC]/ &&
4512 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004513 if (ERROR("SPACING",
4514 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004515 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004516 $line_fixed = 1;
4517 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004518 }
4519
4520 # // is a comment
4521 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004522
Joe Perchesb00e4812014-04-03 14:49:33 -07004523 # : when part of a bitfield
4524 } elsif ($opv eq ':B') {
4525 # skip the bitfield test for now
4526
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004527 # No spaces for:
4528 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004529 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004530 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004531 if (ERROR("SPACING",
4532 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004533 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004534 if (defined $fix_elements[$n + 2]) {
4535 $fix_elements[$n + 2] =~ s/^\s+//;
4536 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004537 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004538 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004539 }
4540
Joe Perches23810972014-12-10 15:51:32 -08004541 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004542 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004543 my $rtrim_before = 0;
4544 my $space_after = 0;
4545 if ($ctx =~ /Wx./) {
4546 if (ERROR("SPACING",
4547 "space prohibited before that '$op' $at\n" . $hereptr)) {
4548 $line_fixed = 1;
4549 $rtrim_before = 1;
4550 }
4551 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004552 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004553 if (ERROR("SPACING",
4554 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004555 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004556 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004557 $space_after = 1;
4558 }
4559 }
4560 if ($rtrim_before || $space_after) {
4561 if ($rtrim_before) {
4562 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4563 } else {
4564 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4565 }
4566 if ($space_after) {
4567 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004568 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004569 }
4570
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004571 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004572 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004573 #warn "'*' is part of type\n";
4574
4575 # unary operators should have a space before and
4576 # none after. May be left adjacent to another
4577 # unary operator, or a cast
4578 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004579 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004580 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004581 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004582 if (ERROR("SPACING",
4583 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004584 if ($n != $last_after + 2) {
4585 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4586 $line_fixed = 1;
4587 }
Joe Perches3705ce52013-07-03 15:05:31 -07004588 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004589 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004590 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004591 # A unary '*' may be const
4592
4593 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004594 if (ERROR("SPACING",
4595 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004596 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004597 if (defined $fix_elements[$n + 2]) {
4598 $fix_elements[$n + 2] =~ s/^\s+//;
4599 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004600 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004601 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004602 }
4603
4604 # unary ++ and unary -- are allowed no space on one side.
4605 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004606 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004607 if (ERROR("SPACING",
4608 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004609 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004610 $line_fixed = 1;
4611 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004612 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004613 if ($ctx =~ /Wx[BE]/ ||
4614 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004615 if (ERROR("SPACING",
4616 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004617 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004618 $line_fixed = 1;
4619 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004620 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004621 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004622 if (ERROR("SPACING",
4623 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004624 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004625 if (defined $fix_elements[$n + 2]) {
4626 $fix_elements[$n + 2] =~ s/^\s+//;
4627 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004628 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004629 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004630 }
4631
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004632 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004633 } elsif ($op eq '<<' or $op eq '>>' or
4634 $op eq '&' or $op eq '^' or $op eq '|' or
4635 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004636 $op eq '*' or $op eq '/' or
4637 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004638 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004639 if ($check) {
4640 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4641 if (CHK("SPACING",
4642 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4643 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4644 $fix_elements[$n + 2] =~ s/^\s+//;
4645 $line_fixed = 1;
4646 }
4647 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4648 if (CHK("SPACING",
4649 "space preferred before that '$op' $at\n" . $hereptr)) {
4650 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4651 $line_fixed = 1;
4652 }
4653 }
4654 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004655 if (ERROR("SPACING",
4656 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004657 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4658 if (defined $fix_elements[$n + 2]) {
4659 $fix_elements[$n + 2] =~ s/^\s+//;
4660 }
Joe Perches3705ce52013-07-03 15:05:31 -07004661 $line_fixed = 1;
4662 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004663 }
4664
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004665 # A colon needs no spaces before when it is
4666 # terminating a case value or a label.
4667 } elsif ($opv eq ':C' || $opv eq ':L') {
4668 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004669 if (ERROR("SPACING",
4670 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004671 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004672 $line_fixed = 1;
4673 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004674 }
4675
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004676 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004677 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004678 my $ok = 0;
4679
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004680 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004681 if (($op eq '<' &&
4682 $cc =~ /^\S+\@\S+>/) ||
4683 ($op eq '>' &&
4684 $ca =~ /<\S+\@\S+$/))
4685 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07004686 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004687 }
4688
Joe Perchese0df7e12015-04-16 12:44:53 -07004689 # for asm volatile statements
4690 # ignore a colon with another
4691 # colon immediately before or after
4692 if (($op eq ':') &&
4693 ($ca =~ /:$/ || $cc =~ /^:/)) {
4694 $ok = 1;
4695 }
4696
Joe Perches84731622013-11-12 15:10:05 -08004697 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004698 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004699 my $msg_level = \&ERROR;
4700 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004701
Jean Delvare0675a8f2017-09-08 16:16:07 -07004702 if (&{$msg_level}("SPACING",
4703 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004704 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4705 if (defined $fix_elements[$n + 2]) {
4706 $fix_elements[$n + 2] =~ s/^\s+//;
4707 }
Joe Perches3705ce52013-07-03 15:05:31 -07004708 $line_fixed = 1;
4709 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004710 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004711 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004712 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004713
4714## print("n: <$n> GOOD: <$good>\n");
4715
4716 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004717 }
Joe Perches3705ce52013-07-03 15:05:31 -07004718
4719 if (($#elements % 2) == 0) {
4720 $fixed_line = $fixed_line . $fix_elements[$#elements];
4721 }
4722
Joe Perches194f66f2014-08-06 16:11:03 -07004723 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4724 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004725 }
4726
4727
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004728 }
4729
Joe Perches786b6322013-07-03 15:05:32 -07004730# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004731 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004732 if (WARN("SPACING",
4733 "space prohibited before semicolon\n" . $herecurr) &&
4734 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004735 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004736 s/^(\+.*\S)\s+;/$1;/;
4737 }
4738 }
4739
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004740# check for multiple assignments
4741 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004742 CHK("MULTIPLE_ASSIGNMENTS",
4743 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004744 }
4745
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004746## # check for multiple declarations, allowing for a function declaration
4747## # continuation.
4748## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4749## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4750##
4751## # Remove any bracketed sections to ensure we do not
4752## # falsly report the parameters of functions.
4753## my $ln = $line;
4754## while ($ln =~ s/\([^\(\)]*\)//g) {
4755## }
4756## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004757## WARN("MULTIPLE_DECLARATION",
4758## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004759## }
4760## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004761
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004762#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004763 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004764 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004765 if (ERROR("SPACING",
4766 "space required before the open brace '{'\n" . $herecurr) &&
4767 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004768 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004769 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004770 }
4771
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004772## # check for blank lines before declarations
4773## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4774## $prevrawline =~ /^.\s*$/) {
4775## WARN("SPACING",
4776## "No blank lines before declarations\n" . $hereprev);
4777## }
4778##
4779
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004780# closing brace should have a space following it when it has anything
4781# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07004782 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004783 if (ERROR("SPACING",
4784 "space required after that close brace '}'\n" . $herecurr) &&
4785 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004786 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004787 s/}((?!(?:,|;|\)))\S)/} $1/;
4788 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004789 }
4790
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004791# check spacing on square brackets
4792 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004793 if (ERROR("SPACING",
4794 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4795 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004796 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004797 s/\[\s+/\[/;
4798 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004799 }
4800 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004801 if (ERROR("SPACING",
4802 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4803 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004804 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004805 s/\s+\]/\]/;
4806 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004807 }
4808
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004809# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004810 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4811 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004812 if (ERROR("SPACING",
4813 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4814 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004815 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004816 s/\(\s+/\(/;
4817 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004818 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004819 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004820 $line !~ /for\s*\(.*;\s+\)/ &&
4821 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004822 if (ERROR("SPACING",
4823 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4824 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004825 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004826 s/\s+\)/\)/;
4827 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004828 }
4829
Joe Perchese2826fd2014-08-06 16:10:48 -07004830# check unnecessary parentheses around addressof/dereference single $Lvals
4831# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4832
4833 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004834 my $var = $1;
4835 if (CHK("UNNECESSARY_PARENTHESES",
4836 "Unnecessary parentheses around $var\n" . $herecurr) &&
4837 $fix) {
4838 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4839 }
4840 }
4841
4842# check for unnecessary parentheses around function pointer uses
4843# ie: (foo->bar)(); should be foo->bar();
4844# but not "if (foo->bar) (" to avoid some false positives
4845 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4846 my $var = $2;
4847 if (CHK("UNNECESSARY_PARENTHESES",
4848 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4849 $fix) {
4850 my $var2 = deparenthesize($var);
4851 $var2 =~ s/\s//g;
4852 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4853 }
4854 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004855
Joe Perches63b7c732017-09-08 16:16:01 -07004856# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004857# when !drivers/staging or command-line uses --strict
4858 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004859 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004860 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4861 my $if_stat = $1;
4862 my $test = substr($2, 1, -1);
4863 my $herectx;
4864 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4865 my $match = $1;
4866 # avoid parentheses around potential macro args
4867 next if ($match =~ /^\s*\w+\s*$/);
4868 if (!defined($herectx)) {
4869 $herectx = $here . "\n";
4870 my $cnt = statement_rawlines($if_stat);
4871 for (my $n = 0; $n < $cnt; $n++) {
4872 my $rl = raw_line($linenr, $n);
4873 $herectx .= $rl . "\n";
4874 last if $rl =~ /^[ \+].*\{/;
4875 }
4876 }
4877 CHK("UNNECESSARY_PARENTHESES",
4878 "Unnecessary parentheses around '$match'\n" . $herectx);
4879 }
4880 }
4881
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004882#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004883 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004884 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004885 if (WARN("INDENTED_LABEL",
4886 "labels should not be indented\n" . $herecurr) &&
4887 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004888 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004889 s/^(.)\s+/$1/;
4890 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004891 }
4892
Joe Perches5b9553a2014-04-03 14:49:21 -07004893# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004894 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004895 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004896 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004897 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4898 my $value = $1;
4899 $value = deparenthesize($value);
4900 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4901 ERROR("RETURN_PARENTHESES",
4902 "return is not a function, parentheses are not required\n" . $herecurr);
4903 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004904 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004905 ERROR("SPACING",
4906 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004907 }
4908 }
Joe Perches507e5142013-11-12 15:10:13 -08004909
Joe Perchesb43ae212014-06-23 13:22:07 -07004910# unnecessary return in a void function
4911# at end-of-function, with the previous line a single leading tab, then return;
4912# and the line before that not a goto label target like "out:"
4913 if ($sline =~ /^[ \+]}\s*$/ &&
4914 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4915 $linenr >= 3 &&
4916 $lines[$linenr - 3] =~ /^[ +]/ &&
4917 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004918 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004919 "void function return statements are not generally useful\n" . $hereprev);
4920 }
Joe Perches9819cf22014-06-04 16:12:09 -07004921
Joe Perches189248d2014-01-23 15:54:47 -08004922# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004923 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004924 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4925 my $openparens = $1;
4926 my $count = $openparens =~ tr@\(@\(@;
4927 my $msg = "";
4928 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4929 my $comp = $4; #Not $1 because of $LvalOrFunc
4930 $msg = " - maybe == should be = ?" if ($comp eq "==");
4931 WARN("UNNECESSARY_PARENTHESES",
4932 "Unnecessary parentheses$msg\n" . $herecurr);
4933 }
4934 }
4935
Joe Perchesc5595fa2015-09-09 15:37:58 -07004936# comparisons with a constant or upper case identifier on the left
4937# avoid cases like "foo + BAR < baz"
4938# only fix matches surrounded by parentheses to avoid incorrect
4939# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004940 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004941 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4942 my $lead = $1;
4943 my $const = $2;
4944 my $comp = $3;
4945 my $to = $4;
4946 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004947 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004948 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4949 WARN("CONSTANT_COMPARISON",
4950 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4951 $fix) {
4952 if ($comp eq "<") {
4953 $newcomp = ">";
4954 } elsif ($comp eq "<=") {
4955 $newcomp = ">=";
4956 } elsif ($comp eq ">") {
4957 $newcomp = "<";
4958 } elsif ($comp eq ">=") {
4959 $newcomp = "<=";
4960 }
4961 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4962 }
4963 }
4964
Joe Perchesf34e4a42015-04-16 12:44:19 -07004965# Return of what appears to be an errno should normally be negative
4966 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004967 my $name = $1;
4968 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004969 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004970 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004971 }
4972 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004973
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004974# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004975 if ($line =~ /\b(if|while|for|switch)\(/) {
4976 if (ERROR("SPACING",
4977 "space required before the open parenthesis '('\n" . $herecurr) &&
4978 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004979 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004980 s/\b(if|while|for|switch)\(/$1 \(/;
4981 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004982 }
4983
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004984# Check for illegal assignment in if conditional -- and check for trailing
4985# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004986 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004987 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4988 ctx_statement_block($linenr, $realcnt, 0)
4989 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004990 my ($stat_next) = ctx_statement_block($line_nr_next,
4991 $remain_next, $off_next);
4992 $stat_next =~ s/\n./\n /g;
4993 ##print "stat<$stat> stat_next<$stat_next>\n";
4994
4995 if ($stat_next =~ /^\s*while\b/) {
4996 # If the statement carries leading newlines,
4997 # then count those as offsets.
4998 my ($whitespace) =
4999 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5000 my $offset =
5001 statement_rawlines($whitespace) - 1;
5002
5003 $suppress_whiletrailers{$line_nr_next +
5004 $offset} = 1;
5005 }
5006 }
5007 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005008 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005009 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005010 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005011
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005012 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005013 ERROR("ASSIGN_IN_IF",
5014 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005015 }
5016
5017 # Find out what is on the end of the line after the
5018 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005019 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005020 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005021 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005022 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5023 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005024 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005025 # Find out how long the conditional actually is.
5026 my @newlines = ($c =~ /\n/gs);
5027 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005028 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005029
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005030 $stat_real = raw_line($linenr, $cond_lines)
5031 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005032 if (defined($stat_real) && $cond_lines > 1) {
5033 $stat_real = "[...]\n$stat_real";
5034 }
5035
Joe Perches000d1cc12011-07-25 17:13:25 -07005036 ERROR("TRAILING_STATEMENTS",
5037 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005038 }
5039 }
5040
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005041# Check for bitwise tests written as boolean
5042 if ($line =~ /
5043 (?:
5044 (?:\[|\(|\&\&|\|\|)
5045 \s*0[xX][0-9]+\s*
5046 (?:\&\&|\|\|)
5047 |
5048 (?:\&\&|\|\|)
5049 \s*0[xX][0-9]+\s*
5050 (?:\&\&|\|\||\)|\])
5051 )/x)
5052 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005053 WARN("HEXADECIMAL_BOOLEAN_TEST",
5054 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005055 }
5056
Andy Whitcroft8905a672007-11-28 16:21:06 -08005057# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005058 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5059 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005060 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005061 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005062 ERROR("TRAILING_STATEMENTS",
5063 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005064 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005065 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005066# if should not continue a brace
5067 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005068 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005069 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005070 $herecurr);
5071 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005072# case and default should not have general statements after them
5073 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5074 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005075 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005076 \s*return\s+
5077 )/xg)
5078 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005079 ERROR("TRAILING_STATEMENTS",
5080 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005081 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005082
5083 # Check for }<nl>else {, these must be at the same
5084 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005085 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5086 $previndent == $indent) {
5087 if (ERROR("ELSE_AFTER_BRACE",
5088 "else should follow close brace '}'\n" . $hereprev) &&
5089 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5090 fix_delete_line($fixlinenr - 1, $prevrawline);
5091 fix_delete_line($fixlinenr, $rawline);
5092 my $fixedline = $prevrawline;
5093 $fixedline =~ s/}\s*$//;
5094 if ($fixedline !~ /^\+\s*$/) {
5095 fix_insert_line($fixlinenr, $fixedline);
5096 }
5097 $fixedline = $rawline;
5098 $fixedline =~ s/^(.\s*)else/$1} else/;
5099 fix_insert_line($fixlinenr, $fixedline);
5100 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005101 }
5102
Joe Perches8b8856f2014-08-06 16:11:14 -07005103 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5104 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005105 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5106
5107 # Find out what is on the end of the line after the
5108 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005109 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005110 $s =~ s/\n.*//g;
5111
5112 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005113 if (ERROR("WHILE_AFTER_BRACE",
5114 "while should follow close brace '}'\n" . $hereprev) &&
5115 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5116 fix_delete_line($fixlinenr - 1, $prevrawline);
5117 fix_delete_line($fixlinenr, $rawline);
5118 my $fixedline = $prevrawline;
5119 my $trailing = $rawline;
5120 $trailing =~ s/^\+//;
5121 $trailing = trim($trailing);
5122 $fixedline =~ s/}\s*$/} $trailing/;
5123 fix_insert_line($fixlinenr, $fixedline);
5124 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005125 }
5126 }
5127
Joe Perches95e2c602013-07-03 15:05:20 -07005128#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005129 while ($line =~ m{($Constant|$Lval)}g) {
5130 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005131
Joe Perches95e2c602013-07-03 15:05:20 -07005132#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005133 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005134 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005135#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005136 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005137#Ignore SI style variants like nS, mV and dB
5138#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5139 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005140#Ignore some three character SI units explicitly, like MiB and KHz
5141 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005142 while ($var =~ m{($Ident)}g) {
5143 my $word = $1;
5144 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005145 if ($check) {
5146 seed_camelcase_includes();
5147 if (!$file && !$camelcase_file_seeded) {
5148 seed_camelcase_file($realfile);
5149 $camelcase_file_seeded = 1;
5150 }
5151 }
Joe Perches7e781f62013-09-11 14:23:55 -07005152 if (!defined $camelcase{$word}) {
5153 $camelcase{$word} = 1;
5154 CHK("CAMELCASE",
5155 "Avoid CamelCase: <$word>\n" . $herecurr);
5156 }
Joe Perches34456862013-07-03 15:05:34 -07005157 }
Joe Perches323c1262012-12-17 16:02:07 -08005158 }
5159 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005160
5161#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005162 if ($line =~ /\#\s*define.*\\\s+$/) {
5163 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5164 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5165 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005166 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005167 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005168 }
5169
Fabian Frederick0e212e02015-04-16 12:44:25 -07005170# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5171# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005172 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005173 my $file = "$1.h";
5174 my $checkfile = "include/linux/$file";
5175 if (-f "$root/$checkfile" &&
5176 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005177 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005178 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005179 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5180 if ($asminclude > 0) {
5181 if ($realfile =~ m{^arch/}) {
5182 CHK("ARCH_INCLUDE_LINUX",
5183 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5184 } else {
5185 WARN("INCLUDE_LINUX",
5186 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5187 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005188 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005189 }
5190 }
5191
Andy Whitcroft653d4872007-06-23 17:16:34 -07005192# multi-statement macros should be enclosed in a do while loop, grab the
5193# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005194# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005195 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5196 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005197 my $ln = $linenr;
5198 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005199 my ($off, $dstat, $dcond, $rest);
5200 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005201 my $has_flow_statement = 0;
5202 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005203 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005204 ctx_statement_block($linenr, $realcnt, 0);
5205 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005206 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005207 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005208
Joe Perches08a28432014-10-13 15:51:55 -07005209 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005210 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005211
Joe Perchesf59b64b2016-10-11 13:52:08 -07005212 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5213 my $define_args = $1;
5214 my $define_stmt = $dstat;
5215 my @def_args = ();
5216
5217 if (defined $define_args && $define_args ne "") {
5218 $define_args = substr($define_args, 1, length($define_args) - 2);
5219 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005220 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005221 @def_args = split(",", $define_args);
5222 }
5223
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005224 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005225 $dstat =~ s/\\\n.//g;
5226 $dstat =~ s/^\s*//s;
5227 $dstat =~ s/\s*$//s;
5228
5229 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005230 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5231 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005232 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005233 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005234 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005235
Antonio Borneo342d3d22020-04-06 20:11:01 -07005236 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005237 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5238 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005239 {
5240 }
5241
Joe Perches42e15292016-03-15 14:58:01 -07005242 # Make asm volatile uses seem like a generic function
5243 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5244
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005245 my $exceptions = qr{
5246 $Declare|
5247 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005248 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005249 DECLARE_PER_CPU|
5250 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005251 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005252 union|
5253 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005254 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005255 ^\"|\"$|
5256 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005257 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005258 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005259
5260 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005261 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005262 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005263
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005264 if ($dstat ne '' &&
5265 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5266 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005267 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005268 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005269 $dstat !~ /$exceptions/ &&
5270 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005271 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005272 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005273 $dstat !~ /^for\s*$Constant$/ && # for (...)
5274 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5275 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005276 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005277 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005278 {
Joe Perchese7955562017-05-08 15:55:48 -07005279 if ($dstat =~ /^\s*if\b/) {
5280 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5281 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5282 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005283 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5284 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5285 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005286 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005287 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005288 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005289
5290 }
Joe Perches52076492016-10-11 13:52:14 -07005291
5292 # Make $define_stmt single line, comment-free, etc
5293 my @stmt_array = split('\n', $define_stmt);
5294 my $first = 1;
5295 $define_stmt = "";
5296 foreach my $l (@stmt_array) {
5297 $l =~ s/\\$//;
5298 if ($first) {
5299 $define_stmt = $l;
5300 $first = 0;
5301 } elsif ($l =~ /^[\+ ]/) {
5302 $define_stmt .= substr($l, 1);
5303 }
5304 }
5305 $define_stmt =~ s/$;//g;
5306 $define_stmt =~ s/\s+/ /g;
5307 $define_stmt = trim($define_stmt);
5308
Joe Perchesf59b64b2016-10-11 13:52:08 -07005309# check if any macro arguments are reused (ignore '...' and 'type')
5310 foreach my $arg (@def_args) {
5311 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005312 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005313 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005314 $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 -07005315 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5316 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005317 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005318 if ($use_cnt > 1) {
5319 CHK("MACRO_ARG_REUSE",
5320 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005321 }
5322# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005323 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005324 ((defined($1) && $1 ne ',') ||
5325 (defined($2) && $2 ne ','))) {
5326 CHK("MACRO_ARG_PRECEDENCE",
5327 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005328 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005329 }
Joe Perches5023d342012-12-17 16:01:47 -08005330
Joe Perches08a28432014-10-13 15:51:55 -07005331# check for macros with flow control, but without ## concatenation
5332# ## concatenation is commonly a macro that defines a function so ignore those
5333 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005334 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005335 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005336
Joe Perches08a28432014-10-13 15:51:55 -07005337 WARN("MACRO_WITH_FLOW_CONTROL",
5338 "Macros with flow control statements should be avoided\n" . "$herectx");
5339 }
5340
Joe Perches481eb482012-12-17 16:01:56 -08005341# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005342
5343 } else {
5344 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005345 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5346 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005347 $line =~ /^\+.*\\$/) {
5348 WARN("LINE_CONTINUATIONS",
5349 "Avoid unnecessary line continuations\n" . $herecurr);
5350 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005351 }
5352
Joe Perchesb13edf72012-07-30 14:41:24 -07005353# do {} while (0) macro tests:
5354# single-statement macros do not need to be enclosed in do while (0) loop,
5355# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005356 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005357 $realfile !~ m@/vmlinux.lds.h$@ &&
5358 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5359 my $ln = $linenr;
5360 my $cnt = $realcnt;
5361 my ($off, $dstat, $dcond, $rest);
5362 my $ctx = '';
5363 ($dstat, $dcond, $ln, $cnt, $off) =
5364 ctx_statement_block($linenr, $realcnt, 0);
5365 $ctx = $dstat;
5366
5367 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005368 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005369
5370 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5371 my $stmts = $2;
5372 my $semis = $3;
5373
5374 $ctx =~ s/\n*$//;
5375 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005376 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005377
Joe Perchesac8e97f2012-08-21 16:15:53 -07005378 if (($stmts =~ tr/;/;/) == 1 &&
5379 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005380 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5381 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5382 }
5383 if (defined $semis && $semis ne "") {
5384 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5385 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5386 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005387 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5388 $ctx =~ s/\n*$//;
5389 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005390 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005391
5392 WARN("TRAILING_SEMICOLON",
5393 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005394 }
5395 }
5396
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005397# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005398 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5399 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005400 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005401 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005402 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5403 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005404 my @allowed = ();
5405 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005406 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005407 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005408 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005409 for my $chunk (@chunks) {
5410 my ($cond, $block) = @{$chunk};
5411
Andy Whitcroft773647a2008-03-28 14:15:58 -07005412 # If the condition carries leading newlines, then count those as offsets.
5413 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5414 my $offset = statement_rawlines($whitespace) - 1;
5415
Joe Perchesaad4f612012-03-23 15:02:19 -07005416 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005417 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5418
5419 # We have looked at and allowed this specific line.
5420 $suppress_ifbraces{$ln + $offset} = 1;
5421
5422 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005423 $ln += statement_rawlines($block) - 1;
5424
Andy Whitcroft773647a2008-03-28 14:15:58 -07005425 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005426
5427 $seen++ if ($block =~ /^\s*{/);
5428
Joe Perchesaad4f612012-03-23 15:02:19 -07005429 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005430 if (statement_lines($cond) > 1) {
5431 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005432 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005433 }
5434 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005435 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005436 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005437 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005438 if (statement_block_size($block) > 1) {
5439 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005440 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005441 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005442 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005443 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005444 if ($seen) {
5445 my $sum_allowed = 0;
5446 foreach (@allowed) {
5447 $sum_allowed += $_;
5448 }
5449 if ($sum_allowed == 0) {
5450 WARN("BRACES",
5451 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5452 } elsif ($sum_allowed != $allow &&
5453 $seen != $allow) {
5454 CHK("BRACES",
5455 "braces {} should be used on all arms of this statement\n" . $herectx);
5456 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005457 }
5458 }
5459 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005460 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005461 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005462 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005463
Andy Whitcroftcf655042008-03-04 14:28:20 -08005464 # Check the pre-context.
5465 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5466 #print "APW: ALLOWED: pre<$1>\n";
5467 $allowed = 1;
5468 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005469
5470 my ($level, $endln, @chunks) =
5471 ctx_statement_full($linenr, $realcnt, $-[0]);
5472
Andy Whitcroftcf655042008-03-04 14:28:20 -08005473 # Check the condition.
5474 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005475 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005476 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005477 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005478 }
5479 if (statement_lines($cond) > 1) {
5480 #print "APW: ALLOWED: cond<$cond>\n";
5481 $allowed = 1;
5482 }
5483 if ($block =~/\b(?:if|for|while)\b/) {
5484 #print "APW: ALLOWED: block<$block>\n";
5485 $allowed = 1;
5486 }
5487 if (statement_block_size($block) > 1) {
5488 #print "APW: ALLOWED: lines block<$block>\n";
5489 $allowed = 1;
5490 }
5491 # Check the post-context.
5492 if (defined $chunks[1]) {
5493 my ($cond, $block) = @{$chunks[1]};
5494 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005495 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005496 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005497 if ($block =~ /^\s*\{/) {
5498 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5499 $allowed = 1;
5500 }
5501 }
5502 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005503 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005504 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005505
Joe Perches000d1cc12011-07-25 17:13:25 -07005506 WARN("BRACES",
5507 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005508 }
5509 }
5510
Joe Perchese4c5bab2017-02-24 15:01:41 -08005511# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005512 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5513 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005514 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5515 }
5516
Joe Perches0979ae62012-12-17 16:01:59 -08005517# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005518 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005519 if (CHK("BRACES",
5520 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5521 $fix && $prevrawline =~ /^\+/) {
5522 fix_delete_line($fixlinenr - 1, $prevrawline);
5523 }
Joe Perches0979ae62012-12-17 16:01:59 -08005524 }
Joe Perches77b9a532013-07-03 15:05:29 -07005525 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005526 if (CHK("BRACES",
5527 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5528 $fix) {
5529 fix_delete_line($fixlinenr, $rawline);
5530 }
Joe Perches0979ae62012-12-17 16:01:59 -08005531 }
5532
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005533# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005534 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5535 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005536 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005537 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005538 }
5539
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005540# Check for user-visible strings broken across lines, which breaks the ability
5541# to grep for the string. Make exceptions when the previous string ends in a
5542# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5543# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005544 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005545 $prevline =~ /"\s*$/ &&
5546 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5547 if (WARN("SPLIT_STRING",
5548 "quoted string split across lines\n" . $hereprev) &&
5549 $fix &&
5550 $prevrawline =~ /^\+.*"\s*$/ &&
5551 $last_coalesced_string_linenr != $linenr - 1) {
5552 my $extracted_string = get_quoted_string($line, $rawline);
5553 my $comma_close = "";
5554 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5555 $comma_close = $1;
5556 }
5557
5558 fix_delete_line($fixlinenr - 1, $prevrawline);
5559 fix_delete_line($fixlinenr, $rawline);
5560 my $fixedline = $prevrawline;
5561 $fixedline =~ s/"\s*$//;
5562 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5563 fix_insert_line($fixlinenr - 1, $fixedline);
5564 $fixedline = $rawline;
5565 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5566 if ($fixedline !~ /\+\s*$/) {
5567 fix_insert_line($fixlinenr, $fixedline);
5568 }
5569 $last_coalesced_string_linenr = $linenr;
5570 }
5571 }
5572
5573# check for missing a space in a string concatenation
5574 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5575 WARN('MISSING_SPACE',
5576 "break quoted strings at a space character\n" . $hereprev);
5577 }
5578
Joe Perchese4b7d302017-05-08 15:55:51 -07005579# check for an embedded function name in a string when the function is known
5580# This does not work very well for -f --file checking as it depends on patch
5581# context providing the function name or a single line form for in-file
5582# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005583 if ($line =~ /^\+.*$String/ &&
5584 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005585 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5586 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005587 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005588 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005589 }
5590
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005591# check for spaces before a quoted newline
5592 if ($rawline =~ /^.*\".*\s\\n/) {
5593 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5594 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5595 $fix) {
5596 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5597 }
5598
5599 }
5600
Joe Perchesf17dba42014-10-13 15:51:51 -07005601# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005602 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5603 if (CHK("CONCATENATED_STRING",
5604 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5605 $fix) {
5606 while ($line =~ /($String)/g) {
5607 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5608 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5609 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5610 }
5611 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005612 }
5613
Joe Perches90ad30e2014-12-10 15:51:59 -08005614# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005615 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005616 if (WARN("STRING_FRAGMENTS",
5617 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5618 $fix) {
5619 while ($line =~ /($String)(?=\s*")/g) {
5620 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5621 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5622 }
5623 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005624 }
5625
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005626# check for non-standard and hex prefixed decimal printf formats
5627 my $show_L = 1; #don't show the same defect twice
5628 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005629 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005630 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005631 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005632 # check for %L
5633 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005634 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005635 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5636 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005637 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005638 # check for %Z
5639 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5640 WARN("PRINTF_Z",
5641 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5642 $show_Z = 0;
5643 }
5644 # check for 0x<decimal>
5645 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5646 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005647 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5648 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005649 }
5650
5651# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005652 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005653 WARN("LINE_CONTINUATIONS",
5654 "Avoid line continuations in quoted strings\n" . $herecurr);
5655 }
5656
Andy Whitcroft00df3442007-06-08 13:47:06 -07005657# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005658 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005659 WARN("IF_0",
5660 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5661 }
5662
5663# warn about #if 1
5664 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5665 WARN("IF_1",
5666 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005667 }
5668
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005669# check for needless "if (<foo>) fn(<foo>)" uses
5670 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005671 my $tested = quotemeta($1);
5672 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5673 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5674 my $func = $1;
5675 if (WARN('NEEDLESS_IF',
5676 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5677 $fix) {
5678 my $do_fix = 1;
5679 my $leading_tabs = "";
5680 my $new_leading_tabs = "";
5681 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5682 $leading_tabs = $1;
5683 } else {
5684 $do_fix = 0;
5685 }
5686 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5687 $new_leading_tabs = $1;
5688 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5689 $do_fix = 0;
5690 }
5691 } else {
5692 $do_fix = 0;
5693 }
5694 if ($do_fix) {
5695 fix_delete_line($fixlinenr - 1, $prevrawline);
5696 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5697 }
5698 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005699 }
5700 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005701
Joe Perchesebfdc402014-08-06 16:10:27 -07005702# check for unnecessary "Out of Memory" messages
5703 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5704 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5705 (defined $1 || defined $3) &&
5706 $linenr > 3) {
5707 my $testval = $2;
5708 my $testline = $lines[$linenr - 3];
5709
5710 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5711# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5712
Joe Perchese29a70f2019-03-07 16:28:35 -08005713 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5714 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005715 WARN("OOM_MESSAGE",
5716 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5717 }
5718 }
5719
Joe Perchesf78d98f2014-10-13 15:52:01 -07005720# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005721 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005722 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5723 my $level = $1;
5724 if (WARN("UNNECESSARY_KERN_LEVEL",
5725 "Possible unnecessary $level\n" . $herecurr) &&
5726 $fix) {
5727 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5728 }
5729 }
5730
Joe Perches45c55e92017-02-24 15:01:31 -08005731# check for logging continuations
5732 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5733 WARN("LOGGING_CONTINUATION",
5734 "Avoid logging continuation uses where feasible\n" . $herecurr);
5735 }
5736
Joe Perchesabb08a52014-12-10 15:51:46 -08005737# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005738 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005739 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5740 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5741 WARN("MASK_THEN_SHIFT",
5742 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5743 }
5744
Joe Perchesb75ac612014-12-10 15:52:02 -08005745# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005746 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005747 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5748 my $val = $1;
5749 my $equal = "!";
5750 $equal = "" if ($4 eq "!=");
5751 if (CHK("COMPARISON_TO_NULL",
5752 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5753 $fix) {
5754 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5755 }
5756 }
5757 }
5758
Joe Perches8716de32013-09-11 14:24:05 -07005759# check for bad placement of section $InitAttribute (e.g.: __initdata)
5760 if ($line =~ /(\b$InitAttribute\b)/) {
5761 my $attr = $1;
5762 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5763 my $ptr = $1;
5764 my $var = $2;
5765 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5766 ERROR("MISPLACED_INIT",
5767 "$attr should be placed after $var\n" . $herecurr)) ||
5768 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5769 WARN("MISPLACED_INIT",
5770 "$attr should be placed after $var\n" . $herecurr))) &&
5771 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005772 $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 -07005773 }
5774 }
5775 }
5776
Joe Perchese970b8842013-11-12 15:10:10 -08005777# check for $InitAttributeData (ie: __initdata) with const
5778 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5779 my $attr = $1;
5780 $attr =~ /($InitAttributePrefix)(.*)/;
5781 my $attr_prefix = $1;
5782 my $attr_type = $2;
5783 if (ERROR("INIT_ATTRIBUTE",
5784 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5785 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005786 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005787 s/$InitAttributeData/${attr_prefix}initconst/;
5788 }
5789 }
5790
5791# check for $InitAttributeConst (ie: __initconst) without const
5792 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5793 my $attr = $1;
5794 if (ERROR("INIT_ATTRIBUTE",
5795 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5796 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005797 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005798 /(^\+\s*(?:static\s+))/;
5799 $lead = rtrim($1);
5800 $lead = "$lead " if ($lead !~ /^\+$/);
5801 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005802 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005803 }
5804 }
5805
Joe Perchesc17893c2015-04-16 12:44:42 -07005806# check for __read_mostly with const non-pointer (should just be const)
5807 if ($line =~ /\b__read_mostly\b/ &&
5808 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5809 if (ERROR("CONST_READ_MOSTLY",
5810 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5811 $fix) {
5812 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5813 }
5814 }
5815
Joe Perchesfbdb8132014-04-03 14:49:14 -07005816# don't use __constant_<foo> functions outside of include/uapi/
5817 if ($realfile !~ m@^include/uapi/@ &&
5818 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5819 my $constant_func = $1;
5820 my $func = $constant_func;
5821 $func =~ s/^__constant_//;
5822 if (WARN("CONSTANT_CONVERSION",
5823 "$constant_func should be $func\n" . $herecurr) &&
5824 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005825 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005826 }
5827 }
5828
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005829# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005830 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005831 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005832 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005833 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005834 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005835 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07005836 }
5837 if ($delay > 2000) {
5838 WARN("LONG_UDELAY",
5839 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005840 }
5841 }
5842
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005843# warn about unexpectedly long msleep's
5844 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5845 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005846 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005847 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005848 }
5849 }
5850
Joe Perches36ec1932013-07-03 15:05:25 -07005851# check for comparisons of jiffies
5852 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5853 WARN("JIFFIES_COMPARISON",
5854 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5855 }
5856
Joe Perches9d7a34a2013-07-03 15:05:26 -07005857# check for comparisons of get_jiffies_64()
5858 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5859 WARN("JIFFIES_COMPARISON",
5860 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5861 }
5862
Andy Whitcroft00df3442007-06-08 13:47:06 -07005863# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005864# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005865# print "#ifdef in C files should be avoided\n";
5866# print "$herecurr";
5867# $clean = 0;
5868# }
5869
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005870# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005871 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005872 if (ERROR("SPACING",
5873 "exactly one space required after that #$1\n" . $herecurr) &&
5874 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005875 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005876 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5877 }
5878
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005879 }
5880
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005881# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005882 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5883 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005884 my $which = $1;
5885 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005886 CHK("UNCOMMENTED_DEFINITION",
5887 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005888 }
5889 }
5890# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005891
5892 my $barriers = qr{
5893 mb|
5894 rmb|
5895 wmb|
5896 read_barrier_depends
5897 }x;
5898 my $barrier_stems = qr{
5899 mb__before_atomic|
5900 mb__after_atomic|
5901 store_release|
5902 load_acquire|
5903 store_mb|
5904 (?:$barriers)
5905 }x;
5906 my $all_barriers = qr{
5907 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005908 smp_(?:$barrier_stems)|
5909 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005910 }x;
5911
5912 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005913 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005914 WARN("MEMORY_BARRIER",
5915 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005916 }
5917 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005918
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005919 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5920
5921 if ($realfile !~ m@^include/asm-generic/@ &&
5922 $realfile !~ m@/barrier\.h$@ &&
5923 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5924 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5925 WARN("MEMORY_BARRIER",
5926 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5927 }
5928
Joe Perchescb426e92015-06-25 15:02:46 -07005929# check for waitqueue_active without a comment.
5930 if ($line =~ /\bwaitqueue_active\s*\(/) {
5931 if (!ctx_has_comment($first_line, $linenr)) {
5932 WARN("WAITQUEUE_ACTIVE",
5933 "waitqueue_active without comment\n" . $herecurr);
5934 }
5935 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005936
Paul E. McKenney91db2592017-11-27 09:37:35 -08005937# check for smp_read_barrier_depends and read_barrier_depends
5938 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5939 WARN("READ_BARRIER_DEPENDS",
5940 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5941 }
5942
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005943# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005944 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005945 CHK("ARCH_DEFINES",
5946 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005947 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005948
Joe Perches596ed452017-07-12 14:37:02 -07005949# check that the storage class is not after a type
5950 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005951 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005952 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5953 }
5954# Check that the storage class is at the beginning of a declaration
5955 if ($line =~ /\b$Storage\b/ &&
5956 $line !~ /^.\s*$Storage/ &&
5957 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5958 $1 !~ /[\,\)]\s*$/) {
5959 WARN("STORAGE_CLASS",
5960 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005961 }
5962
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005963# check the location of the inline attribute, that it is between
5964# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005965 if ($line =~ /\b$Type\s+$Inline\b/ ||
5966 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005967 ERROR("INLINE_LOCATION",
5968 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005969 }
5970
Andy Whitcroft8905a672007-11-28 16:21:06 -08005971# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005972 if ($realfile !~ m@\binclude/uapi/@ &&
5973 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005974 if (WARN("INLINE",
5975 "plain inline is preferred over $1\n" . $herecurr) &&
5976 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005977 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005978
5979 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005980 }
5981
Joe Perches3d130fd2011-01-12 17:00:00 -08005982# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005983 if ($realfile !~ m@\binclude/uapi/@ &&
5984 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005985 WARN("PREFER_PACKED",
5986 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005987 }
5988
Joe Perches39b7e282011-07-25 17:13:24 -07005989# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005990 if ($realfile !~ m@\binclude/uapi/@ &&
5991 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005992 WARN("PREFER_ALIGNED",
5993 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005994 }
5995
Joe Perches462811d2019-09-25 16:46:44 -07005996# Check for __attribute__ section, prefer __section
5997 if ($realfile !~ m@\binclude/uapi/@ &&
5998 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
5999 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6000 my $new = substr($old, 1, -1);
6001 if (WARN("PREFER_SECTION",
6002 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6003 $fix) {
6004 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6005 }
6006 }
6007
Joe Perches5f14d3b2012-01-10 15:09:52 -08006008# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08006009 if ($realfile !~ m@\binclude/uapi/@ &&
6010 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006011 if (WARN("PREFER_PRINTF",
6012 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6013 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006014 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006015
6016 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08006017 }
6018
Joe Perches6061d942012-03-23 15:02:16 -07006019# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08006020 if ($realfile !~ m@\binclude/uapi/@ &&
6021 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006022 if (WARN("PREFER_SCANF",
6023 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6024 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006025 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006026 }
Joe Perches6061d942012-03-23 15:02:16 -07006027 }
6028
Joe Perches619a9082014-12-10 15:51:35 -08006029# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006030 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006031 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6032 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6033 $line =~ /\b__weak\b/)) {
6034 ERROR("WEAK_DECLARATION",
6035 "Using weak declarations can have unintended link defects\n" . $herecurr);
6036 }
6037
Tomas Winklerfd39f902016-12-12 16:46:34 -08006038# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006039 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006040 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006041 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6042 my $type = $1;
6043 if ($type =~ /\b($typeC99Typedefs)\b/) {
6044 $type = $1;
6045 my $kernel_type = 'u';
6046 $kernel_type = 's' if ($type =~ /^_*[si]/);
6047 $type =~ /(\d+)/;
6048 $kernel_type .= $1;
6049 if (CHK("PREFER_KERNEL_TYPES",
6050 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6051 $fix) {
6052 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6053 }
6054 }
6055 }
6056
Joe Perches938224b2016-01-20 14:59:15 -08006057# check for cast of C90 native int or longer types constants
6058 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6059 my $cast = $1;
6060 my $const = $2;
6061 if (WARN("TYPECAST_INT_CONSTANT",
6062 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6063 $fix) {
6064 my $suffix = "";
6065 my $newconst = $const;
6066 $newconst =~ s/${Int_type}$//;
6067 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6068 if ($cast =~ /\blong\s+long\b/) {
6069 $suffix .= 'LL';
6070 } elsif ($cast =~ /\blong\b/) {
6071 $suffix .= 'L';
6072 }
6073 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6074 }
6075 }
6076
Joe Perches8f53a9b2010-03-05 13:43:48 -08006077# check for sizeof(&)
6078 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006079 WARN("SIZEOF_ADDRESS",
6080 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006081 }
6082
Joe Perches66c80b62012-07-30 14:41:22 -07006083# check for sizeof without parenthesis
6084 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006085 if (WARN("SIZEOF_PARENTHESIS",
6086 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6087 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006088 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006089 }
Joe Perches66c80b62012-07-30 14:41:22 -07006090 }
6091
Joe Perches88982fe2012-12-17 16:02:00 -08006092# check for struct spinlock declarations
6093 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6094 WARN("USE_SPINLOCK_T",
6095 "struct spinlock should be spinlock_t\n" . $herecurr);
6096 }
6097
Joe Perchesa6962d72013-04-29 16:18:13 -07006098# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006099 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006100 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006101 $fmt =~ s/%%//g;
6102 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006103 if (WARN("PREFER_SEQ_PUTS",
6104 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6105 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006106 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006107 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006108 }
6109 }
6110
Joe Perches478b1792018-04-10 16:33:34 -07006111# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006112 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006113 defined $stat &&
6114 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6115 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006116 my $stat_real;
6117
Joe Perches0b523762017-05-08 15:55:36 -07006118 my $lc = $stat =~ tr@\n@@;
6119 $lc = $lc + $linenr;
6120 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006121 my $specifier;
6122 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006123 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006124 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006125 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6126 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006127
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006128 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006129 $specifier = $1;
6130 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006131 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006132 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006133 ($extension eq "f" &&
6134 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006135 $bad_specifier = $specifier;
6136 last;
6137 }
6138 if ($extension eq "x" && !defined($stat_real)) {
6139 if (!defined($stat_real)) {
6140 $stat_real = get_stat_real($linenr, $lc);
6141 }
6142 WARN("VSPRINTF_SPECIFIER_PX",
6143 "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");
6144 }
6145 }
6146 if ($bad_specifier ne "") {
6147 my $stat_real = get_stat_real($linenr, $lc);
6148 my $ext_type = "Invalid";
6149 my $use = "";
6150 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006151 $use = " - use %pS instead";
6152 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6153 }
6154
6155 WARN("VSPRINTF_POINTER_EXTENSION",
6156 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6157 }
Joe Perches0b523762017-05-08 15:55:36 -07006158 }
6159 }
6160
Andy Whitcroft554e1652012-01-10 15:09:57 -08006161# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006162 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006163 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006164 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006165
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006166 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006167 my $ms_val = $7;
6168 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006169
Andy Whitcroft554e1652012-01-10 15:09:57 -08006170 if ($ms_size =~ /^(0x|)0$/i) {
6171 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006172 "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 -08006173 } elsif ($ms_size =~ /^(0x|)1$/i) {
6174 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006175 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6176 }
6177 }
6178
Joe Perches98a9bba2014-01-23 15:54:52 -08006179# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006180# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006181# defined $stat &&
6182# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6183# if (WARN("PREFER_ETHER_ADDR_COPY",
6184# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6185# $fix) {
6186# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6187# }
6188# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006189
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006190# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006191# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006192# defined $stat &&
6193# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6194# WARN("PREFER_ETHER_ADDR_EQUAL",
6195# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6196# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006197
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006198# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6199# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006200# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006201# defined $stat &&
6202# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6203#
6204# my $ms_val = $7;
6205#
6206# if ($ms_val =~ /^(?:0x|)0+$/i) {
6207# if (WARN("PREFER_ETH_ZERO_ADDR",
6208# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6209# $fix) {
6210# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6211# }
6212# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6213# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6214# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6215# $fix) {
6216# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6217# }
6218# }
6219# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006220
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006221# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006222 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006223 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006224 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006225 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006226 my $call = $1;
6227 my $cast1 = deparenthesize($2);
6228 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006229 my $cast2 = deparenthesize($7);
6230 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006231 my $cast;
6232
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006233 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006234 $cast = "$cast1 or $cast2";
6235 } elsif ($cast1 ne "") {
6236 $cast = $cast1;
6237 } else {
6238 $cast = $cast2;
6239 }
6240 WARN("MINMAX",
6241 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006242 }
6243 }
6244
Joe Perches4a273192012-07-30 14:41:20 -07006245# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006246 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006247 defined $stat &&
6248 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6249 my $min = $1;
6250 my $max = $7;
6251 if ($min eq $max) {
6252 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006253 "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 -07006254 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6255 $min > $max) {
6256 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006257 "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 -07006258 }
6259 }
6260
Joe Perches823b7942013-11-12 15:10:15 -08006261# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006262 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006263 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006264 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006265 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6266 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6267 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6268 my $lc = $stat =~ tr@\n@@;
6269 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006270 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006271 WARN("NAKED_SSCANF",
6272 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6273 }
6274
Joe Perchesafc819a2014-06-04 16:12:08 -07006275# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006276 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006277 defined $stat &&
6278 $line =~ /\bsscanf\b/) {
6279 my $lc = $stat =~ tr@\n@@;
6280 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006281 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006282 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6283 my $format = $6;
6284 my $count = $format =~ tr@%@%@;
6285 if ($count == 1 &&
6286 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6287 WARN("SSCANF_TO_KSTRTO",
6288 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6289 }
6290 }
6291 }
6292
Joe Perches70dc8a42013-09-11 14:23:58 -07006293# check for new externs in .h files.
6294 if ($realfile =~ /\.h$/ &&
6295 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006296 if (CHK("AVOID_EXTERNS",
6297 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006298 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006299 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006300 }
6301 }
6302
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006303# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006304 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006305 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006306 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006307 my $function_name = $1;
6308 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006309
6310 my $s = $stat;
6311 if (defined $cond) {
6312 substr($s, 0, length($cond), '');
6313 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006314 if ($s =~ /^\s*;/ &&
6315 $function_name ne 'uninitialized_var')
6316 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006317 WARN("AVOID_EXTERNS",
6318 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006319 }
6320
6321 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006322 WARN("FUNCTION_ARGUMENTS",
6323 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006324 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006325
6326 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6327 $stat =~ /^.\s*extern\s+/)
6328 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006329 WARN("AVOID_EXTERNS",
6330 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006331 }
6332
Joe Perchesa0ad7592017-07-10 15:52:19 -07006333# check for function declarations that have arguments without identifier names
Joe Perches16b7f3c2020-04-06 20:11:17 -07006334# while avoiding uninitialized_var(x)
Joe Perchesa0ad7592017-07-10 15:52:19 -07006335 if (defined $stat &&
Joe Perches16b7f3c2020-04-06 20:11:17 -07006336 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:($Ident)|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6337 (!defined($1) ||
6338 (defined($1) && $1 ne "uninitialized_var")) &&
6339 $2 ne "void") {
6340 my $args = trim($2);
Joe Perchesca0d8922016-10-11 13:52:16 -07006341 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6342 my $arg = trim($1);
Joe Perches16b7f3c2020-04-06 20:11:17 -07006343 if ($arg =~ /^$Type$/ &&
6344 $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006345 WARN("FUNCTION_ARGUMENTS",
6346 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6347 }
6348 }
6349 }
6350
Joe Perchesa0ad7592017-07-10 15:52:19 -07006351# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006352 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006353 defined $stat &&
6354 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6355 $context_function = $1;
6356
6357# check for multiline function definition with misplaced open brace
6358 my $ok = 0;
6359 my $cnt = statement_rawlines($stat);
6360 my $herectx = $here . "\n";
6361 for (my $n = 0; $n < $cnt; $n++) {
6362 my $rl = raw_line($linenr, $n);
6363 $herectx .= $rl . "\n";
6364 $ok = 1 if ($rl =~ /^[ \+]\{/);
6365 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6366 last if $rl =~ /^[ \+].*\{/;
6367 }
6368 if (!$ok) {
6369 ERROR("OPEN_BRACE",
6370 "open brace '{' following function definitions go on the next line\n" . $herectx);
6371 }
6372 }
6373
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006374# checks for new __setup's
6375 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6376 my $name = $1;
6377
6378 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006379 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006380 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006381 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006382 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006383
Joe Perchese29a70f2019-03-07 16:28:35 -08006384# check for pointless casting of alloc functions
6385 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006386 WARN("UNNECESSARY_CASTS",
6387 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006388 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006389
Joe Perchesa640d252013-07-03 15:05:21 -07006390# alloc style
6391# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006392 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006393 $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 -07006394 CHK("ALLOC_SIZEOF_STRUCT",
6395 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6396 }
6397
Joe Perches60a55362014-06-04 16:12:07 -07006398# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006399 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006400 defined $stat &&
6401 $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 -07006402 my $oldfunc = $3;
6403 my $a1 = $4;
6404 my $a2 = $10;
6405 my $newfunc = "kmalloc_array";
6406 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006407 my $r1 = $a1;
6408 my $r2 = $a2;
6409 if ($a1 =~ /^sizeof\s*\S/) {
6410 $r1 = $a2;
6411 $r2 = $a1;
6412 }
6413 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6414 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006415 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006416 my $herectx = get_stat_here($linenr, $cnt, $here);
6417
Joe Perches60a55362014-06-04 16:12:07 -07006418 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006419 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6420 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006421 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006422 $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 -07006423 }
6424 }
6425 }
6426
Joe Perches972fdea2013-04-29 16:18:12 -07006427# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006428 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006429 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6430 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006431 WARN("KREALLOC_ARG_REUSE",
6432 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6433 }
6434
Joe Perches5ce59ae2013-02-21 16:44:18 -08006435# check for alloc argument mismatch
6436 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6437 WARN("ALLOC_ARRAY_ARGS",
6438 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6439 }
6440
Joe Perchescaf2a542011-01-12 16:59:56 -08006441# check for multiple semicolons
6442 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006443 if (WARN("ONE_SEMICOLON",
6444 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6445 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006446 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006447 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006448 }
6449
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006450# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6451 if ($realfile !~ m@^include/uapi/@ &&
6452 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006453 my $ull = "";
6454 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6455 if (CHK("BIT_MACRO",
6456 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6457 $fix) {
6458 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6459 }
6460 }
6461
Joe Perches2d632742016-05-20 17:04:00 -07006462# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6463 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6464 my $config = $1;
6465 if (WARN("PREFER_IS_ENABLED",
6466 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6467 $fix) {
6468 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6469 }
6470 }
6471
Joe Perchese81f2392014-08-06 16:11:25 -07006472# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006473 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6474 my $has_break = 0;
6475 my $has_statement = 0;
6476 my $count = 0;
6477 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006478 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006479 $prevline--;
6480 my $rline = $rawlines[$prevline - 1];
6481 my $fline = $lines[$prevline - 1];
6482 last if ($fline =~ /^\@\@/);
6483 next if ($fline =~ /^\-/);
6484 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6485 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6486 next if ($fline =~ /^.[\s$;]*$/);
6487 $has_statement = 1;
6488 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006489 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006490 }
6491 if (!$has_break && $has_statement) {
6492 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006493 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006494 }
6495 }
6496
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006497# check for /* fallthrough */ like comment, prefer fallthrough;
6498 my @fallthroughs = (
6499 'fallthrough',
6500 '@fallthrough@',
6501 'lint -fallthrough[ \t]*',
6502 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6503 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6504 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6505 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6506 );
6507 if ($raw_comment ne '') {
6508 foreach my $ft (@fallthroughs) {
6509 if ($raw_comment =~ /$ft/) {
6510 my $msg_level = \&WARN;
6511 $msg_level = \&CHK if ($file);
6512 &{$msg_level}("PREFER_FALLTHROUGH",
6513 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6514 last;
6515 }
6516 }
6517 }
6518
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006519# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006520 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006521 defined $stat &&
6522 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006523 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006524 my $herectx = get_stat_here($linenr, $cnt, $here);
6525
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006526 WARN("DEFAULT_NO_BREAK",
6527 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006528 }
6529
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006530# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006531 if ($line =~ /\b__FUNCTION__\b/) {
6532 if (WARN("USE_FUNC",
6533 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6534 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006535 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006536 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006537 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006538
Joe Perches62ec8182015-02-13 14:38:18 -08006539# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6540 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6541 ERROR("DATE_TIME",
6542 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6543 }
6544
Joe Perches2c924882012-03-23 15:02:20 -07006545# check for use of yield()
6546 if ($line =~ /\byield\s*\(\s*\)/) {
6547 WARN("YIELD",
6548 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6549 }
6550
Joe Perches179f8f42013-07-03 15:05:30 -07006551# check for comparisons against true and false
6552 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6553 my $lead = $1;
6554 my $arg = $2;
6555 my $test = $3;
6556 my $otype = $4;
6557 my $trail = $5;
6558 my $op = "!";
6559
6560 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6561
6562 my $type = lc($otype);
6563 if ($type =~ /^(?:true|false)$/) {
6564 if (("$test" eq "==" && "$type" eq "true") ||
6565 ("$test" eq "!=" && "$type" eq "false")) {
6566 $op = "";
6567 }
6568
6569 CHK("BOOL_COMPARISON",
6570 "Using comparison to $otype is error prone\n" . $herecurr);
6571
6572## maybe suggesting a correct construct would better
6573## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6574
6575 }
6576 }
6577
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006578# check for semaphores initialized locked
6579 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006580 WARN("CONSIDER_COMPLETION",
6581 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006582 }
Joe Perches6712d852012-03-23 15:02:20 -07006583
Joe Perches67d0a072011-10-31 17:13:10 -07006584# recommend kstrto* over simple_strto* and strict_strto*
6585 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006586 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006587 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006588 }
Joe Perches6712d852012-03-23 15:02:20 -07006589
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006590# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006591 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006592 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006593 "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 -07006594 }
Joe Perches6712d852012-03-23 15:02:20 -07006595
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006596# check for spin_is_locked(), suggest lockdep instead
6597 if ($line =~ /\bspin_is_locked\(/) {
6598 WARN("USE_LOCKDEP",
6599 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6600 }
6601
Joe Perches9189c7e2018-09-07 15:26:18 -07006602# check for deprecated apis
6603 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6604 my $deprecated_api = $1;
6605 my $new_api = $deprecated_apis{$deprecated_api};
6606 WARN("DEPRECATED_API",
6607 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6608 }
6609
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006610# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006611# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006612 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006613 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006614 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006615 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006616 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006617
6618# use of NR_CPUS is usually wrong
6619# ignore definitions of NR_CPUS and usage to define arrays as likely right
6620 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006621 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6622 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006623 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6624 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6625 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006626 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006627 WARN("NR_CPUS",
6628 "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 -07006629 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006630
Joe Perches52ea8502013-11-12 15:10:09 -08006631# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6632 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6633 ERROR("DEFINE_ARCH_HAS",
6634 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6635 }
6636
Joe Perchesacd93622015-02-13 14:38:38 -08006637# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006638 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006639 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6640 WARN("LIKELY_MISUSE",
6641 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6642 }
6643
Denis Efremovde3f1862019-09-25 16:49:25 -07006644# nested likely/unlikely calls
6645 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6646 WARN("LIKELY_MISUSE",
6647 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6648 }
6649
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006650# whine mightly about in_atomic
6651 if ($line =~ /\bin_atomic\s*\(/) {
6652 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006653 ERROR("IN_ATOMIC",
6654 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006655 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006656 WARN("IN_ATOMIC",
6657 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006658 }
6659 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006660
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006661# check for mutex_trylock_recursive usage
6662 if ($line =~ /mutex_trylock_recursive/) {
6663 ERROR("LOCKING",
6664 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6665 }
6666
Peter Zijlstra1704f472010-03-19 01:37:42 +01006667# check for lockdep_set_novalidate_class
6668 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6669 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6670 if ($realfile !~ m@^kernel/lockdep@ &&
6671 $realfile !~ m@^include/linux/lockdep@ &&
6672 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006673 ERROR("LOCKDEP",
6674 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006675 }
6676 }
Dave Jones88f88312011-01-12 16:59:59 -08006677
Joe Perchesb392c642015-04-16 12:44:16 -07006678 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6679 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006680 WARN("EXPORTED_WORLD_WRITABLE",
6681 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006682 }
Joe Perches24358802014-04-03 14:49:13 -07006683
Joe Perches00180462018-02-06 15:38:55 -08006684# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6685# and whether or not function naming is typical and if
6686# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006687 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006688 defined $stat &&
6689 $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*\)/) {
6690 my $var = $1;
6691 my $perms = $2;
6692 my $show = $3;
6693 my $store = $4;
6694 my $octal_perms = perms_to_octal($perms);
6695 if ($show =~ /^${var}_show$/ &&
6696 $store =~ /^${var}_store$/ &&
6697 $octal_perms eq "0644") {
6698 if (WARN("DEVICE_ATTR_RW",
6699 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6700 $fix) {
6701 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6702 }
6703 } elsif ($show =~ /^${var}_show$/ &&
6704 $store =~ /^NULL$/ &&
6705 $octal_perms eq "0444") {
6706 if (WARN("DEVICE_ATTR_RO",
6707 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6708 $fix) {
6709 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6710 }
6711 } elsif ($show =~ /^NULL$/ &&
6712 $store =~ /^${var}_store$/ &&
6713 $octal_perms eq "0200") {
6714 if (WARN("DEVICE_ATTR_WO",
6715 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6716 $fix) {
6717 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6718 }
6719 } elsif ($octal_perms eq "0644" ||
6720 $octal_perms eq "0444" ||
6721 $octal_perms eq "0200") {
6722 my $newshow = "$show";
6723 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6724 my $newstore = $store;
6725 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6726 my $rename = "";
6727 if ($show ne $newshow) {
6728 $rename .= " '$show' to '$newshow'";
6729 }
6730 if ($store ne $newstore) {
6731 $rename .= " '$store' to '$newstore'";
6732 }
6733 WARN("DEVICE_ATTR_FUNCTIONS",
6734 "Consider renaming function(s)$rename\n" . $herecurr);
6735 } else {
6736 WARN("DEVICE_ATTR_PERMS",
6737 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6738 }
6739 }
6740
Joe Perches515a2352014-04-03 14:49:24 -07006741# Mode permission misuses where it seems decimal should be octal
6742# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006743# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6744# specific definition of not visible in sysfs.
6745# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6746# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006747 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006748 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006749 $line =~ /$mode_perms_search/) {
6750 foreach my $entry (@mode_permission_funcs) {
6751 my $func = $entry->[0];
6752 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006753
Joe Perches459cf0a2016-10-11 13:52:19 -07006754 my $lc = $stat =~ tr@\n@@;
6755 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006756 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006757
Joe Perches515a2352014-04-03 14:49:24 -07006758 my $skip_args = "";
6759 if ($arg_pos > 1) {
6760 $arg_pos--;
6761 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6762 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006763 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006764 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006765 my $val = $1;
6766 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006767 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6768 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6769 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006770 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006771 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006772 }
6773 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006774 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006775 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006776 }
Joe Perches24358802014-04-03 14:49:13 -07006777 }
6778 }
6779 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006780
Joe Perches459cf0a2016-10-11 13:52:19 -07006781# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006782 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006783 my $oval = $1;
6784 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006785 if (WARN("SYMBOLIC_PERMS",
6786 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6787 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006788 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006789 }
6790 }
6791
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006792# validate content of MODULE_LICENSE against list from include/linux/module.h
6793 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6794 my $extracted_string = get_quoted_string($line, $rawline);
6795 my $valid_licenses = qr{
6796 GPL|
6797 GPL\ v2|
6798 GPL\ and\ additional\ rights|
6799 Dual\ BSD/GPL|
6800 Dual\ MIT/GPL|
6801 Dual\ MPL/GPL|
6802 Proprietary
6803 }x;
6804 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6805 WARN("MODULE_LICENSE",
6806 "unknown module license " . $extracted_string . "\n" . $herecurr);
6807 }
6808 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07006809
6810# check for sysctl duplicate constants
6811 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6812 WARN("DUPLICATED_SYSCTL_CONST",
6813 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6814 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006815 }
6816
6817 # If we have no input at all, then there is nothing to report on
6818 # so just keep quiet.
6819 if ($#rawlines == -1) {
6820 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006821 }
6822
Andy Whitcroft8905a672007-11-28 16:21:06 -08006823 # In mailback mode only produce a report in the negative, for
6824 # things that appear to be patches.
6825 if ($mailback && ($clean == 1 || !$is_patch)) {
6826 exit(0);
6827 }
6828
6829 # This is not a patch, and we are are in 'no-patch' mode so
6830 # just keep quiet.
6831 if (!$chk_patch && !$is_patch) {
6832 exit(0);
6833 }
6834
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006835 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006836 ERROR("NOT_UNIFIED_DIFF",
6837 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006838 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006839 if ($is_patch && $has_commit_log && $chk_signoff) {
6840 if ($signoff == 0) {
6841 ERROR("MISSING_SIGN_OFF",
6842 "Missing Signed-off-by: line(s)\n");
6843 } elsif (!$authorsignoff) {
6844 WARN("NO_AUTHOR_SIGN_OFF",
6845 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6846 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006847 }
6848
Andy Whitcroft8905a672007-11-28 16:21:06 -08006849 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006850 if ($summary && !($clean == 1 && $quiet == 1)) {
6851 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006852 print "total: $cnt_error errors, $cnt_warn warnings, " .
6853 (($check)? "$cnt_chk checks, " : "") .
6854 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006855 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006856
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006857 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006858 # If there were any defects found and not already fixing them
6859 if (!$clean and !$fix) {
6860 print << "EOM"
6861
6862NOTE: For some of the reported defects, checkpatch may be able to
6863 mechanically convert to the typical style using --fix or --fix-inplace.
6864EOM
6865 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006866 # If there were whitespace errors which cleanpatch can fix
6867 # then suggest that.
6868 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006869 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006870 print << "EOM"
6871
6872NOTE: Whitespace errors detected.
6873 You may wish to use scripts/cleanpatch or scripts/cleanfile
6874EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006875 }
6876 }
6877
Joe Perchesd752fcc2014-08-06 16:11:05 -07006878 if ($clean == 0 && $fix &&
6879 ("@rawlines" ne "@fixed" ||
6880 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006881 my $newfile = $filename;
6882 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006883 my $linecount = 0;
6884 my $f;
6885
Joe Perchesd752fcc2014-08-06 16:11:05 -07006886 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6887
Joe Perches3705ce52013-07-03 15:05:31 -07006888 open($f, '>', $newfile)
6889 or die "$P: Can't open $newfile for write\n";
6890 foreach my $fixed_line (@fixed) {
6891 $linecount++;
6892 if ($file) {
6893 if ($linecount > 3) {
6894 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006895 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006896 }
6897 } else {
6898 print $f $fixed_line . "\n";
6899 }
6900 }
6901 close($f);
6902
6903 if (!$quiet) {
6904 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006905
Joe Perches3705ce52013-07-03 15:05:31 -07006906Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6907
6908Do _NOT_ trust the results written to this file.
6909Do _NOT_ submit these changes without inspecting them for correctness.
6910
6911This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6912No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006913EOM
6914 }
6915 }
6916
Joe Perchesd8469f12015-06-25 15:03:00 -07006917 if ($quiet == 0) {
6918 print "\n";
6919 if ($clean == 1) {
6920 print "$vname has no obvious style problems and is ready for submission.\n";
6921 } else {
6922 print "$vname has style problems, please review.\n";
6923 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006924 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006925 return $clean;
6926}