blob: 880fe8639fb6621224c2d5919709f682455c86a9 [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
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001679 # If c99 comment on the current line, or the line before or after
1680 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1681 return $current_comment if (defined $current_comment);
1682 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1683 return $current_comment if (defined $current_comment);
1684 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1685 return $current_comment if (defined $current_comment);
1686
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001687 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001688 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001689 return $current_comment if (defined $current_comment);
1690
1691 # Look through the context and try and figure out if there is a
1692 # comment.
1693 my $in_comment = 0;
1694 $current_comment = '';
1695 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001696 my $line = $rawlines[$linenr - 1];
1697 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001698 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1699 $in_comment = 1;
1700 }
1701 if ($line =~ m@/\*@) {
1702 $in_comment = 1;
1703 }
1704 if (!$in_comment && $current_comment ne '') {
1705 $current_comment = '';
1706 }
1707 $current_comment .= $line . "\n" if ($in_comment);
1708 if ($line =~ m@\*/@) {
1709 $in_comment = 0;
1710 }
1711 }
1712
1713 chomp($current_comment);
1714 return($current_comment);
1715}
1716sub ctx_has_comment {
1717 my ($first_line, $end_line) = @_;
1718 my $cmt = ctx_locate_comment($first_line, $end_line);
1719
Andy Whitcroft00df3442007-06-08 13:47:06 -07001720 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001721 ##print "CMMT: $cmt\n";
1722
1723 return ($cmt ne '');
1724}
1725
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001726sub raw_line {
1727 my ($linenr, $cnt) = @_;
1728
1729 my $offset = $linenr - 1;
1730 $cnt++;
1731
1732 my $line;
1733 while ($cnt) {
1734 $line = $rawlines[$offset++];
1735 next if (defined($line) && $line =~ /^-/);
1736 $cnt--;
1737 }
1738
1739 return $line;
1740}
1741
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001742sub get_stat_real {
1743 my ($linenr, $lc) = @_;
1744
1745 my $stat_real = raw_line($linenr, 0);
1746 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1747 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1748 }
1749
1750 return $stat_real;
1751}
1752
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001753sub get_stat_here {
1754 my ($linenr, $cnt, $here) = @_;
1755
1756 my $herectx = $here . "\n";
1757 for (my $n = 0; $n < $cnt; $n++) {
1758 $herectx .= raw_line($linenr, $n) . "\n";
1759 }
1760
1761 return $herectx;
1762}
1763
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001764sub cat_vet {
1765 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001766 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001767
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001768 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001769 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1770 $res .= $1;
1771 if ($2 ne '') {
1772 $coded = sprintf("^%c", unpack('C', $2) + 64);
1773 $res .= $coded;
1774 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001775 }
1776 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001777
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001778 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001779}
1780
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001781my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001782my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001783my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001784my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001785
1786sub annotate_reset {
1787 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001788 $av_pending = '_';
1789 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001790 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001791}
1792
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001793sub annotate_values {
1794 my ($stream, $type) = @_;
1795
1796 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001797 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001798 my $cur = $stream;
1799
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001800 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001801
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001802 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001803 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001804 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001805 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001806 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001807 print "WS($1)\n" if ($dbg_values > 1);
1808 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001809 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001810 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001811 }
1812
Florian Micklerc023e4732011-01-12 16:59:58 -08001813 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001814 print "CAST($1)\n" if ($dbg_values > 1);
1815 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001816 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001817
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001818 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001819 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001820 $type = 'T';
1821
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001822 } elsif ($cur =~ /^($Modifier)\s*/) {
1823 print "MODIFIER($1)\n" if ($dbg_values > 1);
1824 $type = 'T';
1825
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001826 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001827 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001828 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001829 push(@av_paren_type, $type);
1830 if ($2 ne '') {
1831 $av_pending = 'N';
1832 }
1833 $type = 'E';
1834
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001835 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001836 print "UNDEF($1)\n" if ($dbg_values > 1);
1837 $av_preprocessor = 1;
1838 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001839
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001840 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001841 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001842 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001843
1844 push(@av_paren_type, $type);
1845 push(@av_paren_type, $type);
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*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001849 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1850 $av_preprocessor = 1;
1851
1852 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1853
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001854 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001855
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001856 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001857 print "PRE_END($1)\n" if ($dbg_values > 1);
1858
1859 $av_preprocessor = 1;
1860
1861 # Assume all arms of the conditional end as this
1862 # one does, and continue as if the #endif was not here.
1863 pop(@av_paren_type);
1864 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001865 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001866
1867 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001868 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001869
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001870 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1871 print "ATTR($1)\n" if ($dbg_values > 1);
1872 $av_pending = $type;
1873 $type = 'N';
1874
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001875 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001876 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001877 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001878 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001879 }
1880 $type = 'N';
1881
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001882 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001883 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001884 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001885 $type = 'N';
1886
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001887 } elsif ($cur =~/^(case)/o) {
1888 print "CASE($1)\n" if ($dbg_values > 1);
1889 $av_pend_colon = 'C';
1890 $type = 'N';
1891
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001892 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001893 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001894 $type = 'N';
1895
1896 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001897 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001898 push(@av_paren_type, $av_pending);
1899 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001900 $type = 'N';
1901
1902 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001903 my $new_type = pop(@av_paren_type);
1904 if ($new_type ne '_') {
1905 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001906 print "PAREN('$1') -> $type\n"
1907 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001908 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001909 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001910 }
1911
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001912 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001913 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001914 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001915 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001916
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001917 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1918 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001919 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001920 } elsif ($type eq 'E') {
1921 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001922 }
1923 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1924 $type = 'V';
1925
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001926 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001927 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001928 $type = 'V';
1929
1930 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001931 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001932 $type = 'N';
1933
Andy Whitcroftcf655042008-03-04 14:28:20 -08001934 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001935 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001936 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001937 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001938
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001939 } elsif ($cur =~/^(,)/) {
1940 print "COMMA($1)\n" if ($dbg_values > 1);
1941 $type = 'C';
1942
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001943 } elsif ($cur =~ /^(\?)/o) {
1944 print "QUESTION($1)\n" if ($dbg_values > 1);
1945 $type = 'N';
1946
1947 } elsif ($cur =~ /^(:)/o) {
1948 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1949
1950 substr($var, length($res), 1, $av_pend_colon);
1951 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1952 $type = 'E';
1953 } else {
1954 $type = 'N';
1955 }
1956 $av_pend_colon = 'O';
1957
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001958 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001959 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001960 $type = 'N';
1961
Andy Whitcroft0d413862008-10-15 22:02:16 -07001962 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001963 my $variant;
1964
1965 print "OPV($1)\n" if ($dbg_values > 1);
1966 if ($type eq 'V') {
1967 $variant = 'B';
1968 } else {
1969 $variant = 'U';
1970 }
1971
1972 substr($var, length($res), 1, $variant);
1973 $type = 'N';
1974
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001975 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001976 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001977 if ($1 ne '++' && $1 ne '--') {
1978 $type = 'N';
1979 }
1980
1981 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001982 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001983 }
1984 if (defined $1) {
1985 $cur = substr($cur, length($1));
1986 $res .= $type x length($1);
1987 }
1988 }
1989
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001990 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001991}
1992
Andy Whitcroft8905a672007-11-28 16:21:06 -08001993sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001994 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001995 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001996 ^(?:
1997 $Modifier|
1998 $Storage|
1999 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002000 DEFINE_\S+
2001 )$|
2002 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002003 goto|
2004 return|
2005 case|
2006 else|
2007 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002008 do|
2009 \#|
2010 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002011 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002012 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002013 )}x;
2014 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2015 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002016 # Check for modifiers.
2017 $possible =~ s/\s*$Storage\s*//g;
2018 $possible =~ s/\s*$Sparse\s*//g;
2019 if ($possible =~ /^\s*$/) {
2020
2021 } elsif ($possible =~ /\s/) {
2022 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002023 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002024 if ($modifier !~ $notPermitted) {
2025 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002026 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002027 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002028 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002029
2030 } else {
2031 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002032 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002033 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002034 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002035 } else {
2036 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002037 }
2038}
2039
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002040my $prefix = '';
2041
Joe Perches000d1cc12011-07-25 17:13:25 -07002042sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002043 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002044
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002045 $type =~ tr/[a-z]/[A-Z]/;
2046
Joe Perchescbec18a2014-04-03 14:49:19 -07002047 return defined $use_type{$type} if (scalar keys %use_type > 0);
2048
2049 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002050}
2051
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002052sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002053 my ($level, $type, $msg) = @_;
2054
2055 if (!show_type($type) ||
2056 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002057 return 0;
2058 }
Joe Perches57230292015-06-25 15:03:03 -07002059 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002060 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002061 if ($level eq 'ERROR') {
2062 $output .= RED;
2063 } elsif ($level eq 'WARNING') {
2064 $output .= YELLOW;
2065 } else {
2066 $output .= GREEN;
2067 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002068 }
Joe Perches57230292015-06-25 15:03:03 -07002069 $output .= $prefix . $level . ':';
2070 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002071 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002072 $output .= "$type:";
2073 }
John Brooks737c0762017-07-10 15:52:24 -07002074 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002075 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002076
2077 if ($showfile) {
2078 my @lines = split("\n", $output, -1);
2079 splice(@lines, 1, 1);
2080 $output = join("\n", @lines);
2081 }
Joe Perches57230292015-06-25 15:03:03 -07002082 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002083
Joe Perches57230292015-06-25 15:03:03 -07002084 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002085
2086 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002087}
Joe Perchescbec18a2014-04-03 14:49:19 -07002088
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002089sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002090 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002091}
Joe Perches000d1cc12011-07-25 17:13:25 -07002092
Joe Perchesd752fcc2014-08-06 16:11:05 -07002093sub fixup_current_range {
2094 my ($lineRef, $offset, $length) = @_;
2095
2096 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2097 my $o = $1;
2098 my $l = $2;
2099 my $no = $o + $offset;
2100 my $nl = $l + $length;
2101 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2102 }
2103}
2104
2105sub fix_inserted_deleted_lines {
2106 my ($linesRef, $insertedRef, $deletedRef) = @_;
2107
2108 my $range_last_linenr = 0;
2109 my $delta_offset = 0;
2110
2111 my $old_linenr = 0;
2112 my $new_linenr = 0;
2113
2114 my $next_insert = 0;
2115 my $next_delete = 0;
2116
2117 my @lines = ();
2118
2119 my $inserted = @{$insertedRef}[$next_insert++];
2120 my $deleted = @{$deletedRef}[$next_delete++];
2121
2122 foreach my $old_line (@{$linesRef}) {
2123 my $save_line = 1;
2124 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002125 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002126 $delta_offset = 0;
2127 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2128 $range_last_linenr = $new_linenr;
2129 fixup_current_range(\$line, $delta_offset, 0);
2130 }
2131
2132 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2133 $deleted = @{$deletedRef}[$next_delete++];
2134 $save_line = 0;
2135 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2136 }
2137
2138 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2139 push(@lines, ${$inserted}{'LINE'});
2140 $inserted = @{$insertedRef}[$next_insert++];
2141 $new_linenr++;
2142 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2143 }
2144
2145 if ($save_line) {
2146 push(@lines, $line);
2147 $new_linenr++;
2148 }
2149
2150 $old_linenr++;
2151 }
2152
2153 return @lines;
2154}
2155
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002156sub fix_insert_line {
2157 my ($linenr, $line) = @_;
2158
2159 my $inserted = {
2160 LINENR => $linenr,
2161 LINE => $line,
2162 };
2163 push(@fixed_inserted, $inserted);
2164}
2165
2166sub fix_delete_line {
2167 my ($linenr, $line) = @_;
2168
2169 my $deleted = {
2170 LINENR => $linenr,
2171 LINE => $line,
2172 };
2173
2174 push(@fixed_deleted, $deleted);
2175}
2176
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002177sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002178 my ($type, $msg) = @_;
2179
2180 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002181 our $clean = 0;
2182 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002183 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002184 }
Joe Perches3705ce52013-07-03 15:05:31 -07002185 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002186}
2187sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002188 my ($type, $msg) = @_;
2189
2190 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002191 our $clean = 0;
2192 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002193 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002194 }
Joe Perches3705ce52013-07-03 15:05:31 -07002195 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002196}
2197sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002198 my ($type, $msg) = @_;
2199
2200 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002201 our $clean = 0;
2202 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002203 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002204 }
Joe Perches3705ce52013-07-03 15:05:31 -07002205 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002206}
2207
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002208sub check_absolute_file {
2209 my ($absolute, $herecurr) = @_;
2210 my $file = $absolute;
2211
2212 ##print "absolute<$absolute>\n";
2213
2214 # See if any suffix of this path is a path within the tree.
2215 while ($file =~ s@^[^/]*/@@) {
2216 if (-f "$root/$file") {
2217 ##print "file<$file>\n";
2218 last;
2219 }
2220 }
2221 if (! -f _) {
2222 return 0;
2223 }
2224
2225 # It is, so see if the prefix is acceptable.
2226 my $prefix = $absolute;
2227 substr($prefix, -length($file)) = '';
2228
2229 ##print "prefix<$prefix>\n";
2230 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002231 WARN("USE_RELATIVE_PATH",
2232 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002233 }
2234}
2235
Joe Perches3705ce52013-07-03 15:05:31 -07002236sub trim {
2237 my ($string) = @_;
2238
Joe Perchesb34c6482013-09-11 14:24:01 -07002239 $string =~ s/^\s+|\s+$//g;
2240
2241 return $string;
2242}
2243
2244sub ltrim {
2245 my ($string) = @_;
2246
2247 $string =~ s/^\s+//;
2248
2249 return $string;
2250}
2251
2252sub rtrim {
2253 my ($string) = @_;
2254
2255 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002256
2257 return $string;
2258}
2259
Joe Perches52ea8502013-11-12 15:10:09 -08002260sub string_find_replace {
2261 my ($string, $find, $replace) = @_;
2262
2263 $string =~ s/$find/$replace/g;
2264
2265 return $string;
2266}
2267
Joe Perches3705ce52013-07-03 15:05:31 -07002268sub tabify {
2269 my ($leading) = @_;
2270
Antonio Borneo713a09d2020-04-06 20:11:07 -07002271 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002272 my $max_spaces_before_tab = $source_indent - 1;
2273 my $spaces_to_tab = " " x $source_indent;
2274
2275 #convert leading spaces to tabs
2276 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2277 #Remove spaces before a tab
2278 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2279
2280 return "$leading";
2281}
2282
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002283sub pos_last_openparen {
2284 my ($line) = @_;
2285
2286 my $pos = 0;
2287
2288 my $opens = $line =~ tr/\(/\(/;
2289 my $closes = $line =~ tr/\)/\)/;
2290
2291 my $last_openparen = 0;
2292
2293 if (($opens == 0) || ($closes >= $opens)) {
2294 return -1;
2295 }
2296
2297 my $len = length($line);
2298
2299 for ($pos = 0; $pos < $len; $pos++) {
2300 my $string = substr($line, $pos);
2301 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2302 $pos += length($1) - 1;
2303 } elsif (substr($line, $pos, 1) eq '(') {
2304 $last_openparen = $pos;
2305 } elsif (index($string, '(') == -1) {
2306 last;
2307 }
2308 }
2309
Joe Perches91cb5192014-04-03 14:49:32 -07002310 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002311}
2312
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002313sub get_raw_comment {
2314 my ($line, $rawline) = @_;
2315 my $comment = '';
2316
2317 for my $i (0 .. (length($line) - 1)) {
2318 if (substr($line, $i, 1) eq "$;") {
2319 $comment .= substr($rawline, $i, 1);
2320 }
2321 }
2322
2323 return $comment;
2324}
2325
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002326sub process {
2327 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002328
2329 my $linenr=0;
2330 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002331 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002332 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002333 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002334
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002335 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002336 my $indent;
2337 my $previndent=0;
2338 my $stashindent=0;
2339
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002340 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002341 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002342 my $author = '';
2343 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002344 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002345 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002346 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002347 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002348 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002349 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002350 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002351 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002352 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002353 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002354 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002355 my $non_utf8_charset = 0;
2356
Joe Perches365dd4e2014-08-06 16:10:42 -07002357 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002358 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002359
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002360 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002361 our $cnt_lines = 0;
2362 our $cnt_error = 0;
2363 our $cnt_warn = 0;
2364 our $cnt_chk = 0;
2365
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002366 # Trace the real file/line as we go.
2367 my $realfile = '';
2368 my $realline = 0;
2369 my $realcnt = 0;
2370 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002371 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002372 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002373 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002374 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002375 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002376
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002377 my $prev_values = 'E';
2378
2379 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002380 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002381 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002382 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002383 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002384
Joe Perches7e51f192013-09-11 14:23:57 -07002385 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002386
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002387 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002388 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002389 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002390 my @setup_docs = ();
2391 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002392
Joe Perchesd8b07712013-11-12 15:10:06 -08002393 my $camelcase_file_seeded = 0;
2394
Rob Herring9f3a8992018-04-10 16:33:13 -07002395 my $checklicenseline = 1;
2396
Andy Whitcroft773647a2008-03-28 14:15:58 -07002397 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002398 my $line;
2399 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002400 $linenr++;
2401 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002402
Joe Perches3705ce52013-07-03 15:05:31 -07002403 push(@fixed, $rawline) if ($fix);
2404
Andy Whitcroft773647a2008-03-28 14:15:58 -07002405 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002406 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002407 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002408 $setup_docs = 1;
2409 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002410 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002411 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002412 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002413 $realline=$1-1;
2414 if (defined $2) {
2415 $realcnt=$3+1;
2416 } else {
2417 $realcnt=1+1;
2418 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002419 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002420
2421 # Guestimate if this is a continuing comment. Run
2422 # the context looking for a comment "edge". If this
2423 # edge is a close comment then we must be in a comment
2424 # at context start.
2425 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002426 my $cnt = $realcnt;
2427 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2428 next if (defined $rawlines[$ln - 1] &&
2429 $rawlines[$ln - 1] =~ /^-/);
2430 $cnt--;
2431 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002432 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002433 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2434 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2435 ($edge) = $1;
2436 last;
2437 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002438 }
2439 if (defined $edge && $edge eq '*/') {
2440 $in_comment = 1;
2441 }
2442
2443 # Guestimate if this is a continuing comment. If this
2444 # is the start of a diff block and this line starts
2445 # ' *' then it is very likely a comment.
2446 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002447 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002448 {
2449 $in_comment = 1;
2450 }
2451
2452 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2453 sanitise_line_reset($in_comment);
2454
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002455 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002456 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002457 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002458 $line = sanitise_line($rawline);
2459 }
2460 push(@lines, $line);
2461
2462 if ($realcnt > 1) {
2463 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2464 } else {
2465 $realcnt = 0;
2466 }
2467
2468 #print "==>$rawline\n";
2469 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002470
2471 if ($setup_docs && $line =~ /^\+/) {
2472 push(@setup_docs, $line);
2473 }
2474 }
2475
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002476 $prefix = '';
2477
Andy Whitcroft773647a2008-03-28 14:15:58 -07002478 $realcnt = 0;
2479 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002480 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002481 foreach my $line (@lines) {
2482 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002483 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002484 my $sline = $line; #copy of $line
2485 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002486
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002487 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002488 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002489
Joe Perches12c253a2018-06-07 17:10:58 -07002490# check if it's a mode change, rename or start of a patch
2491 if (!$in_commit_log &&
2492 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2493 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2494 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2495 $is_patch = 1;
2496 }
2497
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002498#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002499 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002500 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2501 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002502 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002503 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002504 $realline=$1-1;
2505 if (defined $2) {
2506 $realcnt=$3+1;
2507 } else {
2508 $realcnt=1+1;
2509 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002510 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002511 $prev_values = 'E';
2512
Andy Whitcroft773647a2008-03-28 14:15:58 -07002513 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002514 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002515 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002516 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002517 if ($context =~ /\b(\w+)\s*\(/) {
2518 $context_function = $1;
2519 } else {
2520 undef $context_function;
2521 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002522 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002523
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002524# track the line number as we move through the hunk, note that
2525# new versions of GNU diff omit the leading space on completely
2526# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002527 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002528 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002529 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002530
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002531 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002532 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002533
2534 # Track the previous line.
2535 ($prevline, $stashline) = ($stashline, $line);
2536 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002537 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2538
Andy Whitcroft773647a2008-03-28 14:15:58 -07002539 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002540
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002541 } elsif ($realcnt == 1) {
2542 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002543 }
2544
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002545 my $hunk_line = ($realcnt != 0);
2546
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002547 $here = "#$linenr: " if (!$file);
2548 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002549
Joe Perches2ac73b42014-06-04 16:12:05 -07002550 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002551 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002552 if ($line =~ /^diff --git.*?(\S+)$/) {
2553 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002554 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002555 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002556 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002557 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002558 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002559 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002560 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002561
2562 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002563 if (!$file && $tree && $p1_prefix ne '' &&
2564 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002565 WARN("PATCH_PREFIX",
2566 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002567 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002568
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002569 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002570 ERROR("MODIFIED_INCLUDE_ASM",
2571 "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 -07002572 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002573 $found_file = 1;
2574 }
2575
Joe Perches34d88152015-06-25 15:03:05 -07002576#make up the handle for any error we report on this line
2577 if ($showfile) {
2578 $prefix = "$realfile:$realline: "
2579 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002580 if ($file) {
2581 $prefix = "$filename:$realline: ";
2582 } else {
2583 $prefix = "$filename:$linenr: ";
2584 }
Joe Perches34d88152015-06-25 15:03:05 -07002585 }
2586
Joe Perches2ac73b42014-06-04 16:12:05 -07002587 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002588 if (is_maintained_obsolete($realfile)) {
2589 WARN("OBSOLETE",
2590 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2591 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002592 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002593 $check = 1;
2594 } else {
2595 $check = $check_orig;
2596 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002597 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002598
2599 if ($realfile !~ /^MAINTAINERS/) {
2600 my $last_binding_patch = $is_binding_patch;
2601
2602 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2603
2604 if (($last_binding_patch != -1) &&
2605 ($last_binding_patch ^ $is_binding_patch)) {
2606 WARN("DT_SPLIT_BINDING_PATCH",
2607 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2608 }
2609 }
2610
Andy Whitcroft773647a2008-03-28 14:15:58 -07002611 next;
2612 }
2613
Randy Dunlap389834b2007-06-08 13:47:03 -07002614 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002615
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002616 my $hereline = "$here\n$rawline\n";
2617 my $herecurr = "$here\n$rawline\n";
2618 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002619
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002620 $cnt_lines++ if ($realcnt != 0);
2621
Joe Perches490b2922018-08-21 21:58:01 -07002622# Verify the existence of a commit log if appropriate
2623# 2 is used because a $signature is counted in $commit_log_lines
2624 if ($in_commit_log) {
2625 if ($line !~ /^\s*$/) {
2626 $commit_log_lines++; #could be a $signature
2627 }
2628 } elsif ($has_commit_log && $commit_log_lines < 2) {
2629 WARN("COMMIT_MESSAGE",
2630 "Missing commit description - Add an appropriate one\n");
2631 $commit_log_lines = 2; #warn only once
2632 }
2633
Joe Perchese518e9a2015-06-25 15:03:27 -07002634# Check if the commit log has what seems like a diff which can confuse patch
2635 if ($in_commit_log && !$commit_log_has_diff &&
2636 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2637 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2638 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2639 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2640 ERROR("DIFF_IN_COMMIT_MSG",
2641 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2642 $commit_log_has_diff = 1;
2643 }
2644
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002645# Check for incorrect file permissions
2646 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2647 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002648 if ($realfile !~ m@scripts/@ &&
2649 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002650 ERROR("EXECUTE_PERMISSIONS",
2651 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002652 }
2653 }
2654
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002655# Check the patch for a From:
2656 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2657 $author = $1;
2658 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2659 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002660 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002661 }
2662
Joe Perches20112472011-07-25 17:13:23 -07002663# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002664 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002665 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002666 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002667 if ($author ne '') {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002668 if (same_email_addresses($1, $author)) {
2669 $authorsignoff = 1;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002670 }
2671 }
Joe Perches20112472011-07-25 17:13:23 -07002672 }
2673
Joe Perches44d303e2020-04-06 20:11:10 -07002674# Check for patch separator
2675 if ($line =~ /^---$/) {
2676 $has_patch_separator = 1;
2677 $in_commit_log = 0;
2678 }
2679
Joe Perchese0d975b2014-12-10 15:51:49 -08002680# Check if MAINTAINERS is being updated. If so, there's probably no need to
2681# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2682 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2683 $reported_maintainer_file = 1;
2684 }
2685
Joe Perches20112472011-07-25 17:13:23 -07002686# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002687 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002688 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002689 my $space_before = $1;
2690 my $sign_off = $2;
2691 my $space_after = $3;
2692 my $email = $4;
2693 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2694
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002695 if ($sign_off !~ /$signature_tags/) {
2696 WARN("BAD_SIGN_OFF",
2697 "Non-standard signature: $sign_off\n" . $herecurr);
2698 }
Joe Perches20112472011-07-25 17:13:23 -07002699 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002700 if (WARN("BAD_SIGN_OFF",
2701 "Do not use whitespace before $ucfirst_sign_off\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 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002706 }
Joe Perches20112472011-07-25 17:13:23 -07002707 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002708 if (WARN("BAD_SIGN_OFF",
2709 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2710 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002711 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002712 "$ucfirst_sign_off $email";
2713 }
2714
Joe Perches20112472011-07-25 17:13:23 -07002715 }
2716 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002717 if (WARN("BAD_SIGN_OFF",
2718 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2719 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002720 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002721 "$ucfirst_sign_off $email";
2722 }
Joe Perches20112472011-07-25 17:13:23 -07002723 }
2724
Joe Perchesdfa05c22020-04-06 20:10:48 -07002725 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Joe Perches20112472011-07-25 17:13:23 -07002726 my $suggested_email = format_email(($email_name, $email_address));
2727 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002728 ERROR("BAD_SIGN_OFF",
2729 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002730 } else {
2731 my $dequoted = $suggested_email;
2732 $dequoted =~ s/^"//;
2733 $dequoted =~ s/" </ </;
2734 # Don't force email to have quotes
2735 # Allow just an angle bracketed address
Joe Perchesdfa05c22020-04-06 20:10:48 -07002736 if (!same_email_addresses($email, $suggested_email)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002737 WARN("BAD_SIGN_OFF",
2738 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002739 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002740 }
Joe Perches7e51f192013-09-11 14:23:57 -07002741
2742# Check for duplicate signatures
2743 my $sig_nospace = $line;
2744 $sig_nospace =~ s/\s//g;
2745 $sig_nospace = lc($sig_nospace);
2746 if (defined $signatures{$sig_nospace}) {
2747 WARN("BAD_SIGN_OFF",
2748 "Duplicate signature\n" . $herecurr);
2749 } else {
2750 $signatures{$sig_nospace} = 1;
2751 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002752
2753# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2754 if ($sign_off =~ /^co-developed-by:$/i) {
2755 if ($email eq $author) {
2756 WARN("BAD_SIGN_OFF",
2757 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2758 }
2759 if (!defined $lines[$linenr]) {
2760 WARN("BAD_SIGN_OFF",
2761 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2762 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2763 WARN("BAD_SIGN_OFF",
2764 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2765 } elsif ($1 ne $email) {
2766 WARN("BAD_SIGN_OFF",
2767 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2768 }
2769 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002770 }
2771
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002772# Check email subject for common tools that don't need to be mentioned
2773 if ($in_header_lines &&
2774 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2775 WARN("EMAIL_SUBJECT",
2776 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2777 }
2778
Joe Perches44d303e2020-04-06 20:11:10 -07002779# Check for Gerrit Change-Ids not in any patch context
2780 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002781 ERROR("GERRIT_CHANGE_ID",
Joe Perches44d303e2020-04-06 20:11:10 -07002782 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002783 }
2784
Joe Perches369c8dd2015-11-06 16:31:34 -08002785# Check if the commit log is in a possible stack dump
2786 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2787 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2788 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2789 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07002790 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2791 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2792 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2793 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08002794 $commit_log_possible_stack_dump = 1;
2795 }
2796
Joe Perches2a076f42015-04-16 12:44:28 -07002797# Check for line lengths > 75 in commit log, warn once
2798 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002799 length($line) > 75 &&
2800 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2801 # file delta changes
2802 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2803 # filename then :
2804 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2805 # A Fixes: or Link: line
2806 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002807 WARN("COMMIT_LOG_LONG_LINE",
2808 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2809 $commit_log_long_line = 1;
2810 }
2811
Joe Perchesbf4daf12015-09-09 15:37:50 -07002812# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002813 if ($in_commit_log && $commit_log_possible_stack_dump &&
2814 $line =~ /^\s*$/) {
2815 $commit_log_possible_stack_dump = 0;
2816 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002817
Joe Perches0d7835f2015-02-13 14:38:35 -08002818# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002819 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07002820 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002821 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002822 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002823 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002824 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2825 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002826 my $init_char = "c";
2827 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002828 my $short = 1;
2829 my $long = 0;
2830 my $case = 1;
2831 my $space = 1;
2832 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002833 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002834 my $id = '0123456789ab';
2835 my $orig_desc = "commit description";
2836 my $description = "";
2837
Joe Perchesfe043ea2015-09-09 15:37:25 -07002838 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2839 $init_char = $1;
2840 $orig_commit = lc($2);
2841 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2842 $orig_commit = lc($1);
2843 }
2844
Joe Perches0d7835f2015-02-13 14:38:35 -08002845 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2846 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2847 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2848 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2849 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2850 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002851 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002852 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2853 defined $rawlines[$linenr] &&
2854 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2855 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002856 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002857 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2858 defined $rawlines[$linenr] &&
2859 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2860 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2861 $orig_desc = $1;
2862 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2863 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002864 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002865 }
2866
2867 ($id, $description) = git_commit_info($orig_commit,
2868 $id, $orig_desc);
2869
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002870 if (defined($id) &&
2871 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002872 ERROR("GIT_COMMIT_ID",
2873 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2874 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002875 }
2876
Joe Perches13f19372014-08-06 16:10:59 -07002877# Check for added, moved or deleted files
2878 if (!$reported_maintainer_file && !$in_commit_log &&
2879 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2880 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2881 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2882 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002883 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002884 $reported_maintainer_file = 1;
2885 WARN("FILE_PATH_CHANGES",
2886 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2887 }
2888
Rob Herringe400edb2019-09-12 15:18:20 +01002889# Check for adding new DT bindings not in schema format
2890 if (!$in_commit_log &&
2891 ($line =~ /^new file mode\s*\d+\s*$/) &&
2892 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2893 WARN("DT_SCHEMA_BINDING_PATCH",
2894 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2895 }
2896
Andy Whitcroft00df3442007-06-08 13:47:06 -07002897# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002898 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002899 ERROR("CORRUPTED_PATCH",
2900 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002901 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002902 }
2903
2904# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2905 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002906 $rawline !~ m/^$UTF8*$/) {
2907 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2908
2909 my $blank = copy_spacing($rawline);
2910 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2911 my $hereptr = "$hereline$ptr\n";
2912
Joe Perches34d99212011-07-25 17:13:26 -07002913 CHK("INVALID_UTF8",
2914 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002915 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002916
Joe Perches15662b32011-10-31 17:13:12 -07002917# Check if it's the start of a commit log
2918# (not a header line and we haven't seen the patch filename)
2919 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002920 !($rawline =~ /^\s+(?:\S|$)/ ||
2921 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002922 $in_header_lines = 0;
2923 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002924 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002925 }
2926
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002927# Check if there is UTF-8 in a commit log when a mail header has explicitly
2928# declined it, i.e defined some charset where it is missing.
2929 if ($in_header_lines &&
2930 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2931 $1 !~ /utf-8/i) {
2932 $non_utf8_charset = 1;
2933 }
2934
2935 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002936 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002937 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002938 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2939 }
2940
Joe Perchesd6430f72016-12-12 16:46:28 -08002941# Check for absolute kernel paths in commit message
2942 if ($tree && $in_commit_log) {
2943 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2944 my $file = $1;
2945
2946 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2947 check_absolute_file($1, $herecurr)) {
2948 #
2949 } else {
2950 check_absolute_file($file, $herecurr);
2951 }
2952 }
2953 }
2954
Kees Cook66b47b42014-10-13 15:51:57 -07002955# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002956 if (defined($misspellings) &&
2957 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002958 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002959 my $typo = $1;
2960 my $typo_fix = $spelling_fix{lc($typo)};
2961 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2962 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002963 my $msg_level = \&WARN;
2964 $msg_level = \&CHK if ($file);
2965 if (&{$msg_level}("TYPO_SPELLING",
2966 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002967 $fix) {
2968 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2969 }
2970 }
2971 }
2972
Matteo Crocea8dd86b2019-09-25 16:46:38 -07002973# check for invalid commit id
2974 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2975 my $id;
2976 my $description;
2977 ($id, $description) = git_commit_info($2, undef, undef);
2978 if (!defined($id)) {
2979 WARN("UNKNOWN_COMMIT_ID",
2980 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2981 }
2982 }
2983
Andy Whitcroft306708542008-10-15 22:02:28 -07002984# ignore non-hunk lines and lines being removed
2985 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002986
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002987#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002988 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002989 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002990 if (ERROR("DOS_LINE_ENDINGS",
2991 "DOS line endings\n" . $herevet) &&
2992 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002993 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002994 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002995 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2996 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002997 if (ERROR("TRAILING_WHITESPACE",
2998 "trailing whitespace\n" . $herevet) &&
2999 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003000 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003001 }
3002
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003003 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003004 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07003005
Josh Triplett4783f892013-11-12 15:10:12 -08003006# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003007 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003008 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003009 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3010 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003011 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003012 my $msg_level = \&ERROR;
3013 $msg_level = \&CHK if ($file);
3014 &{$msg_level}("FSF_MAILING_ADDRESS",
3015 "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 -08003016 }
3017
Andi Kleen33549572010-05-24 14:33:29 -07003018# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003019# Only applies when adding the entry originally, after that we do not have
3020# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003021 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003022 # 'choice' is usually the last thing on the line (though
3023 # Kconfig supports named choices), so use a word boundary
3024 # (\b) rather than a whitespace character (\s)
3025 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003026 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003027 my $cnt = $realcnt;
3028 my $ln = $linenr + 1;
3029 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003030 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003031 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003032 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003033 $f = $lines[$ln - 1];
3034 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3035 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003036
3037 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003038 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003039
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003040 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003041 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01003042 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
3043 if ($lines[$ln - 1] =~ "---help---") {
3044 WARN("CONFIG_DESCRIPTION",
3045 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
3046 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003047 $length = -1;
3048 }
3049
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003050 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003051 $f =~ s/#.*//;
3052 $f =~ s/^\s+//;
3053 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003054
3055 # This only checks context lines in the patch
3056 # and so hopefully shouldn't trigger false
3057 # positives, even though some of these are
3058 # common words in help texts
3059 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3060 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003061 $is_end = 1;
3062 last;
3063 }
Andi Kleen33549572010-05-24 14:33:29 -07003064 $length++;
3065 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003066 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3067 WARN("CONFIG_DESCRIPTION",
3068 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3069 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003070 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003071 }
3072
Joe Perches7ccf41a2020-06-04 16:50:33 -07003073# check MAINTAINERS entries
3074 if ($realfile =~ /^MAINTAINERS$/) {
3075# check MAINTAINERS entries for the right form
3076 if ($rawline =~ /^\+[A-Z]:/ &&
3077 $rawline !~ /^\+[A-Z]:\t\S/) {
3078 if (WARN("MAINTAINERS_STYLE",
3079 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3080 $fix) {
3081 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3082 }
3083 }
3084# check MAINTAINERS entries for the right ordering too
3085 my $preferred_order = 'MRLSWQBCPTFXNK';
3086 if ($rawline =~ /^\+[A-Z]:/ &&
3087 $prevrawline =~ /^[\+ ][A-Z]:/) {
3088 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3089 my $cur = $1;
3090 my $curval = $2;
3091 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3092 my $prev = $1;
3093 my $prevval = $2;
3094 my $curindex = index($preferred_order, $cur);
3095 my $previndex = index($preferred_order, $prev);
3096 if ($curindex < 0) {
3097 WARN("MAINTAINERS_STYLE",
3098 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3099 } else {
3100 if ($previndex >= 0 && $curindex < $previndex) {
3101 WARN("MAINTAINERS_STYLE",
3102 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3103 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3104 ($prev eq 'X' && $cur eq 'X')) &&
3105 ($prevval cmp $curval) > 0) {
3106 WARN("MAINTAINERS_STYLE",
3107 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3108 }
3109 }
Joe Perches628f91a2017-07-10 15:52:07 -07003110 }
3111 }
3112
Christoph Jaeger327953e2015-02-13 14:38:29 -08003113# discourage the use of boolean for type definition attributes of Kconfig options
3114 if ($realfile =~ /Kconfig/ &&
3115 $line =~ /^\+\s*\bboolean\b/) {
3116 WARN("CONFIG_TYPE_BOOLEAN",
3117 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3118 }
3119
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003120 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3121 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3122 my $flag = $1;
3123 my $replacement = {
3124 'EXTRA_AFLAGS' => 'asflags-y',
3125 'EXTRA_CFLAGS' => 'ccflags-y',
3126 'EXTRA_CPPFLAGS' => 'cppflags-y',
3127 'EXTRA_LDFLAGS' => 'ldflags-y',
3128 };
3129
3130 WARN("DEPRECATED_VARIABLE",
3131 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3132 }
3133
Rob Herringbff5da42014-01-23 15:54:51 -08003134# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003135 if (defined $root &&
3136 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3137 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3138
Rob Herringbff5da42014-01-23 15:54:51 -08003139 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3140
Florian Vaussardcc933192014-04-03 14:49:27 -07003141 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003142 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003143
Rob Herringbff5da42014-01-23 15:54:51 -08003144 foreach my $compat (@compats) {
3145 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003146 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3147 my $compat3 = $compat;
3148 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3149 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003150 if ( $? >> 8 ) {
3151 WARN("UNDOCUMENTED_DT_STRING",
3152 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3153 }
3154
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003155 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3156 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003157 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003158 if ( $? >> 8 ) {
3159 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003160 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003161 }
3162 }
3163 }
3164
Rob Herring9f3a8992018-04-10 16:33:13 -07003165# check for using SPDX license tag at beginning of files
3166 if ($realline == $checklicenseline) {
3167 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3168 $checklicenseline = 2;
3169 } elsif ($rawline =~ /^\+/) {
3170 my $comment = "";
3171 if ($realfile =~ /\.(h|s|S)$/) {
3172 $comment = '/*';
3173 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3174 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003175 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003176 $comment = '#';
3177 } elsif ($realfile =~ /\.rst$/) {
3178 $comment = '..';
3179 }
3180
Joe Perchesfdf13692019-03-07 16:28:32 -08003181# check SPDX comment style for .[chsS] files
3182 if ($realfile =~ /\.[chsS]$/ &&
3183 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003184 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003185 WARN("SPDX_LICENSE_TAG",
3186 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3187 }
3188
Rob Herring9f3a8992018-04-10 16:33:13 -07003189 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003190 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3191 WARN("SPDX_LICENSE_TAG",
3192 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003193 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003194 my $spdx_license = $1;
3195 if (!is_SPDX_License_valid($spdx_license)) {
3196 WARN("SPDX_LICENSE_TAG",
3197 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3198 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003199 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3200 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3201 my $msg_level = \&WARN;
3202 $msg_level = \&CHK if ($file);
3203 if (&{$msg_level}("SPDX_LICENSE_TAG",
3204
3205 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3206 $fix) {
3207 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3208 }
3209 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003210 }
3211 }
3212 }
3213
Andy Whitcroft5368df202008-10-15 22:02:27 -07003214# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003215 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003216
Joe Perchesa8da38a2019-03-07 16:28:42 -08003217# check for using SPDX-License-Identifier on the wrong line number
3218 if ($realline != $checklicenseline &&
3219 $rawline =~ /\bSPDX-License-Identifier:/ &&
3220 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3221 WARN("SPDX_LICENSE_TAG",
3222 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3223 }
3224
Joe Perches47e0c882015-06-25 15:02:57 -07003225# line length limit (with some exclusions)
3226#
3227# There are a few types of lines that may extend beyond $max_line_length:
3228# logging functions like pr_info that end in a string
3229# lines with a single string
3230# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003231# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003232#
3233# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003234# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003235# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3236# LONG_LINE all other lines longer than $max_line_length
3237#
3238# if LONG_LINE is ignored, the other 2 types are also ignored
3239#
3240
Joe Perchesb4749e92015-07-17 16:24:01 -07003241 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003242 my $msg_type = "LONG_LINE";
3243
3244 # Check the allowed long line types first
3245
3246 # logging functions that end in a string that starts
3247 # before $max_line_length
3248 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3249 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3250 $msg_type = "";
3251
3252 # lines with only strings (w/ possible termination)
3253 # #defines with only strings
3254 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3255 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3256 $msg_type = "";
3257
Joe Perchescc147502017-11-17 15:28:44 -08003258 # More special cases
3259 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3260 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003261 $msg_type = "";
3262
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003263 # URL ($rawline is used in case the URL is in a comment)
3264 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3265 $msg_type = "";
3266
Joe Perches47e0c882015-06-25 15:02:57 -07003267 # Otherwise set the alternate message types
3268
3269 # a comment starts before $max_line_length
3270 } elsif ($line =~ /($;[\s$;]*)$/ &&
3271 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3272 $msg_type = "LONG_LINE_COMMENT"
3273
3274 # a quoted string starts before $max_line_length
3275 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3276 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3277 $msg_type = "LONG_LINE_STRING"
3278 }
3279
3280 if ($msg_type ne "" &&
3281 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003282 my $msg_level = \&WARN;
3283 $msg_level = \&CHK if ($file);
3284 &{$msg_level}($msg_type,
3285 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003286 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003287 }
3288
Andy Whitcroft8905a672007-11-28 16:21:06 -08003289# check for adding lines without a newline.
3290 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003291 WARN("MISSING_EOF_NEWLINE",
3292 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003293 }
3294
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003295# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003296 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003297
3298# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003299# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003300 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3301 $rawline =~ /^\+\s* \s*/) {
3302 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003303 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003304 if (ERROR("CODE_INDENT",
3305 "code indent should use tabs where possible\n" . $herevet) &&
3306 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003307 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003308 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003309 }
3310
Alberto Panizzo08e44362010-03-05 13:43:54 -08003311# check for space before tabs.
3312 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3313 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003314 if (WARN("SPACE_BEFORE_TAB",
3315 "please, no space before tabs\n" . $herevet) &&
3316 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003317 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003318 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003319 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003320 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003321 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003322 }
3323
Joe Perches6a487212018-04-10 16:34:04 -07003324# check for assignments on the start of a line
3325 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3326 CHK("ASSIGNMENT_CONTINUATIONS",
3327 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3328 }
3329
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003330# check for && or || at the start of a line
3331 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3332 CHK("LOGICAL_CONTINUATIONS",
3333 "Logical continuations should be on the previous line\n" . $hereprev);
3334 }
3335
Joe Perchesa91e8992016-05-20 17:04:05 -07003336# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003337 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003338 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003339 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003340 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003341 if (WARN("TABSTOP",
3342 "Statements should start on a tabstop\n" . $herecurr) &&
3343 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003344 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003345 }
3346 }
3347 }
3348
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003349# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003350 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003351 $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 -07003352 $prevline =~ /^\+(\t*)(.*)$/;
3353 my $oldindent = $1;
3354 my $rest = $2;
3355
3356 my $pos = pos_last_openparen($rest);
3357 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003358 $line =~ /^(\+| )([ \t]*)/;
3359 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003360
3361 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003362 "\t" x ($pos / $tabsize) .
3363 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003364 my $goodspaceindent = $oldindent . " " x $pos;
3365
3366 if ($newindent ne $goodtabindent &&
3367 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003368
3369 if (CHK("PARENTHESIS_ALIGNMENT",
3370 "Alignment should match open parenthesis\n" . $hereprev) &&
3371 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003372 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003373 s/^\+[ \t]*/\+$goodtabindent/;
3374 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003375 }
3376 }
3377 }
3378
Joe Perches6ab3a972015-04-16 12:44:05 -07003379# check for space after cast like "(int) foo" or "(struct foo) bar"
3380# avoid checking a few false positives:
3381# "sizeof(<type>)" or "__alignof__(<type>)"
3382# function pointer declarations like "(*foo)(int) = bar;"
3383# structure definitions like "(struct foo) { 0 };"
3384# multiline macros that define functions
3385# known attributes or the __attribute__ keyword
3386 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3387 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003388 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003389 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003390 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003391 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003392 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003393 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003394 }
3395
Joe Perches86406b12015-09-09 15:37:41 -07003396# Block comment styles
3397# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003398 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003399 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003400 $rawline =~ /^\+[ \t]*\*/ &&
3401 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003402 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3403 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3404 }
3405
Joe Perches86406b12015-09-09 15:37:41 -07003406# Block comments use * on subsequent lines
3407 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3408 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003409 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003410 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003411 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003412 WARN("BLOCK_COMMENT_STYLE",
3413 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003414 }
3415
Joe Perches86406b12015-09-09 15:37:41 -07003416# Block comments use */ on trailing lines
3417 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003418 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3419 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3420 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003421 WARN("BLOCK_COMMENT_STYLE",
3422 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003423 }
3424
Joe Perches08eb9b82016-10-11 13:51:50 -07003425# Block comment * alignment
3426 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003427 $line =~ /^\+[ \t]*$;/ && #leading comment
3428 $rawline =~ /^\+[ \t]*\*/ && #leading *
3429 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003430 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003431 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3432 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003433 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003434 if (defined($1)) {
3435 $oldindent = expand_tabs($1);
3436 } else {
3437 $prevrawline =~ m@^\+(.*/?)\*@;
3438 $oldindent = expand_tabs($1);
3439 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003440 $rawline =~ m@^\+([ \t]*)\*@;
3441 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003442 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003443 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003444 WARN("BLOCK_COMMENT_STYLE",
3445 "Block comments should align the * on each line\n" . $hereprev);
3446 }
3447 }
3448
Joe Perches7f619192014-08-06 16:10:39 -07003449# check for missing blank lines after struct/union declarations
3450# with exceptions for various attributes and macros
3451 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3452 $line =~ /^\+/ &&
3453 !($line =~ /^\+\s*$/ ||
3454 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3455 $line =~ /^\+\s*MODULE_/i ||
3456 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3457 $line =~ /^\+[a-z_]*init/ ||
3458 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3459 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003460 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003461 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003462 if (CHK("LINE_SPACING",
3463 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3464 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003465 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003466 }
Joe Perches7f619192014-08-06 16:10:39 -07003467 }
3468
Joe Perches365dd4e2014-08-06 16:10:42 -07003469# check for multiple consecutive blank lines
3470 if ($prevline =~ /^[\+ ]\s*$/ &&
3471 $line =~ /^\+\s*$/ &&
3472 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003473 if (CHK("LINE_SPACING",
3474 "Please don't use multiple blank lines\n" . $hereprev) &&
3475 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003476 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003477 }
3478
Joe Perches365dd4e2014-08-06 16:10:42 -07003479 $last_blank_line = $linenr;
3480 }
3481
Joe Perches3b617e32014-04-03 14:49:28 -07003482# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003483 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3484 # actual declarations
3485 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003486 # function pointer declarations
3487 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003488 # foo bar; where foo is some local typedef or #define
3489 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3490 # known declaration macros
3491 $prevline =~ /^\+\s+$declaration_macros/) &&
3492 # for "else if" which can look like "$Ident $Ident"
3493 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3494 # other possible extensions of declaration lines
3495 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3496 # not starting a section or a macro "\" extended line
3497 $prevline =~ /(?:\{\s*|\\)$/) &&
3498 # looks like a declaration
3499 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003500 # function pointer declarations
3501 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003502 # foo bar; where foo is some local typedef or #define
3503 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3504 # known declaration macros
3505 $sline =~ /^\+\s+$declaration_macros/ ||
3506 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003507 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003508 # start or end of block or continuation of declaration
3509 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3510 # bitfield continuation
3511 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3512 # other possible extensions of declaration lines
3513 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3514 # indentation of previous and current line are the same
3515 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003516 if (WARN("LINE_SPACING",
3517 "Missing a blank line after declarations\n" . $hereprev) &&
3518 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003519 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003520 }
Joe Perches3b617e32014-04-03 14:49:28 -07003521 }
3522
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003523# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003524# Exceptions:
3525# 1) within comments
3526# 2) indented preprocessor commands
3527# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003528 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003529 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003530 if (WARN("LEADING_SPACE",
3531 "please, no spaces at the start of a line\n" . $herevet) &&
3532 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003533 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003534 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003535 }
3536
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003537# check we are in a valid C source file if not then ignore this hunk
3538 next if ($realfile !~ /\.(h|c)$/);
3539
Joe Perches5751a242017-11-17 15:28:52 -08003540# check for unusual line ending [ or (
3541 if ($line =~ /^\+.*([\[\(])\s*$/) {
3542 CHK("OPEN_ENDED_LINE",
3543 "Lines should not end with a '$1'\n" . $herecurr);
3544 }
3545
Joe Perches4dbed762017-05-08 15:55:39 -07003546# check if this appears to be the start function declaration, save the name
3547 if ($sline =~ /^\+\{\s*$/ &&
3548 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3549 $context_function = $1;
3550 }
3551
3552# check if this appears to be the end of function declaration
3553 if ($sline =~ /^\+\}\s*$/) {
3554 undef $context_function;
3555 }
3556
Joe Perches032a4c02014-08-06 16:10:29 -07003557# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003558# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003559# if the previous line is a break or return and is indented 1 tab more...
3560 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3561 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003562 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3563 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3564 defined $lines[$linenr] &&
3565 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003566 WARN("UNNECESSARY_ELSE",
3567 "else is not generally useful after a break or return\n" . $hereprev);
3568 }
3569 }
3570
Joe Perchesc00df192014-08-06 16:11:01 -07003571# check indentation of a line with a break;
3572# if the previous line is a goto or return and is indented the same # of tabs
3573 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3574 my $tabs = $1;
3575 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3576 WARN("UNNECESSARY_BREAK",
3577 "break is not useful after a goto or return\n" . $hereprev);
3578 }
3579 }
3580
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003581# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003582 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003583 WARN("CVS_KEYWORD",
3584 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003585 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003586
Joe Perches56e77d72013-02-21 16:44:14 -08003587# check for old HOTPLUG __dev<foo> section markings
3588 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3589 WARN("HOTPLUG_SECTION",
3590 "Using $1 is unnecessary\n" . $herecurr);
3591 }
3592
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003593# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003594 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3595 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003596#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003597 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003598 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003599 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003600 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003601 $stat =~ s/\n./\n /g;
3602 $cond =~ s/\n./\n /g;
3603
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003604#print "linenr<$linenr> <$stat>\n";
3605 # If this statement has no statement boundaries within
3606 # it there is no point in retrying a statement scan
3607 # until we hit end of it.
3608 my $frag = $stat; $frag =~ s/;+\s*$//;
3609 if ($frag !~ /(?:{|;)/) {
3610#print "skip<$line_nr_next>\n";
3611 $suppress_statement = $line_nr_next;
3612 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003613
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003614 # Find the real next line.
3615 $realline_next = $line_nr_next;
3616 if (defined $realline_next &&
3617 (!defined $lines[$realline_next - 1] ||
3618 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3619 $realline_next++;
3620 }
3621
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003622 my $s = $stat;
3623 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003624
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003625 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003626 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003627
3628 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003629 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003630
Andy Whitcroft463f2862009-09-21 17:04:34 -07003631 } elsif ($s =~ /^.\s*else\b/s) {
3632
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003633 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003634 } 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 -07003635 my $type = $1;
3636 $type =~ s/\s+/ /g;
3637 possible($type, "A:" . $s);
3638
Andy Whitcroft8905a672007-11-28 16:21:06 -08003639 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003640 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003641 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003642 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003643
3644 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003645 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003646 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003647 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003648
3649 # Check for any sort of function declaration.
3650 # int foo(something bar, other baz);
3651 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003652 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 -08003653 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003654
Andy Whitcroftcf655042008-03-04 14:28:20 -08003655 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003656 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003657 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003658
Andy Whitcroft8905a672007-11-28 16:21:06 -08003659 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003660 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003661
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003662 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003663 }
3664 }
3665 }
3666
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003667 }
3668
Andy Whitcroft653d4872007-06-23 17:16:34 -07003669#
3670# Checks which may be anchored in the context.
3671#
3672
3673# Check for switch () and associated case and default
3674# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003675 if ($line=~/\bswitch\s*\(.*\)/) {
3676 my $err = '';
3677 my $sep = '';
3678 my @ctx = ctx_block_outer($linenr, $realcnt);
3679 shift(@ctx);
3680 for my $ctx (@ctx) {
3681 my ($clen, $cindent) = line_stats($ctx);
3682 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3683 $indent != $cindent) {
3684 $err .= "$sep$ctx\n";
3685 $sep = '';
3686 } else {
3687 $sep = "[...]\n";
3688 }
3689 }
3690 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003691 ERROR("SWITCH_CASE_INDENT_LEVEL",
3692 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003693 }
3694 }
3695
3696# if/while/etc brace do not go on next line, unless defining a do while loop,
3697# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003698 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 -07003699 my $pre_ctx = "$1$2";
3700
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003701 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003702
3703 if ($line =~ /^\+\t{6,}/) {
3704 WARN("DEEP_INDENTATION",
3705 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3706 }
3707
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003708 my $ctx_cnt = $realcnt - $#ctx - 1;
3709 my $ctx = join("\n", @ctx);
3710
Andy Whitcroft548596d2008-07-23 21:29:01 -07003711 my $ctx_ln = $linenr;
3712 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003713
Andy Whitcroft548596d2008-07-23 21:29:01 -07003714 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3715 defined $lines[$ctx_ln - 1] &&
3716 $lines[$ctx_ln - 1] =~ /^-/)) {
3717 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3718 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003719 $ctx_ln++;
3720 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003721
Andy Whitcroft53210162008-07-23 21:29:03 -07003722 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3723 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003724
Joe Perchesd752fcc2014-08-06 16:11:05 -07003725 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003726 ERROR("OPEN_BRACE",
3727 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003728 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003729 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003730 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3731 $ctx =~ /\)\s*\;\s*$/ &&
3732 defined $lines[$ctx_ln - 1])
3733 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003734 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3735 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003736 WARN("TRAILING_SEMICOLON",
3737 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003738 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003739 }
3740 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003741 }
3742
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003743# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003744 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 -08003745 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3746 ctx_statement_block($linenr, $realcnt, 0)
3747 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003748 my ($s, $c) = ($stat, $cond);
3749
3750 substr($s, 0, length($c), '');
3751
Joe Perches9f5af482015-09-09 15:37:30 -07003752 # remove inline comments
3753 $s =~ s/$;/ /g;
3754 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003755
3756 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003757 my @newlines = ($c =~ /\n/gs);
3758 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003759
Joe Perches9f5af482015-09-09 15:37:30 -07003760 # Make sure we remove the line prefixes as we have
3761 # none on the first line, and are going to readd them
3762 # where necessary.
3763 $s =~ s/\n./\n/gs;
3764 while ($s =~ /\n\s+\\\n/) {
3765 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3766 }
3767
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003768 # We want to check the first line inside the block
3769 # starting at the end of the conditional, so remove:
3770 # 1) any blank line termination
3771 # 2) any opening brace { on end of the line
3772 # 3) any do (...) {
3773 my $continuation = 0;
3774 my $check = 0;
3775 $s =~ s/^.*\bdo\b//;
3776 $s =~ s/^\s*{//;
3777 if ($s =~ s/^\s*\\//) {
3778 $continuation = 1;
3779 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003780 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003781 $check = 1;
3782 $cond_lines++;
3783 }
3784
3785 # Also ignore a loop construct at the end of a
3786 # preprocessor statement.
3787 if (($prevline =~ /^.\s*#\s*define\s/ ||
3788 $prevline =~ /\\\s*$/) && $continuation == 0) {
3789 $check = 0;
3790 }
3791
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003792 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003793 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003794 while ($cond_ptr != $cond_lines) {
3795 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003796
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003797 # If we see an #else/#elif then the code
3798 # is not linear.
3799 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3800 $check = 0;
3801 }
3802
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003803 # Ignore:
3804 # 1) blank lines, they should be at 0,
3805 # 2) preprocessor lines, and
3806 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003807 if ($continuation ||
3808 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003809 $s =~ /^\s*#\s*?/ ||
3810 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003811 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003812 if ($s =~ s/^.*?\n//) {
3813 $cond_lines++;
3814 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003815 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003816 }
3817
3818 my (undef, $sindent) = line_stats("+" . $s);
3819 my $stat_real = raw_line($linenr, $cond_lines);
3820
3821 # Check if either of these lines are modified, else
3822 # this is not this patch's fault.
3823 if (!defined($stat_real) ||
3824 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3825 $check = 0;
3826 }
3827 if (defined($stat_real) && $cond_lines > 1) {
3828 $stat_real = "[...]\n$stat_real";
3829 }
3830
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003831 #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 -07003832
Joe Perches9f5af482015-09-09 15:37:30 -07003833 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07003834 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07003835 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003836 ($sindent == $indent &&
3837 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07003838 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003839 WARN("SUSPECT_CODE_INDENT",
3840 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003841 }
3842 }
3843
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003844 # Track the 'values' across context and added lines.
3845 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003846 my ($curr_values, $curr_vars) =
3847 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003848 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003849 if ($dbg_values) {
3850 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003851 print "$linenr > .$outline\n";
3852 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003853 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003854 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003855 $prev_values = substr($curr_values, -1);
3856
Andy Whitcroft00df3442007-06-08 13:47:06 -07003857#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003858 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003859
Joe Perches11ca40a2016-12-12 16:46:31 -08003860# check for dereferences that span multiple lines
3861 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3862 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3863 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3864 my $ref = $1;
3865 $line =~ /^.\s*($Lval)/;
3866 $ref .= $1;
3867 $ref =~ s/\s//g;
3868 WARN("MULTILINE_DEREFERENCE",
3869 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3870 }
3871
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003872# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003873 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 -07003874 my $type = $1;
3875 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003876 $var = "" if (!defined $var);
3877 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003878 my $sign = $1;
3879 my $pointer = $2;
3880
3881 $pointer = "" if (!defined $pointer);
3882
3883 if (WARN("UNSPECIFIED_INT",
3884 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3885 $fix) {
3886 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003887 my $comp_pointer = $pointer;
3888 $comp_pointer =~ s/\s//g;
3889 $decl .= $comp_pointer;
3890 $decl = rtrim($decl) if ($var eq "");
3891 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003892 }
3893 }
3894 }
3895
Andy Whitcroft653d4872007-06-23 17:16:34 -07003896# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003897 if ($dbg_type) {
3898 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003899 ERROR("TEST_TYPE",
3900 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003901 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003902 ERROR("TEST_NOT_TYPE",
3903 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003904 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003905 next;
3906 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003907# TEST: allow direct testing of the attribute matcher.
3908 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003909 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003910 ERROR("TEST_ATTR",
3911 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003912 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003913 ERROR("TEST_NOT_ATTR",
3914 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003915 }
3916 next;
3917 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003918
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003919# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003920 if ($line =~ /^.\s*{/ &&
3921 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003922 if (ERROR("OPEN_BRACE",
3923 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003924 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3925 fix_delete_line($fixlinenr - 1, $prevrawline);
3926 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003927 my $fixedline = $prevrawline;
3928 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003929 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003930 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003931 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003932 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003933 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003934 }
3935
Andy Whitcroft653d4872007-06-23 17:16:34 -07003936#
3937# Checks which are anchored on the added line.
3938#
3939
3940# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003941 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003942 my $path = $1;
3943 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003944 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003945 "malformed #include filename\n" . $herecurr);
3946 }
3947 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3948 ERROR("UAPI_INCLUDE",
3949 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003950 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003951 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003952
3953# no C99 // comments
3954 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003955 if (ERROR("C99_COMMENTS",
3956 "do not use C99 // comments\n" . $herecurr) &&
3957 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003958 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003959 if ($line =~ /\/\/(.*)$/) {
3960 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003961 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003962 }
3963 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003964 }
3965 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003966 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003967 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003968
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003969# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3970# the whole statement.
3971#print "APW <$lines[$realline_next - 1]>\n";
3972 if (defined $realline_next &&
3973 exists $lines[$realline_next - 1] &&
3974 !defined $suppress_export{$realline_next} &&
3975 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3976 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003977 # Handle definitions which produce identifiers with
3978 # a prefix:
3979 # XXX(foo);
3980 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003981 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003982 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003983 $name =~ /^${Ident}_$2/) {
3984#print "FOO C name<$name>\n";
3985 $suppress_export{$realline_next} = 1;
3986
3987 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003988 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003989 ^.DEFINE_$Ident\(\Q$name\E\)|
3990 ^.DECLARE_$Ident\(\Q$name\E\)|
3991 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003992 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3993 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003994 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003995#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3996 $suppress_export{$realline_next} = 2;
3997 } else {
3998 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003999 }
4000 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004001 if (!defined $suppress_export{$linenr} &&
4002 $prevline =~ /^.\s*$/ &&
4003 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4004 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4005#print "FOO B <$lines[$linenr - 1]>\n";
4006 $suppress_export{$linenr} = 2;
4007 }
4008 if (defined $suppress_export{$linenr} &&
4009 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004010 WARN("EXPORT_SYMBOL",
4011 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004012 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004013
Joe Eloff5150bda2010-08-09 17:21:00 -07004014# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004015 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004016 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004017 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004018 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004019 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004020 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004021 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004022# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004023 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004024 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004025 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004026 $herecurr) &&
4027 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004028 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004029 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004030 }
4031
Joe Perches18130872014-08-06 16:11:22 -07004032# check for misordered declarations of char/short/int/long with signed/unsigned
4033 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4034 my $tmp = trim($1);
4035 WARN("MISORDERED_TYPE",
4036 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4037 }
4038
Joe Perches809e0822018-08-21 21:58:12 -07004039# check for unnecessary <signed> int declarations of short/long/long long
4040 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4041 my $type = trim($1);
4042 next if ($type !~ /\bint\b/);
4043 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4044 my $new_type = $type;
4045 $new_type =~ s/\b\s*int\s*\b/ /;
4046 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4047 $new_type =~ s/^const\s+//;
4048 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4049 $new_type = "const $new_type" if ($type =~ /^const\b/);
4050 $new_type =~ s/\s+/ /g;
4051 $new_type = trim($new_type);
4052 if (WARN("UNNECESSARY_INT",
4053 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4054 $fix) {
4055 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4056 }
4057 }
4058
Joe Perchescb710ec2010-10-26 14:23:20 -07004059# check for static const char * arrays.
4060 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004061 WARN("STATIC_CONST_CHAR_ARRAY",
4062 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004063 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004064 }
4065
4066# check for initialized const char arrays that should be static const
4067 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4068 if (WARN("STATIC_CONST_CHAR_ARRAY",
4069 "const array should probably be static const\n" . $herecurr) &&
4070 $fix) {
4071 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4072 }
4073 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004074
4075# check for static char foo[] = "bar" declarations.
4076 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004077 WARN("STATIC_CONST_CHAR_ARRAY",
4078 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004079 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004080 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004081
Joe Perchesab7e23f2015-04-16 12:44:22 -07004082# check for const <foo> const where <foo> is not a pointer or array type
4083 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4084 my $found = $1;
4085 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4086 WARN("CONST_CONST",
4087 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4088 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4089 WARN("CONST_CONST",
4090 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4091 }
4092 }
4093
Joe Perches9b0fa602014-04-03 14:49:18 -07004094# check for non-global char *foo[] = {"bar", ...} declarations.
4095 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4096 WARN("STATIC_CONST_CHAR_ARRAY",
4097 "char * array declaration might be better as static const\n" .
4098 $herecurr);
4099 }
4100
Joe Perchesb598b672015-04-16 12:44:36 -07004101# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4102 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4103 my $array = $1;
4104 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4105 my $array_div = $1;
4106 if (WARN("ARRAY_SIZE",
4107 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4108 $fix) {
4109 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4110 }
4111 }
4112 }
4113
Joe Perchesb36190c2014-01-27 17:07:18 -08004114# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004115 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004116 if (ERROR("FUNCTION_WITHOUT_ARGS",
4117 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4118 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004119 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004120 }
4121 }
4122
Andy Whitcroft653d4872007-06-23 17:16:34 -07004123# check for new typedefs, only function parameters and sparse annotations
4124# make sense.
4125 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004126 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004127 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004128 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004129 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004130 WARN("NEW_TYPEDEFS",
4131 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004132 }
4133
4134# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004135 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004136 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4137 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004138 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004139
Andy Whitcroft65863862009-01-06 14:41:21 -08004140 # Should start with a space.
4141 $to =~ s/^(\S)/ $1/;
4142 # Should not end with a space.
4143 $to =~ s/\s+$//;
4144 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004145 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004146 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004147
Joe Perches3705ce52013-07-03 15:05:31 -07004148## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004149 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004150 if (ERROR("POINTER_LOCATION",
4151 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4152 $fix) {
4153 my $sub_from = $ident;
4154 my $sub_to = $ident;
4155 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004156 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004157 s@\Q$sub_from\E@$sub_to@;
4158 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004159 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004160 }
4161 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4162 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004163 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004164
Andy Whitcroft65863862009-01-06 14:41:21 -08004165 # Should start with a space.
4166 $to =~ s/^(\S)/ $1/;
4167 # Should not end with a space.
4168 $to =~ s/\s+$//;
4169 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004170 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004171 }
4172 # Modifiers should have spaces.
4173 $to =~ s/(\b$Modifier$)/$1 /;
4174
Joe Perches3705ce52013-07-03 15:05:31 -07004175## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004176 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004177 if (ERROR("POINTER_LOCATION",
4178 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4179 $fix) {
4180
4181 my $sub_from = $match;
4182 my $sub_to = $match;
4183 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004184 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004185 s@\Q$sub_from\E@$sub_to@;
4186 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004187 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004188 }
4189
Joe Perches9d3e3c72015-09-09 15:37:27 -07004190# avoid BUG() or BUG_ON()
4191 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004192 my $msg_level = \&WARN;
4193 $msg_level = \&CHK if ($file);
4194 &{$msg_level}("AVOID_BUG",
4195 "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 -07004196 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004197
Joe Perches9d3e3c72015-09-09 15:37:27 -07004198# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004199 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004200 WARN("LINUX_VERSION_CODE",
4201 "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 -08004202 }
4203
Joe Perches17441222011-06-15 15:08:17 -07004204# check for uses of printk_ratelimit
4205 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004206 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004207 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004208 }
4209
Joe Percheseeef5732017-11-17 15:28:41 -08004210# printk should use KERN_* levels
4211 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4212 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4213 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004214 }
4215
Joe Perches243f3802012-05-31 16:26:09 -07004216 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4217 my $orig = $1;
4218 my $level = lc($orig);
4219 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004220 my $level2 = $level;
4221 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004222 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004223 "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 -07004224 }
4225
Joe Perchesdc139312013-02-21 16:44:13 -08004226 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4227 my $orig = $1;
4228 my $level = lc($orig);
4229 $level = "warn" if ($level eq "warning");
4230 $level = "dbg" if ($level eq "debug");
4231 WARN("PREFER_DEV_LEVEL",
4232 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4233 }
4234
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004235# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4236# number of false positives, but assembly files are not checked, so at
4237# least the arch entry code will not trigger this warning.
4238 if ($line =~ /\bENOSYS\b/) {
4239 WARN("ENOSYS",
4240 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4241 }
4242
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004243# ENOTSUPP is not a standard error code and should be avoided in new patches.
4244# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4245# Similarly to ENOSYS warning a small number of false positives is expected.
4246 if (!$file && $line =~ /\bENOTSUPP\b/) {
4247 if (WARN("ENOTSUPP",
4248 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4249 $fix) {
4250 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4251 }
4252 }
4253
Andy Whitcroft653d4872007-06-23 17:16:34 -07004254# function brace can't be on same line, except for #defines of do while,
4255# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004256 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004257 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4258 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4259 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004260 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004261 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004262 $fix) {
4263 fix_delete_line($fixlinenr, $rawline);
4264 my $fixed_line = $rawline;
4265 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4266 my $line1 = $1;
4267 my $line2 = $2;
4268 fix_insert_line($fixlinenr, ltrim($line1));
4269 fix_insert_line($fixlinenr, "\+{");
4270 if ($line2 !~ /^\s*$/) {
4271 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4272 }
4273 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004274 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004275
Andy Whitcroft8905a672007-11-28 16:21:06 -08004276# open braces for enum, union and struct go on the same line.
4277 if ($line =~ /^.\s*{/ &&
4278 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004279 if (ERROR("OPEN_BRACE",
4280 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4281 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4282 fix_delete_line($fixlinenr - 1, $prevrawline);
4283 fix_delete_line($fixlinenr, $rawline);
4284 my $fixedline = rtrim($prevrawline) . " {";
4285 fix_insert_line($fixlinenr, $fixedline);
4286 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004287 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004288 if ($fixedline !~ /^\+\s*$/) {
4289 fix_insert_line($fixlinenr, $fixedline);
4290 }
4291 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004292 }
4293
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004294# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004295 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4296 if (WARN("SPACING",
4297 "missing space after $1 definition\n" . $herecurr) &&
4298 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004299 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004300 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4301 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004302 }
4303
Joe Perches31070b52014-01-23 15:54:49 -08004304# Function pointer declarations
4305# check spacing between type, funcptr, and args
4306# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004307 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004308 my $declare = $1;
4309 my $pre_pointer_space = $2;
4310 my $post_pointer_space = $3;
4311 my $funcname = $4;
4312 my $post_funcname_space = $5;
4313 my $pre_args_space = $6;
4314
Joe Perches91f72e92014-04-03 14:49:12 -07004315# the $Declare variable will capture all spaces after the type
4316# so check it for a missing trailing missing space but pointer return types
4317# don't need a space so don't warn for those.
4318 my $post_declare_space = "";
4319 if ($declare =~ /(\s+)$/) {
4320 $post_declare_space = $1;
4321 $declare = rtrim($declare);
4322 }
4323 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004324 WARN("SPACING",
4325 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004326 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004327 }
4328
4329# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004330# This test is not currently implemented because these declarations are
4331# equivalent to
4332# int foo(int bar, ...)
4333# and this is form shouldn't/doesn't generate a checkpatch warning.
4334#
4335# elsif ($declare =~ /\s{2,}$/) {
4336# WARN("SPACING",
4337# "Multiple spaces after return type\n" . $herecurr);
4338# }
Joe Perches31070b52014-01-23 15:54:49 -08004339
4340# unnecessary space "type ( *funcptr)(args...)"
4341 if (defined $pre_pointer_space &&
4342 $pre_pointer_space =~ /^\s/) {
4343 WARN("SPACING",
4344 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4345 }
4346
4347# unnecessary space "type (* funcptr)(args...)"
4348 if (defined $post_pointer_space &&
4349 $post_pointer_space =~ /^\s/) {
4350 WARN("SPACING",
4351 "Unnecessary space before function pointer name\n" . $herecurr);
4352 }
4353
4354# unnecessary space "type (*funcptr )(args...)"
4355 if (defined $post_funcname_space &&
4356 $post_funcname_space =~ /^\s/) {
4357 WARN("SPACING",
4358 "Unnecessary space after function pointer name\n" . $herecurr);
4359 }
4360
4361# unnecessary space "type (*funcptr) (args...)"
4362 if (defined $pre_args_space &&
4363 $pre_args_space =~ /^\s/) {
4364 WARN("SPACING",
4365 "Unnecessary space before function pointer arguments\n" . $herecurr);
4366 }
4367
4368 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004369 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004370 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004371 }
4372 }
4373
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004374# check for spacing round square brackets; allowed:
4375# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004376# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4377# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004378 while ($line =~ /(.*?\s)\[/g) {
4379 my ($where, $prefix) = ($-[1], $1);
4380 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004381 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004382 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004383 if (ERROR("BRACKET_SPACE",
4384 "space prohibited before open square bracket '['\n" . $herecurr) &&
4385 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004386 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004387 s/^(\+.*?)\s+\[/$1\[/;
4388 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004389 }
4390 }
4391
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004392# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004393 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004394 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004395 my $ctx_before = substr($line, 0, $-[1]);
4396 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004397
4398 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004399 if ($name =~ /^(?:
4400 if|for|while|switch|return|case|
4401 volatile|__volatile__|
4402 __attribute__|format|__extension__|
4403 asm|__asm__)$/x)
4404 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004405 # cpp #define statements have non-optional spaces, ie
4406 # if there is a space between the name and the open
4407 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004408 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004409
4410 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004411 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004412
4413 # If this whole things ends with a type its most
4414 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004415 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004416
4417 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004418 if (WARN("SPACING",
4419 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4420 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004421 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004422 s/\b$name\s+\(/$name\(/;
4423 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004424 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004425 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004426
Andy Whitcroft653d4872007-06-23 17:16:34 -07004427# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004428 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004429 my $fixed_line = "";
4430 my $line_fixed = 0;
4431
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004432 my $ops = qr{
4433 <<=|>>=|<=|>=|==|!=|
4434 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4435 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004436 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004437 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004438 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004439 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004440
4441## print("element count: <" . $#elements . ">\n");
4442## foreach my $el (@elements) {
4443## print("el: <$el>\n");
4444## }
4445
4446 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004447 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004448
Joe Perches3705ce52013-07-03 15:05:31 -07004449 foreach my $el (@elements) {
4450 push(@fix_elements, substr($rawline, $off, length($el)));
4451 $off += length($el);
4452 }
4453
4454 $off = 0;
4455
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004456 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004457 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004458
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004459 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004460
4461 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4462
4463## print("n: <$n> good: <$good>\n");
4464
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004465 $off += length($elements[$n]);
4466
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004467 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004468 my $ca = substr($opline, 0, $off);
4469 my $cc = '';
4470 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4471 $cc = substr($opline, $off + length($elements[$n + 1]));
4472 }
4473 my $cb = "$ca$;$cc";
4474
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004475 my $a = '';
4476 $a = 'V' if ($elements[$n] ne '');
4477 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004478 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004479 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4480 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004481 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004482
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004483 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004484
4485 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004486 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004487 $c = 'V' if ($elements[$n + 2] ne '');
4488 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004489 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004490 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4491 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004492 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004493 } else {
4494 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004495 }
4496
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004497 my $ctx = "${a}x${c}";
4498
4499 my $at = "(ctx:$ctx)";
4500
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004501 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004502 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004503
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004504 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004505 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004506
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004507 # Get the full operator variant.
4508 my $opv = $op . substr($curr_vars, $off, 1);
4509
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004510 # Ignore operators passed as parameters.
4511 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004512 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004513
Andy Whitcroftcf655042008-03-04 14:28:20 -08004514# # Ignore comments
4515# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004516
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004517 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004518 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004519 if ($ctx !~ /.x[WEBC]/ &&
4520 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004521 if (ERROR("SPACING",
4522 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004523 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004524 $line_fixed = 1;
4525 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004526 }
4527
4528 # // is a comment
4529 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004530
Joe Perchesb00e4812014-04-03 14:49:33 -07004531 # : when part of a bitfield
4532 } elsif ($opv eq ':B') {
4533 # skip the bitfield test for now
4534
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004535 # No spaces for:
4536 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004537 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004538 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004539 if (ERROR("SPACING",
4540 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004541 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004542 if (defined $fix_elements[$n + 2]) {
4543 $fix_elements[$n + 2] =~ s/^\s+//;
4544 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004545 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004546 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004547 }
4548
Joe Perches23810972014-12-10 15:51:32 -08004549 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004550 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004551 my $rtrim_before = 0;
4552 my $space_after = 0;
4553 if ($ctx =~ /Wx./) {
4554 if (ERROR("SPACING",
4555 "space prohibited before that '$op' $at\n" . $hereptr)) {
4556 $line_fixed = 1;
4557 $rtrim_before = 1;
4558 }
4559 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004560 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004561 if (ERROR("SPACING",
4562 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004563 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004564 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004565 $space_after = 1;
4566 }
4567 }
4568 if ($rtrim_before || $space_after) {
4569 if ($rtrim_before) {
4570 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4571 } else {
4572 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4573 }
4574 if ($space_after) {
4575 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004576 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004577 }
4578
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004579 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004580 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004581 #warn "'*' is part of type\n";
4582
4583 # unary operators should have a space before and
4584 # none after. May be left adjacent to another
4585 # unary operator, or a cast
4586 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004587 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004588 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004589 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004590 if (ERROR("SPACING",
4591 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004592 if ($n != $last_after + 2) {
4593 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4594 $line_fixed = 1;
4595 }
Joe Perches3705ce52013-07-03 15:05:31 -07004596 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004597 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004598 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004599 # A unary '*' may be const
4600
4601 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004602 if (ERROR("SPACING",
4603 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004604 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004605 if (defined $fix_elements[$n + 2]) {
4606 $fix_elements[$n + 2] =~ s/^\s+//;
4607 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004608 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004609 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004610 }
4611
4612 # unary ++ and unary -- are allowed no space on one side.
4613 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004614 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004615 if (ERROR("SPACING",
4616 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004617 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004618 $line_fixed = 1;
4619 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004620 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004621 if ($ctx =~ /Wx[BE]/ ||
4622 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004623 if (ERROR("SPACING",
4624 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004625 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004626 $line_fixed = 1;
4627 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004628 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004629 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004630 if (ERROR("SPACING",
4631 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004632 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004633 if (defined $fix_elements[$n + 2]) {
4634 $fix_elements[$n + 2] =~ s/^\s+//;
4635 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004636 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004637 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004638 }
4639
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004640 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004641 } elsif ($op eq '<<' or $op eq '>>' or
4642 $op eq '&' or $op eq '^' or $op eq '|' or
4643 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004644 $op eq '*' or $op eq '/' or
4645 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004646 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004647 if ($check) {
4648 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4649 if (CHK("SPACING",
4650 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4651 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4652 $fix_elements[$n + 2] =~ s/^\s+//;
4653 $line_fixed = 1;
4654 }
4655 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4656 if (CHK("SPACING",
4657 "space preferred before that '$op' $at\n" . $hereptr)) {
4658 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4659 $line_fixed = 1;
4660 }
4661 }
4662 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004663 if (ERROR("SPACING",
4664 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004665 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4666 if (defined $fix_elements[$n + 2]) {
4667 $fix_elements[$n + 2] =~ s/^\s+//;
4668 }
Joe Perches3705ce52013-07-03 15:05:31 -07004669 $line_fixed = 1;
4670 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004671 }
4672
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004673 # A colon needs no spaces before when it is
4674 # terminating a case value or a label.
4675 } elsif ($opv eq ':C' || $opv eq ':L') {
4676 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004677 if (ERROR("SPACING",
4678 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004679 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004680 $line_fixed = 1;
4681 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004682 }
4683
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004684 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004685 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004686 my $ok = 0;
4687
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004688 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004689 if (($op eq '<' &&
4690 $cc =~ /^\S+\@\S+>/) ||
4691 ($op eq '>' &&
4692 $ca =~ /<\S+\@\S+$/))
4693 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07004694 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004695 }
4696
Joe Perchese0df7e12015-04-16 12:44:53 -07004697 # for asm volatile statements
4698 # ignore a colon with another
4699 # colon immediately before or after
4700 if (($op eq ':') &&
4701 ($ca =~ /:$/ || $cc =~ /^:/)) {
4702 $ok = 1;
4703 }
4704
Joe Perches84731622013-11-12 15:10:05 -08004705 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004706 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004707 my $msg_level = \&ERROR;
4708 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004709
Jean Delvare0675a8f2017-09-08 16:16:07 -07004710 if (&{$msg_level}("SPACING",
4711 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004712 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4713 if (defined $fix_elements[$n + 2]) {
4714 $fix_elements[$n + 2] =~ s/^\s+//;
4715 }
Joe Perches3705ce52013-07-03 15:05:31 -07004716 $line_fixed = 1;
4717 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004718 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004719 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004720 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004721
4722## print("n: <$n> GOOD: <$good>\n");
4723
4724 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004725 }
Joe Perches3705ce52013-07-03 15:05:31 -07004726
4727 if (($#elements % 2) == 0) {
4728 $fixed_line = $fixed_line . $fix_elements[$#elements];
4729 }
4730
Joe Perches194f66f2014-08-06 16:11:03 -07004731 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4732 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004733 }
4734
4735
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004736 }
4737
Joe Perches786b6322013-07-03 15:05:32 -07004738# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004739 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004740 if (WARN("SPACING",
4741 "space prohibited before semicolon\n" . $herecurr) &&
4742 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004743 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004744 s/^(\+.*\S)\s+;/$1;/;
4745 }
4746 }
4747
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004748# check for multiple assignments
4749 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004750 CHK("MULTIPLE_ASSIGNMENTS",
4751 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004752 }
4753
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004754## # check for multiple declarations, allowing for a function declaration
4755## # continuation.
4756## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4757## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4758##
4759## # Remove any bracketed sections to ensure we do not
4760## # falsly report the parameters of functions.
4761## my $ln = $line;
4762## while ($ln =~ s/\([^\(\)]*\)//g) {
4763## }
4764## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004765## WARN("MULTIPLE_DECLARATION",
4766## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004767## }
4768## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004769
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004770#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004771 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004772 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004773 if (ERROR("SPACING",
4774 "space required before the open brace '{'\n" . $herecurr) &&
4775 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004776 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004777 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004778 }
4779
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004780## # check for blank lines before declarations
4781## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4782## $prevrawline =~ /^.\s*$/) {
4783## WARN("SPACING",
4784## "No blank lines before declarations\n" . $hereprev);
4785## }
4786##
4787
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004788# closing brace should have a space following it when it has anything
4789# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07004790 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004791 if (ERROR("SPACING",
4792 "space required after that close brace '}'\n" . $herecurr) &&
4793 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004794 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004795 s/}((?!(?:,|;|\)))\S)/} $1/;
4796 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004797 }
4798
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004799# check spacing on square brackets
4800 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004801 if (ERROR("SPACING",
4802 "space prohibited after that open 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 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004809 if (ERROR("SPACING",
4810 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4811 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004812 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004813 s/\s+\]/\]/;
4814 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004815 }
4816
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004817# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004818 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4819 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004820 if (ERROR("SPACING",
4821 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4822 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004823 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004824 s/\(\s+/\(/;
4825 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004826 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004827 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004828 $line !~ /for\s*\(.*;\s+\)/ &&
4829 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004830 if (ERROR("SPACING",
4831 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4832 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004833 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004834 s/\s+\)/\)/;
4835 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004836 }
4837
Joe Perchese2826fd2014-08-06 16:10:48 -07004838# check unnecessary parentheses around addressof/dereference single $Lvals
4839# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4840
4841 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004842 my $var = $1;
4843 if (CHK("UNNECESSARY_PARENTHESES",
4844 "Unnecessary parentheses around $var\n" . $herecurr) &&
4845 $fix) {
4846 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4847 }
4848 }
4849
4850# check for unnecessary parentheses around function pointer uses
4851# ie: (foo->bar)(); should be foo->bar();
4852# but not "if (foo->bar) (" to avoid some false positives
4853 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4854 my $var = $2;
4855 if (CHK("UNNECESSARY_PARENTHESES",
4856 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4857 $fix) {
4858 my $var2 = deparenthesize($var);
4859 $var2 =~ s/\s//g;
4860 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4861 }
4862 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004863
Joe Perches63b7c732017-09-08 16:16:01 -07004864# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004865# when !drivers/staging or command-line uses --strict
4866 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004867 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004868 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4869 my $if_stat = $1;
4870 my $test = substr($2, 1, -1);
4871 my $herectx;
4872 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4873 my $match = $1;
4874 # avoid parentheses around potential macro args
4875 next if ($match =~ /^\s*\w+\s*$/);
4876 if (!defined($herectx)) {
4877 $herectx = $here . "\n";
4878 my $cnt = statement_rawlines($if_stat);
4879 for (my $n = 0; $n < $cnt; $n++) {
4880 my $rl = raw_line($linenr, $n);
4881 $herectx .= $rl . "\n";
4882 last if $rl =~ /^[ \+].*\{/;
4883 }
4884 }
4885 CHK("UNNECESSARY_PARENTHESES",
4886 "Unnecessary parentheses around '$match'\n" . $herectx);
4887 }
4888 }
4889
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004890#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004891 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004892 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004893 if (WARN("INDENTED_LABEL",
4894 "labels should not be indented\n" . $herecurr) &&
4895 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004896 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004897 s/^(.)\s+/$1/;
4898 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004899 }
4900
Joe Perches5b9553a2014-04-03 14:49:21 -07004901# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004902 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004903 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004904 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004905 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4906 my $value = $1;
4907 $value = deparenthesize($value);
4908 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4909 ERROR("RETURN_PARENTHESES",
4910 "return is not a function, parentheses are not required\n" . $herecurr);
4911 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004912 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004913 ERROR("SPACING",
4914 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004915 }
4916 }
Joe Perches507e5142013-11-12 15:10:13 -08004917
Joe Perchesb43ae212014-06-23 13:22:07 -07004918# unnecessary return in a void function
4919# at end-of-function, with the previous line a single leading tab, then return;
4920# and the line before that not a goto label target like "out:"
4921 if ($sline =~ /^[ \+]}\s*$/ &&
4922 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4923 $linenr >= 3 &&
4924 $lines[$linenr - 3] =~ /^[ +]/ &&
4925 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004926 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004927 "void function return statements are not generally useful\n" . $hereprev);
4928 }
Joe Perches9819cf22014-06-04 16:12:09 -07004929
Joe Perches189248d2014-01-23 15:54:47 -08004930# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004931 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004932 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4933 my $openparens = $1;
4934 my $count = $openparens =~ tr@\(@\(@;
4935 my $msg = "";
4936 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4937 my $comp = $4; #Not $1 because of $LvalOrFunc
4938 $msg = " - maybe == should be = ?" if ($comp eq "==");
4939 WARN("UNNECESSARY_PARENTHESES",
4940 "Unnecessary parentheses$msg\n" . $herecurr);
4941 }
4942 }
4943
Joe Perchesc5595fa2015-09-09 15:37:58 -07004944# comparisons with a constant or upper case identifier on the left
4945# avoid cases like "foo + BAR < baz"
4946# only fix matches surrounded by parentheses to avoid incorrect
4947# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004948 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004949 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4950 my $lead = $1;
4951 my $const = $2;
4952 my $comp = $3;
4953 my $to = $4;
4954 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004955 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004956 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4957 WARN("CONSTANT_COMPARISON",
4958 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4959 $fix) {
4960 if ($comp eq "<") {
4961 $newcomp = ">";
4962 } elsif ($comp eq "<=") {
4963 $newcomp = ">=";
4964 } elsif ($comp eq ">") {
4965 $newcomp = "<";
4966 } elsif ($comp eq ">=") {
4967 $newcomp = "<=";
4968 }
4969 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4970 }
4971 }
4972
Joe Perchesf34e4a42015-04-16 12:44:19 -07004973# Return of what appears to be an errno should normally be negative
4974 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004975 my $name = $1;
4976 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004977 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004978 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004979 }
4980 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004981
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004982# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004983 if ($line =~ /\b(if|while|for|switch)\(/) {
4984 if (ERROR("SPACING",
4985 "space required before the open parenthesis '('\n" . $herecurr) &&
4986 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004987 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004988 s/\b(if|while|for|switch)\(/$1 \(/;
4989 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004990 }
4991
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004992# Check for illegal assignment in if conditional -- and check for trailing
4993# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004994 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004995 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4996 ctx_statement_block($linenr, $realcnt, 0)
4997 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004998 my ($stat_next) = ctx_statement_block($line_nr_next,
4999 $remain_next, $off_next);
5000 $stat_next =~ s/\n./\n /g;
5001 ##print "stat<$stat> stat_next<$stat_next>\n";
5002
5003 if ($stat_next =~ /^\s*while\b/) {
5004 # If the statement carries leading newlines,
5005 # then count those as offsets.
5006 my ($whitespace) =
5007 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5008 my $offset =
5009 statement_rawlines($whitespace) - 1;
5010
5011 $suppress_whiletrailers{$line_nr_next +
5012 $offset} = 1;
5013 }
5014 }
5015 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005016 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005017 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005018 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005019
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005020 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005021 ERROR("ASSIGN_IN_IF",
5022 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005023 }
5024
5025 # Find out what is on the end of the line after the
5026 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005027 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005028 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005029 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005030 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5031 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005032 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005033 # Find out how long the conditional actually is.
5034 my @newlines = ($c =~ /\n/gs);
5035 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005036 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005037
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005038 $stat_real = raw_line($linenr, $cond_lines)
5039 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005040 if (defined($stat_real) && $cond_lines > 1) {
5041 $stat_real = "[...]\n$stat_real";
5042 }
5043
Joe Perches000d1cc12011-07-25 17:13:25 -07005044 ERROR("TRAILING_STATEMENTS",
5045 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005046 }
5047 }
5048
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005049# Check for bitwise tests written as boolean
5050 if ($line =~ /
5051 (?:
5052 (?:\[|\(|\&\&|\|\|)
5053 \s*0[xX][0-9]+\s*
5054 (?:\&\&|\|\|)
5055 |
5056 (?:\&\&|\|\|)
5057 \s*0[xX][0-9]+\s*
5058 (?:\&\&|\|\||\)|\])
5059 )/x)
5060 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005061 WARN("HEXADECIMAL_BOOLEAN_TEST",
5062 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005063 }
5064
Andy Whitcroft8905a672007-11-28 16:21:06 -08005065# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005066 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5067 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005068 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005069 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005070 ERROR("TRAILING_STATEMENTS",
5071 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005072 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005073 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005074# if should not continue a brace
5075 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005076 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005077 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005078 $herecurr);
5079 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005080# case and default should not have general statements after them
5081 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5082 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005083 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005084 \s*return\s+
5085 )/xg)
5086 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005087 ERROR("TRAILING_STATEMENTS",
5088 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005089 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005090
5091 # Check for }<nl>else {, these must be at the same
5092 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005093 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5094 $previndent == $indent) {
5095 if (ERROR("ELSE_AFTER_BRACE",
5096 "else should follow close brace '}'\n" . $hereprev) &&
5097 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5098 fix_delete_line($fixlinenr - 1, $prevrawline);
5099 fix_delete_line($fixlinenr, $rawline);
5100 my $fixedline = $prevrawline;
5101 $fixedline =~ s/}\s*$//;
5102 if ($fixedline !~ /^\+\s*$/) {
5103 fix_insert_line($fixlinenr, $fixedline);
5104 }
5105 $fixedline = $rawline;
5106 $fixedline =~ s/^(.\s*)else/$1} else/;
5107 fix_insert_line($fixlinenr, $fixedline);
5108 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005109 }
5110
Joe Perches8b8856f2014-08-06 16:11:14 -07005111 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5112 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005113 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5114
5115 # Find out what is on the end of the line after the
5116 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005117 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005118 $s =~ s/\n.*//g;
5119
5120 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005121 if (ERROR("WHILE_AFTER_BRACE",
5122 "while should follow close brace '}'\n" . $hereprev) &&
5123 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5124 fix_delete_line($fixlinenr - 1, $prevrawline);
5125 fix_delete_line($fixlinenr, $rawline);
5126 my $fixedline = $prevrawline;
5127 my $trailing = $rawline;
5128 $trailing =~ s/^\+//;
5129 $trailing = trim($trailing);
5130 $fixedline =~ s/}\s*$/} $trailing/;
5131 fix_insert_line($fixlinenr, $fixedline);
5132 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005133 }
5134 }
5135
Joe Perches95e2c602013-07-03 15:05:20 -07005136#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005137 while ($line =~ m{($Constant|$Lval)}g) {
5138 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005139
Joe Perches95e2c602013-07-03 15:05:20 -07005140#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005141 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005142 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005143#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005144 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005145#Ignore SI style variants like nS, mV and dB
5146#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5147 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005148#Ignore some three character SI units explicitly, like MiB and KHz
5149 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005150 while ($var =~ m{($Ident)}g) {
5151 my $word = $1;
5152 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005153 if ($check) {
5154 seed_camelcase_includes();
5155 if (!$file && !$camelcase_file_seeded) {
5156 seed_camelcase_file($realfile);
5157 $camelcase_file_seeded = 1;
5158 }
5159 }
Joe Perches7e781f62013-09-11 14:23:55 -07005160 if (!defined $camelcase{$word}) {
5161 $camelcase{$word} = 1;
5162 CHK("CAMELCASE",
5163 "Avoid CamelCase: <$word>\n" . $herecurr);
5164 }
Joe Perches34456862013-07-03 15:05:34 -07005165 }
Joe Perches323c1262012-12-17 16:02:07 -08005166 }
5167 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005168
5169#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005170 if ($line =~ /\#\s*define.*\\\s+$/) {
5171 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5172 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5173 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005174 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005175 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005176 }
5177
Fabian Frederick0e212e02015-04-16 12:44:25 -07005178# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5179# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005180 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005181 my $file = "$1.h";
5182 my $checkfile = "include/linux/$file";
5183 if (-f "$root/$checkfile" &&
5184 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005185 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005186 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005187 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5188 if ($asminclude > 0) {
5189 if ($realfile =~ m{^arch/}) {
5190 CHK("ARCH_INCLUDE_LINUX",
5191 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5192 } else {
5193 WARN("INCLUDE_LINUX",
5194 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5195 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005196 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005197 }
5198 }
5199
Andy Whitcroft653d4872007-06-23 17:16:34 -07005200# multi-statement macros should be enclosed in a do while loop, grab the
5201# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005202# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005203 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5204 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005205 my $ln = $linenr;
5206 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005207 my ($off, $dstat, $dcond, $rest);
5208 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005209 my $has_flow_statement = 0;
5210 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005211 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005212 ctx_statement_block($linenr, $realcnt, 0);
5213 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005214 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005215 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005216
Joe Perches08a28432014-10-13 15:51:55 -07005217 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005218 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005219
Joe Perchesf59b64b2016-10-11 13:52:08 -07005220 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5221 my $define_args = $1;
5222 my $define_stmt = $dstat;
5223 my @def_args = ();
5224
5225 if (defined $define_args && $define_args ne "") {
5226 $define_args = substr($define_args, 1, length($define_args) - 2);
5227 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005228 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005229 @def_args = split(",", $define_args);
5230 }
5231
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005232 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005233 $dstat =~ s/\\\n.//g;
5234 $dstat =~ s/^\s*//s;
5235 $dstat =~ s/\s*$//s;
5236
5237 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005238 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5239 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005240 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005241 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005242 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005243
Antonio Borneo342d3d22020-04-06 20:11:01 -07005244 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005245 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5246 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005247 {
5248 }
5249
Joe Perches42e15292016-03-15 14:58:01 -07005250 # Make asm volatile uses seem like a generic function
5251 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5252
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005253 my $exceptions = qr{
5254 $Declare|
5255 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005256 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005257 DECLARE_PER_CPU|
5258 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005259 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005260 union|
5261 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005262 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005263 ^\"|\"$|
5264 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005265 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005266 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005267
5268 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005269 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005270 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005271
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005272 if ($dstat ne '' &&
5273 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5274 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005275 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005276 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005277 $dstat !~ /$exceptions/ &&
5278 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005279 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005280 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005281 $dstat !~ /^for\s*$Constant$/ && # for (...)
5282 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5283 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005284 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005285 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005286 {
Joe Perchese7955562017-05-08 15:55:48 -07005287 if ($dstat =~ /^\s*if\b/) {
5288 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5289 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5290 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005291 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5292 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5293 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005294 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005295 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005296 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005297
5298 }
Joe Perches52076492016-10-11 13:52:14 -07005299
5300 # Make $define_stmt single line, comment-free, etc
5301 my @stmt_array = split('\n', $define_stmt);
5302 my $first = 1;
5303 $define_stmt = "";
5304 foreach my $l (@stmt_array) {
5305 $l =~ s/\\$//;
5306 if ($first) {
5307 $define_stmt = $l;
5308 $first = 0;
5309 } elsif ($l =~ /^[\+ ]/) {
5310 $define_stmt .= substr($l, 1);
5311 }
5312 }
5313 $define_stmt =~ s/$;//g;
5314 $define_stmt =~ s/\s+/ /g;
5315 $define_stmt = trim($define_stmt);
5316
Joe Perchesf59b64b2016-10-11 13:52:08 -07005317# check if any macro arguments are reused (ignore '...' and 'type')
5318 foreach my $arg (@def_args) {
5319 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005320 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005321 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005322 $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 -07005323 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5324 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005325 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005326 if ($use_cnt > 1) {
5327 CHK("MACRO_ARG_REUSE",
5328 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005329 }
5330# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005331 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005332 ((defined($1) && $1 ne ',') ||
5333 (defined($2) && $2 ne ','))) {
5334 CHK("MACRO_ARG_PRECEDENCE",
5335 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005336 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005337 }
Joe Perches5023d342012-12-17 16:01:47 -08005338
Joe Perches08a28432014-10-13 15:51:55 -07005339# check for macros with flow control, but without ## concatenation
5340# ## concatenation is commonly a macro that defines a function so ignore those
5341 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005342 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005343 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005344
Joe Perches08a28432014-10-13 15:51:55 -07005345 WARN("MACRO_WITH_FLOW_CONTROL",
5346 "Macros with flow control statements should be avoided\n" . "$herectx");
5347 }
5348
Joe Perches481eb482012-12-17 16:01:56 -08005349# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005350
5351 } else {
5352 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005353 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5354 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005355 $line =~ /^\+.*\\$/) {
5356 WARN("LINE_CONTINUATIONS",
5357 "Avoid unnecessary line continuations\n" . $herecurr);
5358 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005359 }
5360
Joe Perchesb13edf72012-07-30 14:41:24 -07005361# do {} while (0) macro tests:
5362# single-statement macros do not need to be enclosed in do while (0) loop,
5363# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005364 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005365 $realfile !~ m@/vmlinux.lds.h$@ &&
5366 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5367 my $ln = $linenr;
5368 my $cnt = $realcnt;
5369 my ($off, $dstat, $dcond, $rest);
5370 my $ctx = '';
5371 ($dstat, $dcond, $ln, $cnt, $off) =
5372 ctx_statement_block($linenr, $realcnt, 0);
5373 $ctx = $dstat;
5374
5375 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005376 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005377
5378 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5379 my $stmts = $2;
5380 my $semis = $3;
5381
5382 $ctx =~ s/\n*$//;
5383 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005384 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005385
Joe Perchesac8e97f2012-08-21 16:15:53 -07005386 if (($stmts =~ tr/;/;/) == 1 &&
5387 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005388 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5389 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5390 }
5391 if (defined $semis && $semis ne "") {
5392 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5393 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5394 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005395 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5396 $ctx =~ s/\n*$//;
5397 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005398 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005399
5400 WARN("TRAILING_SEMICOLON",
5401 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005402 }
5403 }
5404
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005405# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005406 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5407 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005408 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005409 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005410 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5411 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005412 my @allowed = ();
5413 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005414 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005415 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005416 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005417 for my $chunk (@chunks) {
5418 my ($cond, $block) = @{$chunk};
5419
Andy Whitcroft773647a2008-03-28 14:15:58 -07005420 # If the condition carries leading newlines, then count those as offsets.
5421 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5422 my $offset = statement_rawlines($whitespace) - 1;
5423
Joe Perchesaad4f612012-03-23 15:02:19 -07005424 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005425 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5426
5427 # We have looked at and allowed this specific line.
5428 $suppress_ifbraces{$ln + $offset} = 1;
5429
5430 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005431 $ln += statement_rawlines($block) - 1;
5432
Andy Whitcroft773647a2008-03-28 14:15:58 -07005433 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005434
5435 $seen++ if ($block =~ /^\s*{/);
5436
Joe Perchesaad4f612012-03-23 15:02:19 -07005437 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005438 if (statement_lines($cond) > 1) {
5439 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005440 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005441 }
5442 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005443 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005444 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005445 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005446 if (statement_block_size($block) > 1) {
5447 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005448 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005449 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005450 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005451 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005452 if ($seen) {
5453 my $sum_allowed = 0;
5454 foreach (@allowed) {
5455 $sum_allowed += $_;
5456 }
5457 if ($sum_allowed == 0) {
5458 WARN("BRACES",
5459 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5460 } elsif ($sum_allowed != $allow &&
5461 $seen != $allow) {
5462 CHK("BRACES",
5463 "braces {} should be used on all arms of this statement\n" . $herectx);
5464 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005465 }
5466 }
5467 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005468 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005469 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005470 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005471
Andy Whitcroftcf655042008-03-04 14:28:20 -08005472 # Check the pre-context.
5473 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5474 #print "APW: ALLOWED: pre<$1>\n";
5475 $allowed = 1;
5476 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005477
5478 my ($level, $endln, @chunks) =
5479 ctx_statement_full($linenr, $realcnt, $-[0]);
5480
Andy Whitcroftcf655042008-03-04 14:28:20 -08005481 # Check the condition.
5482 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005483 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005484 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005485 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005486 }
5487 if (statement_lines($cond) > 1) {
5488 #print "APW: ALLOWED: cond<$cond>\n";
5489 $allowed = 1;
5490 }
5491 if ($block =~/\b(?:if|for|while)\b/) {
5492 #print "APW: ALLOWED: block<$block>\n";
5493 $allowed = 1;
5494 }
5495 if (statement_block_size($block) > 1) {
5496 #print "APW: ALLOWED: lines block<$block>\n";
5497 $allowed = 1;
5498 }
5499 # Check the post-context.
5500 if (defined $chunks[1]) {
5501 my ($cond, $block) = @{$chunks[1]};
5502 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005503 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005504 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005505 if ($block =~ /^\s*\{/) {
5506 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5507 $allowed = 1;
5508 }
5509 }
5510 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005511 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005512 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005513
Joe Perches000d1cc12011-07-25 17:13:25 -07005514 WARN("BRACES",
5515 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005516 }
5517 }
5518
Joe Perchese4c5bab2017-02-24 15:01:41 -08005519# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005520 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5521 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005522 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5523 }
5524
Joe Perches0979ae62012-12-17 16:01:59 -08005525# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005526 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005527 if (CHK("BRACES",
5528 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5529 $fix && $prevrawline =~ /^\+/) {
5530 fix_delete_line($fixlinenr - 1, $prevrawline);
5531 }
Joe Perches0979ae62012-12-17 16:01:59 -08005532 }
Joe Perches77b9a532013-07-03 15:05:29 -07005533 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005534 if (CHK("BRACES",
5535 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5536 $fix) {
5537 fix_delete_line($fixlinenr, $rawline);
5538 }
Joe Perches0979ae62012-12-17 16:01:59 -08005539 }
5540
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005541# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005542 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5543 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005544 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005545 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005546 }
5547
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005548# Check for user-visible strings broken across lines, which breaks the ability
5549# to grep for the string. Make exceptions when the previous string ends in a
5550# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5551# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005552 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005553 $prevline =~ /"\s*$/ &&
5554 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5555 if (WARN("SPLIT_STRING",
5556 "quoted string split across lines\n" . $hereprev) &&
5557 $fix &&
5558 $prevrawline =~ /^\+.*"\s*$/ &&
5559 $last_coalesced_string_linenr != $linenr - 1) {
5560 my $extracted_string = get_quoted_string($line, $rawline);
5561 my $comma_close = "";
5562 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5563 $comma_close = $1;
5564 }
5565
5566 fix_delete_line($fixlinenr - 1, $prevrawline);
5567 fix_delete_line($fixlinenr, $rawline);
5568 my $fixedline = $prevrawline;
5569 $fixedline =~ s/"\s*$//;
5570 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5571 fix_insert_line($fixlinenr - 1, $fixedline);
5572 $fixedline = $rawline;
5573 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5574 if ($fixedline !~ /\+\s*$/) {
5575 fix_insert_line($fixlinenr, $fixedline);
5576 }
5577 $last_coalesced_string_linenr = $linenr;
5578 }
5579 }
5580
5581# check for missing a space in a string concatenation
5582 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5583 WARN('MISSING_SPACE',
5584 "break quoted strings at a space character\n" . $hereprev);
5585 }
5586
Joe Perchese4b7d302017-05-08 15:55:51 -07005587# check for an embedded function name in a string when the function is known
5588# This does not work very well for -f --file checking as it depends on patch
5589# context providing the function name or a single line form for in-file
5590# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005591 if ($line =~ /^\+.*$String/ &&
5592 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005593 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5594 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005595 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005596 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005597 }
5598
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005599# check for spaces before a quoted newline
5600 if ($rawline =~ /^.*\".*\s\\n/) {
5601 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5602 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5603 $fix) {
5604 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5605 }
5606
5607 }
5608
Joe Perchesf17dba42014-10-13 15:51:51 -07005609# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005610 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5611 if (CHK("CONCATENATED_STRING",
5612 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5613 $fix) {
5614 while ($line =~ /($String)/g) {
5615 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5616 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5617 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5618 }
5619 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005620 }
5621
Joe Perches90ad30e2014-12-10 15:51:59 -08005622# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005623 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005624 if (WARN("STRING_FRAGMENTS",
5625 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5626 $fix) {
5627 while ($line =~ /($String)(?=\s*")/g) {
5628 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5629 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5630 }
5631 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005632 }
5633
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005634# check for non-standard and hex prefixed decimal printf formats
5635 my $show_L = 1; #don't show the same defect twice
5636 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005637 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005638 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005639 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005640 # check for %L
5641 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005642 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005643 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5644 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005645 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005646 # check for %Z
5647 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5648 WARN("PRINTF_Z",
5649 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5650 $show_Z = 0;
5651 }
5652 # check for 0x<decimal>
5653 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5654 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005655 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5656 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005657 }
5658
5659# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005660 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005661 WARN("LINE_CONTINUATIONS",
5662 "Avoid line continuations in quoted strings\n" . $herecurr);
5663 }
5664
Andy Whitcroft00df3442007-06-08 13:47:06 -07005665# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005666 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005667 WARN("IF_0",
5668 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5669 }
5670
5671# warn about #if 1
5672 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5673 WARN("IF_1",
5674 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005675 }
5676
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005677# check for needless "if (<foo>) fn(<foo>)" uses
5678 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005679 my $tested = quotemeta($1);
5680 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5681 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5682 my $func = $1;
5683 if (WARN('NEEDLESS_IF',
5684 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5685 $fix) {
5686 my $do_fix = 1;
5687 my $leading_tabs = "";
5688 my $new_leading_tabs = "";
5689 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5690 $leading_tabs = $1;
5691 } else {
5692 $do_fix = 0;
5693 }
5694 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5695 $new_leading_tabs = $1;
5696 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5697 $do_fix = 0;
5698 }
5699 } else {
5700 $do_fix = 0;
5701 }
5702 if ($do_fix) {
5703 fix_delete_line($fixlinenr - 1, $prevrawline);
5704 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5705 }
5706 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005707 }
5708 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005709
Joe Perchesebfdc402014-08-06 16:10:27 -07005710# check for unnecessary "Out of Memory" messages
5711 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5712 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5713 (defined $1 || defined $3) &&
5714 $linenr > 3) {
5715 my $testval = $2;
5716 my $testline = $lines[$linenr - 3];
5717
5718 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5719# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5720
Joe Perchese29a70f2019-03-07 16:28:35 -08005721 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5722 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005723 WARN("OOM_MESSAGE",
5724 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5725 }
5726 }
5727
Joe Perchesf78d98f2014-10-13 15:52:01 -07005728# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005729 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005730 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5731 my $level = $1;
5732 if (WARN("UNNECESSARY_KERN_LEVEL",
5733 "Possible unnecessary $level\n" . $herecurr) &&
5734 $fix) {
5735 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5736 }
5737 }
5738
Joe Perches45c55e92017-02-24 15:01:31 -08005739# check for logging continuations
5740 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5741 WARN("LOGGING_CONTINUATION",
5742 "Avoid logging continuation uses where feasible\n" . $herecurr);
5743 }
5744
Joe Perchesabb08a52014-12-10 15:51:46 -08005745# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005746 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005747 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5748 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5749 WARN("MASK_THEN_SHIFT",
5750 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5751 }
5752
Joe Perchesb75ac612014-12-10 15:52:02 -08005753# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005754 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005755 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5756 my $val = $1;
5757 my $equal = "!";
5758 $equal = "" if ($4 eq "!=");
5759 if (CHK("COMPARISON_TO_NULL",
5760 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5761 $fix) {
5762 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5763 }
5764 }
5765 }
5766
Joe Perches8716de32013-09-11 14:24:05 -07005767# check for bad placement of section $InitAttribute (e.g.: __initdata)
5768 if ($line =~ /(\b$InitAttribute\b)/) {
5769 my $attr = $1;
5770 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5771 my $ptr = $1;
5772 my $var = $2;
5773 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5774 ERROR("MISPLACED_INIT",
5775 "$attr should be placed after $var\n" . $herecurr)) ||
5776 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5777 WARN("MISPLACED_INIT",
5778 "$attr should be placed after $var\n" . $herecurr))) &&
5779 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005780 $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 -07005781 }
5782 }
5783 }
5784
Joe Perchese970b8842013-11-12 15:10:10 -08005785# check for $InitAttributeData (ie: __initdata) with const
5786 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5787 my $attr = $1;
5788 $attr =~ /($InitAttributePrefix)(.*)/;
5789 my $attr_prefix = $1;
5790 my $attr_type = $2;
5791 if (ERROR("INIT_ATTRIBUTE",
5792 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5793 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005794 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005795 s/$InitAttributeData/${attr_prefix}initconst/;
5796 }
5797 }
5798
5799# check for $InitAttributeConst (ie: __initconst) without const
5800 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5801 my $attr = $1;
5802 if (ERROR("INIT_ATTRIBUTE",
5803 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5804 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005805 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005806 /(^\+\s*(?:static\s+))/;
5807 $lead = rtrim($1);
5808 $lead = "$lead " if ($lead !~ /^\+$/);
5809 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005810 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005811 }
5812 }
5813
Joe Perchesc17893c2015-04-16 12:44:42 -07005814# check for __read_mostly with const non-pointer (should just be const)
5815 if ($line =~ /\b__read_mostly\b/ &&
5816 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5817 if (ERROR("CONST_READ_MOSTLY",
5818 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5819 $fix) {
5820 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5821 }
5822 }
5823
Joe Perchesfbdb8132014-04-03 14:49:14 -07005824# don't use __constant_<foo> functions outside of include/uapi/
5825 if ($realfile !~ m@^include/uapi/@ &&
5826 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5827 my $constant_func = $1;
5828 my $func = $constant_func;
5829 $func =~ s/^__constant_//;
5830 if (WARN("CONSTANT_CONVERSION",
5831 "$constant_func should be $func\n" . $herecurr) &&
5832 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005833 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005834 }
5835 }
5836
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005837# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005838 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005839 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005840 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005841 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005842 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005843 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07005844 }
5845 if ($delay > 2000) {
5846 WARN("LONG_UDELAY",
5847 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005848 }
5849 }
5850
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005851# warn about unexpectedly long msleep's
5852 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5853 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005854 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005855 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005856 }
5857 }
5858
Joe Perches36ec1932013-07-03 15:05:25 -07005859# check for comparisons of jiffies
5860 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5861 WARN("JIFFIES_COMPARISON",
5862 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5863 }
5864
Joe Perches9d7a34a2013-07-03 15:05:26 -07005865# check for comparisons of get_jiffies_64()
5866 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5867 WARN("JIFFIES_COMPARISON",
5868 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5869 }
5870
Andy Whitcroft00df3442007-06-08 13:47:06 -07005871# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005872# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005873# print "#ifdef in C files should be avoided\n";
5874# print "$herecurr";
5875# $clean = 0;
5876# }
5877
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005878# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005879 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005880 if (ERROR("SPACING",
5881 "exactly one space required after that #$1\n" . $herecurr) &&
5882 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005883 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005884 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5885 }
5886
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005887 }
5888
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005889# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005890 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5891 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005892 my $which = $1;
5893 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005894 CHK("UNCOMMENTED_DEFINITION",
5895 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005896 }
5897 }
5898# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005899
5900 my $barriers = qr{
5901 mb|
5902 rmb|
5903 wmb|
5904 read_barrier_depends
5905 }x;
5906 my $barrier_stems = qr{
5907 mb__before_atomic|
5908 mb__after_atomic|
5909 store_release|
5910 load_acquire|
5911 store_mb|
5912 (?:$barriers)
5913 }x;
5914 my $all_barriers = qr{
5915 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005916 smp_(?:$barrier_stems)|
5917 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005918 }x;
5919
5920 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005921 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005922 WARN("MEMORY_BARRIER",
5923 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005924 }
5925 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005926
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005927 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5928
5929 if ($realfile !~ m@^include/asm-generic/@ &&
5930 $realfile !~ m@/barrier\.h$@ &&
5931 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5932 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5933 WARN("MEMORY_BARRIER",
5934 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5935 }
5936
Joe Perchescb426e92015-06-25 15:02:46 -07005937# check for waitqueue_active without a comment.
5938 if ($line =~ /\bwaitqueue_active\s*\(/) {
5939 if (!ctx_has_comment($first_line, $linenr)) {
5940 WARN("WAITQUEUE_ACTIVE",
5941 "waitqueue_active without comment\n" . $herecurr);
5942 }
5943 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005944
Paul E. McKenney91db2592017-11-27 09:37:35 -08005945# check for smp_read_barrier_depends and read_barrier_depends
5946 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5947 WARN("READ_BARRIER_DEPENDS",
5948 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5949 }
5950
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005951# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005952 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005953 CHK("ARCH_DEFINES",
5954 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005955 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005956
Joe Perches596ed452017-07-12 14:37:02 -07005957# check that the storage class is not after a type
5958 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005959 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005960 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5961 }
5962# Check that the storage class is at the beginning of a declaration
5963 if ($line =~ /\b$Storage\b/ &&
5964 $line !~ /^.\s*$Storage/ &&
5965 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5966 $1 !~ /[\,\)]\s*$/) {
5967 WARN("STORAGE_CLASS",
5968 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005969 }
5970
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005971# check the location of the inline attribute, that it is between
5972# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005973 if ($line =~ /\b$Type\s+$Inline\b/ ||
5974 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005975 ERROR("INLINE_LOCATION",
5976 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005977 }
5978
Andy Whitcroft8905a672007-11-28 16:21:06 -08005979# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005980 if ($realfile !~ m@\binclude/uapi/@ &&
5981 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005982 if (WARN("INLINE",
5983 "plain inline is preferred over $1\n" . $herecurr) &&
5984 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005985 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005986
5987 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005988 }
5989
Joe Perches3d130fd2011-01-12 17:00:00 -08005990# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005991 if ($realfile !~ m@\binclude/uapi/@ &&
5992 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005993 WARN("PREFER_PACKED",
5994 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005995 }
5996
Joe Perches39b7e282011-07-25 17:13:24 -07005997# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005998 if ($realfile !~ m@\binclude/uapi/@ &&
5999 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006000 WARN("PREFER_ALIGNED",
6001 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07006002 }
6003
Joe Perches462811d2019-09-25 16:46:44 -07006004# Check for __attribute__ section, prefer __section
6005 if ($realfile !~ m@\binclude/uapi/@ &&
6006 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
6007 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6008 my $new = substr($old, 1, -1);
6009 if (WARN("PREFER_SECTION",
6010 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6011 $fix) {
6012 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6013 }
6014 }
6015
Joe Perches5f14d3b2012-01-10 15:09:52 -08006016# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08006017 if ($realfile !~ m@\binclude/uapi/@ &&
6018 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006019 if (WARN("PREFER_PRINTF",
6020 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6021 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006022 $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 -07006023
6024 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08006025 }
6026
Joe Perches6061d942012-03-23 15:02:16 -07006027# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08006028 if ($realfile !~ m@\binclude/uapi/@ &&
6029 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006030 if (WARN("PREFER_SCANF",
6031 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6032 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006033 $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 -07006034 }
Joe Perches6061d942012-03-23 15:02:16 -07006035 }
6036
Joe Perches619a9082014-12-10 15:51:35 -08006037# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006038 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006039 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6040 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6041 $line =~ /\b__weak\b/)) {
6042 ERROR("WEAK_DECLARATION",
6043 "Using weak declarations can have unintended link defects\n" . $herecurr);
6044 }
6045
Tomas Winklerfd39f902016-12-12 16:46:34 -08006046# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006047 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006048 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006049 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6050 my $type = $1;
6051 if ($type =~ /\b($typeC99Typedefs)\b/) {
6052 $type = $1;
6053 my $kernel_type = 'u';
6054 $kernel_type = 's' if ($type =~ /^_*[si]/);
6055 $type =~ /(\d+)/;
6056 $kernel_type .= $1;
6057 if (CHK("PREFER_KERNEL_TYPES",
6058 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6059 $fix) {
6060 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6061 }
6062 }
6063 }
6064
Joe Perches938224b2016-01-20 14:59:15 -08006065# check for cast of C90 native int or longer types constants
6066 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6067 my $cast = $1;
6068 my $const = $2;
6069 if (WARN("TYPECAST_INT_CONSTANT",
6070 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6071 $fix) {
6072 my $suffix = "";
6073 my $newconst = $const;
6074 $newconst =~ s/${Int_type}$//;
6075 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6076 if ($cast =~ /\blong\s+long\b/) {
6077 $suffix .= 'LL';
6078 } elsif ($cast =~ /\blong\b/) {
6079 $suffix .= 'L';
6080 }
6081 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6082 }
6083 }
6084
Joe Perches8f53a9b2010-03-05 13:43:48 -08006085# check for sizeof(&)
6086 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006087 WARN("SIZEOF_ADDRESS",
6088 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006089 }
6090
Joe Perches66c80b62012-07-30 14:41:22 -07006091# check for sizeof without parenthesis
6092 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006093 if (WARN("SIZEOF_PARENTHESIS",
6094 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6095 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006096 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006097 }
Joe Perches66c80b62012-07-30 14:41:22 -07006098 }
6099
Joe Perches88982fe2012-12-17 16:02:00 -08006100# check for struct spinlock declarations
6101 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6102 WARN("USE_SPINLOCK_T",
6103 "struct spinlock should be spinlock_t\n" . $herecurr);
6104 }
6105
Joe Perchesa6962d72013-04-29 16:18:13 -07006106# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006107 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006108 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006109 $fmt =~ s/%%//g;
6110 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006111 if (WARN("PREFER_SEQ_PUTS",
6112 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6113 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006114 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006115 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006116 }
6117 }
6118
Joe Perches478b1792018-04-10 16:33:34 -07006119# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006120 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006121 defined $stat &&
6122 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6123 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006124 my $stat_real;
6125
Joe Perches0b523762017-05-08 15:55:36 -07006126 my $lc = $stat =~ tr@\n@@;
6127 $lc = $lc + $linenr;
6128 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006129 my $specifier;
6130 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006131 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006132 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006133 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6134 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006135
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006136 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006137 $specifier = $1;
6138 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006139 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006140 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006141 ($extension eq "f" &&
6142 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006143 $bad_specifier = $specifier;
6144 last;
6145 }
6146 if ($extension eq "x" && !defined($stat_real)) {
6147 if (!defined($stat_real)) {
6148 $stat_real = get_stat_real($linenr, $lc);
6149 }
6150 WARN("VSPRINTF_SPECIFIER_PX",
6151 "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");
6152 }
6153 }
6154 if ($bad_specifier ne "") {
6155 my $stat_real = get_stat_real($linenr, $lc);
6156 my $ext_type = "Invalid";
6157 my $use = "";
6158 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006159 $use = " - use %pS instead";
6160 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6161 }
6162
6163 WARN("VSPRINTF_POINTER_EXTENSION",
6164 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6165 }
Joe Perches0b523762017-05-08 15:55:36 -07006166 }
6167 }
6168
Andy Whitcroft554e1652012-01-10 15:09:57 -08006169# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006170 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006171 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006172 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006173
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006174 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006175 my $ms_val = $7;
6176 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006177
Andy Whitcroft554e1652012-01-10 15:09:57 -08006178 if ($ms_size =~ /^(0x|)0$/i) {
6179 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006180 "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 -08006181 } elsif ($ms_size =~ /^(0x|)1$/i) {
6182 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006183 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6184 }
6185 }
6186
Joe Perches98a9bba2014-01-23 15:54:52 -08006187# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006188# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006189# defined $stat &&
6190# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6191# if (WARN("PREFER_ETHER_ADDR_COPY",
6192# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6193# $fix) {
6194# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6195# }
6196# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006197
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006198# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006199# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006200# defined $stat &&
6201# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6202# WARN("PREFER_ETHER_ADDR_EQUAL",
6203# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6204# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006205
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006206# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6207# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006208# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006209# defined $stat &&
6210# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6211#
6212# my $ms_val = $7;
6213#
6214# if ($ms_val =~ /^(?:0x|)0+$/i) {
6215# if (WARN("PREFER_ETH_ZERO_ADDR",
6216# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6217# $fix) {
6218# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6219# }
6220# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6221# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6222# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6223# $fix) {
6224# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6225# }
6226# }
6227# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006228
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006229# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006230 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006231 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006232 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006233 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006234 my $call = $1;
6235 my $cast1 = deparenthesize($2);
6236 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006237 my $cast2 = deparenthesize($7);
6238 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006239 my $cast;
6240
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006241 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006242 $cast = "$cast1 or $cast2";
6243 } elsif ($cast1 ne "") {
6244 $cast = $cast1;
6245 } else {
6246 $cast = $cast2;
6247 }
6248 WARN("MINMAX",
6249 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006250 }
6251 }
6252
Joe Perches4a273192012-07-30 14:41:20 -07006253# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006254 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006255 defined $stat &&
6256 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6257 my $min = $1;
6258 my $max = $7;
6259 if ($min eq $max) {
6260 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006261 "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 -07006262 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6263 $min > $max) {
6264 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006265 "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 -07006266 }
6267 }
6268
Joe Perches823b7942013-11-12 15:10:15 -08006269# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006270 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006271 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006272 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006273 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6274 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6275 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6276 my $lc = $stat =~ tr@\n@@;
6277 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006278 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006279 WARN("NAKED_SSCANF",
6280 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6281 }
6282
Joe Perchesafc819a2014-06-04 16:12:08 -07006283# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006284 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006285 defined $stat &&
6286 $line =~ /\bsscanf\b/) {
6287 my $lc = $stat =~ tr@\n@@;
6288 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006289 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006290 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6291 my $format = $6;
6292 my $count = $format =~ tr@%@%@;
6293 if ($count == 1 &&
6294 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6295 WARN("SSCANF_TO_KSTRTO",
6296 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6297 }
6298 }
6299 }
6300
Joe Perches70dc8a42013-09-11 14:23:58 -07006301# check for new externs in .h files.
6302 if ($realfile =~ /\.h$/ &&
6303 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006304 if (CHK("AVOID_EXTERNS",
6305 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006306 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006307 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006308 }
6309 }
6310
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006311# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006312 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006313 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006314 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006315 my $function_name = $1;
6316 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006317
6318 my $s = $stat;
6319 if (defined $cond) {
6320 substr($s, 0, length($cond), '');
6321 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006322 if ($s =~ /^\s*;/ &&
6323 $function_name ne 'uninitialized_var')
6324 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006325 WARN("AVOID_EXTERNS",
6326 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006327 }
6328
6329 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006330 WARN("FUNCTION_ARGUMENTS",
6331 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006332 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006333
6334 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6335 $stat =~ /^.\s*extern\s+/)
6336 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006337 WARN("AVOID_EXTERNS",
6338 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006339 }
6340
Joe Perchesa0ad7592017-07-10 15:52:19 -07006341# check for function declarations that have arguments without identifier names
Joe Perches16b7f3c2020-04-06 20:11:17 -07006342# while avoiding uninitialized_var(x)
Joe Perchesa0ad7592017-07-10 15:52:19 -07006343 if (defined $stat &&
Joe Perches16b7f3c2020-04-06 20:11:17 -07006344 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:($Ident)|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6345 (!defined($1) ||
6346 (defined($1) && $1 ne "uninitialized_var")) &&
6347 $2 ne "void") {
6348 my $args = trim($2);
Joe Perchesca0d8922016-10-11 13:52:16 -07006349 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6350 my $arg = trim($1);
Joe Perches16b7f3c2020-04-06 20:11:17 -07006351 if ($arg =~ /^$Type$/ &&
6352 $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006353 WARN("FUNCTION_ARGUMENTS",
6354 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6355 }
6356 }
6357 }
6358
Joe Perchesa0ad7592017-07-10 15:52:19 -07006359# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006360 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006361 defined $stat &&
6362 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6363 $context_function = $1;
6364
6365# check for multiline function definition with misplaced open brace
6366 my $ok = 0;
6367 my $cnt = statement_rawlines($stat);
6368 my $herectx = $here . "\n";
6369 for (my $n = 0; $n < $cnt; $n++) {
6370 my $rl = raw_line($linenr, $n);
6371 $herectx .= $rl . "\n";
6372 $ok = 1 if ($rl =~ /^[ \+]\{/);
6373 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6374 last if $rl =~ /^[ \+].*\{/;
6375 }
6376 if (!$ok) {
6377 ERROR("OPEN_BRACE",
6378 "open brace '{' following function definitions go on the next line\n" . $herectx);
6379 }
6380 }
6381
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006382# checks for new __setup's
6383 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6384 my $name = $1;
6385
6386 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006387 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006388 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006389 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006390 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006391
Joe Perchese29a70f2019-03-07 16:28:35 -08006392# check for pointless casting of alloc functions
6393 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006394 WARN("UNNECESSARY_CASTS",
6395 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006396 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006397
Joe Perchesa640d252013-07-03 15:05:21 -07006398# alloc style
6399# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006400 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006401 $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 -07006402 CHK("ALLOC_SIZEOF_STRUCT",
6403 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6404 }
6405
Joe Perches60a55362014-06-04 16:12:07 -07006406# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006407 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006408 defined $stat &&
6409 $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 -07006410 my $oldfunc = $3;
6411 my $a1 = $4;
6412 my $a2 = $10;
6413 my $newfunc = "kmalloc_array";
6414 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006415 my $r1 = $a1;
6416 my $r2 = $a2;
6417 if ($a1 =~ /^sizeof\s*\S/) {
6418 $r1 = $a2;
6419 $r2 = $a1;
6420 }
6421 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6422 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006423 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006424 my $herectx = get_stat_here($linenr, $cnt, $here);
6425
Joe Perches60a55362014-06-04 16:12:07 -07006426 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006427 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6428 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006429 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006430 $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 -07006431 }
6432 }
6433 }
6434
Joe Perches972fdea2013-04-29 16:18:12 -07006435# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006436 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006437 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6438 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006439 WARN("KREALLOC_ARG_REUSE",
6440 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6441 }
6442
Joe Perches5ce59ae2013-02-21 16:44:18 -08006443# check for alloc argument mismatch
6444 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6445 WARN("ALLOC_ARRAY_ARGS",
6446 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6447 }
6448
Joe Perchescaf2a542011-01-12 16:59:56 -08006449# check for multiple semicolons
6450 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006451 if (WARN("ONE_SEMICOLON",
6452 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6453 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006454 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006455 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006456 }
6457
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006458# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6459 if ($realfile !~ m@^include/uapi/@ &&
6460 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006461 my $ull = "";
6462 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6463 if (CHK("BIT_MACRO",
6464 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6465 $fix) {
6466 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6467 }
6468 }
6469
Joe Perches2d632742016-05-20 17:04:00 -07006470# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6471 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6472 my $config = $1;
6473 if (WARN("PREFER_IS_ENABLED",
6474 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6475 $fix) {
6476 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6477 }
6478 }
6479
Joe Perchese81f2392014-08-06 16:11:25 -07006480# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006481 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6482 my $has_break = 0;
6483 my $has_statement = 0;
6484 my $count = 0;
6485 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006486 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006487 $prevline--;
6488 my $rline = $rawlines[$prevline - 1];
6489 my $fline = $lines[$prevline - 1];
6490 last if ($fline =~ /^\@\@/);
6491 next if ($fline =~ /^\-/);
6492 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6493 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6494 next if ($fline =~ /^.[\s$;]*$/);
6495 $has_statement = 1;
6496 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006497 $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 -08006498 }
6499 if (!$has_break && $has_statement) {
6500 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006501 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006502 }
6503 }
6504
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006505# check for /* fallthrough */ like comment, prefer fallthrough;
6506 my @fallthroughs = (
6507 'fallthrough',
6508 '@fallthrough@',
6509 'lint -fallthrough[ \t]*',
6510 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6511 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6512 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6513 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6514 );
6515 if ($raw_comment ne '') {
6516 foreach my $ft (@fallthroughs) {
6517 if ($raw_comment =~ /$ft/) {
6518 my $msg_level = \&WARN;
6519 $msg_level = \&CHK if ($file);
6520 &{$msg_level}("PREFER_FALLTHROUGH",
6521 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6522 last;
6523 }
6524 }
6525 }
6526
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006527# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006528 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006529 defined $stat &&
6530 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006531 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006532 my $herectx = get_stat_here($linenr, $cnt, $here);
6533
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006534 WARN("DEFAULT_NO_BREAK",
6535 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006536 }
6537
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006538# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006539 if ($line =~ /\b__FUNCTION__\b/) {
6540 if (WARN("USE_FUNC",
6541 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6542 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006543 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006544 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006545 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006546
Joe Perches62ec8182015-02-13 14:38:18 -08006547# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6548 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6549 ERROR("DATE_TIME",
6550 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6551 }
6552
Joe Perches2c924882012-03-23 15:02:20 -07006553# check for use of yield()
6554 if ($line =~ /\byield\s*\(\s*\)/) {
6555 WARN("YIELD",
6556 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6557 }
6558
Joe Perches179f8f42013-07-03 15:05:30 -07006559# check for comparisons against true and false
6560 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6561 my $lead = $1;
6562 my $arg = $2;
6563 my $test = $3;
6564 my $otype = $4;
6565 my $trail = $5;
6566 my $op = "!";
6567
6568 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6569
6570 my $type = lc($otype);
6571 if ($type =~ /^(?:true|false)$/) {
6572 if (("$test" eq "==" && "$type" eq "true") ||
6573 ("$test" eq "!=" && "$type" eq "false")) {
6574 $op = "";
6575 }
6576
6577 CHK("BOOL_COMPARISON",
6578 "Using comparison to $otype is error prone\n" . $herecurr);
6579
6580## maybe suggesting a correct construct would better
6581## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6582
6583 }
6584 }
6585
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006586# check for semaphores initialized locked
6587 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006588 WARN("CONSIDER_COMPLETION",
6589 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006590 }
Joe Perches6712d852012-03-23 15:02:20 -07006591
Joe Perches67d0a072011-10-31 17:13:10 -07006592# recommend kstrto* over simple_strto* and strict_strto*
6593 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006594 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006595 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006596 }
Joe Perches6712d852012-03-23 15:02:20 -07006597
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006598# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006599 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006600 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006601 "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 -07006602 }
Joe Perches6712d852012-03-23 15:02:20 -07006603
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006604# check for spin_is_locked(), suggest lockdep instead
6605 if ($line =~ /\bspin_is_locked\(/) {
6606 WARN("USE_LOCKDEP",
6607 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6608 }
6609
Joe Perches9189c7e2018-09-07 15:26:18 -07006610# check for deprecated apis
6611 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6612 my $deprecated_api = $1;
6613 my $new_api = $deprecated_apis{$deprecated_api};
6614 WARN("DEPRECATED_API",
6615 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6616 }
6617
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006618# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006619# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006620 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006621 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006622 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006623 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006624 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006625
6626# use of NR_CPUS is usually wrong
6627# ignore definitions of NR_CPUS and usage to define arrays as likely right
6628 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006629 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6630 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006631 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6632 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6633 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006634 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006635 WARN("NR_CPUS",
6636 "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 -07006637 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006638
Joe Perches52ea8502013-11-12 15:10:09 -08006639# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6640 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6641 ERROR("DEFINE_ARCH_HAS",
6642 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6643 }
6644
Joe Perchesacd93622015-02-13 14:38:38 -08006645# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006646 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006647 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6648 WARN("LIKELY_MISUSE",
6649 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6650 }
6651
Denis Efremovde3f1862019-09-25 16:49:25 -07006652# nested likely/unlikely calls
6653 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6654 WARN("LIKELY_MISUSE",
6655 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6656 }
6657
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006658# whine mightly about in_atomic
6659 if ($line =~ /\bin_atomic\s*\(/) {
6660 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006661 ERROR("IN_ATOMIC",
6662 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006663 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006664 WARN("IN_ATOMIC",
6665 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006666 }
6667 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006668
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006669# check for mutex_trylock_recursive usage
6670 if ($line =~ /mutex_trylock_recursive/) {
6671 ERROR("LOCKING",
6672 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6673 }
6674
Peter Zijlstra1704f472010-03-19 01:37:42 +01006675# check for lockdep_set_novalidate_class
6676 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6677 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6678 if ($realfile !~ m@^kernel/lockdep@ &&
6679 $realfile !~ m@^include/linux/lockdep@ &&
6680 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006681 ERROR("LOCKDEP",
6682 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006683 }
6684 }
Dave Jones88f88312011-01-12 16:59:59 -08006685
Joe Perchesb392c642015-04-16 12:44:16 -07006686 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6687 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006688 WARN("EXPORTED_WORLD_WRITABLE",
6689 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006690 }
Joe Perches24358802014-04-03 14:49:13 -07006691
Joe Perches00180462018-02-06 15:38:55 -08006692# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6693# and whether or not function naming is typical and if
6694# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006695 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006696 defined $stat &&
6697 $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*\)/) {
6698 my $var = $1;
6699 my $perms = $2;
6700 my $show = $3;
6701 my $store = $4;
6702 my $octal_perms = perms_to_octal($perms);
6703 if ($show =~ /^${var}_show$/ &&
6704 $store =~ /^${var}_store$/ &&
6705 $octal_perms eq "0644") {
6706 if (WARN("DEVICE_ATTR_RW",
6707 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6708 $fix) {
6709 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6710 }
6711 } elsif ($show =~ /^${var}_show$/ &&
6712 $store =~ /^NULL$/ &&
6713 $octal_perms eq "0444") {
6714 if (WARN("DEVICE_ATTR_RO",
6715 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6716 $fix) {
6717 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6718 }
6719 } elsif ($show =~ /^NULL$/ &&
6720 $store =~ /^${var}_store$/ &&
6721 $octal_perms eq "0200") {
6722 if (WARN("DEVICE_ATTR_WO",
6723 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6724 $fix) {
6725 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6726 }
6727 } elsif ($octal_perms eq "0644" ||
6728 $octal_perms eq "0444" ||
6729 $octal_perms eq "0200") {
6730 my $newshow = "$show";
6731 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6732 my $newstore = $store;
6733 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6734 my $rename = "";
6735 if ($show ne $newshow) {
6736 $rename .= " '$show' to '$newshow'";
6737 }
6738 if ($store ne $newstore) {
6739 $rename .= " '$store' to '$newstore'";
6740 }
6741 WARN("DEVICE_ATTR_FUNCTIONS",
6742 "Consider renaming function(s)$rename\n" . $herecurr);
6743 } else {
6744 WARN("DEVICE_ATTR_PERMS",
6745 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6746 }
6747 }
6748
Joe Perches515a2352014-04-03 14:49:24 -07006749# Mode permission misuses where it seems decimal should be octal
6750# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006751# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6752# specific definition of not visible in sysfs.
6753# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6754# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006755 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006756 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006757 $line =~ /$mode_perms_search/) {
6758 foreach my $entry (@mode_permission_funcs) {
6759 my $func = $entry->[0];
6760 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006761
Joe Perches459cf0a2016-10-11 13:52:19 -07006762 my $lc = $stat =~ tr@\n@@;
6763 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006764 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006765
Joe Perches515a2352014-04-03 14:49:24 -07006766 my $skip_args = "";
6767 if ($arg_pos > 1) {
6768 $arg_pos--;
6769 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6770 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006771 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006772 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006773 my $val = $1;
6774 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006775 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6776 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6777 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006778 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006779 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006780 }
6781 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006782 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006783 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006784 }
Joe Perches24358802014-04-03 14:49:13 -07006785 }
6786 }
6787 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006788
Joe Perches459cf0a2016-10-11 13:52:19 -07006789# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006790 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006791 my $oval = $1;
6792 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006793 if (WARN("SYMBOLIC_PERMS",
6794 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6795 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006796 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006797 }
6798 }
6799
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006800# validate content of MODULE_LICENSE against list from include/linux/module.h
6801 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6802 my $extracted_string = get_quoted_string($line, $rawline);
6803 my $valid_licenses = qr{
6804 GPL|
6805 GPL\ v2|
6806 GPL\ and\ additional\ rights|
6807 Dual\ BSD/GPL|
6808 Dual\ MIT/GPL|
6809 Dual\ MPL/GPL|
6810 Proprietary
6811 }x;
6812 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6813 WARN("MODULE_LICENSE",
6814 "unknown module license " . $extracted_string . "\n" . $herecurr);
6815 }
6816 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07006817
6818# check for sysctl duplicate constants
6819 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6820 WARN("DUPLICATED_SYSCTL_CONST",
6821 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6822 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006823 }
6824
6825 # If we have no input at all, then there is nothing to report on
6826 # so just keep quiet.
6827 if ($#rawlines == -1) {
6828 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006829 }
6830
Andy Whitcroft8905a672007-11-28 16:21:06 -08006831 # In mailback mode only produce a report in the negative, for
6832 # things that appear to be patches.
6833 if ($mailback && ($clean == 1 || !$is_patch)) {
6834 exit(0);
6835 }
6836
6837 # This is not a patch, and we are are in 'no-patch' mode so
6838 # just keep quiet.
6839 if (!$chk_patch && !$is_patch) {
6840 exit(0);
6841 }
6842
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006843 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006844 ERROR("NOT_UNIFIED_DIFF",
6845 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006846 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006847 if ($is_patch && $has_commit_log && $chk_signoff) {
6848 if ($signoff == 0) {
6849 ERROR("MISSING_SIGN_OFF",
6850 "Missing Signed-off-by: line(s)\n");
6851 } elsif (!$authorsignoff) {
6852 WARN("NO_AUTHOR_SIGN_OFF",
6853 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6854 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006855 }
6856
Andy Whitcroft8905a672007-11-28 16:21:06 -08006857 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006858 if ($summary && !($clean == 1 && $quiet == 1)) {
6859 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006860 print "total: $cnt_error errors, $cnt_warn warnings, " .
6861 (($check)? "$cnt_chk checks, " : "") .
6862 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006863 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006864
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006865 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006866 # If there were any defects found and not already fixing them
6867 if (!$clean and !$fix) {
6868 print << "EOM"
6869
6870NOTE: For some of the reported defects, checkpatch may be able to
6871 mechanically convert to the typical style using --fix or --fix-inplace.
6872EOM
6873 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006874 # If there were whitespace errors which cleanpatch can fix
6875 # then suggest that.
6876 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006877 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006878 print << "EOM"
6879
6880NOTE: Whitespace errors detected.
6881 You may wish to use scripts/cleanpatch or scripts/cleanfile
6882EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006883 }
6884 }
6885
Joe Perchesd752fcc2014-08-06 16:11:05 -07006886 if ($clean == 0 && $fix &&
6887 ("@rawlines" ne "@fixed" ||
6888 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006889 my $newfile = $filename;
6890 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006891 my $linecount = 0;
6892 my $f;
6893
Joe Perchesd752fcc2014-08-06 16:11:05 -07006894 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6895
Joe Perches3705ce52013-07-03 15:05:31 -07006896 open($f, '>', $newfile)
6897 or die "$P: Can't open $newfile for write\n";
6898 foreach my $fixed_line (@fixed) {
6899 $linecount++;
6900 if ($file) {
6901 if ($linecount > 3) {
6902 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006903 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006904 }
6905 } else {
6906 print $f $fixed_line . "\n";
6907 }
6908 }
6909 close($f);
6910
6911 if (!$quiet) {
6912 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006913
Joe Perches3705ce52013-07-03 15:05:31 -07006914Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6915
6916Do _NOT_ trust the results written to this file.
6917Do _NOT_ submit these changes without inspecting them for correctness.
6918
6919This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6920No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006921EOM
6922 }
6923 }
6924
Joe Perchesd8469f12015-06-25 15:03:00 -07006925 if ($quiet == 0) {
6926 print "\n";
6927 if ($clean == 1) {
6928 print "$vname has no obvious style problems and is ready for submission.\n";
6929 } else {
6930 print "$vname has style problems, please review.\n";
6931 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006932 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006933 return $clean;
6934}