blob: 1351e052eb99c4b007cc1bb0210fcf136f9ade62 [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
Joe Perches32f30ca2020-06-04 16:50:40 -0700249die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
250
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700251my $exit = 0;
252
Joe Perches5b579802018-08-21 21:57:33 -0700253my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700254if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700255 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700256 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700257 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700258}
259
Allen Hubbe45107ff2016-08-02 14:04:47 -0700260#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700261if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700262 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700263}
264
John Brooks737c0762017-07-10 15:52:24 -0700265if ($color =~ /^[01]$/) {
266 $color = !$color;
267} elsif ($color =~ /^always$/i) {
268 $color = 1;
269} elsif ($color =~ /^never$/i) {
270 $color = 0;
271} elsif ($color =~ /^auto$/i) {
272 $color = (-t STDOUT);
273} else {
Joe Perches32f30ca2020-06-04 16:50:40 -0700274 die "$P: Invalid color mode: $color\n";
John Brooks737c0762017-07-10 15:52:24 -0700275}
276
Antonio Borneo713a09d2020-04-06 20:11:07 -0700277# skip TAB size 1 to avoid additional checks on $tabsize - 1
Joe Perches32f30ca2020-06-04 16:50:40 -0700278die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
Antonio Borneo713a09d2020-04-06 20:11:07 -0700279
Joe Perches91bfe482013-09-11 14:23:59 -0700280sub hash_save_array_words {
281 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700282
Joe Perches91bfe482013-09-11 14:23:59 -0700283 my @array = split(/,/, join(',', @$arrayRef));
284 foreach my $word (@array) {
285 $word =~ s/\s*\n?$//g;
286 $word =~ s/^\s*//g;
287 $word =~ s/\s+/ /g;
288 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700289
Joe Perches91bfe482013-09-11 14:23:59 -0700290 next if ($word =~ m/^\s*#/);
291 next if ($word =~ m/^\s*$/);
292
293 $hashRef->{$word}++;
294 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700295}
296
Joe Perches91bfe482013-09-11 14:23:59 -0700297sub hash_show_words {
298 my ($hashRef, $prefix) = @_;
299
Joe Perches3c816e42015-06-25 15:03:29 -0700300 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700301 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700302 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700303 print " $word";
304 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700305 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700306 }
307}
308
309hash_save_array_words(\%ignore_type, \@ignore);
310hash_save_array_words(\%use_type, \@use);
311
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800312my $dbg_values = 0;
313my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700314my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700315my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800316for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800317 ## no critic
318 eval "\${dbg_$key} = '$debug{$key}';";
319 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800320}
321
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700322my $rpt_cleaners = 0;
323
Andy Whitcroft8905a672007-11-28 16:21:06 -0800324if ($terse) {
325 $emacs = 1;
326 $quiet++;
327}
328
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700329if ($tree) {
330 if (defined $root) {
331 if (!top_of_kernel_tree($root)) {
332 die "$P: $root: --root does not point at a valid tree\n";
333 }
334 } else {
335 if (top_of_kernel_tree('.')) {
336 $root = '.';
337 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
338 top_of_kernel_tree($1)) {
339 $root = $1;
340 }
341 }
342
343 if (!defined $root) {
344 print "Must be run from the top-level dir. of a kernel tree\n";
345 exit(2);
346 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700347}
348
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700349my $emitted_corrupt = 0;
350
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700351our $Ident = qr{
352 [A-Za-z_][A-Za-z\d_]*
353 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
354 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700355our $Storage = qr{extern|static|asmlinkage};
356our $Sparse = qr{
357 __user|
358 __kernel|
359 __force|
360 __iomem|
361 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800362 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700363 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700364 __refconst|
365 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800366 __rcu|
367 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700368 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800369our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
370our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
371our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
372our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
373our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700374
Wolfram Sang52131292010-03-05 13:43:51 -0800375# Notes to $Attribute:
376# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700377our $Attribute = qr{
378 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700379 __percpu|
380 __nocast|
381 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200382 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700383 __packed__|
384 __packed2__|
385 __naked|
386 __maybe_unused|
387 __always_unused|
388 __noreturn|
389 __used|
390 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800391 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700392 __noclone|
393 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700394 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700395 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700396 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700397 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700398 ____cacheline_aligned|
399 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800400 ____cacheline_internodealigned_in_smp|
401 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700402 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700403our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700404our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700405our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
406our $Lval = qr{$Ident(?:$Member)*};
407
Joe Perches95e2c602013-07-03 15:05:20 -0700408our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
409our $Binary = qr{(?i)0b[01]+$Int_type?};
410our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
411our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700412our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800413our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800414our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
415our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
416our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800417our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700418our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800419our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700420our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700421our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700422our $Operators = qr{
423 <=|>=|==|!=|
424 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700425 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700426 }x;
427
Joe Perches91cb5192014-04-03 14:49:32 -0700428our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
429
Joe Perchesab7e23f2015-04-16 12:44:22 -0700430our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800431our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700432our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700433our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800434our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700435our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800436our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700437our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800438
Joe Perches15662b32011-10-31 17:13:12 -0700439our $NON_ASCII_UTF8 = qr{
440 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700441 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
442 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
443 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
444 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
445 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
446 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
447}x;
448
Joe Perches15662b32011-10-31 17:13:12 -0700449our $UTF8 = qr{
450 [\x09\x0A\x0D\x20-\x7E] # ASCII
451 | $NON_ASCII_UTF8
452}x;
453
Joe Perchese6176fa2015-06-25 15:02:49 -0700454our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800455our $typeOtherOSTypedefs = qr{(?x:
456 u_(?:char|short|int|long) | # bsd
457 u(?:nchar|short|int|long) # sysv
458)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700459our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700460 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700461 atomic_t
462)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700463our $typeTypedefs = qr{(?x:
464 $typeC99Typedefs\b|
465 $typeOtherOSTypedefs\b|
466 $typeKernelTypedefs\b
467)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700468
Joe Perches6d32f7a2015-11-06 16:31:37 -0800469our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
470
Joe Perches691e6692010-03-05 13:43:51 -0800471our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800472 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700473 (?:[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 -0800474 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700475 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700476 panic|
Joe Perches06668722013-11-12 15:10:07 -0800477 MODULE_[A-Z_]+|
478 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800479)};
480
Joe Perchese29a70f2019-03-07 16:28:35 -0800481our $allocFunctions = qr{(?x:
482 (?:(?:devm_)?
483 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
484 kstrdup(?:_const)? |
485 kmemdup(?:_nul)?) |
Christophe JAILLET461e1562020-04-20 18:13:58 -0700486 (?:\w+)?alloc_skb(?:_ip_align)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800487 # dev_alloc_skb/netdev_alloc_skb, et al
488 dma_alloc_coherent
489)};
490
Joe Perches20112472011-07-25 17:13:23 -0700491our $signature_tags = qr{(?xi:
492 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800493 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700494 Acked-by:|
495 Tested-by:|
496 Reviewed-by:|
497 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700498 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700499 To:|
500 Cc:
501)};
502
Joe Perches18130872014-08-06 16:11:22 -0700503our @typeListMisordered = (
504 qr{char\s+(?:un)?signed},
505 qr{int\s+(?:(?:un)?signed\s+)?short\s},
506 qr{int\s+short(?:\s+(?:un)?signed)},
507 qr{short\s+int(?:\s+(?:un)?signed)},
508 qr{(?:un)?signed\s+int\s+short},
509 qr{short\s+(?:un)?signed},
510 qr{long\s+int\s+(?:un)?signed},
511 qr{int\s+long\s+(?:un)?signed},
512 qr{long\s+(?:un)?signed\s+int},
513 qr{int\s+(?:un)?signed\s+long},
514 qr{int\s+(?:un)?signed},
515 qr{int\s+long\s+long\s+(?:un)?signed},
516 qr{long\s+long\s+int\s+(?:un)?signed},
517 qr{long\s+long\s+(?:un)?signed\s+int},
518 qr{long\s+long\s+(?:un)?signed},
519 qr{long\s+(?:un)?signed},
520);
521
Andy Whitcroft8905a672007-11-28 16:21:06 -0800522our @typeList = (
523 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700524 qr{(?:(?:un)?signed\s+)?char},
525 qr{(?:(?:un)?signed\s+)?short\s+int},
526 qr{(?:(?:un)?signed\s+)?short},
527 qr{(?:(?:un)?signed\s+)?int},
528 qr{(?:(?:un)?signed\s+)?long\s+int},
529 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
530 qr{(?:(?:un)?signed\s+)?long\s+long},
531 qr{(?:(?:un)?signed\s+)?long},
532 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800533 qr{float},
534 qr{double},
535 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800536 qr{struct\s+$Ident},
537 qr{union\s+$Ident},
538 qr{enum\s+$Ident},
539 qr{${Ident}_t},
540 qr{${Ident}_handler},
541 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700542 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800543);
Joe Perches938224b2016-01-20 14:59:15 -0800544
545our $C90_int_types = qr{(?x:
546 long\s+long\s+int\s+(?:un)?signed|
547 long\s+long\s+(?:un)?signed\s+int|
548 long\s+long\s+(?:un)?signed|
549 (?:(?:un)?signed\s+)?long\s+long\s+int|
550 (?:(?:un)?signed\s+)?long\s+long|
551 int\s+long\s+long\s+(?:un)?signed|
552 int\s+(?:(?:un)?signed\s+)?long\s+long|
553
554 long\s+int\s+(?:un)?signed|
555 long\s+(?:un)?signed\s+int|
556 long\s+(?:un)?signed|
557 (?:(?:un)?signed\s+)?long\s+int|
558 (?:(?:un)?signed\s+)?long|
559 int\s+long\s+(?:un)?signed|
560 int\s+(?:(?:un)?signed\s+)?long|
561
562 int\s+(?:un)?signed|
563 (?:(?:un)?signed\s+)?int
564)};
565
Alex Dowad485ff232015-06-25 15:02:52 -0700566our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700567our @typeListWithAttr = (
568 @typeList,
569 qr{struct\s+$InitAttribute\s+$Ident},
570 qr{union\s+$InitAttribute\s+$Ident},
571);
572
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700573our @modifierList = (
574 qr{fastcall},
575);
Alex Dowad485ff232015-06-25 15:02:52 -0700576our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800577
Joe Perches24358802014-04-03 14:49:13 -0700578our @mode_permission_funcs = (
579 ["module_param", 3],
580 ["module_param_(?:array|named|string)", 4],
581 ["module_param_array_named", 5],
582 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
583 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700584 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
585 ["IIO_DEV_ATTR_[A-Z_]+", 1],
586 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
587 ["SENSOR_TEMPLATE(?:_2|)", 3],
588 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700589);
590
Joe Perches515a2352014-04-03 14:49:24 -0700591#Create a search pattern for all these functions to speed up a loop below
592our $mode_perms_search = "";
593foreach my $entry (@mode_permission_funcs) {
594 $mode_perms_search .= '|' if ($mode_perms_search ne "");
595 $mode_perms_search .= $entry->[0];
596}
Joe Perches00180462018-02-06 15:38:55 -0800597$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700598
Joe Perches9189c7e2018-09-07 15:26:18 -0700599our %deprecated_apis = (
600 "synchronize_rcu_bh" => "synchronize_rcu",
601 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
602 "call_rcu_bh" => "call_rcu",
603 "rcu_barrier_bh" => "rcu_barrier",
604 "synchronize_sched" => "synchronize_rcu",
605 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
606 "call_rcu_sched" => "call_rcu",
607 "rcu_barrier_sched" => "rcu_barrier",
608 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
609 "cond_synchronize_sched" => "cond_synchronize_rcu",
610);
611
612#Create a search pattern for all these strings to speed up a loop below
613our $deprecated_apis_search = "";
614foreach my $entry (keys %deprecated_apis) {
615 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
616 $deprecated_apis_search .= $entry;
617}
618$deprecated_apis_search = "(?:${deprecated_apis_search})";
619
Joe Perchesb392c642015-04-16 12:44:16 -0700620our $mode_perms_world_writable = qr{
621 S_IWUGO |
622 S_IWOTH |
623 S_IRWXUGO |
624 S_IALLUGO |
625 0[0-7][0-7][2367]
626}x;
627
Joe Perchesf90774e2016-10-11 13:51:47 -0700628our %mode_permission_string_types = (
629 "S_IRWXU" => 0700,
630 "S_IRUSR" => 0400,
631 "S_IWUSR" => 0200,
632 "S_IXUSR" => 0100,
633 "S_IRWXG" => 0070,
634 "S_IRGRP" => 0040,
635 "S_IWGRP" => 0020,
636 "S_IXGRP" => 0010,
637 "S_IRWXO" => 0007,
638 "S_IROTH" => 0004,
639 "S_IWOTH" => 0002,
640 "S_IXOTH" => 0001,
641 "S_IRWXUGO" => 0777,
642 "S_IRUGO" => 0444,
643 "S_IWUGO" => 0222,
644 "S_IXUGO" => 0111,
645);
646
647#Create a search pattern for all these strings to speed up a loop below
648our $mode_perms_string_search = "";
649foreach my $entry (keys %mode_permission_string_types) {
650 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
651 $mode_perms_string_search .= $entry;
652}
Joe Perches00180462018-02-06 15:38:55 -0800653our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
654our $multi_mode_perms_string_search = qr{
655 ${single_mode_perms_string_search}
656 (?:\s*\|\s*${single_mode_perms_string_search})*
657}x;
658
659sub perms_to_octal {
660 my ($string) = @_;
661
662 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
663
664 my $val = "";
665 my $oval = "";
666 my $to = 0;
667 my $curpos = 0;
668 my $lastpos = 0;
669 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
670 $curpos = pos($string);
671 my $match = $2;
672 my $omatch = $1;
673 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
674 $lastpos = $curpos;
675 $to |= $mode_permission_string_types{$match};
676 $val .= '\s*\|\s*' if ($val ne "");
677 $val .= $match;
678 $oval .= $omatch;
679 }
680 $oval =~ s/^\s*\|\s*//;
681 $oval =~ s/\s*\|\s*$//;
682 return sprintf("%04o", $to);
683}
Joe Perchesf90774e2016-10-11 13:51:47 -0700684
Wolfram Sang7840a942010-08-09 17:20:57 -0700685our $allowed_asm_includes = qr{(?x:
686 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700687 memory|
688 time|
689 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700690)};
691# memory.h: ARM has a custom one
692
Kees Cook66b47b42014-10-13 15:51:57 -0700693# Load common spelling mistakes and build regular expression list.
694my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700695my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700696
Joe Perches36061e32014-12-10 15:51:43 -0800697if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800698 while (<$spelling>) {
699 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700700
Joe Perches36061e32014-12-10 15:51:43 -0800701 $line =~ s/\s*\n?$//g;
702 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700703
Joe Perches36061e32014-12-10 15:51:43 -0800704 next if ($line =~ m/^\s*#/);
705 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700706
Joe Perches36061e32014-12-10 15:51:43 -0800707 my ($suspect, $fix) = split(/\|\|/, $line);
708
Joe Perches36061e32014-12-10 15:51:43 -0800709 $spelling_fix{$suspect} = $fix;
710 }
711 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800712} else {
713 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700714}
Kees Cook66b47b42014-10-13 15:51:57 -0700715
Joe Perchesebfd7d62015-04-16 12:44:14 -0700716if ($codespell) {
717 if (open(my $spelling, '<', $codespellfile)) {
718 while (<$spelling>) {
719 my $line = $_;
720
721 $line =~ s/\s*\n?$//g;
722 $line =~ s/^\s*//g;
723
724 next if ($line =~ m/^\s*#/);
725 next if ($line =~ m/^\s*$/);
726 next if ($line =~ m/, disabled/i);
727
728 $line =~ s/,.*$//;
729
730 my ($suspect, $fix) = split(/->/, $line);
731
732 $spelling_fix{$suspect} = $fix;
733 }
734 close($spelling);
735 } else {
736 warn "No codespell typos will be found - file '$codespellfile': $!\n";
737 }
738}
739
740$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
741
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700742sub read_words {
743 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700744
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700745 if (open(my $words, '<', $file)) {
746 while (<$words>) {
747 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700748
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700749 $line =~ s/\s*\n?$//g;
750 $line =~ s/^\s*//g;
751
752 next if ($line =~ m/^\s*#/);
753 next if ($line =~ m/^\s*$/);
754 if ($line =~ /\s/) {
755 print("$file: '$line' invalid - ignored\n");
756 next;
757 }
758
759 $$wordsRef .= '|' if ($$wordsRef ne "");
760 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700761 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700762 close($file);
763 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700764 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700765
766 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700767}
768
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700769my $const_structs = "";
770read_words(\$const_structs, $conststructsfile)
771 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
772
773my $typeOtherTypedefs = "";
774if (length($typedefsfile)) {
775 read_words(\$typeOtherTypedefs, $typedefsfile)
776 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
777}
778$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
779
Andy Whitcroft8905a672007-11-28 16:21:06 -0800780sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700781 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
782 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700783 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700784 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700785 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700786 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700787 (?:$typeTypedefs\b)|
788 (?:${all}\b)
789 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800790 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700791 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800792 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800793 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700794 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700795 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800796 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700797 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800798 }x;
Joe Perches18130872014-08-06 16:11:22 -0700799 $NonptrTypeMisordered = qr{
800 (?:$Modifier\s+|const\s+)*
801 (?:
802 (?:${Misordered}\b)
803 )
804 (?:\s+$Modifier|\s+const)*
805 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700806 $NonptrTypeWithAttr = qr{
807 (?:$Modifier\s+|const\s+)*
808 (?:
809 (?:typeof|__typeof__)\s*\([^\)]*\)|
810 (?:$typeTypedefs\b)|
811 (?:${allWithAttr}\b)
812 )
813 (?:\s+$Modifier|\s+const)*
814 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800815 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700816 $NonptrType
Antonio Borneo7b184962020-04-06 20:11:04 -0700817 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700818 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800819 }x;
Joe Perches18130872014-08-06 16:11:22 -0700820 $TypeMisordered = qr{
821 $NonptrTypeMisordered
Antonio Borneo7b184962020-04-06 20:11:04 -0700822 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Perches18130872014-08-06 16:11:22 -0700823 (?:\s+$Inline|\s+$Modifier)*
824 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700825 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700826 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800827}
828build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700829
Joe Perches7d2367a2011-07-25 17:13:22 -0700830our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700831
832# Using $balanced_parens, $LvalOrFunc, or $FuncArg
833# requires at least perl version v5.10.0
834# Any use must be runtime checked with $^V
835
836our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700837our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800838our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700839
Joe Perchesf8422302014-08-06 16:11:31 -0700840our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700841 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700842 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700843 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
844 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700845)};
846
Joe Perches7d2367a2011-07-25 17:13:22 -0700847sub deparenthesize {
848 my ($string) = @_;
849 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700850
851 while ($string =~ /^\s*\(.*\)\s*$/) {
852 $string =~ s@^\s*\(\s*@@;
853 $string =~ s@\s*\)\s*$@@;
854 }
855
Joe Perches7d2367a2011-07-25 17:13:22 -0700856 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700857
Joe Perches7d2367a2011-07-25 17:13:22 -0700858 return $string;
859}
860
Joe Perches34456862013-07-03 15:05:34 -0700861sub seed_camelcase_file {
862 my ($file) = @_;
863
864 return if (!(-f $file));
865
866 local $/;
867
868 open(my $include_file, '<', "$file")
869 or warn "$P: Can't read '$file' $!\n";
870 my $text = <$include_file>;
871 close($include_file);
872
873 my @lines = split('\n', $text);
874
875 foreach my $line (@lines) {
876 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
877 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
878 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800879 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
880 $camelcase{$1} = 1;
881 } 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 -0700882 $camelcase{$1} = 1;
883 }
884 }
885}
886
Joe Perchescd28b112019-12-04 16:52:09 -0800887our %maintained_status = ();
888
Joe Perches85b0ee12016-10-11 13:51:44 -0700889sub is_maintained_obsolete {
890 my ($filename) = @_;
891
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800892 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700893
Joe Perchescd28b112019-12-04 16:52:09 -0800894 if (!exists($maintained_status{$filename})) {
895 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
896 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700897
Joe Perchescd28b112019-12-04 16:52:09 -0800898 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700899}
900
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700901sub is_SPDX_License_valid {
902 my ($license) = @_;
903
Joe Perches56294112018-08-21 21:58:04 -0700904 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700905
Joe Perches56294112018-08-21 21:58:04 -0700906 my $root_path = abs_path($root);
907 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700908 return 0 if ($status ne "");
909 return 1;
910}
911
Joe Perches34456862013-07-03 15:05:34 -0700912my $camelcase_seeded = 0;
913sub seed_camelcase_includes {
914 return if ($camelcase_seeded);
915
916 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700917 my $camelcase_cache = "";
918 my @include_files = ();
919
920 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700921
Richard Genoud3645e322014-02-10 14:25:32 -0800922 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700923 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -0700924 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700925 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700926 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700927 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700928 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700929 @include_files = split('\n', $files);
930 foreach my $file (@include_files) {
931 my $date = POSIX::strftime("%Y%m%d%H%M",
932 localtime((stat $file)[9]));
933 $last_mod_date = $date if ($last_mod_date < $date);
934 }
935 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700936 }
Joe Perchesc707a812013-07-08 16:00:43 -0700937
938 if ($camelcase_cache ne "" && -f $camelcase_cache) {
939 open(my $camelcase_file, '<', "$camelcase_cache")
940 or warn "$P: Can't read '$camelcase_cache' $!\n";
941 while (<$camelcase_file>) {
942 chomp;
943 $camelcase{$_} = 1;
944 }
945 close($camelcase_file);
946
947 return;
948 }
949
Richard Genoud3645e322014-02-10 14:25:32 -0800950 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700951 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700952 @include_files = split('\n', $files);
953 }
954
Joe Perches34456862013-07-03 15:05:34 -0700955 foreach my $file (@include_files) {
956 seed_camelcase_file($file);
957 }
Joe Perches351b2a12013-07-03 15:05:36 -0700958
Joe Perchesc707a812013-07-08 16:00:43 -0700959 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700960 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700961 open(my $camelcase_file, '>', "$camelcase_cache")
962 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700963 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
964 print $camelcase_file ("$_\n");
965 }
966 close($camelcase_file);
967 }
Joe Perches34456862013-07-03 15:05:34 -0700968}
969
Joe Perchesd311cd42014-08-06 16:10:57 -0700970sub git_commit_info {
971 my ($commit, $id, $desc) = @_;
972
973 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
974
Joe Perchesdbbf8692019-09-25 16:46:52 -0700975 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -0700976 $output =~ s/^\s*//gm;
977 my @lines = split("\n", $output);
978
Joe Perches0d7835f2015-02-13 14:38:35 -0800979 return ($id, $desc) if ($#lines < 0);
980
Sean Christopherson5a7f4452019-09-25 16:46:49 -0700981 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -0700982# Maybe one day convert this block of bash into something that returns
983# all matching commit ids, but it's very slow...
984#
985# echo "checking commits $1..."
986# git rev-list --remotes | grep -i "^$1" |
987# while read line ; do
988# git log --format='%H %s' -1 $line |
989# echo "commit $(cut -c 1-12,41-)"
990# done
991 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700992 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700993 } else {
994 $id = substr($lines[0], 0, 12);
995 $desc = substr($lines[0], 41);
996 }
997
998 return ($id, $desc);
999}
1000
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001001$chk_signoff = 0 if ($file);
1002
Andy Whitcroft00df3442007-06-08 13:47:06 -07001003my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001004my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001005my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001006my @fixed_inserted = ();
1007my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001008my $fixlinenr = -1;
1009
Du, Changbin4a593c32016-05-20 17:04:16 -07001010# If input is git commits, extract all commits from the commit expressions.
1011# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1012die "$P: No git repository found\n" if ($git && !-e ".git");
1013
1014if ($git) {
1015 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001016 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001017 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001018 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1019 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001020 } elsif ($commit_expr =~ m/\.\./) {
1021 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001022 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001023 $git_range = "-1 $commit_expr";
1024 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001025 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001026 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001027 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1028 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001029 my $sha1 = $1;
1030 my $subject = $2;
1031 unshift(@commits, $sha1);
1032 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001033 }
1034 }
1035 die "$P: no git commits after extraction!\n" if (@commits == 0);
1036 @ARGV = @commits;
1037}
1038
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001039my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001040$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001041for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001042 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -07001043 if ($git) {
1044 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1045 die "$P: $filename: git format-patch failed - $!\n";
1046 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001047 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001048 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001049 } elsif ($filename eq '-') {
1050 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001051 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001052 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001053 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001054 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001055 if ($filename eq '-') {
1056 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001057 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001058 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001059 } else {
1060 $vname = $filename;
1061 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001062 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001063 chomp;
1064 push(@rawlines, $_);
Geert Uytterhoevenc7f574d2020-06-04 16:50:43 -07001065 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001066 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001067 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001068
1069 if ($#ARGV > 0 && $quiet == 0) {
1070 print '-' x length($vname) . "\n";
1071 print "$vname\n";
1072 print '-' x length($vname) . "\n";
1073 }
1074
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001075 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001076 $exit = 1;
1077 }
1078 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001079 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001080 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001081 @fixed_inserted = ();
1082 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001083 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001084 @modifierListFile = ();
1085 @typeListFile = ();
1086 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001087}
1088
Joe Perchesd8469f12015-06-25 15:03:00 -07001089if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001090 hash_show_words(\%use_type, "Used");
1091 hash_show_words(\%ignore_type, "Ignored");
1092
Joe Perches5b579802018-08-21 21:57:33 -07001093 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001094 print << "EOM"
1095
1096NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001097 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001098EOM
1099 }
1100 if ($exit) {
1101 print << "EOM"
1102
1103NOTE: If any of the errors are false positives, please report
1104 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1105EOM
1106 }
1107}
1108
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001109exit($exit);
1110
1111sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001112 my ($root) = @_;
1113
1114 my @tree_check = (
1115 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1116 "README", "Documentation", "arch", "include", "drivers",
1117 "fs", "init", "ipc", "kernel", "lib", "scripts",
1118 );
1119
1120 foreach my $check (@tree_check) {
1121 if (! -e $root . '/' . $check) {
1122 return 0;
1123 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001124 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001125 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001126}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001127
Joe Perches20112472011-07-25 17:13:23 -07001128sub parse_email {
1129 my ($formatted_email) = @_;
1130
1131 my $name = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001132 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001133 my $address = "";
1134 my $comment = "";
1135
1136 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1137 $name = $1;
1138 $address = $2;
1139 $comment = $3 if defined $3;
1140 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1141 $address = $1;
1142 $comment = $2 if defined $2;
1143 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1144 $address = $1;
1145 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001146 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001147 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001148 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001149 $name =~ s/^\"|\"$//g;
1150 # If there's a name left after stripping spaces and
1151 # leading quotes, and the address doesn't have both
1152 # leading and trailing angle brackets, the address
1153 # is invalid. ie:
1154 # "joe smith joe@smith.com" bad
1155 # "joe smith <joe@smith.com" bad
1156 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1157 $name = "";
1158 $address = "";
1159 $comment = "";
1160 }
1161 }
1162
Joe Perches3705ce52013-07-03 15:05:31 -07001163 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001164 $name =~ s/^\"|\"$//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001165 $name =~ s/(\s*\([^\)]+\))\s*//;
1166 if (defined($1)) {
1167 $name_comment = trim($1);
1168 }
Joe Perches3705ce52013-07-03 15:05:31 -07001169 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001170 $address =~ s/^\<|\>$//g;
1171
1172 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1173 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1174 $name = "\"$name\"";
1175 }
1176
Joe Perchesdfa05c22020-04-06 20:10:48 -07001177 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001178}
1179
1180sub format_email {
1181 my ($name, $address) = @_;
1182
1183 my $formatted_email;
1184
Joe Perches3705ce52013-07-03 15:05:31 -07001185 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001186 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001187 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001188
1189 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1190 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1191 $name = "\"$name\"";
1192 }
1193
1194 if ("$name" eq "") {
1195 $formatted_email = "$address";
1196 } else {
1197 $formatted_email = "$name <$address>";
1198 }
1199
1200 return $formatted_email;
1201}
1202
Joe Perchesdfa05c22020-04-06 20:10:48 -07001203sub reformat_email {
1204 my ($email) = @_;
1205
1206 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1207 return format_email($email_name, $email_address);
1208}
1209
1210sub same_email_addresses {
1211 my ($email1, $email2) = @_;
1212
1213 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1214 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1215
1216 return $email1_name eq $email2_name &&
1217 $email1_address eq $email2_address;
1218}
1219
Joe Perchesd311cd42014-08-06 16:10:57 -07001220sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001221 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001222
Joe Perchesbd474ca2014-08-06 16:11:10 -07001223 foreach my $path (split(/:/, $ENV{PATH})) {
1224 if (-e "$path/$bin") {
1225 return "$path/$bin";
1226 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001227 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001228
Joe Perchesbd474ca2014-08-06 16:11:10 -07001229 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001230}
1231
Joe Perches000d1cc12011-07-25 17:13:25 -07001232sub which_conf {
1233 my ($conf) = @_;
1234
1235 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1236 if (-e "$path/$conf") {
1237 return "$path/$conf";
1238 }
1239 }
1240
1241 return "";
1242}
1243
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001244sub expand_tabs {
1245 my ($str) = @_;
1246
1247 my $res = '';
1248 my $n = 0;
1249 for my $c (split(//, $str)) {
1250 if ($c eq "\t") {
1251 $res .= ' ';
1252 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001253 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001254 $res .= ' ';
1255 }
1256 next;
1257 }
1258 $res .= $c;
1259 $n++;
1260 }
1261
1262 return $res;
1263}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001264sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001265 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001266 return $res;
1267}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001268
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001269sub line_stats {
1270 my ($line) = @_;
1271
1272 # Drop the diff line leader and expand tabs
1273 $line =~ s/^.//;
1274 $line = expand_tabs($line);
1275
1276 # Pick the indent from the front of the line.
1277 my ($white) = ($line =~ /^(\s*)/);
1278
1279 return (length($line), length($white));
1280}
1281
Andy Whitcroft773647a2008-03-28 14:15:58 -07001282my $sanitise_quote = '';
1283
1284sub sanitise_line_reset {
1285 my ($in_comment) = @_;
1286
1287 if ($in_comment) {
1288 $sanitise_quote = '*/';
1289 } else {
1290 $sanitise_quote = '';
1291 }
1292}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001293sub sanitise_line {
1294 my ($line) = @_;
1295
1296 my $res = '';
1297 my $l = '';
1298
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001299 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001300 my $off = 0;
1301 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001302
Andy Whitcroft773647a2008-03-28 14:15:58 -07001303 # Always copy over the diff marker.
1304 $res = substr($line, 0, 1);
1305
1306 for ($off = 1; $off < length($line); $off++) {
1307 $c = substr($line, $off, 1);
1308
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001309 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001310 # and end, all to $;.
1311 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1312 $sanitise_quote = '*/';
1313
1314 substr($res, $off, 2, "$;$;");
1315 $off++;
1316 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001317 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001318 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001319 $sanitise_quote = '';
1320 substr($res, $off, 2, "$;$;");
1321 $off++;
1322 next;
1323 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001324 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1325 $sanitise_quote = '//';
1326
1327 substr($res, $off, 2, $sanitise_quote);
1328 $off++;
1329 next;
1330 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001331
1332 # A \ in a string means ignore the next character.
1333 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1334 $c eq "\\") {
1335 substr($res, $off, 2, 'XX');
1336 $off++;
1337 next;
1338 }
1339 # Regular quotes.
1340 if ($c eq "'" || $c eq '"') {
1341 if ($sanitise_quote eq '') {
1342 $sanitise_quote = $c;
1343
1344 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001345 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001346 } elsif ($sanitise_quote eq $c) {
1347 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001348 }
1349 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001350
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001351 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001352 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1353 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001354 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1355 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001356 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1357 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001358 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001359 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001360 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001361 }
1362
Daniel Walker113f04a2009-09-21 17:04:35 -07001363 if ($sanitise_quote eq '//') {
1364 $sanitise_quote = '';
1365 }
1366
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001367 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001368 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001369 my $clean = 'X' x length($1);
1370 $res =~ s@\<.*\>@<$clean>@;
1371
1372 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001373 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001374 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001375 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001376 }
1377
Joe Perchesdadf6802016-08-02 14:04:33 -07001378 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1379 my $match = $1;
1380 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1381 }
1382
Andy Whitcroft00df3442007-06-08 13:47:06 -07001383 return $res;
1384}
1385
Joe Perchesa6962d72013-04-29 16:18:13 -07001386sub get_quoted_string {
1387 my ($line, $rawline) = @_;
1388
Joe Perches478b1792018-04-10 16:33:34 -07001389 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001390 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001391 return substr($rawline, $-[0], $+[0] - $-[0]);
1392}
1393
Andy Whitcroft8905a672007-11-28 16:21:06 -08001394sub ctx_statement_block {
1395 my ($linenr, $remain, $off) = @_;
1396 my $line = $linenr - 1;
1397 my $blk = '';
1398 my $soff = $off;
1399 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001400 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001401
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001402 my $loff = 0;
1403
Andy Whitcroft8905a672007-11-28 16:21:06 -08001404 my $type = '';
1405 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001406 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001407 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001408 my $c;
1409 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001410
1411 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001412 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001413 @stack = (['', 0]) if ($#stack == -1);
1414
Andy Whitcroft773647a2008-03-28 14:15:58 -07001415 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001416 # If we are about to drop off the end, pull in more
1417 # context.
1418 if ($off >= $len) {
1419 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001420 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001421 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001422 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001423 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001424 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001425 $len = length($blk);
1426 $line++;
1427 last;
1428 }
1429 # Bail if there is no further context.
1430 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001431 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001432 last;
1433 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001434 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1435 $level++;
1436 $type = '#';
1437 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001438 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001439 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001440 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001441 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001442
Andy Whitcroft773647a2008-03-28 14:15:58 -07001443 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001444
1445 # Handle nested #if/#else.
1446 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1447 push(@stack, [ $type, $level ]);
1448 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1449 ($type, $level) = @{$stack[$#stack - 1]};
1450 } elsif ($remainder =~ /^#\s*endif\b/) {
1451 ($type, $level) = @{pop(@stack)};
1452 }
1453
Andy Whitcroft8905a672007-11-28 16:21:06 -08001454 # Statement ends at the ';' or a close '}' at the
1455 # outermost level.
1456 if ($level == 0 && $c eq ';') {
1457 last;
1458 }
1459
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001460 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001461 if ($level == 0 && $coff_set == 0 &&
1462 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1463 $remainder =~ /^(else)(?:\s|{)/ &&
1464 $remainder !~ /^else\s+if\b/) {
1465 $coff = $off + length($1) - 1;
1466 $coff_set = 1;
1467 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1468 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001469 }
1470
Andy Whitcroft8905a672007-11-28 16:21:06 -08001471 if (($type eq '' || $type eq '(') && $c eq '(') {
1472 $level++;
1473 $type = '(';
1474 }
1475 if ($type eq '(' && $c eq ')') {
1476 $level--;
1477 $type = ($level != 0)? '(' : '';
1478
1479 if ($level == 0 && $coff < $soff) {
1480 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001481 $coff_set = 1;
1482 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001483 }
1484 }
1485 if (($type eq '' || $type eq '{') && $c eq '{') {
1486 $level++;
1487 $type = '{';
1488 }
1489 if ($type eq '{' && $c eq '}') {
1490 $level--;
1491 $type = ($level != 0)? '{' : '';
1492
1493 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001494 if (substr($blk, $off + 1, 1) eq ';') {
1495 $off++;
1496 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001497 last;
1498 }
1499 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001500 # Preprocessor commands end at the newline unless escaped.
1501 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1502 $level--;
1503 $type = '';
1504 $off++;
1505 last;
1506 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001507 $off++;
1508 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001509 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001510 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001511 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001512 $line++;
1513 $remain--;
1514 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001515
1516 my $statement = substr($blk, $soff, $off - $soff + 1);
1517 my $condition = substr($blk, $soff, $coff - $soff + 1);
1518
1519 #warn "STATEMENT<$statement>\n";
1520 #warn "CONDITION<$condition>\n";
1521
Andy Whitcroft773647a2008-03-28 14:15:58 -07001522 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001523
1524 return ($statement, $condition,
1525 $line, $remain + 1, $off - $loff + 1, $level);
1526}
1527
Andy Whitcroftcf655042008-03-04 14:28:20 -08001528sub statement_lines {
1529 my ($stmt) = @_;
1530
1531 # Strip the diff line prefixes and rip blank lines at start and end.
1532 $stmt =~ s/(^|\n)./$1/g;
1533 $stmt =~ s/^\s*//;
1534 $stmt =~ s/\s*$//;
1535
1536 my @stmt_lines = ($stmt =~ /\n/g);
1537
1538 return $#stmt_lines + 2;
1539}
1540
1541sub statement_rawlines {
1542 my ($stmt) = @_;
1543
1544 my @stmt_lines = ($stmt =~ /\n/g);
1545
1546 return $#stmt_lines + 2;
1547}
1548
1549sub statement_block_size {
1550 my ($stmt) = @_;
1551
1552 $stmt =~ s/(^|\n)./$1/g;
1553 $stmt =~ s/^\s*{//;
1554 $stmt =~ s/}\s*$//;
1555 $stmt =~ s/^\s*//;
1556 $stmt =~ s/\s*$//;
1557
1558 my @stmt_lines = ($stmt =~ /\n/g);
1559 my @stmt_statements = ($stmt =~ /;/g);
1560
1561 my $stmt_lines = $#stmt_lines + 2;
1562 my $stmt_statements = $#stmt_statements + 1;
1563
1564 if ($stmt_lines > $stmt_statements) {
1565 return $stmt_lines;
1566 } else {
1567 return $stmt_statements;
1568 }
1569}
1570
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001571sub ctx_statement_full {
1572 my ($linenr, $remain, $off) = @_;
1573 my ($statement, $condition, $level);
1574
1575 my (@chunks);
1576
Andy Whitcroftcf655042008-03-04 14:28:20 -08001577 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001578 ($statement, $condition, $linenr, $remain, $off, $level) =
1579 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001580 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001581 push(@chunks, [ $condition, $statement ]);
1582 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1583 return ($level, $linenr, @chunks);
1584 }
1585
1586 # Pull in the following conditional/block pairs and see if they
1587 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001588 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001589 ($statement, $condition, $linenr, $remain, $off, $level) =
1590 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001591 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001592 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001593 #print "C: push\n";
1594 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001595 }
1596
1597 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001598}
1599
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001600sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001601 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001602 my $line;
1603 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001604 my $blk = '';
1605 my @o;
1606 my @c;
1607 my @res = ();
1608
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001609 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001610 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001611 for ($line = $start; $remain > 0; $line++) {
1612 next if ($rawlines[$line] =~ /^-/);
1613 $remain--;
1614
1615 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001616
1617 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001618 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001619 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001620 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001621 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001622 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001623 $level = pop(@stack);
1624 }
1625
Andy Whitcroft01464f32010-10-26 14:23:19 -07001626 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001627 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1628 if ($off > 0) {
1629 $off--;
1630 next;
1631 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001632
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001633 if ($c eq $close && $level > 0) {
1634 $level--;
1635 last if ($level == 0);
1636 } elsif ($c eq $open) {
1637 $level++;
1638 }
1639 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001640
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001641 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001642 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001643 }
1644
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001645 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001646 }
1647
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001648 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001649}
1650sub ctx_block_outer {
1651 my ($linenr, $remain) = @_;
1652
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001653 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1654 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001655}
1656sub ctx_block {
1657 my ($linenr, $remain) = @_;
1658
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001659 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1660 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001661}
1662sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001663 my ($linenr, $remain, $off) = @_;
1664
1665 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1666 return @r;
1667}
1668sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001669 my ($linenr, $remain) = @_;
1670
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001671 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001672}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001673sub ctx_statement_level {
1674 my ($linenr, $remain, $off) = @_;
1675
1676 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1677}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001678
1679sub ctx_locate_comment {
1680 my ($first_line, $end_line) = @_;
1681
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001682 # If c99 comment on the current line, or the line before or after
1683 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1684 return $current_comment if (defined $current_comment);
1685 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1686 return $current_comment if (defined $current_comment);
1687 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1688 return $current_comment if (defined $current_comment);
1689
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001690 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001691 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001692 return $current_comment if (defined $current_comment);
1693
1694 # Look through the context and try and figure out if there is a
1695 # comment.
1696 my $in_comment = 0;
1697 $current_comment = '';
1698 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001699 my $line = $rawlines[$linenr - 1];
1700 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001701 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1702 $in_comment = 1;
1703 }
1704 if ($line =~ m@/\*@) {
1705 $in_comment = 1;
1706 }
1707 if (!$in_comment && $current_comment ne '') {
1708 $current_comment = '';
1709 }
1710 $current_comment .= $line . "\n" if ($in_comment);
1711 if ($line =~ m@\*/@) {
1712 $in_comment = 0;
1713 }
1714 }
1715
1716 chomp($current_comment);
1717 return($current_comment);
1718}
1719sub ctx_has_comment {
1720 my ($first_line, $end_line) = @_;
1721 my $cmt = ctx_locate_comment($first_line, $end_line);
1722
Andy Whitcroft00df3442007-06-08 13:47:06 -07001723 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001724 ##print "CMMT: $cmt\n";
1725
1726 return ($cmt ne '');
1727}
1728
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001729sub raw_line {
1730 my ($linenr, $cnt) = @_;
1731
1732 my $offset = $linenr - 1;
1733 $cnt++;
1734
1735 my $line;
1736 while ($cnt) {
1737 $line = $rawlines[$offset++];
1738 next if (defined($line) && $line =~ /^-/);
1739 $cnt--;
1740 }
1741
1742 return $line;
1743}
1744
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001745sub get_stat_real {
1746 my ($linenr, $lc) = @_;
1747
1748 my $stat_real = raw_line($linenr, 0);
1749 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1750 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1751 }
1752
1753 return $stat_real;
1754}
1755
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001756sub get_stat_here {
1757 my ($linenr, $cnt, $here) = @_;
1758
1759 my $herectx = $here . "\n";
1760 for (my $n = 0; $n < $cnt; $n++) {
1761 $herectx .= raw_line($linenr, $n) . "\n";
1762 }
1763
1764 return $herectx;
1765}
1766
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001767sub cat_vet {
1768 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001769 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001770
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001771 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001772 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1773 $res .= $1;
1774 if ($2 ne '') {
1775 $coded = sprintf("^%c", unpack('C', $2) + 64);
1776 $res .= $coded;
1777 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001778 }
1779 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001780
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001781 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001782}
1783
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001784my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001785my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001786my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001787my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001788
1789sub annotate_reset {
1790 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001791 $av_pending = '_';
1792 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001793 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001794}
1795
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001796sub annotate_values {
1797 my ($stream, $type) = @_;
1798
1799 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001800 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001801 my $cur = $stream;
1802
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001803 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001804
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001805 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001806 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001807 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001808 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001809 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001810 print "WS($1)\n" if ($dbg_values > 1);
1811 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001812 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001813 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001814 }
1815
Florian Micklerc023e4732011-01-12 16:59:58 -08001816 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001817 print "CAST($1)\n" if ($dbg_values > 1);
1818 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001819 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001820
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001821 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001822 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001823 $type = 'T';
1824
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001825 } elsif ($cur =~ /^($Modifier)\s*/) {
1826 print "MODIFIER($1)\n" if ($dbg_values > 1);
1827 $type = 'T';
1828
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001829 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001830 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001831 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001832 push(@av_paren_type, $type);
1833 if ($2 ne '') {
1834 $av_pending = 'N';
1835 }
1836 $type = 'E';
1837
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001838 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001839 print "UNDEF($1)\n" if ($dbg_values > 1);
1840 $av_preprocessor = 1;
1841 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001842
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001843 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001844 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001845 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001846
1847 push(@av_paren_type, $type);
1848 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001849 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001850
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001851 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001852 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1853 $av_preprocessor = 1;
1854
1855 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1856
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001857 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001858
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001859 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001860 print "PRE_END($1)\n" if ($dbg_values > 1);
1861
1862 $av_preprocessor = 1;
1863
1864 # Assume all arms of the conditional end as this
1865 # one does, and continue as if the #endif was not here.
1866 pop(@av_paren_type);
1867 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001868 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001869
1870 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001871 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001872
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001873 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1874 print "ATTR($1)\n" if ($dbg_values > 1);
1875 $av_pending = $type;
1876 $type = 'N';
1877
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001878 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001879 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001880 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001881 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001882 }
1883 $type = 'N';
1884
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001885 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001886 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001887 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001888 $type = 'N';
1889
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001890 } elsif ($cur =~/^(case)/o) {
1891 print "CASE($1)\n" if ($dbg_values > 1);
1892 $av_pend_colon = 'C';
1893 $type = 'N';
1894
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001895 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001896 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001897 $type = 'N';
1898
1899 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001900 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001901 push(@av_paren_type, $av_pending);
1902 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001903 $type = 'N';
1904
1905 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001906 my $new_type = pop(@av_paren_type);
1907 if ($new_type ne '_') {
1908 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001909 print "PAREN('$1') -> $type\n"
1910 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001911 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001912 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001913 }
1914
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001915 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001916 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001917 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001918 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001919
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001920 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1921 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001922 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001923 } elsif ($type eq 'E') {
1924 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001925 }
1926 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1927 $type = 'V';
1928
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001929 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001930 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001931 $type = 'V';
1932
1933 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001934 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001935 $type = 'N';
1936
Andy Whitcroftcf655042008-03-04 14:28:20 -08001937 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001938 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001939 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001940 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001941
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001942 } elsif ($cur =~/^(,)/) {
1943 print "COMMA($1)\n" if ($dbg_values > 1);
1944 $type = 'C';
1945
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001946 } elsif ($cur =~ /^(\?)/o) {
1947 print "QUESTION($1)\n" if ($dbg_values > 1);
1948 $type = 'N';
1949
1950 } elsif ($cur =~ /^(:)/o) {
1951 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1952
1953 substr($var, length($res), 1, $av_pend_colon);
1954 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1955 $type = 'E';
1956 } else {
1957 $type = 'N';
1958 }
1959 $av_pend_colon = 'O';
1960
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001961 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001962 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001963 $type = 'N';
1964
Andy Whitcroft0d413862008-10-15 22:02:16 -07001965 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001966 my $variant;
1967
1968 print "OPV($1)\n" if ($dbg_values > 1);
1969 if ($type eq 'V') {
1970 $variant = 'B';
1971 } else {
1972 $variant = 'U';
1973 }
1974
1975 substr($var, length($res), 1, $variant);
1976 $type = 'N';
1977
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001978 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001979 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001980 if ($1 ne '++' && $1 ne '--') {
1981 $type = 'N';
1982 }
1983
1984 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001985 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001986 }
1987 if (defined $1) {
1988 $cur = substr($cur, length($1));
1989 $res .= $type x length($1);
1990 }
1991 }
1992
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001993 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001994}
1995
Andy Whitcroft8905a672007-11-28 16:21:06 -08001996sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001997 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001998 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001999 ^(?:
2000 $Modifier|
2001 $Storage|
2002 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002003 DEFINE_\S+
2004 )$|
2005 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002006 goto|
2007 return|
2008 case|
2009 else|
2010 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002011 do|
2012 \#|
2013 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002014 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002015 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002016 )}x;
2017 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2018 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002019 # Check for modifiers.
2020 $possible =~ s/\s*$Storage\s*//g;
2021 $possible =~ s/\s*$Sparse\s*//g;
2022 if ($possible =~ /^\s*$/) {
2023
2024 } elsif ($possible =~ /\s/) {
2025 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002026 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002027 if ($modifier !~ $notPermitted) {
2028 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002029 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002030 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002031 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002032
2033 } else {
2034 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002035 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002036 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002037 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002038 } else {
2039 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002040 }
2041}
2042
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002043my $prefix = '';
2044
Joe Perches000d1cc12011-07-25 17:13:25 -07002045sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002046 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002047
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002048 $type =~ tr/[a-z]/[A-Z]/;
2049
Joe Perchescbec18a2014-04-03 14:49:19 -07002050 return defined $use_type{$type} if (scalar keys %use_type > 0);
2051
2052 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002053}
2054
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002055sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002056 my ($level, $type, $msg) = @_;
2057
2058 if (!show_type($type) ||
2059 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002060 return 0;
2061 }
Joe Perches57230292015-06-25 15:03:03 -07002062 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002063 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002064 if ($level eq 'ERROR') {
2065 $output .= RED;
2066 } elsif ($level eq 'WARNING') {
2067 $output .= YELLOW;
2068 } else {
2069 $output .= GREEN;
2070 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002071 }
Joe Perches57230292015-06-25 15:03:03 -07002072 $output .= $prefix . $level . ':';
2073 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002074 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002075 $output .= "$type:";
2076 }
John Brooks737c0762017-07-10 15:52:24 -07002077 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002078 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002079
2080 if ($showfile) {
2081 my @lines = split("\n", $output, -1);
2082 splice(@lines, 1, 1);
2083 $output = join("\n", @lines);
2084 }
Joe Perches57230292015-06-25 15:03:03 -07002085 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002086
Joe Perches57230292015-06-25 15:03:03 -07002087 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002088
2089 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002090}
Joe Perchescbec18a2014-04-03 14:49:19 -07002091
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002092sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002093 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002094}
Joe Perches000d1cc12011-07-25 17:13:25 -07002095
Joe Perchesd752fcc2014-08-06 16:11:05 -07002096sub fixup_current_range {
2097 my ($lineRef, $offset, $length) = @_;
2098
2099 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2100 my $o = $1;
2101 my $l = $2;
2102 my $no = $o + $offset;
2103 my $nl = $l + $length;
2104 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2105 }
2106}
2107
2108sub fix_inserted_deleted_lines {
2109 my ($linesRef, $insertedRef, $deletedRef) = @_;
2110
2111 my $range_last_linenr = 0;
2112 my $delta_offset = 0;
2113
2114 my $old_linenr = 0;
2115 my $new_linenr = 0;
2116
2117 my $next_insert = 0;
2118 my $next_delete = 0;
2119
2120 my @lines = ();
2121
2122 my $inserted = @{$insertedRef}[$next_insert++];
2123 my $deleted = @{$deletedRef}[$next_delete++];
2124
2125 foreach my $old_line (@{$linesRef}) {
2126 my $save_line = 1;
2127 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002128 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002129 $delta_offset = 0;
2130 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2131 $range_last_linenr = $new_linenr;
2132 fixup_current_range(\$line, $delta_offset, 0);
2133 }
2134
2135 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2136 $deleted = @{$deletedRef}[$next_delete++];
2137 $save_line = 0;
2138 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2139 }
2140
2141 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2142 push(@lines, ${$inserted}{'LINE'});
2143 $inserted = @{$insertedRef}[$next_insert++];
2144 $new_linenr++;
2145 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2146 }
2147
2148 if ($save_line) {
2149 push(@lines, $line);
2150 $new_linenr++;
2151 }
2152
2153 $old_linenr++;
2154 }
2155
2156 return @lines;
2157}
2158
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002159sub fix_insert_line {
2160 my ($linenr, $line) = @_;
2161
2162 my $inserted = {
2163 LINENR => $linenr,
2164 LINE => $line,
2165 };
2166 push(@fixed_inserted, $inserted);
2167}
2168
2169sub fix_delete_line {
2170 my ($linenr, $line) = @_;
2171
2172 my $deleted = {
2173 LINENR => $linenr,
2174 LINE => $line,
2175 };
2176
2177 push(@fixed_deleted, $deleted);
2178}
2179
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002180sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002181 my ($type, $msg) = @_;
2182
2183 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002184 our $clean = 0;
2185 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002186 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002187 }
Joe Perches3705ce52013-07-03 15:05:31 -07002188 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002189}
2190sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002191 my ($type, $msg) = @_;
2192
2193 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002194 our $clean = 0;
2195 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002196 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002197 }
Joe Perches3705ce52013-07-03 15:05:31 -07002198 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002199}
2200sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002201 my ($type, $msg) = @_;
2202
2203 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002204 our $clean = 0;
2205 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002206 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002207 }
Joe Perches3705ce52013-07-03 15:05:31 -07002208 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002209}
2210
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002211sub check_absolute_file {
2212 my ($absolute, $herecurr) = @_;
2213 my $file = $absolute;
2214
2215 ##print "absolute<$absolute>\n";
2216
2217 # See if any suffix of this path is a path within the tree.
2218 while ($file =~ s@^[^/]*/@@) {
2219 if (-f "$root/$file") {
2220 ##print "file<$file>\n";
2221 last;
2222 }
2223 }
2224 if (! -f _) {
2225 return 0;
2226 }
2227
2228 # It is, so see if the prefix is acceptable.
2229 my $prefix = $absolute;
2230 substr($prefix, -length($file)) = '';
2231
2232 ##print "prefix<$prefix>\n";
2233 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002234 WARN("USE_RELATIVE_PATH",
2235 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002236 }
2237}
2238
Joe Perches3705ce52013-07-03 15:05:31 -07002239sub trim {
2240 my ($string) = @_;
2241
Joe Perchesb34c6482013-09-11 14:24:01 -07002242 $string =~ s/^\s+|\s+$//g;
2243
2244 return $string;
2245}
2246
2247sub ltrim {
2248 my ($string) = @_;
2249
2250 $string =~ s/^\s+//;
2251
2252 return $string;
2253}
2254
2255sub rtrim {
2256 my ($string) = @_;
2257
2258 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002259
2260 return $string;
2261}
2262
Joe Perches52ea8502013-11-12 15:10:09 -08002263sub string_find_replace {
2264 my ($string, $find, $replace) = @_;
2265
2266 $string =~ s/$find/$replace/g;
2267
2268 return $string;
2269}
2270
Joe Perches3705ce52013-07-03 15:05:31 -07002271sub tabify {
2272 my ($leading) = @_;
2273
Antonio Borneo713a09d2020-04-06 20:11:07 -07002274 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002275 my $max_spaces_before_tab = $source_indent - 1;
2276 my $spaces_to_tab = " " x $source_indent;
2277
2278 #convert leading spaces to tabs
2279 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2280 #Remove spaces before a tab
2281 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2282
2283 return "$leading";
2284}
2285
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002286sub pos_last_openparen {
2287 my ($line) = @_;
2288
2289 my $pos = 0;
2290
2291 my $opens = $line =~ tr/\(/\(/;
2292 my $closes = $line =~ tr/\)/\)/;
2293
2294 my $last_openparen = 0;
2295
2296 if (($opens == 0) || ($closes >= $opens)) {
2297 return -1;
2298 }
2299
2300 my $len = length($line);
2301
2302 for ($pos = 0; $pos < $len; $pos++) {
2303 my $string = substr($line, $pos);
2304 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2305 $pos += length($1) - 1;
2306 } elsif (substr($line, $pos, 1) eq '(') {
2307 $last_openparen = $pos;
2308 } elsif (index($string, '(') == -1) {
2309 last;
2310 }
2311 }
2312
Joe Perches91cb5192014-04-03 14:49:32 -07002313 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002314}
2315
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002316sub get_raw_comment {
2317 my ($line, $rawline) = @_;
2318 my $comment = '';
2319
2320 for my $i (0 .. (length($line) - 1)) {
2321 if (substr($line, $i, 1) eq "$;") {
2322 $comment .= substr($rawline, $i, 1);
2323 }
2324 }
2325
2326 return $comment;
2327}
2328
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002329sub process {
2330 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002331
2332 my $linenr=0;
2333 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002334 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002335 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002336 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002337
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002338 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002339 my $indent;
2340 my $previndent=0;
2341 my $stashindent=0;
2342
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002343 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002344 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002345 my $author = '';
2346 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002347 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002348 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002349 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002350 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002351 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002352 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002353 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002354 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002355 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002356 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002357 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002358 my $non_utf8_charset = 0;
2359
Joe Perches365dd4e2014-08-06 16:10:42 -07002360 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002361 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002362
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002363 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002364 our $cnt_lines = 0;
2365 our $cnt_error = 0;
2366 our $cnt_warn = 0;
2367 our $cnt_chk = 0;
2368
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002369 # Trace the real file/line as we go.
2370 my $realfile = '';
2371 my $realline = 0;
2372 my $realcnt = 0;
2373 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002374 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002375 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002376 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002377 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002378 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002379
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002380 my $prev_values = 'E';
2381
2382 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002383 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002384 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002385 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002386 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002387
Joe Perches7e51f192013-09-11 14:23:57 -07002388 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002389
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002390 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002391 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002392 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002393 my @setup_docs = ();
2394 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002395
Joe Perchesd8b07712013-11-12 15:10:06 -08002396 my $camelcase_file_seeded = 0;
2397
Rob Herring9f3a8992018-04-10 16:33:13 -07002398 my $checklicenseline = 1;
2399
Andy Whitcroft773647a2008-03-28 14:15:58 -07002400 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002401 my $line;
2402 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002403 $linenr++;
2404 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002405
Joe Perches3705ce52013-07-03 15:05:31 -07002406 push(@fixed, $rawline) if ($fix);
2407
Andy Whitcroft773647a2008-03-28 14:15:58 -07002408 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002409 $setup_docs = 0;
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07002410 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002411 $setup_docs = 1;
2412 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002413 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002414 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002415 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002416 $realline=$1-1;
2417 if (defined $2) {
2418 $realcnt=$3+1;
2419 } else {
2420 $realcnt=1+1;
2421 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002422 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002423
2424 # Guestimate if this is a continuing comment. Run
2425 # the context looking for a comment "edge". If this
2426 # edge is a close comment then we must be in a comment
2427 # at context start.
2428 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002429 my $cnt = $realcnt;
2430 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2431 next if (defined $rawlines[$ln - 1] &&
2432 $rawlines[$ln - 1] =~ /^-/);
2433 $cnt--;
2434 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002435 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002436 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2437 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2438 ($edge) = $1;
2439 last;
2440 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002441 }
2442 if (defined $edge && $edge eq '*/') {
2443 $in_comment = 1;
2444 }
2445
2446 # Guestimate if this is a continuing comment. If this
2447 # is the start of a diff block and this line starts
2448 # ' *' then it is very likely a comment.
2449 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002450 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002451 {
2452 $in_comment = 1;
2453 }
2454
2455 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2456 sanitise_line_reset($in_comment);
2457
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002458 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002459 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002460 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002461 $line = sanitise_line($rawline);
2462 }
2463 push(@lines, $line);
2464
2465 if ($realcnt > 1) {
2466 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2467 } else {
2468 $realcnt = 0;
2469 }
2470
2471 #print "==>$rawline\n";
2472 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002473
2474 if ($setup_docs && $line =~ /^\+/) {
2475 push(@setup_docs, $line);
2476 }
2477 }
2478
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002479 $prefix = '';
2480
Andy Whitcroft773647a2008-03-28 14:15:58 -07002481 $realcnt = 0;
2482 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002483 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002484 foreach my $line (@lines) {
2485 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002486 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002487 my $sline = $line; #copy of $line
2488 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002489
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002490 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002491 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002492
Joe Perches12c253a2018-06-07 17:10:58 -07002493# check if it's a mode change, rename or start of a patch
2494 if (!$in_commit_log &&
2495 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2496 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2497 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2498 $is_patch = 1;
2499 }
2500
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002501#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002502 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002503 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2504 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002505 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002506 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002507 $realline=$1-1;
2508 if (defined $2) {
2509 $realcnt=$3+1;
2510 } else {
2511 $realcnt=1+1;
2512 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002513 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002514 $prev_values = 'E';
2515
Andy Whitcroft773647a2008-03-28 14:15:58 -07002516 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002517 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002518 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002519 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002520 if ($context =~ /\b(\w+)\s*\(/) {
2521 $context_function = $1;
2522 } else {
2523 undef $context_function;
2524 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002525 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002526
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002527# track the line number as we move through the hunk, note that
2528# new versions of GNU diff omit the leading space on completely
2529# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002530 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002531 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002532 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002533
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002534 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002535 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002536
2537 # Track the previous line.
2538 ($prevline, $stashline) = ($stashline, $line);
2539 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002540 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2541
Andy Whitcroft773647a2008-03-28 14:15:58 -07002542 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002543
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002544 } elsif ($realcnt == 1) {
2545 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002546 }
2547
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002548 my $hunk_line = ($realcnt != 0);
2549
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002550 $here = "#$linenr: " if (!$file);
2551 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002552
Joe Perches2ac73b42014-06-04 16:12:05 -07002553 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002554 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002555 if ($line =~ /^diff --git.*?(\S+)$/) {
2556 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002557 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002558 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002559 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002560 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002561 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002562 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002563 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002564
2565 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002566 if (!$file && $tree && $p1_prefix ne '' &&
2567 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002568 WARN("PATCH_PREFIX",
2569 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002570 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002571
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002572 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002573 ERROR("MODIFIED_INCLUDE_ASM",
2574 "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 -07002575 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002576 $found_file = 1;
2577 }
2578
Joe Perches34d88152015-06-25 15:03:05 -07002579#make up the handle for any error we report on this line
2580 if ($showfile) {
2581 $prefix = "$realfile:$realline: "
2582 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002583 if ($file) {
2584 $prefix = "$filename:$realline: ";
2585 } else {
2586 $prefix = "$filename:$linenr: ";
2587 }
Joe Perches34d88152015-06-25 15:03:05 -07002588 }
2589
Joe Perches2ac73b42014-06-04 16:12:05 -07002590 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002591 if (is_maintained_obsolete($realfile)) {
2592 WARN("OBSOLETE",
2593 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2594 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002595 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002596 $check = 1;
2597 } else {
2598 $check = $check_orig;
2599 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002600 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002601
2602 if ($realfile !~ /^MAINTAINERS/) {
2603 my $last_binding_patch = $is_binding_patch;
2604
2605 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2606
2607 if (($last_binding_patch != -1) &&
2608 ($last_binding_patch ^ $is_binding_patch)) {
2609 WARN("DT_SPLIT_BINDING_PATCH",
Mauro Carvalho Chehab858e6842020-04-15 16:45:25 +02002610 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
Rob Herring133712a2018-08-21 21:58:16 -07002611 }
2612 }
2613
Andy Whitcroft773647a2008-03-28 14:15:58 -07002614 next;
2615 }
2616
Randy Dunlap389834b2007-06-08 13:47:03 -07002617 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002618
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002619 my $hereline = "$here\n$rawline\n";
2620 my $herecurr = "$here\n$rawline\n";
2621 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002622
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002623 $cnt_lines++ if ($realcnt != 0);
2624
Joe Perches490b2922018-08-21 21:58:01 -07002625# Verify the existence of a commit log if appropriate
2626# 2 is used because a $signature is counted in $commit_log_lines
2627 if ($in_commit_log) {
2628 if ($line !~ /^\s*$/) {
2629 $commit_log_lines++; #could be a $signature
2630 }
2631 } elsif ($has_commit_log && $commit_log_lines < 2) {
2632 WARN("COMMIT_MESSAGE",
2633 "Missing commit description - Add an appropriate one\n");
2634 $commit_log_lines = 2; #warn only once
2635 }
2636
Joe Perchese518e9a2015-06-25 15:03:27 -07002637# Check if the commit log has what seems like a diff which can confuse patch
2638 if ($in_commit_log && !$commit_log_has_diff &&
2639 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2640 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2641 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2642 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2643 ERROR("DIFF_IN_COMMIT_MSG",
2644 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2645 $commit_log_has_diff = 1;
2646 }
2647
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002648# Check for incorrect file permissions
2649 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2650 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002651 if ($realfile !~ m@scripts/@ &&
2652 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002653 ERROR("EXECUTE_PERMISSIONS",
2654 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002655 }
2656 }
2657
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002658# Check the patch for a From:
2659 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2660 $author = $1;
2661 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2662 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002663 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002664 }
2665
Joe Perches20112472011-07-25 17:13:23 -07002666# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002667 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002668 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002669 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002670 if ($author ne '') {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002671 if (same_email_addresses($1, $author)) {
2672 $authorsignoff = 1;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002673 }
2674 }
Joe Perches20112472011-07-25 17:13:23 -07002675 }
2676
Joe Perches44d303e2020-04-06 20:11:10 -07002677# Check for patch separator
2678 if ($line =~ /^---$/) {
2679 $has_patch_separator = 1;
2680 $in_commit_log = 0;
2681 }
2682
Joe Perchese0d975b2014-12-10 15:51:49 -08002683# Check if MAINTAINERS is being updated. If so, there's probably no need to
2684# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2685 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2686 $reported_maintainer_file = 1;
2687 }
2688
Joe Perches20112472011-07-25 17:13:23 -07002689# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002690 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002691 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002692 my $space_before = $1;
2693 my $sign_off = $2;
2694 my $space_after = $3;
2695 my $email = $4;
2696 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2697
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002698 if ($sign_off !~ /$signature_tags/) {
2699 WARN("BAD_SIGN_OFF",
2700 "Non-standard signature: $sign_off\n" . $herecurr);
2701 }
Joe Perches20112472011-07-25 17:13:23 -07002702 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002703 if (WARN("BAD_SIGN_OFF",
2704 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2705 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002706 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002707 "$ucfirst_sign_off $email";
2708 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002709 }
Joe Perches20112472011-07-25 17:13:23 -07002710 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002711 if (WARN("BAD_SIGN_OFF",
2712 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2713 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002714 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002715 "$ucfirst_sign_off $email";
2716 }
2717
Joe Perches20112472011-07-25 17:13:23 -07002718 }
2719 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002720 if (WARN("BAD_SIGN_OFF",
2721 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2722 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002723 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002724 "$ucfirst_sign_off $email";
2725 }
Joe Perches20112472011-07-25 17:13:23 -07002726 }
2727
Joe Perchesdfa05c22020-04-06 20:10:48 -07002728 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Joe Perches20112472011-07-25 17:13:23 -07002729 my $suggested_email = format_email(($email_name, $email_address));
2730 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002731 ERROR("BAD_SIGN_OFF",
2732 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002733 } else {
2734 my $dequoted = $suggested_email;
2735 $dequoted =~ s/^"//;
2736 $dequoted =~ s/" </ </;
2737 # Don't force email to have quotes
2738 # Allow just an angle bracketed address
Joe Perchesdfa05c22020-04-06 20:10:48 -07002739 if (!same_email_addresses($email, $suggested_email)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002740 WARN("BAD_SIGN_OFF",
2741 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002742 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002743 }
Joe Perches7e51f192013-09-11 14:23:57 -07002744
2745# Check for duplicate signatures
2746 my $sig_nospace = $line;
2747 $sig_nospace =~ s/\s//g;
2748 $sig_nospace = lc($sig_nospace);
2749 if (defined $signatures{$sig_nospace}) {
2750 WARN("BAD_SIGN_OFF",
2751 "Duplicate signature\n" . $herecurr);
2752 } else {
2753 $signatures{$sig_nospace} = 1;
2754 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002755
2756# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2757 if ($sign_off =~ /^co-developed-by:$/i) {
2758 if ($email eq $author) {
2759 WARN("BAD_SIGN_OFF",
2760 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2761 }
2762 if (!defined $lines[$linenr]) {
2763 WARN("BAD_SIGN_OFF",
2764 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2765 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2766 WARN("BAD_SIGN_OFF",
2767 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2768 } elsif ($1 ne $email) {
2769 WARN("BAD_SIGN_OFF",
2770 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2771 }
2772 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002773 }
2774
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002775# Check email subject for common tools that don't need to be mentioned
2776 if ($in_header_lines &&
2777 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2778 WARN("EMAIL_SUBJECT",
2779 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2780 }
2781
Joe Perches44d303e2020-04-06 20:11:10 -07002782# Check for Gerrit Change-Ids not in any patch context
2783 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002784 ERROR("GERRIT_CHANGE_ID",
Joe Perches44d303e2020-04-06 20:11:10 -07002785 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002786 }
2787
Joe Perches369c8dd2015-11-06 16:31:34 -08002788# Check if the commit log is in a possible stack dump
2789 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2790 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2791 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2792 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07002793 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2794 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2795 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2796 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08002797 $commit_log_possible_stack_dump = 1;
2798 }
2799
Joe Perches2a076f42015-04-16 12:44:28 -07002800# Check for line lengths > 75 in commit log, warn once
2801 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002802 length($line) > 75 &&
2803 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2804 # file delta changes
2805 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2806 # filename then :
2807 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2808 # A Fixes: or Link: line
2809 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002810 WARN("COMMIT_LOG_LONG_LINE",
2811 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2812 $commit_log_long_line = 1;
2813 }
2814
Joe Perchesbf4daf12015-09-09 15:37:50 -07002815# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002816 if ($in_commit_log && $commit_log_possible_stack_dump &&
2817 $line =~ /^\s*$/) {
2818 $commit_log_possible_stack_dump = 0;
2819 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002820
Joe Perches0d7835f2015-02-13 14:38:35 -08002821# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002822 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07002823 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002824 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002825 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002826 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002827 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2828 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002829 my $init_char = "c";
2830 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002831 my $short = 1;
2832 my $long = 0;
2833 my $case = 1;
2834 my $space = 1;
2835 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002836 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002837 my $id = '0123456789ab';
2838 my $orig_desc = "commit description";
2839 my $description = "";
2840
Joe Perchesfe043ea2015-09-09 15:37:25 -07002841 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2842 $init_char = $1;
2843 $orig_commit = lc($2);
2844 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2845 $orig_commit = lc($1);
2846 }
2847
Joe Perches0d7835f2015-02-13 14:38:35 -08002848 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2849 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2850 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2851 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2852 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2853 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002854 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002855 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2856 defined $rawlines[$linenr] &&
2857 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2858 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002859 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002860 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2861 defined $rawlines[$linenr] &&
2862 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2863 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2864 $orig_desc = $1;
2865 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2866 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002867 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002868 }
2869
2870 ($id, $description) = git_commit_info($orig_commit,
2871 $id, $orig_desc);
2872
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002873 if (defined($id) &&
2874 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002875 ERROR("GIT_COMMIT_ID",
2876 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2877 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002878 }
2879
Joe Perches13f19372014-08-06 16:10:59 -07002880# Check for added, moved or deleted files
2881 if (!$reported_maintainer_file && !$in_commit_log &&
2882 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2883 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2884 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2885 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002886 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002887 $reported_maintainer_file = 1;
2888 WARN("FILE_PATH_CHANGES",
2889 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2890 }
2891
Rob Herringe400edb2019-09-12 15:18:20 +01002892# Check for adding new DT bindings not in schema format
2893 if (!$in_commit_log &&
2894 ($line =~ /^new file mode\s*\d+\s*$/) &&
2895 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2896 WARN("DT_SCHEMA_BINDING_PATCH",
2897 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2898 }
2899
Andy Whitcroft00df3442007-06-08 13:47:06 -07002900# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002901 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002902 ERROR("CORRUPTED_PATCH",
2903 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002904 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002905 }
2906
2907# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2908 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002909 $rawline !~ m/^$UTF8*$/) {
2910 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2911
2912 my $blank = copy_spacing($rawline);
2913 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2914 my $hereptr = "$hereline$ptr\n";
2915
Joe Perches34d99212011-07-25 17:13:26 -07002916 CHK("INVALID_UTF8",
2917 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002918 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002919
Joe Perches15662b32011-10-31 17:13:12 -07002920# Check if it's the start of a commit log
2921# (not a header line and we haven't seen the patch filename)
2922 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002923 !($rawline =~ /^\s+(?:\S|$)/ ||
2924 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002925 $in_header_lines = 0;
2926 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002927 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002928 }
2929
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002930# Check if there is UTF-8 in a commit log when a mail header has explicitly
2931# declined it, i.e defined some charset where it is missing.
2932 if ($in_header_lines &&
2933 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2934 $1 !~ /utf-8/i) {
2935 $non_utf8_charset = 1;
2936 }
2937
2938 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002939 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002940 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002941 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2942 }
2943
Joe Perchesd6430f72016-12-12 16:46:28 -08002944# Check for absolute kernel paths in commit message
2945 if ($tree && $in_commit_log) {
2946 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2947 my $file = $1;
2948
2949 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2950 check_absolute_file($1, $herecurr)) {
2951 #
2952 } else {
2953 check_absolute_file($file, $herecurr);
2954 }
2955 }
2956 }
2957
Kees Cook66b47b42014-10-13 15:51:57 -07002958# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002959 if (defined($misspellings) &&
2960 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002961 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002962 my $typo = $1;
2963 my $typo_fix = $spelling_fix{lc($typo)};
2964 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2965 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002966 my $msg_level = \&WARN;
2967 $msg_level = \&CHK if ($file);
2968 if (&{$msg_level}("TYPO_SPELLING",
2969 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002970 $fix) {
2971 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2972 }
2973 }
2974 }
2975
Matteo Crocea8dd86b2019-09-25 16:46:38 -07002976# check for invalid commit id
2977 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2978 my $id;
2979 my $description;
2980 ($id, $description) = git_commit_info($2, undef, undef);
2981 if (!defined($id)) {
2982 WARN("UNKNOWN_COMMIT_ID",
2983 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2984 }
2985 }
2986
Andy Whitcroft306708542008-10-15 22:02:28 -07002987# ignore non-hunk lines and lines being removed
2988 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002989
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002990#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002991 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002992 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002993 if (ERROR("DOS_LINE_ENDINGS",
2994 "DOS line endings\n" . $herevet) &&
2995 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002996 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002997 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002998 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2999 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003000 if (ERROR("TRAILING_WHITESPACE",
3001 "trailing whitespace\n" . $herevet) &&
3002 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003003 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003004 }
3005
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003006 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003007 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07003008
Josh Triplett4783f892013-11-12 15:10:12 -08003009# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003010 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003011 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003012 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3013 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003014 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003015 my $msg_level = \&ERROR;
3016 $msg_level = \&CHK if ($file);
3017 &{$msg_level}("FSF_MAILING_ADDRESS",
3018 "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 -08003019 }
3020
Andi Kleen33549572010-05-24 14:33:29 -07003021# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003022# Only applies when adding the entry originally, after that we do not have
3023# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003024 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003025 # 'choice' is usually the last thing on the line (though
3026 # Kconfig supports named choices), so use a word boundary
3027 # (\b) rather than a whitespace character (\s)
3028 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003029 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003030 my $cnt = $realcnt;
3031 my $ln = $linenr + 1;
3032 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003033 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003034 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003035 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003036 $f = $lines[$ln - 1];
3037 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3038 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003039
3040 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003041 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003042
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003043 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003044 $is_start = 1;
Masahiro Yamada22a4ac02020-06-17 12:02:20 +09003045 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003046 $length = -1;
3047 }
3048
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003049 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003050 $f =~ s/#.*//;
3051 $f =~ s/^\s+//;
3052 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003053
3054 # This only checks context lines in the patch
3055 # and so hopefully shouldn't trigger false
3056 # positives, even though some of these are
3057 # common words in help texts
3058 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3059 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003060 $is_end = 1;
3061 last;
3062 }
Andi Kleen33549572010-05-24 14:33:29 -07003063 $length++;
3064 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003065 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3066 WARN("CONFIG_DESCRIPTION",
3067 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3068 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003069 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003070 }
3071
Joe Perches7ccf41a2020-06-04 16:50:33 -07003072# check MAINTAINERS entries
3073 if ($realfile =~ /^MAINTAINERS$/) {
3074# check MAINTAINERS entries for the right form
3075 if ($rawline =~ /^\+[A-Z]:/ &&
3076 $rawline !~ /^\+[A-Z]:\t\S/) {
3077 if (WARN("MAINTAINERS_STYLE",
3078 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3079 $fix) {
3080 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3081 }
3082 }
3083# check MAINTAINERS entries for the right ordering too
3084 my $preferred_order = 'MRLSWQBCPTFXNK';
3085 if ($rawline =~ /^\+[A-Z]:/ &&
3086 $prevrawline =~ /^[\+ ][A-Z]:/) {
3087 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3088 my $cur = $1;
3089 my $curval = $2;
3090 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3091 my $prev = $1;
3092 my $prevval = $2;
3093 my $curindex = index($preferred_order, $cur);
3094 my $previndex = index($preferred_order, $prev);
3095 if ($curindex < 0) {
3096 WARN("MAINTAINERS_STYLE",
3097 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3098 } else {
3099 if ($previndex >= 0 && $curindex < $previndex) {
3100 WARN("MAINTAINERS_STYLE",
3101 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3102 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3103 ($prev eq 'X' && $cur eq 'X')) &&
3104 ($prevval cmp $curval) > 0) {
3105 WARN("MAINTAINERS_STYLE",
3106 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3107 }
3108 }
Joe Perches628f91a2017-07-10 15:52:07 -07003109 }
3110 }
3111
Christoph Jaeger327953e2015-02-13 14:38:29 -08003112# discourage the use of boolean for type definition attributes of Kconfig options
3113 if ($realfile =~ /Kconfig/ &&
3114 $line =~ /^\+\s*\bboolean\b/) {
3115 WARN("CONFIG_TYPE_BOOLEAN",
3116 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3117 }
3118
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003119 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3120 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3121 my $flag = $1;
3122 my $replacement = {
3123 'EXTRA_AFLAGS' => 'asflags-y',
3124 'EXTRA_CFLAGS' => 'ccflags-y',
3125 'EXTRA_CPPFLAGS' => 'cppflags-y',
3126 'EXTRA_LDFLAGS' => 'ldflags-y',
3127 };
3128
3129 WARN("DEPRECATED_VARIABLE",
3130 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3131 }
3132
Rob Herringbff5da42014-01-23 15:54:51 -08003133# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003134 if (defined $root &&
3135 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3136 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3137
Rob Herringbff5da42014-01-23 15:54:51 -08003138 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3139
Florian Vaussardcc933192014-04-03 14:49:27 -07003140 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003141 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003142
Rob Herringbff5da42014-01-23 15:54:51 -08003143 foreach my $compat (@compats) {
3144 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003145 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3146 my $compat3 = $compat;
3147 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3148 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003149 if ( $? >> 8 ) {
3150 WARN("UNDOCUMENTED_DT_STRING",
3151 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3152 }
3153
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003154 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3155 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003156 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003157 if ( $? >> 8 ) {
3158 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003159 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003160 }
3161 }
3162 }
3163
Rob Herring9f3a8992018-04-10 16:33:13 -07003164# check for using SPDX license tag at beginning of files
3165 if ($realline == $checklicenseline) {
3166 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3167 $checklicenseline = 2;
3168 } elsif ($rawline =~ /^\+/) {
3169 my $comment = "";
3170 if ($realfile =~ /\.(h|s|S)$/) {
3171 $comment = '/*';
3172 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3173 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003174 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003175 $comment = '#';
3176 } elsif ($realfile =~ /\.rst$/) {
3177 $comment = '..';
3178 }
3179
Joe Perchesfdf13692019-03-07 16:28:32 -08003180# check SPDX comment style for .[chsS] files
3181 if ($realfile =~ /\.[chsS]$/ &&
3182 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003183 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003184 WARN("SPDX_LICENSE_TAG",
3185 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3186 }
3187
Rob Herring9f3a8992018-04-10 16:33:13 -07003188 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003189 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3190 WARN("SPDX_LICENSE_TAG",
3191 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003192 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003193 my $spdx_license = $1;
3194 if (!is_SPDX_License_valid($spdx_license)) {
3195 WARN("SPDX_LICENSE_TAG",
3196 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3197 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003198 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3199 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3200 my $msg_level = \&WARN;
3201 $msg_level = \&CHK if ($file);
3202 if (&{$msg_level}("SPDX_LICENSE_TAG",
3203
3204 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3205 $fix) {
3206 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3207 }
3208 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003209 }
3210 }
3211 }
3212
Andy Whitcroft5368df202008-10-15 22:02:27 -07003213# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003214 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003215
Joe Perchesa8da38a2019-03-07 16:28:42 -08003216# check for using SPDX-License-Identifier on the wrong line number
3217 if ($realline != $checklicenseline &&
3218 $rawline =~ /\bSPDX-License-Identifier:/ &&
3219 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3220 WARN("SPDX_LICENSE_TAG",
3221 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3222 }
3223
Joe Perches47e0c882015-06-25 15:02:57 -07003224# line length limit (with some exclusions)
3225#
3226# There are a few types of lines that may extend beyond $max_line_length:
3227# logging functions like pr_info that end in a string
3228# lines with a single string
3229# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003230# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003231#
3232# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003233# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003234# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3235# LONG_LINE all other lines longer than $max_line_length
3236#
3237# if LONG_LINE is ignored, the other 2 types are also ignored
3238#
3239
Joe Perchesb4749e92015-07-17 16:24:01 -07003240 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003241 my $msg_type = "LONG_LINE";
3242
3243 # Check the allowed long line types first
3244
3245 # logging functions that end in a string that starts
3246 # before $max_line_length
3247 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3248 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3249 $msg_type = "";
3250
3251 # lines with only strings (w/ possible termination)
3252 # #defines with only strings
3253 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3254 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3255 $msg_type = "";
3256
Joe Perchescc147502017-11-17 15:28:44 -08003257 # More special cases
3258 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3259 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003260 $msg_type = "";
3261
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003262 # URL ($rawline is used in case the URL is in a comment)
3263 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3264 $msg_type = "";
3265
Joe Perches47e0c882015-06-25 15:02:57 -07003266 # Otherwise set the alternate message types
3267
3268 # a comment starts before $max_line_length
3269 } elsif ($line =~ /($;[\s$;]*)$/ &&
3270 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3271 $msg_type = "LONG_LINE_COMMENT"
3272
3273 # a quoted string starts before $max_line_length
3274 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3275 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3276 $msg_type = "LONG_LINE_STRING"
3277 }
3278
3279 if ($msg_type ne "" &&
3280 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003281 my $msg_level = \&WARN;
3282 $msg_level = \&CHK if ($file);
3283 &{$msg_level}($msg_type,
3284 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003285 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003286 }
3287
Andy Whitcroft8905a672007-11-28 16:21:06 -08003288# check for adding lines without a newline.
3289 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003290 WARN("MISSING_EOF_NEWLINE",
3291 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003292 }
3293
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003294# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003295 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003296
3297# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003298# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003299 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3300 $rawline =~ /^\+\s* \s*/) {
3301 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003302 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003303 if (ERROR("CODE_INDENT",
3304 "code indent should use tabs where possible\n" . $herevet) &&
3305 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003306 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003307 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003308 }
3309
Alberto Panizzo08e44362010-03-05 13:43:54 -08003310# check for space before tabs.
3311 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3312 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003313 if (WARN("SPACE_BEFORE_TAB",
3314 "please, no space before tabs\n" . $herevet) &&
3315 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003316 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003317 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003318 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003319 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003320 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003321 }
3322
Joe Perches6a487212018-04-10 16:34:04 -07003323# check for assignments on the start of a line
3324 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3325 CHK("ASSIGNMENT_CONTINUATIONS",
3326 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3327 }
3328
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003329# check for && or || at the start of a line
3330 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3331 CHK("LOGICAL_CONTINUATIONS",
3332 "Logical continuations should be on the previous line\n" . $hereprev);
3333 }
3334
Joe Perchesa91e8992016-05-20 17:04:05 -07003335# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003336 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003337 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003338 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003339 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003340 if (WARN("TABSTOP",
3341 "Statements should start on a tabstop\n" . $herecurr) &&
3342 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003343 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003344 }
3345 }
3346 }
3347
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003348# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003349 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003350 $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 -07003351 $prevline =~ /^\+(\t*)(.*)$/;
3352 my $oldindent = $1;
3353 my $rest = $2;
3354
3355 my $pos = pos_last_openparen($rest);
3356 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003357 $line =~ /^(\+| )([ \t]*)/;
3358 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003359
3360 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003361 "\t" x ($pos / $tabsize) .
3362 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003363 my $goodspaceindent = $oldindent . " " x $pos;
3364
3365 if ($newindent ne $goodtabindent &&
3366 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003367
3368 if (CHK("PARENTHESIS_ALIGNMENT",
3369 "Alignment should match open parenthesis\n" . $hereprev) &&
3370 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003371 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003372 s/^\+[ \t]*/\+$goodtabindent/;
3373 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003374 }
3375 }
3376 }
3377
Joe Perches6ab3a972015-04-16 12:44:05 -07003378# check for space after cast like "(int) foo" or "(struct foo) bar"
3379# avoid checking a few false positives:
3380# "sizeof(<type>)" or "__alignof__(<type>)"
3381# function pointer declarations like "(*foo)(int) = bar;"
3382# structure definitions like "(struct foo) { 0 };"
3383# multiline macros that define functions
3384# known attributes or the __attribute__ keyword
3385 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3386 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003387 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003388 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003389 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003390 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003391 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003392 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003393 }
3394
Joe Perches86406b12015-09-09 15:37:41 -07003395# Block comment styles
3396# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003397 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003398 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003399 $rawline =~ /^\+[ \t]*\*/ &&
3400 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003401 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3402 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3403 }
3404
Joe Perches86406b12015-09-09 15:37:41 -07003405# Block comments use * on subsequent lines
3406 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3407 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003408 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003409 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003410 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003411 WARN("BLOCK_COMMENT_STYLE",
3412 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003413 }
3414
Joe Perches86406b12015-09-09 15:37:41 -07003415# Block comments use */ on trailing lines
3416 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003417 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3418 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3419 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003420 WARN("BLOCK_COMMENT_STYLE",
3421 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003422 }
3423
Joe Perches08eb9b82016-10-11 13:51:50 -07003424# Block comment * alignment
3425 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003426 $line =~ /^\+[ \t]*$;/ && #leading comment
3427 $rawline =~ /^\+[ \t]*\*/ && #leading *
3428 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003429 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003430 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3431 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003432 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003433 if (defined($1)) {
3434 $oldindent = expand_tabs($1);
3435 } else {
3436 $prevrawline =~ m@^\+(.*/?)\*@;
3437 $oldindent = expand_tabs($1);
3438 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003439 $rawline =~ m@^\+([ \t]*)\*@;
3440 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003441 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003442 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003443 WARN("BLOCK_COMMENT_STYLE",
3444 "Block comments should align the * on each line\n" . $hereprev);
3445 }
3446 }
3447
Joe Perches7f619192014-08-06 16:10:39 -07003448# check for missing blank lines after struct/union declarations
3449# with exceptions for various attributes and macros
3450 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3451 $line =~ /^\+/ &&
3452 !($line =~ /^\+\s*$/ ||
3453 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3454 $line =~ /^\+\s*MODULE_/i ||
3455 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3456 $line =~ /^\+[a-z_]*init/ ||
3457 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3458 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003459 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003460 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003461 if (CHK("LINE_SPACING",
3462 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3463 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003464 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003465 }
Joe Perches7f619192014-08-06 16:10:39 -07003466 }
3467
Joe Perches365dd4e2014-08-06 16:10:42 -07003468# check for multiple consecutive blank lines
3469 if ($prevline =~ /^[\+ ]\s*$/ &&
3470 $line =~ /^\+\s*$/ &&
3471 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003472 if (CHK("LINE_SPACING",
3473 "Please don't use multiple blank lines\n" . $hereprev) &&
3474 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003475 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003476 }
3477
Joe Perches365dd4e2014-08-06 16:10:42 -07003478 $last_blank_line = $linenr;
3479 }
3480
Joe Perches3b617e32014-04-03 14:49:28 -07003481# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003482 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3483 # actual declarations
3484 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003485 # function pointer declarations
3486 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003487 # foo bar; where foo is some local typedef or #define
3488 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3489 # known declaration macros
3490 $prevline =~ /^\+\s+$declaration_macros/) &&
3491 # for "else if" which can look like "$Ident $Ident"
3492 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3493 # other possible extensions of declaration lines
3494 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3495 # not starting a section or a macro "\" extended line
3496 $prevline =~ /(?:\{\s*|\\)$/) &&
3497 # looks like a declaration
3498 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003499 # function pointer declarations
3500 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003501 # foo bar; where foo is some local typedef or #define
3502 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3503 # known declaration macros
3504 $sline =~ /^\+\s+$declaration_macros/ ||
3505 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003506 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003507 # start or end of block or continuation of declaration
3508 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3509 # bitfield continuation
3510 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3511 # other possible extensions of declaration lines
3512 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3513 # indentation of previous and current line are the same
3514 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003515 if (WARN("LINE_SPACING",
3516 "Missing a blank line after declarations\n" . $hereprev) &&
3517 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003518 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003519 }
Joe Perches3b617e32014-04-03 14:49:28 -07003520 }
3521
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003522# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003523# Exceptions:
3524# 1) within comments
3525# 2) indented preprocessor commands
3526# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003527 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003528 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003529 if (WARN("LEADING_SPACE",
3530 "please, no spaces at the start of a line\n" . $herevet) &&
3531 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003532 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003533 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003534 }
3535
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003536# check we are in a valid C source file if not then ignore this hunk
3537 next if ($realfile !~ /\.(h|c)$/);
3538
Joe Perches5751a242017-11-17 15:28:52 -08003539# check for unusual line ending [ or (
3540 if ($line =~ /^\+.*([\[\(])\s*$/) {
3541 CHK("OPEN_ENDED_LINE",
3542 "Lines should not end with a '$1'\n" . $herecurr);
3543 }
3544
Joe Perches4dbed762017-05-08 15:55:39 -07003545# check if this appears to be the start function declaration, save the name
3546 if ($sline =~ /^\+\{\s*$/ &&
3547 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3548 $context_function = $1;
3549 }
3550
3551# check if this appears to be the end of function declaration
3552 if ($sline =~ /^\+\}\s*$/) {
3553 undef $context_function;
3554 }
3555
Joe Perches032a4c02014-08-06 16:10:29 -07003556# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003557# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003558# if the previous line is a break or return and is indented 1 tab more...
3559 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3560 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003561 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3562 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3563 defined $lines[$linenr] &&
3564 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003565 WARN("UNNECESSARY_ELSE",
3566 "else is not generally useful after a break or return\n" . $hereprev);
3567 }
3568 }
3569
Joe Perchesc00df192014-08-06 16:11:01 -07003570# check indentation of a line with a break;
3571# if the previous line is a goto or return and is indented the same # of tabs
3572 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3573 my $tabs = $1;
3574 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3575 WARN("UNNECESSARY_BREAK",
3576 "break is not useful after a goto or return\n" . $hereprev);
3577 }
3578 }
3579
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003580# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003581 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003582 WARN("CVS_KEYWORD",
3583 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003584 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003585
Joe Perches56e77d72013-02-21 16:44:14 -08003586# check for old HOTPLUG __dev<foo> section markings
3587 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3588 WARN("HOTPLUG_SECTION",
3589 "Using $1 is unnecessary\n" . $herecurr);
3590 }
3591
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003592# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003593 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3594 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003595#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003596 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003597 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003598 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003599 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003600 $stat =~ s/\n./\n /g;
3601 $cond =~ s/\n./\n /g;
3602
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003603#print "linenr<$linenr> <$stat>\n";
3604 # If this statement has no statement boundaries within
3605 # it there is no point in retrying a statement scan
3606 # until we hit end of it.
3607 my $frag = $stat; $frag =~ s/;+\s*$//;
3608 if ($frag !~ /(?:{|;)/) {
3609#print "skip<$line_nr_next>\n";
3610 $suppress_statement = $line_nr_next;
3611 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003612
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003613 # Find the real next line.
3614 $realline_next = $line_nr_next;
3615 if (defined $realline_next &&
3616 (!defined $lines[$realline_next - 1] ||
3617 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3618 $realline_next++;
3619 }
3620
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003621 my $s = $stat;
3622 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003623
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003624 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003625 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003626
3627 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003628 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003629
Andy Whitcroft463f2862009-09-21 17:04:34 -07003630 } elsif ($s =~ /^.\s*else\b/s) {
3631
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003632 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003633 } 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 -07003634 my $type = $1;
3635 $type =~ s/\s+/ /g;
3636 possible($type, "A:" . $s);
3637
Andy Whitcroft8905a672007-11-28 16:21:06 -08003638 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003639 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003640 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003641 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003642
3643 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003644 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003645 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003646 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003647
3648 # Check for any sort of function declaration.
3649 # int foo(something bar, other baz);
3650 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003651 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 -08003652 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003653
Andy Whitcroftcf655042008-03-04 14:28:20 -08003654 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003655 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003656 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003657
Andy Whitcroft8905a672007-11-28 16:21:06 -08003658 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003659 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003660
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003661 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003662 }
3663 }
3664 }
3665
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003666 }
3667
Andy Whitcroft653d4872007-06-23 17:16:34 -07003668#
3669# Checks which may be anchored in the context.
3670#
3671
3672# Check for switch () and associated case and default
3673# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003674 if ($line=~/\bswitch\s*\(.*\)/) {
3675 my $err = '';
3676 my $sep = '';
3677 my @ctx = ctx_block_outer($linenr, $realcnt);
3678 shift(@ctx);
3679 for my $ctx (@ctx) {
3680 my ($clen, $cindent) = line_stats($ctx);
3681 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3682 $indent != $cindent) {
3683 $err .= "$sep$ctx\n";
3684 $sep = '';
3685 } else {
3686 $sep = "[...]\n";
3687 }
3688 }
3689 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003690 ERROR("SWITCH_CASE_INDENT_LEVEL",
3691 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003692 }
3693 }
3694
3695# if/while/etc brace do not go on next line, unless defining a do while loop,
3696# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003697 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 -07003698 my $pre_ctx = "$1$2";
3699
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003700 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003701
3702 if ($line =~ /^\+\t{6,}/) {
3703 WARN("DEEP_INDENTATION",
3704 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3705 }
3706
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003707 my $ctx_cnt = $realcnt - $#ctx - 1;
3708 my $ctx = join("\n", @ctx);
3709
Andy Whitcroft548596d2008-07-23 21:29:01 -07003710 my $ctx_ln = $linenr;
3711 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003712
Andy Whitcroft548596d2008-07-23 21:29:01 -07003713 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3714 defined $lines[$ctx_ln - 1] &&
3715 $lines[$ctx_ln - 1] =~ /^-/)) {
3716 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3717 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003718 $ctx_ln++;
3719 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003720
Andy Whitcroft53210162008-07-23 21:29:03 -07003721 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3722 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003723
Joe Perchesd752fcc2014-08-06 16:11:05 -07003724 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003725 ERROR("OPEN_BRACE",
3726 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003727 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003728 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003729 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3730 $ctx =~ /\)\s*\;\s*$/ &&
3731 defined $lines[$ctx_ln - 1])
3732 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003733 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3734 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003735 WARN("TRAILING_SEMICOLON",
3736 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003737 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003738 }
3739 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003740 }
3741
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003742# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003743 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 -08003744 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3745 ctx_statement_block($linenr, $realcnt, 0)
3746 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003747 my ($s, $c) = ($stat, $cond);
3748
3749 substr($s, 0, length($c), '');
3750
Joe Perches9f5af482015-09-09 15:37:30 -07003751 # remove inline comments
3752 $s =~ s/$;/ /g;
3753 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003754
3755 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003756 my @newlines = ($c =~ /\n/gs);
3757 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003758
Joe Perches9f5af482015-09-09 15:37:30 -07003759 # Make sure we remove the line prefixes as we have
3760 # none on the first line, and are going to readd them
3761 # where necessary.
3762 $s =~ s/\n./\n/gs;
3763 while ($s =~ /\n\s+\\\n/) {
3764 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3765 }
3766
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003767 # We want to check the first line inside the block
3768 # starting at the end of the conditional, so remove:
3769 # 1) any blank line termination
3770 # 2) any opening brace { on end of the line
3771 # 3) any do (...) {
3772 my $continuation = 0;
3773 my $check = 0;
3774 $s =~ s/^.*\bdo\b//;
3775 $s =~ s/^\s*{//;
3776 if ($s =~ s/^\s*\\//) {
3777 $continuation = 1;
3778 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003779 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003780 $check = 1;
3781 $cond_lines++;
3782 }
3783
3784 # Also ignore a loop construct at the end of a
3785 # preprocessor statement.
3786 if (($prevline =~ /^.\s*#\s*define\s/ ||
3787 $prevline =~ /\\\s*$/) && $continuation == 0) {
3788 $check = 0;
3789 }
3790
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003791 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003792 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003793 while ($cond_ptr != $cond_lines) {
3794 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003795
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003796 # If we see an #else/#elif then the code
3797 # is not linear.
3798 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3799 $check = 0;
3800 }
3801
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003802 # Ignore:
3803 # 1) blank lines, they should be at 0,
3804 # 2) preprocessor lines, and
3805 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003806 if ($continuation ||
3807 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003808 $s =~ /^\s*#\s*?/ ||
3809 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003810 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003811 if ($s =~ s/^.*?\n//) {
3812 $cond_lines++;
3813 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003814 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003815 }
3816
3817 my (undef, $sindent) = line_stats("+" . $s);
3818 my $stat_real = raw_line($linenr, $cond_lines);
3819
3820 # Check if either of these lines are modified, else
3821 # this is not this patch's fault.
3822 if (!defined($stat_real) ||
3823 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3824 $check = 0;
3825 }
3826 if (defined($stat_real) && $cond_lines > 1) {
3827 $stat_real = "[...]\n$stat_real";
3828 }
3829
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003830 #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 -07003831
Joe Perches9f5af482015-09-09 15:37:30 -07003832 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07003833 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07003834 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003835 ($sindent == $indent &&
3836 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07003837 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003838 WARN("SUSPECT_CODE_INDENT",
3839 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003840 }
3841 }
3842
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003843 # Track the 'values' across context and added lines.
3844 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003845 my ($curr_values, $curr_vars) =
3846 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003847 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003848 if ($dbg_values) {
3849 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003850 print "$linenr > .$outline\n";
3851 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003852 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003853 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003854 $prev_values = substr($curr_values, -1);
3855
Andy Whitcroft00df3442007-06-08 13:47:06 -07003856#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003857 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003858
Joe Perches11ca40a2016-12-12 16:46:31 -08003859# check for dereferences that span multiple lines
3860 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3861 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3862 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3863 my $ref = $1;
3864 $line =~ /^.\s*($Lval)/;
3865 $ref .= $1;
3866 $ref =~ s/\s//g;
3867 WARN("MULTILINE_DEREFERENCE",
3868 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3869 }
3870
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003871# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003872 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 -07003873 my $type = $1;
3874 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003875 $var = "" if (!defined $var);
3876 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003877 my $sign = $1;
3878 my $pointer = $2;
3879
3880 $pointer = "" if (!defined $pointer);
3881
3882 if (WARN("UNSPECIFIED_INT",
3883 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3884 $fix) {
3885 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003886 my $comp_pointer = $pointer;
3887 $comp_pointer =~ s/\s//g;
3888 $decl .= $comp_pointer;
3889 $decl = rtrim($decl) if ($var eq "");
3890 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003891 }
3892 }
3893 }
3894
Andy Whitcroft653d4872007-06-23 17:16:34 -07003895# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003896 if ($dbg_type) {
3897 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003898 ERROR("TEST_TYPE",
3899 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003900 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003901 ERROR("TEST_NOT_TYPE",
3902 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003903 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003904 next;
3905 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003906# TEST: allow direct testing of the attribute matcher.
3907 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003908 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003909 ERROR("TEST_ATTR",
3910 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003911 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003912 ERROR("TEST_NOT_ATTR",
3913 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003914 }
3915 next;
3916 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003917
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003918# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003919 if ($line =~ /^.\s*{/ &&
3920 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003921 if (ERROR("OPEN_BRACE",
3922 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003923 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3924 fix_delete_line($fixlinenr - 1, $prevrawline);
3925 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003926 my $fixedline = $prevrawline;
3927 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003928 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003929 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003930 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003931 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003932 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003933 }
3934
Andy Whitcroft653d4872007-06-23 17:16:34 -07003935#
3936# Checks which are anchored on the added line.
3937#
3938
3939# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003940 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003941 my $path = $1;
3942 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003943 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003944 "malformed #include filename\n" . $herecurr);
3945 }
3946 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3947 ERROR("UAPI_INCLUDE",
3948 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003949 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003950 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003951
3952# no C99 // comments
3953 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003954 if (ERROR("C99_COMMENTS",
3955 "do not use C99 // comments\n" . $herecurr) &&
3956 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003957 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003958 if ($line =~ /\/\/(.*)$/) {
3959 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003960 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003961 }
3962 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003963 }
3964 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003965 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003966 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003967
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003968# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3969# the whole statement.
3970#print "APW <$lines[$realline_next - 1]>\n";
3971 if (defined $realline_next &&
3972 exists $lines[$realline_next - 1] &&
3973 !defined $suppress_export{$realline_next} &&
3974 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3975 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003976 # Handle definitions which produce identifiers with
3977 # a prefix:
3978 # XXX(foo);
3979 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003980 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003981 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003982 $name =~ /^${Ident}_$2/) {
3983#print "FOO C name<$name>\n";
3984 $suppress_export{$realline_next} = 1;
3985
3986 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003987 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003988 ^.DEFINE_$Ident\(\Q$name\E\)|
3989 ^.DECLARE_$Ident\(\Q$name\E\)|
3990 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003991 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3992 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003993 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003994#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3995 $suppress_export{$realline_next} = 2;
3996 } else {
3997 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003998 }
3999 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004000 if (!defined $suppress_export{$linenr} &&
4001 $prevline =~ /^.\s*$/ &&
4002 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4003 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4004#print "FOO B <$lines[$linenr - 1]>\n";
4005 $suppress_export{$linenr} = 2;
4006 }
4007 if (defined $suppress_export{$linenr} &&
4008 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004009 WARN("EXPORT_SYMBOL",
4010 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004011 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004012
Joe Eloff5150bda2010-08-09 17:21:00 -07004013# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004014 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004015 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004016 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004017 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004018 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004019 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004020 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004021# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004022 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004023 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004024 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004025 $herecurr) &&
4026 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004027 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004028 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004029 }
4030
Joe Perches18130872014-08-06 16:11:22 -07004031# check for misordered declarations of char/short/int/long with signed/unsigned
4032 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4033 my $tmp = trim($1);
4034 WARN("MISORDERED_TYPE",
4035 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4036 }
4037
Joe Perches809e0822018-08-21 21:58:12 -07004038# check for unnecessary <signed> int declarations of short/long/long long
4039 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4040 my $type = trim($1);
4041 next if ($type !~ /\bint\b/);
4042 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4043 my $new_type = $type;
4044 $new_type =~ s/\b\s*int\s*\b/ /;
4045 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4046 $new_type =~ s/^const\s+//;
4047 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4048 $new_type = "const $new_type" if ($type =~ /^const\b/);
4049 $new_type =~ s/\s+/ /g;
4050 $new_type = trim($new_type);
4051 if (WARN("UNNECESSARY_INT",
4052 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4053 $fix) {
4054 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4055 }
4056 }
4057
Joe Perchescb710ec2010-10-26 14:23:20 -07004058# check for static const char * arrays.
4059 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004060 WARN("STATIC_CONST_CHAR_ARRAY",
4061 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004062 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004063 }
4064
4065# check for initialized const char arrays that should be static const
4066 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4067 if (WARN("STATIC_CONST_CHAR_ARRAY",
4068 "const array should probably be static const\n" . $herecurr) &&
4069 $fix) {
4070 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4071 }
4072 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004073
4074# check for static char foo[] = "bar" declarations.
4075 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004076 WARN("STATIC_CONST_CHAR_ARRAY",
4077 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004078 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004079 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004080
Joe Perchesab7e23f2015-04-16 12:44:22 -07004081# check for const <foo> const where <foo> is not a pointer or array type
4082 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4083 my $found = $1;
4084 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4085 WARN("CONST_CONST",
4086 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4087 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4088 WARN("CONST_CONST",
4089 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4090 }
4091 }
4092
Joe Perches9b0fa602014-04-03 14:49:18 -07004093# check for non-global char *foo[] = {"bar", ...} declarations.
4094 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4095 WARN("STATIC_CONST_CHAR_ARRAY",
4096 "char * array declaration might be better as static const\n" .
4097 $herecurr);
4098 }
4099
Joe Perchesb598b672015-04-16 12:44:36 -07004100# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4101 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4102 my $array = $1;
4103 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4104 my $array_div = $1;
4105 if (WARN("ARRAY_SIZE",
4106 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4107 $fix) {
4108 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4109 }
4110 }
4111 }
4112
Joe Perchesb36190c2014-01-27 17:07:18 -08004113# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004114 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004115 if (ERROR("FUNCTION_WITHOUT_ARGS",
4116 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4117 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004118 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004119 }
4120 }
4121
Andy Whitcroft653d4872007-06-23 17:16:34 -07004122# check for new typedefs, only function parameters and sparse annotations
4123# make sense.
4124 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004125 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004126 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004127 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004128 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004129 WARN("NEW_TYPEDEFS",
4130 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004131 }
4132
4133# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004134 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004135 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4136 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004137 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004138
Andy Whitcroft65863862009-01-06 14:41:21 -08004139 # Should start with a space.
4140 $to =~ s/^(\S)/ $1/;
4141 # Should not end with a space.
4142 $to =~ s/\s+$//;
4143 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004144 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004145 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004146
Joe Perches3705ce52013-07-03 15:05:31 -07004147## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004148 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004149 if (ERROR("POINTER_LOCATION",
4150 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4151 $fix) {
4152 my $sub_from = $ident;
4153 my $sub_to = $ident;
4154 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004155 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004156 s@\Q$sub_from\E@$sub_to@;
4157 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004158 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004159 }
4160 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4161 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004162 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004163
Andy Whitcroft65863862009-01-06 14:41:21 -08004164 # Should start with a space.
4165 $to =~ s/^(\S)/ $1/;
4166 # Should not end with a space.
4167 $to =~ s/\s+$//;
4168 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004169 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004170 }
4171 # Modifiers should have spaces.
4172 $to =~ s/(\b$Modifier$)/$1 /;
4173
Joe Perches3705ce52013-07-03 15:05:31 -07004174## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004175 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004176 if (ERROR("POINTER_LOCATION",
4177 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4178 $fix) {
4179
4180 my $sub_from = $match;
4181 my $sub_to = $match;
4182 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004183 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004184 s@\Q$sub_from\E@$sub_to@;
4185 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004186 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004187 }
4188
Joe Perches9d3e3c72015-09-09 15:37:27 -07004189# avoid BUG() or BUG_ON()
4190 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004191 my $msg_level = \&WARN;
4192 $msg_level = \&CHK if ($file);
4193 &{$msg_level}("AVOID_BUG",
4194 "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 -07004195 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004196
Joe Perches9d3e3c72015-09-09 15:37:27 -07004197# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004198 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004199 WARN("LINUX_VERSION_CODE",
4200 "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 -08004201 }
4202
Joe Perches17441222011-06-15 15:08:17 -07004203# check for uses of printk_ratelimit
4204 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004205 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004206 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004207 }
4208
Joe Percheseeef5732017-11-17 15:28:41 -08004209# printk should use KERN_* levels
4210 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4211 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4212 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004213 }
4214
Joe Perches243f3802012-05-31 16:26:09 -07004215 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4216 my $orig = $1;
4217 my $level = lc($orig);
4218 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004219 my $level2 = $level;
4220 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004221 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004222 "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 -07004223 }
4224
Joe Perchesdc139312013-02-21 16:44:13 -08004225 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4226 my $orig = $1;
4227 my $level = lc($orig);
4228 $level = "warn" if ($level eq "warning");
4229 $level = "dbg" if ($level eq "debug");
4230 WARN("PREFER_DEV_LEVEL",
4231 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4232 }
4233
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004234# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4235# number of false positives, but assembly files are not checked, so at
4236# least the arch entry code will not trigger this warning.
4237 if ($line =~ /\bENOSYS\b/) {
4238 WARN("ENOSYS",
4239 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4240 }
4241
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004242# ENOTSUPP is not a standard error code and should be avoided in new patches.
4243# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4244# Similarly to ENOSYS warning a small number of false positives is expected.
4245 if (!$file && $line =~ /\bENOTSUPP\b/) {
4246 if (WARN("ENOTSUPP",
4247 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4248 $fix) {
4249 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4250 }
4251 }
4252
Andy Whitcroft653d4872007-06-23 17:16:34 -07004253# function brace can't be on same line, except for #defines of do while,
4254# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004255 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004256 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4257 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4258 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004259 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004260 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004261 $fix) {
4262 fix_delete_line($fixlinenr, $rawline);
4263 my $fixed_line = $rawline;
4264 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4265 my $line1 = $1;
4266 my $line2 = $2;
4267 fix_insert_line($fixlinenr, ltrim($line1));
4268 fix_insert_line($fixlinenr, "\+{");
4269 if ($line2 !~ /^\s*$/) {
4270 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4271 }
4272 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004273 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004274
Andy Whitcroft8905a672007-11-28 16:21:06 -08004275# open braces for enum, union and struct go on the same line.
4276 if ($line =~ /^.\s*{/ &&
4277 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004278 if (ERROR("OPEN_BRACE",
4279 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4280 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4281 fix_delete_line($fixlinenr - 1, $prevrawline);
4282 fix_delete_line($fixlinenr, $rawline);
4283 my $fixedline = rtrim($prevrawline) . " {";
4284 fix_insert_line($fixlinenr, $fixedline);
4285 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004286 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004287 if ($fixedline !~ /^\+\s*$/) {
4288 fix_insert_line($fixlinenr, $fixedline);
4289 }
4290 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004291 }
4292
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004293# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004294 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4295 if (WARN("SPACING",
4296 "missing space after $1 definition\n" . $herecurr) &&
4297 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004298 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004299 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4300 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004301 }
4302
Joe Perches31070b52014-01-23 15:54:49 -08004303# Function pointer declarations
4304# check spacing between type, funcptr, and args
4305# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004306 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004307 my $declare = $1;
4308 my $pre_pointer_space = $2;
4309 my $post_pointer_space = $3;
4310 my $funcname = $4;
4311 my $post_funcname_space = $5;
4312 my $pre_args_space = $6;
4313
Joe Perches91f72e92014-04-03 14:49:12 -07004314# the $Declare variable will capture all spaces after the type
4315# so check it for a missing trailing missing space but pointer return types
4316# don't need a space so don't warn for those.
4317 my $post_declare_space = "";
4318 if ($declare =~ /(\s+)$/) {
4319 $post_declare_space = $1;
4320 $declare = rtrim($declare);
4321 }
4322 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004323 WARN("SPACING",
4324 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004325 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004326 }
4327
4328# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004329# This test is not currently implemented because these declarations are
4330# equivalent to
4331# int foo(int bar, ...)
4332# and this is form shouldn't/doesn't generate a checkpatch warning.
4333#
4334# elsif ($declare =~ /\s{2,}$/) {
4335# WARN("SPACING",
4336# "Multiple spaces after return type\n" . $herecurr);
4337# }
Joe Perches31070b52014-01-23 15:54:49 -08004338
4339# unnecessary space "type ( *funcptr)(args...)"
4340 if (defined $pre_pointer_space &&
4341 $pre_pointer_space =~ /^\s/) {
4342 WARN("SPACING",
4343 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4344 }
4345
4346# unnecessary space "type (* funcptr)(args...)"
4347 if (defined $post_pointer_space &&
4348 $post_pointer_space =~ /^\s/) {
4349 WARN("SPACING",
4350 "Unnecessary space before function pointer name\n" . $herecurr);
4351 }
4352
4353# unnecessary space "type (*funcptr )(args...)"
4354 if (defined $post_funcname_space &&
4355 $post_funcname_space =~ /^\s/) {
4356 WARN("SPACING",
4357 "Unnecessary space after function pointer name\n" . $herecurr);
4358 }
4359
4360# unnecessary space "type (*funcptr) (args...)"
4361 if (defined $pre_args_space &&
4362 $pre_args_space =~ /^\s/) {
4363 WARN("SPACING",
4364 "Unnecessary space before function pointer arguments\n" . $herecurr);
4365 }
4366
4367 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004368 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004369 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004370 }
4371 }
4372
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004373# check for spacing round square brackets; allowed:
4374# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004375# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4376# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004377 while ($line =~ /(.*?\s)\[/g) {
4378 my ($where, $prefix) = ($-[1], $1);
4379 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004380 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004381 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004382 if (ERROR("BRACKET_SPACE",
4383 "space prohibited before open square bracket '['\n" . $herecurr) &&
4384 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004385 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004386 s/^(\+.*?)\s+\[/$1\[/;
4387 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004388 }
4389 }
4390
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004391# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004392 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004393 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004394 my $ctx_before = substr($line, 0, $-[1]);
4395 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004396
4397 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004398 if ($name =~ /^(?:
4399 if|for|while|switch|return|case|
4400 volatile|__volatile__|
4401 __attribute__|format|__extension__|
4402 asm|__asm__)$/x)
4403 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004404 # cpp #define statements have non-optional spaces, ie
4405 # if there is a space between the name and the open
4406 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004407 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004408
4409 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004410 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004411
4412 # If this whole things ends with a type its most
4413 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004414 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004415
4416 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004417 if (WARN("SPACING",
4418 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4419 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004420 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004421 s/\b$name\s+\(/$name\(/;
4422 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004423 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004424 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004425
Andy Whitcroft653d4872007-06-23 17:16:34 -07004426# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004427 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004428 my $fixed_line = "";
4429 my $line_fixed = 0;
4430
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004431 my $ops = qr{
4432 <<=|>>=|<=|>=|==|!=|
4433 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4434 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004435 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004436 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004437 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004438 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004439
4440## print("element count: <" . $#elements . ">\n");
4441## foreach my $el (@elements) {
4442## print("el: <$el>\n");
4443## }
4444
4445 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004446 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004447
Joe Perches3705ce52013-07-03 15:05:31 -07004448 foreach my $el (@elements) {
4449 push(@fix_elements, substr($rawline, $off, length($el)));
4450 $off += length($el);
4451 }
4452
4453 $off = 0;
4454
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004455 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004456 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004457
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004458 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004459
4460 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4461
4462## print("n: <$n> good: <$good>\n");
4463
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004464 $off += length($elements[$n]);
4465
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004466 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004467 my $ca = substr($opline, 0, $off);
4468 my $cc = '';
4469 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4470 $cc = substr($opline, $off + length($elements[$n + 1]));
4471 }
4472 my $cb = "$ca$;$cc";
4473
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004474 my $a = '';
4475 $a = 'V' if ($elements[$n] ne '');
4476 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004477 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004478 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4479 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004480 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004481
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004482 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004483
4484 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004485 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004486 $c = 'V' if ($elements[$n + 2] ne '');
4487 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004488 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004489 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4490 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004491 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004492 } else {
4493 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004494 }
4495
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004496 my $ctx = "${a}x${c}";
4497
4498 my $at = "(ctx:$ctx)";
4499
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004500 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004501 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004502
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004503 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004504 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004505
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004506 # Get the full operator variant.
4507 my $opv = $op . substr($curr_vars, $off, 1);
4508
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004509 # Ignore operators passed as parameters.
4510 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004511 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004512
Andy Whitcroftcf655042008-03-04 14:28:20 -08004513# # Ignore comments
4514# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004515
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004516 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004517 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004518 if ($ctx !~ /.x[WEBC]/ &&
4519 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004520 if (ERROR("SPACING",
4521 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004522 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004523 $line_fixed = 1;
4524 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004525 }
4526
4527 # // is a comment
4528 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004529
Joe Perchesb00e4812014-04-03 14:49:33 -07004530 # : when part of a bitfield
4531 } elsif ($opv eq ':B') {
4532 # skip the bitfield test for now
4533
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004534 # No spaces for:
4535 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004536 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004537 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004538 if (ERROR("SPACING",
4539 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004540 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004541 if (defined $fix_elements[$n + 2]) {
4542 $fix_elements[$n + 2] =~ s/^\s+//;
4543 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004544 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004545 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004546 }
4547
Joe Perches23810972014-12-10 15:51:32 -08004548 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004549 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004550 my $rtrim_before = 0;
4551 my $space_after = 0;
4552 if ($ctx =~ /Wx./) {
4553 if (ERROR("SPACING",
4554 "space prohibited before that '$op' $at\n" . $hereptr)) {
4555 $line_fixed = 1;
4556 $rtrim_before = 1;
4557 }
4558 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004559 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004560 if (ERROR("SPACING",
4561 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004562 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004563 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004564 $space_after = 1;
4565 }
4566 }
4567 if ($rtrim_before || $space_after) {
4568 if ($rtrim_before) {
4569 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4570 } else {
4571 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4572 }
4573 if ($space_after) {
4574 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004575 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004576 }
4577
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004578 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004579 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004580 #warn "'*' is part of type\n";
4581
4582 # unary operators should have a space before and
4583 # none after. May be left adjacent to another
4584 # unary operator, or a cast
4585 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004586 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004587 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004588 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004589 if (ERROR("SPACING",
4590 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004591 if ($n != $last_after + 2) {
4592 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4593 $line_fixed = 1;
4594 }
Joe Perches3705ce52013-07-03 15:05:31 -07004595 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004596 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004597 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004598 # A unary '*' may be const
4599
4600 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004601 if (ERROR("SPACING",
4602 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004603 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004604 if (defined $fix_elements[$n + 2]) {
4605 $fix_elements[$n + 2] =~ s/^\s+//;
4606 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004607 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004608 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004609 }
4610
4611 # unary ++ and unary -- are allowed no space on one side.
4612 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004613 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004614 if (ERROR("SPACING",
4615 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004616 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004617 $line_fixed = 1;
4618 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004619 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004620 if ($ctx =~ /Wx[BE]/ ||
4621 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004622 if (ERROR("SPACING",
4623 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004624 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004625 $line_fixed = 1;
4626 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004627 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004628 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004629 if (ERROR("SPACING",
4630 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004631 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004632 if (defined $fix_elements[$n + 2]) {
4633 $fix_elements[$n + 2] =~ s/^\s+//;
4634 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004635 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004636 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004637 }
4638
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004639 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004640 } elsif ($op eq '<<' or $op eq '>>' or
4641 $op eq '&' or $op eq '^' or $op eq '|' or
4642 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004643 $op eq '*' or $op eq '/' or
4644 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004645 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004646 if ($check) {
4647 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4648 if (CHK("SPACING",
4649 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4650 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4651 $fix_elements[$n + 2] =~ s/^\s+//;
4652 $line_fixed = 1;
4653 }
4654 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4655 if (CHK("SPACING",
4656 "space preferred before that '$op' $at\n" . $hereptr)) {
4657 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4658 $line_fixed = 1;
4659 }
4660 }
4661 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004662 if (ERROR("SPACING",
4663 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004664 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4665 if (defined $fix_elements[$n + 2]) {
4666 $fix_elements[$n + 2] =~ s/^\s+//;
4667 }
Joe Perches3705ce52013-07-03 15:05:31 -07004668 $line_fixed = 1;
4669 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004670 }
4671
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004672 # A colon needs no spaces before when it is
4673 # terminating a case value or a label.
4674 } elsif ($opv eq ':C' || $opv eq ':L') {
4675 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004676 if (ERROR("SPACING",
4677 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004678 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004679 $line_fixed = 1;
4680 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004681 }
4682
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004683 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004684 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004685 my $ok = 0;
4686
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004687 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004688 if (($op eq '<' &&
4689 $cc =~ /^\S+\@\S+>/) ||
4690 ($op eq '>' &&
4691 $ca =~ /<\S+\@\S+$/))
4692 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07004693 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004694 }
4695
Joe Perchese0df7e12015-04-16 12:44:53 -07004696 # for asm volatile statements
4697 # ignore a colon with another
4698 # colon immediately before or after
4699 if (($op eq ':') &&
4700 ($ca =~ /:$/ || $cc =~ /^:/)) {
4701 $ok = 1;
4702 }
4703
Joe Perches84731622013-11-12 15:10:05 -08004704 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004705 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004706 my $msg_level = \&ERROR;
4707 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004708
Jean Delvare0675a8f2017-09-08 16:16:07 -07004709 if (&{$msg_level}("SPACING",
4710 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004711 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4712 if (defined $fix_elements[$n + 2]) {
4713 $fix_elements[$n + 2] =~ s/^\s+//;
4714 }
Joe Perches3705ce52013-07-03 15:05:31 -07004715 $line_fixed = 1;
4716 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004717 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004718 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004719 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004720
4721## print("n: <$n> GOOD: <$good>\n");
4722
4723 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004724 }
Joe Perches3705ce52013-07-03 15:05:31 -07004725
4726 if (($#elements % 2) == 0) {
4727 $fixed_line = $fixed_line . $fix_elements[$#elements];
4728 }
4729
Joe Perches194f66f2014-08-06 16:11:03 -07004730 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4731 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004732 }
4733
4734
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004735 }
4736
Joe Perches786b6322013-07-03 15:05:32 -07004737# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004738 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004739 if (WARN("SPACING",
4740 "space prohibited before semicolon\n" . $herecurr) &&
4741 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004742 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004743 s/^(\+.*\S)\s+;/$1;/;
4744 }
4745 }
4746
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004747# check for multiple assignments
4748 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004749 CHK("MULTIPLE_ASSIGNMENTS",
4750 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004751 }
4752
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004753## # check for multiple declarations, allowing for a function declaration
4754## # continuation.
4755## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4756## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4757##
4758## # Remove any bracketed sections to ensure we do not
4759## # falsly report the parameters of functions.
4760## my $ln = $line;
4761## while ($ln =~ s/\([^\(\)]*\)//g) {
4762## }
4763## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004764## WARN("MULTIPLE_DECLARATION",
4765## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004766## }
4767## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004768
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004769#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004770 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004771 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004772 if (ERROR("SPACING",
4773 "space required before the open brace '{'\n" . $herecurr) &&
4774 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004775 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004776 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004777 }
4778
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004779## # check for blank lines before declarations
4780## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4781## $prevrawline =~ /^.\s*$/) {
4782## WARN("SPACING",
4783## "No blank lines before declarations\n" . $hereprev);
4784## }
4785##
4786
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004787# closing brace should have a space following it when it has anything
4788# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07004789 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004790 if (ERROR("SPACING",
4791 "space required after that close brace '}'\n" . $herecurr) &&
4792 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004793 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004794 s/}((?!(?:,|;|\)))\S)/} $1/;
4795 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004796 }
4797
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004798# check spacing on square brackets
4799 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004800 if (ERROR("SPACING",
4801 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4802 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004803 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004804 s/\[\s+/\[/;
4805 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004806 }
4807 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004808 if (ERROR("SPACING",
4809 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4810 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004811 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004812 s/\s+\]/\]/;
4813 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004814 }
4815
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004816# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004817 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4818 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004819 if (ERROR("SPACING",
4820 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4821 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004822 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004823 s/\(\s+/\(/;
4824 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004825 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004826 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004827 $line !~ /for\s*\(.*;\s+\)/ &&
4828 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004829 if (ERROR("SPACING",
4830 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4831 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004832 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004833 s/\s+\)/\)/;
4834 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004835 }
4836
Joe Perchese2826fd2014-08-06 16:10:48 -07004837# check unnecessary parentheses around addressof/dereference single $Lvals
4838# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4839
4840 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004841 my $var = $1;
4842 if (CHK("UNNECESSARY_PARENTHESES",
4843 "Unnecessary parentheses around $var\n" . $herecurr) &&
4844 $fix) {
4845 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4846 }
4847 }
4848
4849# check for unnecessary parentheses around function pointer uses
4850# ie: (foo->bar)(); should be foo->bar();
4851# but not "if (foo->bar) (" to avoid some false positives
4852 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4853 my $var = $2;
4854 if (CHK("UNNECESSARY_PARENTHESES",
4855 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4856 $fix) {
4857 my $var2 = deparenthesize($var);
4858 $var2 =~ s/\s//g;
4859 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4860 }
4861 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004862
Joe Perches63b7c732017-09-08 16:16:01 -07004863# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004864# when !drivers/staging or command-line uses --strict
4865 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004866 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004867 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4868 my $if_stat = $1;
4869 my $test = substr($2, 1, -1);
4870 my $herectx;
4871 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4872 my $match = $1;
4873 # avoid parentheses around potential macro args
4874 next if ($match =~ /^\s*\w+\s*$/);
4875 if (!defined($herectx)) {
4876 $herectx = $here . "\n";
4877 my $cnt = statement_rawlines($if_stat);
4878 for (my $n = 0; $n < $cnt; $n++) {
4879 my $rl = raw_line($linenr, $n);
4880 $herectx .= $rl . "\n";
4881 last if $rl =~ /^[ \+].*\{/;
4882 }
4883 }
4884 CHK("UNNECESSARY_PARENTHESES",
4885 "Unnecessary parentheses around '$match'\n" . $herectx);
4886 }
4887 }
4888
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004889#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004890 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004891 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004892 if (WARN("INDENTED_LABEL",
4893 "labels should not be indented\n" . $herecurr) &&
4894 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004895 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004896 s/^(.)\s+/$1/;
4897 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004898 }
4899
Joe Perches5b9553a2014-04-03 14:49:21 -07004900# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004901 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004902 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004903 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004904 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4905 my $value = $1;
4906 $value = deparenthesize($value);
4907 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4908 ERROR("RETURN_PARENTHESES",
4909 "return is not a function, parentheses are not required\n" . $herecurr);
4910 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004911 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004912 ERROR("SPACING",
4913 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004914 }
4915 }
Joe Perches507e5142013-11-12 15:10:13 -08004916
Joe Perchesb43ae212014-06-23 13:22:07 -07004917# unnecessary return in a void function
4918# at end-of-function, with the previous line a single leading tab, then return;
4919# and the line before that not a goto label target like "out:"
4920 if ($sline =~ /^[ \+]}\s*$/ &&
4921 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4922 $linenr >= 3 &&
4923 $lines[$linenr - 3] =~ /^[ +]/ &&
4924 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004925 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004926 "void function return statements are not generally useful\n" . $hereprev);
4927 }
Joe Perches9819cf22014-06-04 16:12:09 -07004928
Joe Perches189248d2014-01-23 15:54:47 -08004929# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004930 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004931 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4932 my $openparens = $1;
4933 my $count = $openparens =~ tr@\(@\(@;
4934 my $msg = "";
4935 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4936 my $comp = $4; #Not $1 because of $LvalOrFunc
4937 $msg = " - maybe == should be = ?" if ($comp eq "==");
4938 WARN("UNNECESSARY_PARENTHESES",
4939 "Unnecessary parentheses$msg\n" . $herecurr);
4940 }
4941 }
4942
Joe Perchesc5595fa2015-09-09 15:37:58 -07004943# comparisons with a constant or upper case identifier on the left
4944# avoid cases like "foo + BAR < baz"
4945# only fix matches surrounded by parentheses to avoid incorrect
4946# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004947 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004948 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4949 my $lead = $1;
4950 my $const = $2;
4951 my $comp = $3;
4952 my $to = $4;
4953 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004954 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004955 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4956 WARN("CONSTANT_COMPARISON",
4957 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4958 $fix) {
4959 if ($comp eq "<") {
4960 $newcomp = ">";
4961 } elsif ($comp eq "<=") {
4962 $newcomp = ">=";
4963 } elsif ($comp eq ">") {
4964 $newcomp = "<";
4965 } elsif ($comp eq ">=") {
4966 $newcomp = "<=";
4967 }
4968 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4969 }
4970 }
4971
Joe Perchesf34e4a42015-04-16 12:44:19 -07004972# Return of what appears to be an errno should normally be negative
4973 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004974 my $name = $1;
4975 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004976 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004977 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004978 }
4979 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004980
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004981# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004982 if ($line =~ /\b(if|while|for|switch)\(/) {
4983 if (ERROR("SPACING",
4984 "space required before the open parenthesis '('\n" . $herecurr) &&
4985 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004986 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004987 s/\b(if|while|for|switch)\(/$1 \(/;
4988 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004989 }
4990
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004991# Check for illegal assignment in if conditional -- and check for trailing
4992# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004993 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004994 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4995 ctx_statement_block($linenr, $realcnt, 0)
4996 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004997 my ($stat_next) = ctx_statement_block($line_nr_next,
4998 $remain_next, $off_next);
4999 $stat_next =~ s/\n./\n /g;
5000 ##print "stat<$stat> stat_next<$stat_next>\n";
5001
5002 if ($stat_next =~ /^\s*while\b/) {
5003 # If the statement carries leading newlines,
5004 # then count those as offsets.
5005 my ($whitespace) =
5006 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5007 my $offset =
5008 statement_rawlines($whitespace) - 1;
5009
5010 $suppress_whiletrailers{$line_nr_next +
5011 $offset} = 1;
5012 }
5013 }
5014 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005015 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005016 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005017 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005018
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005019 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005020 ERROR("ASSIGN_IN_IF",
5021 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005022 }
5023
5024 # Find out what is on the end of the line after the
5025 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005026 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005027 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005028 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005029 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5030 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005031 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005032 # Find out how long the conditional actually is.
5033 my @newlines = ($c =~ /\n/gs);
5034 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005035 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005036
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005037 $stat_real = raw_line($linenr, $cond_lines)
5038 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005039 if (defined($stat_real) && $cond_lines > 1) {
5040 $stat_real = "[...]\n$stat_real";
5041 }
5042
Joe Perches000d1cc12011-07-25 17:13:25 -07005043 ERROR("TRAILING_STATEMENTS",
5044 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005045 }
5046 }
5047
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005048# Check for bitwise tests written as boolean
5049 if ($line =~ /
5050 (?:
5051 (?:\[|\(|\&\&|\|\|)
5052 \s*0[xX][0-9]+\s*
5053 (?:\&\&|\|\|)
5054 |
5055 (?:\&\&|\|\|)
5056 \s*0[xX][0-9]+\s*
5057 (?:\&\&|\|\||\)|\])
5058 )/x)
5059 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005060 WARN("HEXADECIMAL_BOOLEAN_TEST",
5061 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005062 }
5063
Andy Whitcroft8905a672007-11-28 16:21:06 -08005064# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005065 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5066 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005067 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005068 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005069 ERROR("TRAILING_STATEMENTS",
5070 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005071 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005072 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005073# if should not continue a brace
5074 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005075 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005076 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005077 $herecurr);
5078 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005079# case and default should not have general statements after them
5080 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5081 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005082 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005083 \s*return\s+
5084 )/xg)
5085 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005086 ERROR("TRAILING_STATEMENTS",
5087 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005088 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005089
5090 # Check for }<nl>else {, these must be at the same
5091 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005092 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5093 $previndent == $indent) {
5094 if (ERROR("ELSE_AFTER_BRACE",
5095 "else should follow close brace '}'\n" . $hereprev) &&
5096 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5097 fix_delete_line($fixlinenr - 1, $prevrawline);
5098 fix_delete_line($fixlinenr, $rawline);
5099 my $fixedline = $prevrawline;
5100 $fixedline =~ s/}\s*$//;
5101 if ($fixedline !~ /^\+\s*$/) {
5102 fix_insert_line($fixlinenr, $fixedline);
5103 }
5104 $fixedline = $rawline;
5105 $fixedline =~ s/^(.\s*)else/$1} else/;
5106 fix_insert_line($fixlinenr, $fixedline);
5107 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005108 }
5109
Joe Perches8b8856f2014-08-06 16:11:14 -07005110 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5111 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005112 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5113
5114 # Find out what is on the end of the line after the
5115 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005116 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005117 $s =~ s/\n.*//g;
5118
5119 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005120 if (ERROR("WHILE_AFTER_BRACE",
5121 "while should follow close brace '}'\n" . $hereprev) &&
5122 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5123 fix_delete_line($fixlinenr - 1, $prevrawline);
5124 fix_delete_line($fixlinenr, $rawline);
5125 my $fixedline = $prevrawline;
5126 my $trailing = $rawline;
5127 $trailing =~ s/^\+//;
5128 $trailing = trim($trailing);
5129 $fixedline =~ s/}\s*$/} $trailing/;
5130 fix_insert_line($fixlinenr, $fixedline);
5131 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005132 }
5133 }
5134
Joe Perches95e2c602013-07-03 15:05:20 -07005135#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005136 while ($line =~ m{($Constant|$Lval)}g) {
5137 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005138
Joe Perches95e2c602013-07-03 15:05:20 -07005139#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005140 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005141 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005142#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005143 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005144#Ignore SI style variants like nS, mV and dB
5145#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5146 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005147#Ignore some three character SI units explicitly, like MiB and KHz
5148 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005149 while ($var =~ m{($Ident)}g) {
5150 my $word = $1;
5151 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005152 if ($check) {
5153 seed_camelcase_includes();
5154 if (!$file && !$camelcase_file_seeded) {
5155 seed_camelcase_file($realfile);
5156 $camelcase_file_seeded = 1;
5157 }
5158 }
Joe Perches7e781f62013-09-11 14:23:55 -07005159 if (!defined $camelcase{$word}) {
5160 $camelcase{$word} = 1;
5161 CHK("CAMELCASE",
5162 "Avoid CamelCase: <$word>\n" . $herecurr);
5163 }
Joe Perches34456862013-07-03 15:05:34 -07005164 }
Joe Perches323c1262012-12-17 16:02:07 -08005165 }
5166 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005167
5168#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005169 if ($line =~ /\#\s*define.*\\\s+$/) {
5170 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5171 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5172 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005173 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005174 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005175 }
5176
Fabian Frederick0e212e02015-04-16 12:44:25 -07005177# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5178# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005179 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005180 my $file = "$1.h";
5181 my $checkfile = "include/linux/$file";
5182 if (-f "$root/$checkfile" &&
5183 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005184 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005185 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005186 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5187 if ($asminclude > 0) {
5188 if ($realfile =~ m{^arch/}) {
5189 CHK("ARCH_INCLUDE_LINUX",
5190 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5191 } else {
5192 WARN("INCLUDE_LINUX",
5193 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5194 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005195 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005196 }
5197 }
5198
Andy Whitcroft653d4872007-06-23 17:16:34 -07005199# multi-statement macros should be enclosed in a do while loop, grab the
5200# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005201# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005202 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5203 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005204 my $ln = $linenr;
5205 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005206 my ($off, $dstat, $dcond, $rest);
5207 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005208 my $has_flow_statement = 0;
5209 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005210 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005211 ctx_statement_block($linenr, $realcnt, 0);
5212 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005213 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005214 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005215
Joe Perches08a28432014-10-13 15:51:55 -07005216 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005217 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005218
Joe Perchesf59b64b2016-10-11 13:52:08 -07005219 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5220 my $define_args = $1;
5221 my $define_stmt = $dstat;
5222 my @def_args = ();
5223
5224 if (defined $define_args && $define_args ne "") {
5225 $define_args = substr($define_args, 1, length($define_args) - 2);
5226 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005227 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005228 @def_args = split(",", $define_args);
5229 }
5230
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005231 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005232 $dstat =~ s/\\\n.//g;
5233 $dstat =~ s/^\s*//s;
5234 $dstat =~ s/\s*$//s;
5235
5236 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005237 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5238 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005239 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005240 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005241 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005242
Antonio Borneo342d3d22020-04-06 20:11:01 -07005243 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005244 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5245 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005246 {
5247 }
5248
Joe Perches42e15292016-03-15 14:58:01 -07005249 # Make asm volatile uses seem like a generic function
5250 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5251
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005252 my $exceptions = qr{
5253 $Declare|
5254 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005255 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005256 DECLARE_PER_CPU|
5257 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005258 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005259 union|
5260 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005261 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005262 ^\"|\"$|
5263 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005264 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005265 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005266
5267 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005268 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005269 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005270
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005271 if ($dstat ne '' &&
5272 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5273 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005274 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005275 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005276 $dstat !~ /$exceptions/ &&
5277 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005278 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005279 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005280 $dstat !~ /^for\s*$Constant$/ && # for (...)
5281 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5282 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005283 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005284 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005285 {
Joe Perchese7955562017-05-08 15:55:48 -07005286 if ($dstat =~ /^\s*if\b/) {
5287 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5288 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5289 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005290 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5291 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5292 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005293 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005294 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005295 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005296
5297 }
Joe Perches52076492016-10-11 13:52:14 -07005298
5299 # Make $define_stmt single line, comment-free, etc
5300 my @stmt_array = split('\n', $define_stmt);
5301 my $first = 1;
5302 $define_stmt = "";
5303 foreach my $l (@stmt_array) {
5304 $l =~ s/\\$//;
5305 if ($first) {
5306 $define_stmt = $l;
5307 $first = 0;
5308 } elsif ($l =~ /^[\+ ]/) {
5309 $define_stmt .= substr($l, 1);
5310 }
5311 }
5312 $define_stmt =~ s/$;//g;
5313 $define_stmt =~ s/\s+/ /g;
5314 $define_stmt = trim($define_stmt);
5315
Joe Perchesf59b64b2016-10-11 13:52:08 -07005316# check if any macro arguments are reused (ignore '...' and 'type')
5317 foreach my $arg (@def_args) {
5318 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005319 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005320 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005321 $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 -07005322 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5323 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005324 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005325 if ($use_cnt > 1) {
5326 CHK("MACRO_ARG_REUSE",
5327 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005328 }
5329# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005330 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005331 ((defined($1) && $1 ne ',') ||
5332 (defined($2) && $2 ne ','))) {
5333 CHK("MACRO_ARG_PRECEDENCE",
5334 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005335 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005336 }
Joe Perches5023d342012-12-17 16:01:47 -08005337
Joe Perches08a28432014-10-13 15:51:55 -07005338# check for macros with flow control, but without ## concatenation
5339# ## concatenation is commonly a macro that defines a function so ignore those
5340 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005341 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005342 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005343
Joe Perches08a28432014-10-13 15:51:55 -07005344 WARN("MACRO_WITH_FLOW_CONTROL",
5345 "Macros with flow control statements should be avoided\n" . "$herectx");
5346 }
5347
Joe Perches481eb482012-12-17 16:01:56 -08005348# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005349
5350 } else {
5351 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005352 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5353 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005354 $line =~ /^\+.*\\$/) {
5355 WARN("LINE_CONTINUATIONS",
5356 "Avoid unnecessary line continuations\n" . $herecurr);
5357 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005358 }
5359
Joe Perchesb13edf72012-07-30 14:41:24 -07005360# do {} while (0) macro tests:
5361# single-statement macros do not need to be enclosed in do while (0) loop,
5362# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005363 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005364 $realfile !~ m@/vmlinux.lds.h$@ &&
5365 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5366 my $ln = $linenr;
5367 my $cnt = $realcnt;
5368 my ($off, $dstat, $dcond, $rest);
5369 my $ctx = '';
5370 ($dstat, $dcond, $ln, $cnt, $off) =
5371 ctx_statement_block($linenr, $realcnt, 0);
5372 $ctx = $dstat;
5373
5374 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005375 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005376
5377 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5378 my $stmts = $2;
5379 my $semis = $3;
5380
5381 $ctx =~ s/\n*$//;
5382 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005383 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005384
Joe Perchesac8e97f2012-08-21 16:15:53 -07005385 if (($stmts =~ tr/;/;/) == 1 &&
5386 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005387 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5388 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5389 }
5390 if (defined $semis && $semis ne "") {
5391 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5392 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5393 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005394 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5395 $ctx =~ s/\n*$//;
5396 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005397 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005398
5399 WARN("TRAILING_SEMICOLON",
5400 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005401 }
5402 }
5403
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005404# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005405 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5406 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005407 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005408 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005409 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5410 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005411 my @allowed = ();
5412 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005413 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005414 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005415 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005416 for my $chunk (@chunks) {
5417 my ($cond, $block) = @{$chunk};
5418
Andy Whitcroft773647a2008-03-28 14:15:58 -07005419 # If the condition carries leading newlines, then count those as offsets.
5420 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5421 my $offset = statement_rawlines($whitespace) - 1;
5422
Joe Perchesaad4f612012-03-23 15:02:19 -07005423 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005424 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5425
5426 # We have looked at and allowed this specific line.
5427 $suppress_ifbraces{$ln + $offset} = 1;
5428
5429 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005430 $ln += statement_rawlines($block) - 1;
5431
Andy Whitcroft773647a2008-03-28 14:15:58 -07005432 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005433
5434 $seen++ if ($block =~ /^\s*{/);
5435
Joe Perchesaad4f612012-03-23 15:02:19 -07005436 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005437 if (statement_lines($cond) > 1) {
5438 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005439 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005440 }
5441 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005442 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005443 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005444 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005445 if (statement_block_size($block) > 1) {
5446 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005447 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005448 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005449 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005450 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005451 if ($seen) {
5452 my $sum_allowed = 0;
5453 foreach (@allowed) {
5454 $sum_allowed += $_;
5455 }
5456 if ($sum_allowed == 0) {
5457 WARN("BRACES",
5458 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5459 } elsif ($sum_allowed != $allow &&
5460 $seen != $allow) {
5461 CHK("BRACES",
5462 "braces {} should be used on all arms of this statement\n" . $herectx);
5463 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005464 }
5465 }
5466 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005467 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005468 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005469 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005470
Andy Whitcroftcf655042008-03-04 14:28:20 -08005471 # Check the pre-context.
5472 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5473 #print "APW: ALLOWED: pre<$1>\n";
5474 $allowed = 1;
5475 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005476
5477 my ($level, $endln, @chunks) =
5478 ctx_statement_full($linenr, $realcnt, $-[0]);
5479
Andy Whitcroftcf655042008-03-04 14:28:20 -08005480 # Check the condition.
5481 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005482 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005483 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005484 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005485 }
5486 if (statement_lines($cond) > 1) {
5487 #print "APW: ALLOWED: cond<$cond>\n";
5488 $allowed = 1;
5489 }
5490 if ($block =~/\b(?:if|for|while)\b/) {
5491 #print "APW: ALLOWED: block<$block>\n";
5492 $allowed = 1;
5493 }
5494 if (statement_block_size($block) > 1) {
5495 #print "APW: ALLOWED: lines block<$block>\n";
5496 $allowed = 1;
5497 }
5498 # Check the post-context.
5499 if (defined $chunks[1]) {
5500 my ($cond, $block) = @{$chunks[1]};
5501 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005502 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005503 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005504 if ($block =~ /^\s*\{/) {
5505 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5506 $allowed = 1;
5507 }
5508 }
5509 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005510 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005511 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005512
Joe Perches000d1cc12011-07-25 17:13:25 -07005513 WARN("BRACES",
5514 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005515 }
5516 }
5517
Joe Perchese4c5bab2017-02-24 15:01:41 -08005518# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005519 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5520 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005521 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5522 }
5523
Joe Perches0979ae62012-12-17 16:01:59 -08005524# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005525 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005526 if (CHK("BRACES",
5527 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5528 $fix && $prevrawline =~ /^\+/) {
5529 fix_delete_line($fixlinenr - 1, $prevrawline);
5530 }
Joe Perches0979ae62012-12-17 16:01:59 -08005531 }
Joe Perches77b9a532013-07-03 15:05:29 -07005532 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005533 if (CHK("BRACES",
5534 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5535 $fix) {
5536 fix_delete_line($fixlinenr, $rawline);
5537 }
Joe Perches0979ae62012-12-17 16:01:59 -08005538 }
5539
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005540# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005541 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5542 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005543 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005544 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005545 }
5546
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005547# Check for user-visible strings broken across lines, which breaks the ability
5548# to grep for the string. Make exceptions when the previous string ends in a
5549# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5550# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005551 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005552 $prevline =~ /"\s*$/ &&
5553 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5554 if (WARN("SPLIT_STRING",
5555 "quoted string split across lines\n" . $hereprev) &&
5556 $fix &&
5557 $prevrawline =~ /^\+.*"\s*$/ &&
5558 $last_coalesced_string_linenr != $linenr - 1) {
5559 my $extracted_string = get_quoted_string($line, $rawline);
5560 my $comma_close = "";
5561 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5562 $comma_close = $1;
5563 }
5564
5565 fix_delete_line($fixlinenr - 1, $prevrawline);
5566 fix_delete_line($fixlinenr, $rawline);
5567 my $fixedline = $prevrawline;
5568 $fixedline =~ s/"\s*$//;
5569 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5570 fix_insert_line($fixlinenr - 1, $fixedline);
5571 $fixedline = $rawline;
5572 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5573 if ($fixedline !~ /\+\s*$/) {
5574 fix_insert_line($fixlinenr, $fixedline);
5575 }
5576 $last_coalesced_string_linenr = $linenr;
5577 }
5578 }
5579
5580# check for missing a space in a string concatenation
5581 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5582 WARN('MISSING_SPACE',
5583 "break quoted strings at a space character\n" . $hereprev);
5584 }
5585
Joe Perchese4b7d302017-05-08 15:55:51 -07005586# check for an embedded function name in a string when the function is known
5587# This does not work very well for -f --file checking as it depends on patch
5588# context providing the function name or a single line form for in-file
5589# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005590 if ($line =~ /^\+.*$String/ &&
5591 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005592 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5593 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005594 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005595 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005596 }
5597
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005598# check for spaces before a quoted newline
5599 if ($rawline =~ /^.*\".*\s\\n/) {
5600 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5601 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5602 $fix) {
5603 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5604 }
5605
5606 }
5607
Joe Perchesf17dba42014-10-13 15:51:51 -07005608# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005609 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5610 if (CHK("CONCATENATED_STRING",
5611 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5612 $fix) {
5613 while ($line =~ /($String)/g) {
5614 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5615 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5616 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5617 }
5618 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005619 }
5620
Joe Perches90ad30e2014-12-10 15:51:59 -08005621# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005622 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005623 if (WARN("STRING_FRAGMENTS",
5624 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5625 $fix) {
5626 while ($line =~ /($String)(?=\s*")/g) {
5627 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5628 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5629 }
5630 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005631 }
5632
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005633# check for non-standard and hex prefixed decimal printf formats
5634 my $show_L = 1; #don't show the same defect twice
5635 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005636 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005637 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005638 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005639 # check for %L
5640 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005641 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005642 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5643 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005644 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005645 # check for %Z
5646 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5647 WARN("PRINTF_Z",
5648 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5649 $show_Z = 0;
5650 }
5651 # check for 0x<decimal>
5652 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5653 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005654 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5655 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005656 }
5657
5658# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005659 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005660 WARN("LINE_CONTINUATIONS",
5661 "Avoid line continuations in quoted strings\n" . $herecurr);
5662 }
5663
Andy Whitcroft00df3442007-06-08 13:47:06 -07005664# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005665 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005666 WARN("IF_0",
5667 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5668 }
5669
5670# warn about #if 1
5671 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5672 WARN("IF_1",
5673 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005674 }
5675
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005676# check for needless "if (<foo>) fn(<foo>)" uses
5677 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005678 my $tested = quotemeta($1);
5679 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5680 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5681 my $func = $1;
5682 if (WARN('NEEDLESS_IF',
5683 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5684 $fix) {
5685 my $do_fix = 1;
5686 my $leading_tabs = "";
5687 my $new_leading_tabs = "";
5688 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5689 $leading_tabs = $1;
5690 } else {
5691 $do_fix = 0;
5692 }
5693 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5694 $new_leading_tabs = $1;
5695 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5696 $do_fix = 0;
5697 }
5698 } else {
5699 $do_fix = 0;
5700 }
5701 if ($do_fix) {
5702 fix_delete_line($fixlinenr - 1, $prevrawline);
5703 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5704 }
5705 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005706 }
5707 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005708
Joe Perchesebfdc402014-08-06 16:10:27 -07005709# check for unnecessary "Out of Memory" messages
5710 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5711 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5712 (defined $1 || defined $3) &&
5713 $linenr > 3) {
5714 my $testval = $2;
5715 my $testline = $lines[$linenr - 3];
5716
5717 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5718# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5719
Joe Perchese29a70f2019-03-07 16:28:35 -08005720 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5721 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005722 WARN("OOM_MESSAGE",
5723 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5724 }
5725 }
5726
Joe Perchesf78d98f2014-10-13 15:52:01 -07005727# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005728 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005729 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5730 my $level = $1;
5731 if (WARN("UNNECESSARY_KERN_LEVEL",
5732 "Possible unnecessary $level\n" . $herecurr) &&
5733 $fix) {
5734 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5735 }
5736 }
5737
Joe Perches45c55e92017-02-24 15:01:31 -08005738# check for logging continuations
5739 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5740 WARN("LOGGING_CONTINUATION",
5741 "Avoid logging continuation uses where feasible\n" . $herecurr);
5742 }
5743
Joe Perchesabb08a52014-12-10 15:51:46 -08005744# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005745 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005746 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5747 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5748 WARN("MASK_THEN_SHIFT",
5749 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5750 }
5751
Joe Perchesb75ac612014-12-10 15:52:02 -08005752# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005753 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005754 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5755 my $val = $1;
5756 my $equal = "!";
5757 $equal = "" if ($4 eq "!=");
5758 if (CHK("COMPARISON_TO_NULL",
5759 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5760 $fix) {
5761 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5762 }
5763 }
5764 }
5765
Joe Perches8716de32013-09-11 14:24:05 -07005766# check for bad placement of section $InitAttribute (e.g.: __initdata)
5767 if ($line =~ /(\b$InitAttribute\b)/) {
5768 my $attr = $1;
5769 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5770 my $ptr = $1;
5771 my $var = $2;
5772 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5773 ERROR("MISPLACED_INIT",
5774 "$attr should be placed after $var\n" . $herecurr)) ||
5775 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5776 WARN("MISPLACED_INIT",
5777 "$attr should be placed after $var\n" . $herecurr))) &&
5778 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005779 $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 -07005780 }
5781 }
5782 }
5783
Joe Perchese970b8842013-11-12 15:10:10 -08005784# check for $InitAttributeData (ie: __initdata) with const
5785 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5786 my $attr = $1;
5787 $attr =~ /($InitAttributePrefix)(.*)/;
5788 my $attr_prefix = $1;
5789 my $attr_type = $2;
5790 if (ERROR("INIT_ATTRIBUTE",
5791 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5792 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005793 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005794 s/$InitAttributeData/${attr_prefix}initconst/;
5795 }
5796 }
5797
5798# check for $InitAttributeConst (ie: __initconst) without const
5799 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5800 my $attr = $1;
5801 if (ERROR("INIT_ATTRIBUTE",
5802 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5803 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005804 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005805 /(^\+\s*(?:static\s+))/;
5806 $lead = rtrim($1);
5807 $lead = "$lead " if ($lead !~ /^\+$/);
5808 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005809 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005810 }
5811 }
5812
Joe Perchesc17893c2015-04-16 12:44:42 -07005813# check for __read_mostly with const non-pointer (should just be const)
5814 if ($line =~ /\b__read_mostly\b/ &&
5815 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5816 if (ERROR("CONST_READ_MOSTLY",
5817 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5818 $fix) {
5819 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5820 }
5821 }
5822
Joe Perchesfbdb8132014-04-03 14:49:14 -07005823# don't use __constant_<foo> functions outside of include/uapi/
5824 if ($realfile !~ m@^include/uapi/@ &&
5825 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5826 my $constant_func = $1;
5827 my $func = $constant_func;
5828 $func =~ s/^__constant_//;
5829 if (WARN("CONSTANT_CONVERSION",
5830 "$constant_func should be $func\n" . $herecurr) &&
5831 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005832 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005833 }
5834 }
5835
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005836# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005837 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005838 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005839 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005840 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005841 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005842 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07005843 }
5844 if ($delay > 2000) {
5845 WARN("LONG_UDELAY",
5846 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005847 }
5848 }
5849
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005850# warn about unexpectedly long msleep's
5851 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5852 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005853 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005854 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005855 }
5856 }
5857
Joe Perches36ec1932013-07-03 15:05:25 -07005858# check for comparisons of jiffies
5859 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5860 WARN("JIFFIES_COMPARISON",
5861 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5862 }
5863
Joe Perches9d7a34a2013-07-03 15:05:26 -07005864# check for comparisons of get_jiffies_64()
5865 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5866 WARN("JIFFIES_COMPARISON",
5867 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5868 }
5869
Andy Whitcroft00df3442007-06-08 13:47:06 -07005870# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005871# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005872# print "#ifdef in C files should be avoided\n";
5873# print "$herecurr";
5874# $clean = 0;
5875# }
5876
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005877# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005878 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005879 if (ERROR("SPACING",
5880 "exactly one space required after that #$1\n" . $herecurr) &&
5881 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005882 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005883 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5884 }
5885
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005886 }
5887
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005888# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005889 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5890 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005891 my $which = $1;
5892 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005893 CHK("UNCOMMENTED_DEFINITION",
5894 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005895 }
5896 }
5897# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005898
5899 my $barriers = qr{
5900 mb|
5901 rmb|
5902 wmb|
5903 read_barrier_depends
5904 }x;
5905 my $barrier_stems = qr{
5906 mb__before_atomic|
5907 mb__after_atomic|
5908 store_release|
5909 load_acquire|
5910 store_mb|
5911 (?:$barriers)
5912 }x;
5913 my $all_barriers = qr{
5914 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005915 smp_(?:$barrier_stems)|
5916 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005917 }x;
5918
5919 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005920 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005921 WARN("MEMORY_BARRIER",
5922 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005923 }
5924 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005925
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005926 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5927
5928 if ($realfile !~ m@^include/asm-generic/@ &&
5929 $realfile !~ m@/barrier\.h$@ &&
5930 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5931 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5932 WARN("MEMORY_BARRIER",
5933 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5934 }
5935
Joe Perchescb426e92015-06-25 15:02:46 -07005936# check for waitqueue_active without a comment.
5937 if ($line =~ /\bwaitqueue_active\s*\(/) {
5938 if (!ctx_has_comment($first_line, $linenr)) {
5939 WARN("WAITQUEUE_ACTIVE",
5940 "waitqueue_active without comment\n" . $herecurr);
5941 }
5942 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005943
Marco Elver5099a722020-04-01 12:17:14 +02005944# check for data_race without a comment.
5945 if ($line =~ /\bdata_race\s*\(/) {
5946 if (!ctx_has_comment($first_line, $linenr)) {
5947 WARN("DATA_RACE",
5948 "data_race without comment\n" . $herecurr);
5949 }
5950 }
5951
Paul E. McKenney91db2592017-11-27 09:37:35 -08005952# check for smp_read_barrier_depends and read_barrier_depends
5953 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5954 WARN("READ_BARRIER_DEPENDS",
5955 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5956 }
5957
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005958# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005959 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005960 CHK("ARCH_DEFINES",
5961 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005962 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005963
Joe Perches596ed452017-07-12 14:37:02 -07005964# check that the storage class is not after a type
5965 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005966 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005967 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5968 }
5969# Check that the storage class is at the beginning of a declaration
5970 if ($line =~ /\b$Storage\b/ &&
5971 $line !~ /^.\s*$Storage/ &&
5972 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5973 $1 !~ /[\,\)]\s*$/) {
5974 WARN("STORAGE_CLASS",
5975 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005976 }
5977
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005978# check the location of the inline attribute, that it is between
5979# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005980 if ($line =~ /\b$Type\s+$Inline\b/ ||
5981 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005982 ERROR("INLINE_LOCATION",
5983 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005984 }
5985
Andy Whitcroft8905a672007-11-28 16:21:06 -08005986# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005987 if ($realfile !~ m@\binclude/uapi/@ &&
5988 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005989 if (WARN("INLINE",
5990 "plain inline is preferred over $1\n" . $herecurr) &&
5991 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005992 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005993
5994 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005995 }
5996
Joe Perches3d130fd2011-01-12 17:00:00 -08005997# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005998 if ($realfile !~ m@\binclude/uapi/@ &&
5999 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006000 WARN("PREFER_PACKED",
6001 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08006002 }
6003
Joe Perches39b7e282011-07-25 17:13:24 -07006004# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08006005 if ($realfile !~ m@\binclude/uapi/@ &&
6006 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006007 WARN("PREFER_ALIGNED",
6008 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07006009 }
6010
Joe Perches462811d2019-09-25 16:46:44 -07006011# Check for __attribute__ section, prefer __section
6012 if ($realfile !~ m@\binclude/uapi/@ &&
6013 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
6014 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6015 my $new = substr($old, 1, -1);
6016 if (WARN("PREFER_SECTION",
6017 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6018 $fix) {
6019 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6020 }
6021 }
6022
Joe Perches5f14d3b2012-01-10 15:09:52 -08006023# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08006024 if ($realfile !~ m@\binclude/uapi/@ &&
6025 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006026 if (WARN("PREFER_PRINTF",
6027 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6028 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006029 $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 -07006030
6031 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08006032 }
6033
Joe Perches6061d942012-03-23 15:02:16 -07006034# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08006035 if ($realfile !~ m@\binclude/uapi/@ &&
6036 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006037 if (WARN("PREFER_SCANF",
6038 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6039 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006040 $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 -07006041 }
Joe Perches6061d942012-03-23 15:02:16 -07006042 }
6043
Joe Perches619a9082014-12-10 15:51:35 -08006044# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006045 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006046 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6047 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6048 $line =~ /\b__weak\b/)) {
6049 ERROR("WEAK_DECLARATION",
6050 "Using weak declarations can have unintended link defects\n" . $herecurr);
6051 }
6052
Tomas Winklerfd39f902016-12-12 16:46:34 -08006053# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006054 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006055 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006056 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6057 my $type = $1;
6058 if ($type =~ /\b($typeC99Typedefs)\b/) {
6059 $type = $1;
6060 my $kernel_type = 'u';
6061 $kernel_type = 's' if ($type =~ /^_*[si]/);
6062 $type =~ /(\d+)/;
6063 $kernel_type .= $1;
6064 if (CHK("PREFER_KERNEL_TYPES",
6065 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6066 $fix) {
6067 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6068 }
6069 }
6070 }
6071
Joe Perches938224b2016-01-20 14:59:15 -08006072# check for cast of C90 native int or longer types constants
6073 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6074 my $cast = $1;
6075 my $const = $2;
6076 if (WARN("TYPECAST_INT_CONSTANT",
6077 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6078 $fix) {
6079 my $suffix = "";
6080 my $newconst = $const;
6081 $newconst =~ s/${Int_type}$//;
6082 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6083 if ($cast =~ /\blong\s+long\b/) {
6084 $suffix .= 'LL';
6085 } elsif ($cast =~ /\blong\b/) {
6086 $suffix .= 'L';
6087 }
6088 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6089 }
6090 }
6091
Joe Perches8f53a9b2010-03-05 13:43:48 -08006092# check for sizeof(&)
6093 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006094 WARN("SIZEOF_ADDRESS",
6095 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006096 }
6097
Joe Perches66c80b62012-07-30 14:41:22 -07006098# check for sizeof without parenthesis
6099 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006100 if (WARN("SIZEOF_PARENTHESIS",
6101 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6102 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006103 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006104 }
Joe Perches66c80b62012-07-30 14:41:22 -07006105 }
6106
Joe Perches88982fe2012-12-17 16:02:00 -08006107# check for struct spinlock declarations
6108 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6109 WARN("USE_SPINLOCK_T",
6110 "struct spinlock should be spinlock_t\n" . $herecurr);
6111 }
6112
Joe Perchesa6962d72013-04-29 16:18:13 -07006113# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006114 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006115 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006116 $fmt =~ s/%%//g;
6117 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006118 if (WARN("PREFER_SEQ_PUTS",
6119 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6120 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006121 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006122 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006123 }
6124 }
6125
Joe Perches478b1792018-04-10 16:33:34 -07006126# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006127 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006128 defined $stat &&
6129 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6130 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006131 my $stat_real;
6132
Joe Perches0b523762017-05-08 15:55:36 -07006133 my $lc = $stat =~ tr@\n@@;
6134 $lc = $lc + $linenr;
6135 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006136 my $specifier;
6137 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006138 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006139 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006140 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6141 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006142
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006143 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006144 $specifier = $1;
6145 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006146 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006147 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006148 ($extension eq "f" &&
6149 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006150 $bad_specifier = $specifier;
6151 last;
6152 }
6153 if ($extension eq "x" && !defined($stat_real)) {
6154 if (!defined($stat_real)) {
6155 $stat_real = get_stat_real($linenr, $lc);
6156 }
6157 WARN("VSPRINTF_SPECIFIER_PX",
6158 "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");
6159 }
6160 }
6161 if ($bad_specifier ne "") {
6162 my $stat_real = get_stat_real($linenr, $lc);
6163 my $ext_type = "Invalid";
6164 my $use = "";
6165 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006166 $use = " - use %pS instead";
6167 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6168 }
6169
6170 WARN("VSPRINTF_POINTER_EXTENSION",
6171 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6172 }
Joe Perches0b523762017-05-08 15:55:36 -07006173 }
6174 }
6175
Andy Whitcroft554e1652012-01-10 15:09:57 -08006176# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006177 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006178 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006179 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006180
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006181 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006182 my $ms_val = $7;
6183 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006184
Andy Whitcroft554e1652012-01-10 15:09:57 -08006185 if ($ms_size =~ /^(0x|)0$/i) {
6186 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006187 "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 -08006188 } elsif ($ms_size =~ /^(0x|)1$/i) {
6189 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006190 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6191 }
6192 }
6193
Joe Perches98a9bba2014-01-23 15:54:52 -08006194# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006195# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006196# defined $stat &&
6197# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6198# if (WARN("PREFER_ETHER_ADDR_COPY",
6199# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6200# $fix) {
6201# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6202# }
6203# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006204
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006205# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006206# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006207# defined $stat &&
6208# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6209# WARN("PREFER_ETHER_ADDR_EQUAL",
6210# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6211# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006212
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006213# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6214# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006215# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006216# defined $stat &&
6217# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6218#
6219# my $ms_val = $7;
6220#
6221# if ($ms_val =~ /^(?:0x|)0+$/i) {
6222# if (WARN("PREFER_ETH_ZERO_ADDR",
6223# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6224# $fix) {
6225# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6226# }
6227# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6228# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6229# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6230# $fix) {
6231# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6232# }
6233# }
6234# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006235
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006236# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006237 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006238 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006239 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006240 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006241 my $call = $1;
6242 my $cast1 = deparenthesize($2);
6243 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006244 my $cast2 = deparenthesize($7);
6245 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006246 my $cast;
6247
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006248 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006249 $cast = "$cast1 or $cast2";
6250 } elsif ($cast1 ne "") {
6251 $cast = $cast1;
6252 } else {
6253 $cast = $cast2;
6254 }
6255 WARN("MINMAX",
6256 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006257 }
6258 }
6259
Joe Perches4a273192012-07-30 14:41:20 -07006260# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006261 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006262 defined $stat &&
6263 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6264 my $min = $1;
6265 my $max = $7;
6266 if ($min eq $max) {
6267 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006268 "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 -07006269 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6270 $min > $max) {
6271 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006272 "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 -07006273 }
6274 }
6275
Joe Perches823b7942013-11-12 15:10:15 -08006276# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006277 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006278 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006279 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006280 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6281 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6282 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6283 my $lc = $stat =~ tr@\n@@;
6284 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006285 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006286 WARN("NAKED_SSCANF",
6287 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6288 }
6289
Joe Perchesafc819a2014-06-04 16:12:08 -07006290# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006291 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006292 defined $stat &&
6293 $line =~ /\bsscanf\b/) {
6294 my $lc = $stat =~ tr@\n@@;
6295 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006296 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006297 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6298 my $format = $6;
6299 my $count = $format =~ tr@%@%@;
6300 if ($count == 1 &&
6301 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6302 WARN("SSCANF_TO_KSTRTO",
6303 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6304 }
6305 }
6306 }
6307
Joe Perches70dc8a42013-09-11 14:23:58 -07006308# check for new externs in .h files.
6309 if ($realfile =~ /\.h$/ &&
6310 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006311 if (CHK("AVOID_EXTERNS",
6312 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006313 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006314 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006315 }
6316 }
6317
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006318# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006319 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006320 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006321 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006322 my $function_name = $1;
6323 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006324
6325 my $s = $stat;
6326 if (defined $cond) {
6327 substr($s, 0, length($cond), '');
6328 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006329 if ($s =~ /^\s*;/ &&
6330 $function_name ne 'uninitialized_var')
6331 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006332 WARN("AVOID_EXTERNS",
6333 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006334 }
6335
6336 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006337 WARN("FUNCTION_ARGUMENTS",
6338 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006339 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006340
6341 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6342 $stat =~ /^.\s*extern\s+/)
6343 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006344 WARN("AVOID_EXTERNS",
6345 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006346 }
6347
Joe Perchesa0ad7592017-07-10 15:52:19 -07006348# check for function declarations that have arguments without identifier names
Joe Perches16b7f3c2020-04-06 20:11:17 -07006349# while avoiding uninitialized_var(x)
Joe Perchesa0ad7592017-07-10 15:52:19 -07006350 if (defined $stat &&
Joe Perches16b7f3c2020-04-06 20:11:17 -07006351 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:($Ident)|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6352 (!defined($1) ||
6353 (defined($1) && $1 ne "uninitialized_var")) &&
6354 $2 ne "void") {
6355 my $args = trim($2);
Joe Perchesca0d8922016-10-11 13:52:16 -07006356 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6357 my $arg = trim($1);
Joe Perches16b7f3c2020-04-06 20:11:17 -07006358 if ($arg =~ /^$Type$/ &&
6359 $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006360 WARN("FUNCTION_ARGUMENTS",
6361 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6362 }
6363 }
6364 }
6365
Joe Perchesa0ad7592017-07-10 15:52:19 -07006366# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006367 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006368 defined $stat &&
6369 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6370 $context_function = $1;
6371
6372# check for multiline function definition with misplaced open brace
6373 my $ok = 0;
6374 my $cnt = statement_rawlines($stat);
6375 my $herectx = $here . "\n";
6376 for (my $n = 0; $n < $cnt; $n++) {
6377 my $rl = raw_line($linenr, $n);
6378 $herectx .= $rl . "\n";
6379 $ok = 1 if ($rl =~ /^[ \+]\{/);
6380 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6381 last if $rl =~ /^[ \+].*\{/;
6382 }
6383 if (!$ok) {
6384 ERROR("OPEN_BRACE",
6385 "open brace '{' following function definitions go on the next line\n" . $herectx);
6386 }
6387 }
6388
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006389# checks for new __setup's
6390 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6391 my $name = $1;
6392
6393 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006394 CHK("UNDOCUMENTED_SETUP",
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07006395 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006396 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006397 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006398
Joe Perchese29a70f2019-03-07 16:28:35 -08006399# check for pointless casting of alloc functions
6400 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006401 WARN("UNNECESSARY_CASTS",
6402 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006403 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006404
Joe Perchesa640d252013-07-03 15:05:21 -07006405# alloc style
6406# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006407 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006408 $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 -07006409 CHK("ALLOC_SIZEOF_STRUCT",
6410 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6411 }
6412
Joe Perches60a55362014-06-04 16:12:07 -07006413# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006414 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006415 defined $stat &&
6416 $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 -07006417 my $oldfunc = $3;
6418 my $a1 = $4;
6419 my $a2 = $10;
6420 my $newfunc = "kmalloc_array";
6421 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006422 my $r1 = $a1;
6423 my $r2 = $a2;
6424 if ($a1 =~ /^sizeof\s*\S/) {
6425 $r1 = $a2;
6426 $r2 = $a1;
6427 }
6428 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6429 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006430 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006431 my $herectx = get_stat_here($linenr, $cnt, $here);
6432
Joe Perches60a55362014-06-04 16:12:07 -07006433 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006434 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6435 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006436 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006437 $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 -07006438 }
6439 }
6440 }
6441
Joe Perches972fdea2013-04-29 16:18:12 -07006442# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006443 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006444 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6445 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006446 WARN("KREALLOC_ARG_REUSE",
6447 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6448 }
6449
Joe Perches5ce59ae2013-02-21 16:44:18 -08006450# check for alloc argument mismatch
6451 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6452 WARN("ALLOC_ARRAY_ARGS",
6453 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6454 }
6455
Joe Perchescaf2a542011-01-12 16:59:56 -08006456# check for multiple semicolons
6457 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006458 if (WARN("ONE_SEMICOLON",
6459 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6460 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006461 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006462 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006463 }
6464
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006465# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6466 if ($realfile !~ m@^include/uapi/@ &&
6467 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006468 my $ull = "";
6469 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6470 if (CHK("BIT_MACRO",
6471 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6472 $fix) {
6473 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6474 }
6475 }
6476
Joe Perches2d632742016-05-20 17:04:00 -07006477# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6478 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6479 my $config = $1;
6480 if (WARN("PREFER_IS_ENABLED",
6481 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6482 $fix) {
6483 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6484 }
6485 }
6486
Joe Perchese81f2392014-08-06 16:11:25 -07006487# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006488 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6489 my $has_break = 0;
6490 my $has_statement = 0;
6491 my $count = 0;
6492 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006493 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006494 $prevline--;
6495 my $rline = $rawlines[$prevline - 1];
6496 my $fline = $lines[$prevline - 1];
6497 last if ($fline =~ /^\@\@/);
6498 next if ($fline =~ /^\-/);
6499 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6500 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6501 next if ($fline =~ /^.[\s$;]*$/);
6502 $has_statement = 1;
6503 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006504 $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 -08006505 }
6506 if (!$has_break && $has_statement) {
6507 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006508 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006509 }
6510 }
6511
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006512# check for /* fallthrough */ like comment, prefer fallthrough;
6513 my @fallthroughs = (
6514 'fallthrough',
6515 '@fallthrough@',
6516 'lint -fallthrough[ \t]*',
6517 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6518 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6519 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6520 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6521 );
6522 if ($raw_comment ne '') {
6523 foreach my $ft (@fallthroughs) {
6524 if ($raw_comment =~ /$ft/) {
6525 my $msg_level = \&WARN;
6526 $msg_level = \&CHK if ($file);
6527 &{$msg_level}("PREFER_FALLTHROUGH",
6528 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6529 last;
6530 }
6531 }
6532 }
6533
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006534# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006535 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006536 defined $stat &&
6537 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006538 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006539 my $herectx = get_stat_here($linenr, $cnt, $here);
6540
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006541 WARN("DEFAULT_NO_BREAK",
6542 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006543 }
6544
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006545# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006546 if ($line =~ /\b__FUNCTION__\b/) {
6547 if (WARN("USE_FUNC",
6548 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6549 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006550 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006551 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006552 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006553
Joe Perches62ec8182015-02-13 14:38:18 -08006554# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6555 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6556 ERROR("DATE_TIME",
6557 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6558 }
6559
Joe Perches2c924882012-03-23 15:02:20 -07006560# check for use of yield()
6561 if ($line =~ /\byield\s*\(\s*\)/) {
6562 WARN("YIELD",
6563 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6564 }
6565
Joe Perches179f8f42013-07-03 15:05:30 -07006566# check for comparisons against true and false
6567 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6568 my $lead = $1;
6569 my $arg = $2;
6570 my $test = $3;
6571 my $otype = $4;
6572 my $trail = $5;
6573 my $op = "!";
6574
6575 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6576
6577 my $type = lc($otype);
6578 if ($type =~ /^(?:true|false)$/) {
6579 if (("$test" eq "==" && "$type" eq "true") ||
6580 ("$test" eq "!=" && "$type" eq "false")) {
6581 $op = "";
6582 }
6583
6584 CHK("BOOL_COMPARISON",
6585 "Using comparison to $otype is error prone\n" . $herecurr);
6586
6587## maybe suggesting a correct construct would better
6588## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6589
6590 }
6591 }
6592
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006593# check for semaphores initialized locked
6594 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006595 WARN("CONSIDER_COMPLETION",
6596 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006597 }
Joe Perches6712d852012-03-23 15:02:20 -07006598
Joe Perches67d0a072011-10-31 17:13:10 -07006599# recommend kstrto* over simple_strto* and strict_strto*
6600 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006601 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006602 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006603 }
Joe Perches6712d852012-03-23 15:02:20 -07006604
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006605# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006606 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006607 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006608 "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 -07006609 }
Joe Perches6712d852012-03-23 15:02:20 -07006610
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006611# check for spin_is_locked(), suggest lockdep instead
6612 if ($line =~ /\bspin_is_locked\(/) {
6613 WARN("USE_LOCKDEP",
6614 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6615 }
6616
Joe Perches9189c7e2018-09-07 15:26:18 -07006617# check for deprecated apis
6618 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6619 my $deprecated_api = $1;
6620 my $new_api = $deprecated_apis{$deprecated_api};
6621 WARN("DEPRECATED_API",
6622 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6623 }
6624
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006625# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006626# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006627 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006628 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006629 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006630 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006631 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006632
6633# use of NR_CPUS is usually wrong
6634# ignore definitions of NR_CPUS and usage to define arrays as likely right
6635 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006636 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6637 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006638 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6639 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6640 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006641 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006642 WARN("NR_CPUS",
6643 "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 -07006644 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006645
Joe Perches52ea8502013-11-12 15:10:09 -08006646# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6647 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6648 ERROR("DEFINE_ARCH_HAS",
6649 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6650 }
6651
Joe Perchesacd93622015-02-13 14:38:38 -08006652# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006653 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006654 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6655 WARN("LIKELY_MISUSE",
6656 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6657 }
6658
Denis Efremovde3f1862019-09-25 16:49:25 -07006659# nested likely/unlikely calls
6660 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6661 WARN("LIKELY_MISUSE",
6662 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6663 }
6664
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006665# whine mightly about in_atomic
6666 if ($line =~ /\bin_atomic\s*\(/) {
6667 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006668 ERROR("IN_ATOMIC",
6669 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006670 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006671 WARN("IN_ATOMIC",
6672 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006673 }
6674 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006675
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006676# check for mutex_trylock_recursive usage
6677 if ($line =~ /mutex_trylock_recursive/) {
6678 ERROR("LOCKING",
6679 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6680 }
6681
Peter Zijlstra1704f472010-03-19 01:37:42 +01006682# check for lockdep_set_novalidate_class
6683 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6684 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6685 if ($realfile !~ m@^kernel/lockdep@ &&
6686 $realfile !~ m@^include/linux/lockdep@ &&
6687 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006688 ERROR("LOCKDEP",
6689 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006690 }
6691 }
Dave Jones88f88312011-01-12 16:59:59 -08006692
Joe Perchesb392c642015-04-16 12:44:16 -07006693 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6694 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006695 WARN("EXPORTED_WORLD_WRITABLE",
6696 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006697 }
Joe Perches24358802014-04-03 14:49:13 -07006698
Joe Perches00180462018-02-06 15:38:55 -08006699# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6700# and whether or not function naming is typical and if
6701# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006702 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006703 defined $stat &&
6704 $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*\)/) {
6705 my $var = $1;
6706 my $perms = $2;
6707 my $show = $3;
6708 my $store = $4;
6709 my $octal_perms = perms_to_octal($perms);
6710 if ($show =~ /^${var}_show$/ &&
6711 $store =~ /^${var}_store$/ &&
6712 $octal_perms eq "0644") {
6713 if (WARN("DEVICE_ATTR_RW",
6714 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6715 $fix) {
6716 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6717 }
6718 } elsif ($show =~ /^${var}_show$/ &&
6719 $store =~ /^NULL$/ &&
6720 $octal_perms eq "0444") {
6721 if (WARN("DEVICE_ATTR_RO",
6722 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6723 $fix) {
6724 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6725 }
6726 } elsif ($show =~ /^NULL$/ &&
6727 $store =~ /^${var}_store$/ &&
6728 $octal_perms eq "0200") {
6729 if (WARN("DEVICE_ATTR_WO",
6730 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6731 $fix) {
6732 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6733 }
6734 } elsif ($octal_perms eq "0644" ||
6735 $octal_perms eq "0444" ||
6736 $octal_perms eq "0200") {
6737 my $newshow = "$show";
6738 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6739 my $newstore = $store;
6740 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6741 my $rename = "";
6742 if ($show ne $newshow) {
6743 $rename .= " '$show' to '$newshow'";
6744 }
6745 if ($store ne $newstore) {
6746 $rename .= " '$store' to '$newstore'";
6747 }
6748 WARN("DEVICE_ATTR_FUNCTIONS",
6749 "Consider renaming function(s)$rename\n" . $herecurr);
6750 } else {
6751 WARN("DEVICE_ATTR_PERMS",
6752 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6753 }
6754 }
6755
Joe Perches515a2352014-04-03 14:49:24 -07006756# Mode permission misuses where it seems decimal should be octal
6757# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006758# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6759# specific definition of not visible in sysfs.
6760# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6761# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006762 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006763 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006764 $line =~ /$mode_perms_search/) {
6765 foreach my $entry (@mode_permission_funcs) {
6766 my $func = $entry->[0];
6767 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006768
Joe Perches459cf0a2016-10-11 13:52:19 -07006769 my $lc = $stat =~ tr@\n@@;
6770 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006771 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006772
Joe Perches515a2352014-04-03 14:49:24 -07006773 my $skip_args = "";
6774 if ($arg_pos > 1) {
6775 $arg_pos--;
6776 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6777 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006778 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006779 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006780 my $val = $1;
6781 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006782 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6783 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6784 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006785 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006786 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006787 }
6788 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006789 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006790 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006791 }
Joe Perches24358802014-04-03 14:49:13 -07006792 }
6793 }
6794 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006795
Joe Perches459cf0a2016-10-11 13:52:19 -07006796# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006797 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006798 my $oval = $1;
6799 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006800 if (WARN("SYMBOLIC_PERMS",
6801 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6802 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006803 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006804 }
6805 }
6806
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006807# validate content of MODULE_LICENSE against list from include/linux/module.h
6808 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6809 my $extracted_string = get_quoted_string($line, $rawline);
6810 my $valid_licenses = qr{
6811 GPL|
6812 GPL\ v2|
6813 GPL\ and\ additional\ rights|
6814 Dual\ BSD/GPL|
6815 Dual\ MIT/GPL|
6816 Dual\ MPL/GPL|
6817 Proprietary
6818 }x;
6819 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6820 WARN("MODULE_LICENSE",
6821 "unknown module license " . $extracted_string . "\n" . $herecurr);
6822 }
6823 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07006824
6825# check for sysctl duplicate constants
6826 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6827 WARN("DUPLICATED_SYSCTL_CONST",
6828 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6829 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006830 }
6831
6832 # If we have no input at all, then there is nothing to report on
6833 # so just keep quiet.
6834 if ($#rawlines == -1) {
6835 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006836 }
6837
Andy Whitcroft8905a672007-11-28 16:21:06 -08006838 # In mailback mode only produce a report in the negative, for
6839 # things that appear to be patches.
6840 if ($mailback && ($clean == 1 || !$is_patch)) {
6841 exit(0);
6842 }
6843
6844 # This is not a patch, and we are are in 'no-patch' mode so
6845 # just keep quiet.
6846 if (!$chk_patch && !$is_patch) {
6847 exit(0);
6848 }
6849
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006850 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006851 ERROR("NOT_UNIFIED_DIFF",
6852 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006853 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006854 if ($is_patch && $has_commit_log && $chk_signoff) {
6855 if ($signoff == 0) {
6856 ERROR("MISSING_SIGN_OFF",
6857 "Missing Signed-off-by: line(s)\n");
6858 } elsif (!$authorsignoff) {
6859 WARN("NO_AUTHOR_SIGN_OFF",
6860 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6861 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006862 }
6863
Andy Whitcroft8905a672007-11-28 16:21:06 -08006864 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006865 if ($summary && !($clean == 1 && $quiet == 1)) {
6866 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006867 print "total: $cnt_error errors, $cnt_warn warnings, " .
6868 (($check)? "$cnt_chk checks, " : "") .
6869 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006870 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006871
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006872 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006873 # If there were any defects found and not already fixing them
6874 if (!$clean and !$fix) {
6875 print << "EOM"
6876
6877NOTE: For some of the reported defects, checkpatch may be able to
6878 mechanically convert to the typical style using --fix or --fix-inplace.
6879EOM
6880 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006881 # If there were whitespace errors which cleanpatch can fix
6882 # then suggest that.
6883 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006884 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006885 print << "EOM"
6886
6887NOTE: Whitespace errors detected.
6888 You may wish to use scripts/cleanpatch or scripts/cleanfile
6889EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006890 }
6891 }
6892
Joe Perchesd752fcc2014-08-06 16:11:05 -07006893 if ($clean == 0 && $fix &&
6894 ("@rawlines" ne "@fixed" ||
6895 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006896 my $newfile = $filename;
6897 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006898 my $linecount = 0;
6899 my $f;
6900
Joe Perchesd752fcc2014-08-06 16:11:05 -07006901 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6902
Joe Perches3705ce52013-07-03 15:05:31 -07006903 open($f, '>', $newfile)
6904 or die "$P: Can't open $newfile for write\n";
6905 foreach my $fixed_line (@fixed) {
6906 $linecount++;
6907 if ($file) {
6908 if ($linecount > 3) {
6909 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006910 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006911 }
6912 } else {
6913 print $f $fixed_line . "\n";
6914 }
6915 }
6916 close($f);
6917
6918 if (!$quiet) {
6919 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006920
Joe Perches3705ce52013-07-03 15:05:31 -07006921Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6922
6923Do _NOT_ trust the results written to this file.
6924Do _NOT_ submit these changes without inspecting them for correctness.
6925
6926This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6927No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006928EOM
6929 }
6930 }
6931
Joe Perchesd8469f12015-06-25 15:03:00 -07006932 if ($quiet == 0) {
6933 print "\n";
6934 if ($clean == 1) {
6935 print "$vname has no obvious style problems and is ready for submission.\n";
6936 } else {
6937 print "$vname has style problems, please review.\n";
6938 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006939 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006940 return $clean;
6941}