blob: 4c820607540bf646f8defa5417876a987c1bc74d [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 Whitcroft5368df22008-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;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01003045 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
3046 if ($lines[$ln - 1] =~ "---help---") {
3047 WARN("CONFIG_DESCRIPTION",
3048 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
3049 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003050 $length = -1;
3051 }
3052
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003053 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003054 $f =~ s/#.*//;
3055 $f =~ s/^\s+//;
3056 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003057
3058 # This only checks context lines in the patch
3059 # and so hopefully shouldn't trigger false
3060 # positives, even though some of these are
3061 # common words in help texts
3062 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3063 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003064 $is_end = 1;
3065 last;
3066 }
Andi Kleen33549572010-05-24 14:33:29 -07003067 $length++;
3068 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003069 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3070 WARN("CONFIG_DESCRIPTION",
3071 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3072 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003073 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003074 }
3075
Joe Perches7ccf41a2020-06-04 16:50:33 -07003076# check MAINTAINERS entries
3077 if ($realfile =~ /^MAINTAINERS$/) {
3078# check MAINTAINERS entries for the right form
3079 if ($rawline =~ /^\+[A-Z]:/ &&
3080 $rawline !~ /^\+[A-Z]:\t\S/) {
3081 if (WARN("MAINTAINERS_STYLE",
3082 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3083 $fix) {
3084 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3085 }
3086 }
3087# check MAINTAINERS entries for the right ordering too
3088 my $preferred_order = 'MRLSWQBCPTFXNK';
3089 if ($rawline =~ /^\+[A-Z]:/ &&
3090 $prevrawline =~ /^[\+ ][A-Z]:/) {
3091 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3092 my $cur = $1;
3093 my $curval = $2;
3094 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3095 my $prev = $1;
3096 my $prevval = $2;
3097 my $curindex = index($preferred_order, $cur);
3098 my $previndex = index($preferred_order, $prev);
3099 if ($curindex < 0) {
3100 WARN("MAINTAINERS_STYLE",
3101 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3102 } else {
3103 if ($previndex >= 0 && $curindex < $previndex) {
3104 WARN("MAINTAINERS_STYLE",
3105 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3106 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3107 ($prev eq 'X' && $cur eq 'X')) &&
3108 ($prevval cmp $curval) > 0) {
3109 WARN("MAINTAINERS_STYLE",
3110 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3111 }
3112 }
Joe Perches628f91a2017-07-10 15:52:07 -07003113 }
3114 }
3115
Christoph Jaeger327953e2015-02-13 14:38:29 -08003116# discourage the use of boolean for type definition attributes of Kconfig options
3117 if ($realfile =~ /Kconfig/ &&
3118 $line =~ /^\+\s*\bboolean\b/) {
3119 WARN("CONFIG_TYPE_BOOLEAN",
3120 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3121 }
3122
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003123 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3124 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3125 my $flag = $1;
3126 my $replacement = {
3127 'EXTRA_AFLAGS' => 'asflags-y',
3128 'EXTRA_CFLAGS' => 'ccflags-y',
3129 'EXTRA_CPPFLAGS' => 'cppflags-y',
3130 'EXTRA_LDFLAGS' => 'ldflags-y',
3131 };
3132
3133 WARN("DEPRECATED_VARIABLE",
3134 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3135 }
3136
Rob Herringbff5da42014-01-23 15:54:51 -08003137# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003138 if (defined $root &&
3139 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3140 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3141
Rob Herringbff5da42014-01-23 15:54:51 -08003142 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3143
Florian Vaussardcc933192014-04-03 14:49:27 -07003144 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003145 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003146
Rob Herringbff5da42014-01-23 15:54:51 -08003147 foreach my $compat (@compats) {
3148 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003149 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3150 my $compat3 = $compat;
3151 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3152 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003153 if ( $? >> 8 ) {
3154 WARN("UNDOCUMENTED_DT_STRING",
3155 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3156 }
3157
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003158 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3159 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003160 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003161 if ( $? >> 8 ) {
3162 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003163 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003164 }
3165 }
3166 }
3167
Rob Herring9f3a8992018-04-10 16:33:13 -07003168# check for using SPDX license tag at beginning of files
3169 if ($realline == $checklicenseline) {
3170 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3171 $checklicenseline = 2;
3172 } elsif ($rawline =~ /^\+/) {
3173 my $comment = "";
3174 if ($realfile =~ /\.(h|s|S)$/) {
3175 $comment = '/*';
3176 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3177 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003178 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003179 $comment = '#';
3180 } elsif ($realfile =~ /\.rst$/) {
3181 $comment = '..';
3182 }
3183
Joe Perchesfdf13692019-03-07 16:28:32 -08003184# check SPDX comment style for .[chsS] files
3185 if ($realfile =~ /\.[chsS]$/ &&
3186 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003187 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003188 WARN("SPDX_LICENSE_TAG",
3189 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3190 }
3191
Rob Herring9f3a8992018-04-10 16:33:13 -07003192 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003193 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3194 WARN("SPDX_LICENSE_TAG",
3195 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003196 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003197 my $spdx_license = $1;
3198 if (!is_SPDX_License_valid($spdx_license)) {
3199 WARN("SPDX_LICENSE_TAG",
3200 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3201 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003202 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3203 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3204 my $msg_level = \&WARN;
3205 $msg_level = \&CHK if ($file);
3206 if (&{$msg_level}("SPDX_LICENSE_TAG",
3207
3208 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3209 $fix) {
3210 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3211 }
3212 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003213 }
3214 }
3215 }
3216
Andy Whitcroft5368df22008-10-15 22:02:27 -07003217# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003218 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07003219
Joe Perchesa8da38a2019-03-07 16:28:42 -08003220# check for using SPDX-License-Identifier on the wrong line number
3221 if ($realline != $checklicenseline &&
3222 $rawline =~ /\bSPDX-License-Identifier:/ &&
3223 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3224 WARN("SPDX_LICENSE_TAG",
3225 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3226 }
3227
Joe Perches47e0c882015-06-25 15:02:57 -07003228# line length limit (with some exclusions)
3229#
3230# There are a few types of lines that may extend beyond $max_line_length:
3231# logging functions like pr_info that end in a string
3232# lines with a single string
3233# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003234# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003235#
3236# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003237# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003238# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3239# LONG_LINE all other lines longer than $max_line_length
3240#
3241# if LONG_LINE is ignored, the other 2 types are also ignored
3242#
3243
Joe Perchesb4749e92015-07-17 16:24:01 -07003244 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003245 my $msg_type = "LONG_LINE";
3246
3247 # Check the allowed long line types first
3248
3249 # logging functions that end in a string that starts
3250 # before $max_line_length
3251 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3252 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3253 $msg_type = "";
3254
3255 # lines with only strings (w/ possible termination)
3256 # #defines with only strings
3257 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3258 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3259 $msg_type = "";
3260
Joe Perchescc147502017-11-17 15:28:44 -08003261 # More special cases
3262 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3263 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003264 $msg_type = "";
3265
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003266 # URL ($rawline is used in case the URL is in a comment)
3267 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3268 $msg_type = "";
3269
Joe Perches47e0c882015-06-25 15:02:57 -07003270 # Otherwise set the alternate message types
3271
3272 # a comment starts before $max_line_length
3273 } elsif ($line =~ /($;[\s$;]*)$/ &&
3274 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3275 $msg_type = "LONG_LINE_COMMENT"
3276
3277 # a quoted string starts before $max_line_length
3278 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3279 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3280 $msg_type = "LONG_LINE_STRING"
3281 }
3282
3283 if ($msg_type ne "" &&
3284 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003285 my $msg_level = \&WARN;
3286 $msg_level = \&CHK if ($file);
3287 &{$msg_level}($msg_type,
3288 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003289 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003290 }
3291
Andy Whitcroft8905a672007-11-28 16:21:06 -08003292# check for adding lines without a newline.
3293 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003294 WARN("MISSING_EOF_NEWLINE",
3295 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003296 }
3297
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003298# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003299 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003300
3301# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003302# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003303 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3304 $rawline =~ /^\+\s* \s*/) {
3305 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003306 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003307 if (ERROR("CODE_INDENT",
3308 "code indent should use tabs where possible\n" . $herevet) &&
3309 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003310 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003311 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003312 }
3313
Alberto Panizzo08e44362010-03-05 13:43:54 -08003314# check for space before tabs.
3315 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3316 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003317 if (WARN("SPACE_BEFORE_TAB",
3318 "please, no space before tabs\n" . $herevet) &&
3319 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003320 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003321 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003322 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003323 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003324 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003325 }
3326
Joe Perches6a487212018-04-10 16:34:04 -07003327# check for assignments on the start of a line
3328 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3329 CHK("ASSIGNMENT_CONTINUATIONS",
3330 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3331 }
3332
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003333# check for && or || at the start of a line
3334 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3335 CHK("LOGICAL_CONTINUATIONS",
3336 "Logical continuations should be on the previous line\n" . $hereprev);
3337 }
3338
Joe Perchesa91e8992016-05-20 17:04:05 -07003339# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003340 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003341 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003342 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003343 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003344 if (WARN("TABSTOP",
3345 "Statements should start on a tabstop\n" . $herecurr) &&
3346 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003347 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003348 }
3349 }
3350 }
3351
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003352# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003353 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003354 $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 -07003355 $prevline =~ /^\+(\t*)(.*)$/;
3356 my $oldindent = $1;
3357 my $rest = $2;
3358
3359 my $pos = pos_last_openparen($rest);
3360 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003361 $line =~ /^(\+| )([ \t]*)/;
3362 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003363
3364 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003365 "\t" x ($pos / $tabsize) .
3366 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003367 my $goodspaceindent = $oldindent . " " x $pos;
3368
3369 if ($newindent ne $goodtabindent &&
3370 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003371
3372 if (CHK("PARENTHESIS_ALIGNMENT",
3373 "Alignment should match open parenthesis\n" . $hereprev) &&
3374 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003375 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003376 s/^\+[ \t]*/\+$goodtabindent/;
3377 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003378 }
3379 }
3380 }
3381
Joe Perches6ab3a972015-04-16 12:44:05 -07003382# check for space after cast like "(int) foo" or "(struct foo) bar"
3383# avoid checking a few false positives:
3384# "sizeof(<type>)" or "__alignof__(<type>)"
3385# function pointer declarations like "(*foo)(int) = bar;"
3386# structure definitions like "(struct foo) { 0 };"
3387# multiline macros that define functions
3388# known attributes or the __attribute__ keyword
3389 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3390 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003391 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003392 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003393 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003394 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003395 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003396 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003397 }
3398
Joe Perches86406b12015-09-09 15:37:41 -07003399# Block comment styles
3400# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003401 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003402 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003403 $rawline =~ /^\+[ \t]*\*/ &&
3404 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003405 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3406 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3407 }
3408
Joe Perches86406b12015-09-09 15:37:41 -07003409# Block comments use * on subsequent lines
3410 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3411 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003412 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003413 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003414 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003415 WARN("BLOCK_COMMENT_STYLE",
3416 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003417 }
3418
Joe Perches86406b12015-09-09 15:37:41 -07003419# Block comments use */ on trailing lines
3420 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003421 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3422 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3423 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003424 WARN("BLOCK_COMMENT_STYLE",
3425 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003426 }
3427
Joe Perches08eb9b82016-10-11 13:51:50 -07003428# Block comment * alignment
3429 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003430 $line =~ /^\+[ \t]*$;/ && #leading comment
3431 $rawline =~ /^\+[ \t]*\*/ && #leading *
3432 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003433 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003434 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3435 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003436 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003437 if (defined($1)) {
3438 $oldindent = expand_tabs($1);
3439 } else {
3440 $prevrawline =~ m@^\+(.*/?)\*@;
3441 $oldindent = expand_tabs($1);
3442 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003443 $rawline =~ m@^\+([ \t]*)\*@;
3444 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003445 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003446 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003447 WARN("BLOCK_COMMENT_STYLE",
3448 "Block comments should align the * on each line\n" . $hereprev);
3449 }
3450 }
3451
Joe Perches7f619192014-08-06 16:10:39 -07003452# check for missing blank lines after struct/union declarations
3453# with exceptions for various attributes and macros
3454 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3455 $line =~ /^\+/ &&
3456 !($line =~ /^\+\s*$/ ||
3457 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3458 $line =~ /^\+\s*MODULE_/i ||
3459 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3460 $line =~ /^\+[a-z_]*init/ ||
3461 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3462 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003463 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003464 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003465 if (CHK("LINE_SPACING",
3466 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3467 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003468 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003469 }
Joe Perches7f619192014-08-06 16:10:39 -07003470 }
3471
Joe Perches365dd4e2014-08-06 16:10:42 -07003472# check for multiple consecutive blank lines
3473 if ($prevline =~ /^[\+ ]\s*$/ &&
3474 $line =~ /^\+\s*$/ &&
3475 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003476 if (CHK("LINE_SPACING",
3477 "Please don't use multiple blank lines\n" . $hereprev) &&
3478 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003479 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003480 }
3481
Joe Perches365dd4e2014-08-06 16:10:42 -07003482 $last_blank_line = $linenr;
3483 }
3484
Joe Perches3b617e32014-04-03 14:49:28 -07003485# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003486 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3487 # actual declarations
3488 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003489 # function pointer declarations
3490 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003491 # foo bar; where foo is some local typedef or #define
3492 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3493 # known declaration macros
3494 $prevline =~ /^\+\s+$declaration_macros/) &&
3495 # for "else if" which can look like "$Ident $Ident"
3496 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3497 # other possible extensions of declaration lines
3498 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3499 # not starting a section or a macro "\" extended line
3500 $prevline =~ /(?:\{\s*|\\)$/) &&
3501 # looks like a declaration
3502 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003503 # function pointer declarations
3504 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003505 # foo bar; where foo is some local typedef or #define
3506 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3507 # known declaration macros
3508 $sline =~ /^\+\s+$declaration_macros/ ||
3509 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003510 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003511 # start or end of block or continuation of declaration
3512 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3513 # bitfield continuation
3514 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3515 # other possible extensions of declaration lines
3516 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3517 # indentation of previous and current line are the same
3518 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003519 if (WARN("LINE_SPACING",
3520 "Missing a blank line after declarations\n" . $hereprev) &&
3521 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003522 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003523 }
Joe Perches3b617e32014-04-03 14:49:28 -07003524 }
3525
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003526# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003527# Exceptions:
3528# 1) within comments
3529# 2) indented preprocessor commands
3530# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003531 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003532 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003533 if (WARN("LEADING_SPACE",
3534 "please, no spaces at the start of a line\n" . $herevet) &&
3535 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003536 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003537 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003538 }
3539
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003540# check we are in a valid C source file if not then ignore this hunk
3541 next if ($realfile !~ /\.(h|c)$/);
3542
Joe Perches5751a242017-11-17 15:28:52 -08003543# check for unusual line ending [ or (
3544 if ($line =~ /^\+.*([\[\(])\s*$/) {
3545 CHK("OPEN_ENDED_LINE",
3546 "Lines should not end with a '$1'\n" . $herecurr);
3547 }
3548
Joe Perches4dbed762017-05-08 15:55:39 -07003549# check if this appears to be the start function declaration, save the name
3550 if ($sline =~ /^\+\{\s*$/ &&
3551 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3552 $context_function = $1;
3553 }
3554
3555# check if this appears to be the end of function declaration
3556 if ($sline =~ /^\+\}\s*$/) {
3557 undef $context_function;
3558 }
3559
Joe Perches032a4c02014-08-06 16:10:29 -07003560# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003561# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003562# if the previous line is a break or return and is indented 1 tab more...
3563 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3564 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003565 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3566 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3567 defined $lines[$linenr] &&
3568 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003569 WARN("UNNECESSARY_ELSE",
3570 "else is not generally useful after a break or return\n" . $hereprev);
3571 }
3572 }
3573
Joe Perchesc00df192014-08-06 16:11:01 -07003574# check indentation of a line with a break;
3575# if the previous line is a goto or return and is indented the same # of tabs
3576 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3577 my $tabs = $1;
3578 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3579 WARN("UNNECESSARY_BREAK",
3580 "break is not useful after a goto or return\n" . $hereprev);
3581 }
3582 }
3583
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003584# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003585 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003586 WARN("CVS_KEYWORD",
3587 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003588 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003589
Joe Perches56e77d72013-02-21 16:44:14 -08003590# check for old HOTPLUG __dev<foo> section markings
3591 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3592 WARN("HOTPLUG_SECTION",
3593 "Using $1 is unnecessary\n" . $herecurr);
3594 }
3595
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003596# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003597 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3598 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003599#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003600 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003601 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003602 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003603 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003604 $stat =~ s/\n./\n /g;
3605 $cond =~ s/\n./\n /g;
3606
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003607#print "linenr<$linenr> <$stat>\n";
3608 # If this statement has no statement boundaries within
3609 # it there is no point in retrying a statement scan
3610 # until we hit end of it.
3611 my $frag = $stat; $frag =~ s/;+\s*$//;
3612 if ($frag !~ /(?:{|;)/) {
3613#print "skip<$line_nr_next>\n";
3614 $suppress_statement = $line_nr_next;
3615 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003616
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003617 # Find the real next line.
3618 $realline_next = $line_nr_next;
3619 if (defined $realline_next &&
3620 (!defined $lines[$realline_next - 1] ||
3621 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3622 $realline_next++;
3623 }
3624
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003625 my $s = $stat;
3626 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003627
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003628 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003629 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003630
3631 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003632 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003633
Andy Whitcroft463f2862009-09-21 17:04:34 -07003634 } elsif ($s =~ /^.\s*else\b/s) {
3635
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003636 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003637 } 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 -07003638 my $type = $1;
3639 $type =~ s/\s+/ /g;
3640 possible($type, "A:" . $s);
3641
Andy Whitcroft8905a672007-11-28 16:21:06 -08003642 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003643 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003644 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003645 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003646
3647 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003648 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003649 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003650 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003651
3652 # Check for any sort of function declaration.
3653 # int foo(something bar, other baz);
3654 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003655 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 -08003656 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003657
Andy Whitcroftcf655042008-03-04 14:28:20 -08003658 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003659 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003660 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003661
Andy Whitcroft8905a672007-11-28 16:21:06 -08003662 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003663 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003664
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003665 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003666 }
3667 }
3668 }
3669
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003670 }
3671
Andy Whitcroft653d4872007-06-23 17:16:34 -07003672#
3673# Checks which may be anchored in the context.
3674#
3675
3676# Check for switch () and associated case and default
3677# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003678 if ($line=~/\bswitch\s*\(.*\)/) {
3679 my $err = '';
3680 my $sep = '';
3681 my @ctx = ctx_block_outer($linenr, $realcnt);
3682 shift(@ctx);
3683 for my $ctx (@ctx) {
3684 my ($clen, $cindent) = line_stats($ctx);
3685 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3686 $indent != $cindent) {
3687 $err .= "$sep$ctx\n";
3688 $sep = '';
3689 } else {
3690 $sep = "[...]\n";
3691 }
3692 }
3693 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003694 ERROR("SWITCH_CASE_INDENT_LEVEL",
3695 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003696 }
3697 }
3698
3699# if/while/etc brace do not go on next line, unless defining a do while loop,
3700# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003701 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 -07003702 my $pre_ctx = "$1$2";
3703
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003704 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003705
3706 if ($line =~ /^\+\t{6,}/) {
3707 WARN("DEEP_INDENTATION",
3708 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3709 }
3710
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003711 my $ctx_cnt = $realcnt - $#ctx - 1;
3712 my $ctx = join("\n", @ctx);
3713
Andy Whitcroft548596d2008-07-23 21:29:01 -07003714 my $ctx_ln = $linenr;
3715 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003716
Andy Whitcroft548596d2008-07-23 21:29:01 -07003717 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3718 defined $lines[$ctx_ln - 1] &&
3719 $lines[$ctx_ln - 1] =~ /^-/)) {
3720 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3721 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003722 $ctx_ln++;
3723 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003724
Andy Whitcroft53210162008-07-23 21:29:03 -07003725 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3726 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003727
Joe Perchesd752fcc2014-08-06 16:11:05 -07003728 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003729 ERROR("OPEN_BRACE",
3730 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003731 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003732 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003733 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3734 $ctx =~ /\)\s*\;\s*$/ &&
3735 defined $lines[$ctx_ln - 1])
3736 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003737 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3738 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003739 WARN("TRAILING_SEMICOLON",
3740 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003741 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003742 }
3743 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003744 }
3745
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003746# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003747 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 -08003748 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3749 ctx_statement_block($linenr, $realcnt, 0)
3750 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003751 my ($s, $c) = ($stat, $cond);
3752
3753 substr($s, 0, length($c), '');
3754
Joe Perches9f5af482015-09-09 15:37:30 -07003755 # remove inline comments
3756 $s =~ s/$;/ /g;
3757 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003758
3759 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003760 my @newlines = ($c =~ /\n/gs);
3761 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003762
Joe Perches9f5af482015-09-09 15:37:30 -07003763 # Make sure we remove the line prefixes as we have
3764 # none on the first line, and are going to readd them
3765 # where necessary.
3766 $s =~ s/\n./\n/gs;
3767 while ($s =~ /\n\s+\\\n/) {
3768 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3769 }
3770
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003771 # We want to check the first line inside the block
3772 # starting at the end of the conditional, so remove:
3773 # 1) any blank line termination
3774 # 2) any opening brace { on end of the line
3775 # 3) any do (...) {
3776 my $continuation = 0;
3777 my $check = 0;
3778 $s =~ s/^.*\bdo\b//;
3779 $s =~ s/^\s*{//;
3780 if ($s =~ s/^\s*\\//) {
3781 $continuation = 1;
3782 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003783 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003784 $check = 1;
3785 $cond_lines++;
3786 }
3787
3788 # Also ignore a loop construct at the end of a
3789 # preprocessor statement.
3790 if (($prevline =~ /^.\s*#\s*define\s/ ||
3791 $prevline =~ /\\\s*$/) && $continuation == 0) {
3792 $check = 0;
3793 }
3794
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003795 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003796 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003797 while ($cond_ptr != $cond_lines) {
3798 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003799
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003800 # If we see an #else/#elif then the code
3801 # is not linear.
3802 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3803 $check = 0;
3804 }
3805
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003806 # Ignore:
3807 # 1) blank lines, they should be at 0,
3808 # 2) preprocessor lines, and
3809 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003810 if ($continuation ||
3811 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003812 $s =~ /^\s*#\s*?/ ||
3813 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003814 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003815 if ($s =~ s/^.*?\n//) {
3816 $cond_lines++;
3817 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003818 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003819 }
3820
3821 my (undef, $sindent) = line_stats("+" . $s);
3822 my $stat_real = raw_line($linenr, $cond_lines);
3823
3824 # Check if either of these lines are modified, else
3825 # this is not this patch's fault.
3826 if (!defined($stat_real) ||
3827 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3828 $check = 0;
3829 }
3830 if (defined($stat_real) && $cond_lines > 1) {
3831 $stat_real = "[...]\n$stat_real";
3832 }
3833
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003834 #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 -07003835
Joe Perches9f5af482015-09-09 15:37:30 -07003836 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07003837 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07003838 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003839 ($sindent == $indent &&
3840 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07003841 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003842 WARN("SUSPECT_CODE_INDENT",
3843 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003844 }
3845 }
3846
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003847 # Track the 'values' across context and added lines.
3848 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003849 my ($curr_values, $curr_vars) =
3850 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003851 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003852 if ($dbg_values) {
3853 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003854 print "$linenr > .$outline\n";
3855 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003856 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003857 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003858 $prev_values = substr($curr_values, -1);
3859
Andy Whitcroft00df3442007-06-08 13:47:06 -07003860#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003861 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003862
Joe Perches11ca40a2016-12-12 16:46:31 -08003863# check for dereferences that span multiple lines
3864 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3865 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3866 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3867 my $ref = $1;
3868 $line =~ /^.\s*($Lval)/;
3869 $ref .= $1;
3870 $ref =~ s/\s//g;
3871 WARN("MULTILINE_DEREFERENCE",
3872 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3873 }
3874
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003875# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003876 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 -07003877 my $type = $1;
3878 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003879 $var = "" if (!defined $var);
3880 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003881 my $sign = $1;
3882 my $pointer = $2;
3883
3884 $pointer = "" if (!defined $pointer);
3885
3886 if (WARN("UNSPECIFIED_INT",
3887 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3888 $fix) {
3889 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003890 my $comp_pointer = $pointer;
3891 $comp_pointer =~ s/\s//g;
3892 $decl .= $comp_pointer;
3893 $decl = rtrim($decl) if ($var eq "");
3894 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003895 }
3896 }
3897 }
3898
Andy Whitcroft653d4872007-06-23 17:16:34 -07003899# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003900 if ($dbg_type) {
3901 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003902 ERROR("TEST_TYPE",
3903 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003904 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003905 ERROR("TEST_NOT_TYPE",
3906 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003907 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003908 next;
3909 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003910# TEST: allow direct testing of the attribute matcher.
3911 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003912 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003913 ERROR("TEST_ATTR",
3914 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003915 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003916 ERROR("TEST_NOT_ATTR",
3917 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003918 }
3919 next;
3920 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003921
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003922# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003923 if ($line =~ /^.\s*{/ &&
3924 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003925 if (ERROR("OPEN_BRACE",
3926 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003927 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3928 fix_delete_line($fixlinenr - 1, $prevrawline);
3929 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003930 my $fixedline = $prevrawline;
3931 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003932 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003933 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003934 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003935 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003936 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003937 }
3938
Andy Whitcroft653d4872007-06-23 17:16:34 -07003939#
3940# Checks which are anchored on the added line.
3941#
3942
3943# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003944 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003945 my $path = $1;
3946 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003947 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003948 "malformed #include filename\n" . $herecurr);
3949 }
3950 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3951 ERROR("UAPI_INCLUDE",
3952 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003953 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003954 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003955
3956# no C99 // comments
3957 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003958 if (ERROR("C99_COMMENTS",
3959 "do not use C99 // comments\n" . $herecurr) &&
3960 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003961 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003962 if ($line =~ /\/\/(.*)$/) {
3963 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003964 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003965 }
3966 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003967 }
3968 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003969 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003970 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003971
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003972# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3973# the whole statement.
3974#print "APW <$lines[$realline_next - 1]>\n";
3975 if (defined $realline_next &&
3976 exists $lines[$realline_next - 1] &&
3977 !defined $suppress_export{$realline_next} &&
3978 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3979 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003980 # Handle definitions which produce identifiers with
3981 # a prefix:
3982 # XXX(foo);
3983 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003984 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003985 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003986 $name =~ /^${Ident}_$2/) {
3987#print "FOO C name<$name>\n";
3988 $suppress_export{$realline_next} = 1;
3989
3990 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003991 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003992 ^.DEFINE_$Ident\(\Q$name\E\)|
3993 ^.DECLARE_$Ident\(\Q$name\E\)|
3994 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003995 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3996 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003997 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003998#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3999 $suppress_export{$realline_next} = 2;
4000 } else {
4001 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004002 }
4003 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004004 if (!defined $suppress_export{$linenr} &&
4005 $prevline =~ /^.\s*$/ &&
4006 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4007 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4008#print "FOO B <$lines[$linenr - 1]>\n";
4009 $suppress_export{$linenr} = 2;
4010 }
4011 if (defined $suppress_export{$linenr} &&
4012 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004013 WARN("EXPORT_SYMBOL",
4014 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004015 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004016
Joe Eloff5150bda2010-08-09 17:21:00 -07004017# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004018 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004019 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004020 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004021 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004022 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004023 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004024 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004025# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004026 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004027 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004028 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004029 $herecurr) &&
4030 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004031 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004032 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004033 }
4034
Joe Perches18130872014-08-06 16:11:22 -07004035# check for misordered declarations of char/short/int/long with signed/unsigned
4036 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4037 my $tmp = trim($1);
4038 WARN("MISORDERED_TYPE",
4039 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4040 }
4041
Joe Perches809e0822018-08-21 21:58:12 -07004042# check for unnecessary <signed> int declarations of short/long/long long
4043 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4044 my $type = trim($1);
4045 next if ($type !~ /\bint\b/);
4046 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4047 my $new_type = $type;
4048 $new_type =~ s/\b\s*int\s*\b/ /;
4049 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4050 $new_type =~ s/^const\s+//;
4051 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4052 $new_type = "const $new_type" if ($type =~ /^const\b/);
4053 $new_type =~ s/\s+/ /g;
4054 $new_type = trim($new_type);
4055 if (WARN("UNNECESSARY_INT",
4056 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4057 $fix) {
4058 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4059 }
4060 }
4061
Joe Perchescb710ec2010-10-26 14:23:20 -07004062# check for static const char * arrays.
4063 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004064 WARN("STATIC_CONST_CHAR_ARRAY",
4065 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004066 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004067 }
4068
4069# check for initialized const char arrays that should be static const
4070 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4071 if (WARN("STATIC_CONST_CHAR_ARRAY",
4072 "const array should probably be static const\n" . $herecurr) &&
4073 $fix) {
4074 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4075 }
4076 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004077
4078# check for static char foo[] = "bar" declarations.
4079 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004080 WARN("STATIC_CONST_CHAR_ARRAY",
4081 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004082 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004083 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004084
Joe Perchesab7e23f2015-04-16 12:44:22 -07004085# check for const <foo> const where <foo> is not a pointer or array type
4086 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4087 my $found = $1;
4088 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4089 WARN("CONST_CONST",
4090 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4091 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4092 WARN("CONST_CONST",
4093 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4094 }
4095 }
4096
Joe Perches9b0fa602014-04-03 14:49:18 -07004097# check for non-global char *foo[] = {"bar", ...} declarations.
4098 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4099 WARN("STATIC_CONST_CHAR_ARRAY",
4100 "char * array declaration might be better as static const\n" .
4101 $herecurr);
4102 }
4103
Joe Perchesb598b672015-04-16 12:44:36 -07004104# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4105 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4106 my $array = $1;
4107 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4108 my $array_div = $1;
4109 if (WARN("ARRAY_SIZE",
4110 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4111 $fix) {
4112 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4113 }
4114 }
4115 }
4116
Joe Perchesb36190c2014-01-27 17:07:18 -08004117# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004118 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004119 if (ERROR("FUNCTION_WITHOUT_ARGS",
4120 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4121 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004122 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004123 }
4124 }
4125
Andy Whitcroft653d4872007-06-23 17:16:34 -07004126# check for new typedefs, only function parameters and sparse annotations
4127# make sense.
4128 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004129 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004130 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004131 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004132 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004133 WARN("NEW_TYPEDEFS",
4134 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004135 }
4136
4137# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004138 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004139 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4140 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004141 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004142
Andy Whitcroft65863862009-01-06 14:41:21 -08004143 # Should start with a space.
4144 $to =~ s/^(\S)/ $1/;
4145 # Should not end with a space.
4146 $to =~ s/\s+$//;
4147 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004148 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004149 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004150
Joe Perches3705ce52013-07-03 15:05:31 -07004151## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004152 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004153 if (ERROR("POINTER_LOCATION",
4154 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4155 $fix) {
4156 my $sub_from = $ident;
4157 my $sub_to = $ident;
4158 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004159 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004160 s@\Q$sub_from\E@$sub_to@;
4161 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004162 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004163 }
4164 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4165 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004166 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004167
Andy Whitcroft65863862009-01-06 14:41:21 -08004168 # Should start with a space.
4169 $to =~ s/^(\S)/ $1/;
4170 # Should not end with a space.
4171 $to =~ s/\s+$//;
4172 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004173 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004174 }
4175 # Modifiers should have spaces.
4176 $to =~ s/(\b$Modifier$)/$1 /;
4177
Joe Perches3705ce52013-07-03 15:05:31 -07004178## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004179 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004180 if (ERROR("POINTER_LOCATION",
4181 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4182 $fix) {
4183
4184 my $sub_from = $match;
4185 my $sub_to = $match;
4186 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004187 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004188 s@\Q$sub_from\E@$sub_to@;
4189 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004190 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004191 }
4192
Joe Perches9d3e3c72015-09-09 15:37:27 -07004193# avoid BUG() or BUG_ON()
4194 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004195 my $msg_level = \&WARN;
4196 $msg_level = \&CHK if ($file);
4197 &{$msg_level}("AVOID_BUG",
4198 "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 -07004199 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004200
Joe Perches9d3e3c72015-09-09 15:37:27 -07004201# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004202 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004203 WARN("LINUX_VERSION_CODE",
4204 "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 -08004205 }
4206
Joe Perches17441222011-06-15 15:08:17 -07004207# check for uses of printk_ratelimit
4208 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004209 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004210 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004211 }
4212
Joe Percheseeef5732017-11-17 15:28:41 -08004213# printk should use KERN_* levels
4214 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4215 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4216 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004217 }
4218
Joe Perches243f3802012-05-31 16:26:09 -07004219 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4220 my $orig = $1;
4221 my $level = lc($orig);
4222 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004223 my $level2 = $level;
4224 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004225 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004226 "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 -07004227 }
4228
Joe Perchesdc139312013-02-21 16:44:13 -08004229 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4230 my $orig = $1;
4231 my $level = lc($orig);
4232 $level = "warn" if ($level eq "warning");
4233 $level = "dbg" if ($level eq "debug");
4234 WARN("PREFER_DEV_LEVEL",
4235 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4236 }
4237
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004238# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4239# number of false positives, but assembly files are not checked, so at
4240# least the arch entry code will not trigger this warning.
4241 if ($line =~ /\bENOSYS\b/) {
4242 WARN("ENOSYS",
4243 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4244 }
4245
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004246# ENOTSUPP is not a standard error code and should be avoided in new patches.
4247# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4248# Similarly to ENOSYS warning a small number of false positives is expected.
4249 if (!$file && $line =~ /\bENOTSUPP\b/) {
4250 if (WARN("ENOTSUPP",
4251 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4252 $fix) {
4253 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4254 }
4255 }
4256
Andy Whitcroft653d4872007-06-23 17:16:34 -07004257# function brace can't be on same line, except for #defines of do while,
4258# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004259 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004260 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4261 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4262 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004263 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004264 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004265 $fix) {
4266 fix_delete_line($fixlinenr, $rawline);
4267 my $fixed_line = $rawline;
4268 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4269 my $line1 = $1;
4270 my $line2 = $2;
4271 fix_insert_line($fixlinenr, ltrim($line1));
4272 fix_insert_line($fixlinenr, "\+{");
4273 if ($line2 !~ /^\s*$/) {
4274 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4275 }
4276 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004277 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004278
Andy Whitcroft8905a672007-11-28 16:21:06 -08004279# open braces for enum, union and struct go on the same line.
4280 if ($line =~ /^.\s*{/ &&
4281 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004282 if (ERROR("OPEN_BRACE",
4283 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4284 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4285 fix_delete_line($fixlinenr - 1, $prevrawline);
4286 fix_delete_line($fixlinenr, $rawline);
4287 my $fixedline = rtrim($prevrawline) . " {";
4288 fix_insert_line($fixlinenr, $fixedline);
4289 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004290 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004291 if ($fixedline !~ /^\+\s*$/) {
4292 fix_insert_line($fixlinenr, $fixedline);
4293 }
4294 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004295 }
4296
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004297# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004298 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4299 if (WARN("SPACING",
4300 "missing space after $1 definition\n" . $herecurr) &&
4301 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004302 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004303 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4304 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004305 }
4306
Joe Perches31070b52014-01-23 15:54:49 -08004307# Function pointer declarations
4308# check spacing between type, funcptr, and args
4309# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004310 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004311 my $declare = $1;
4312 my $pre_pointer_space = $2;
4313 my $post_pointer_space = $3;
4314 my $funcname = $4;
4315 my $post_funcname_space = $5;
4316 my $pre_args_space = $6;
4317
Joe Perches91f72e92014-04-03 14:49:12 -07004318# the $Declare variable will capture all spaces after the type
4319# so check it for a missing trailing missing space but pointer return types
4320# don't need a space so don't warn for those.
4321 my $post_declare_space = "";
4322 if ($declare =~ /(\s+)$/) {
4323 $post_declare_space = $1;
4324 $declare = rtrim($declare);
4325 }
4326 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004327 WARN("SPACING",
4328 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004329 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004330 }
4331
4332# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004333# This test is not currently implemented because these declarations are
4334# equivalent to
4335# int foo(int bar, ...)
4336# and this is form shouldn't/doesn't generate a checkpatch warning.
4337#
4338# elsif ($declare =~ /\s{2,}$/) {
4339# WARN("SPACING",
4340# "Multiple spaces after return type\n" . $herecurr);
4341# }
Joe Perches31070b52014-01-23 15:54:49 -08004342
4343# unnecessary space "type ( *funcptr)(args...)"
4344 if (defined $pre_pointer_space &&
4345 $pre_pointer_space =~ /^\s/) {
4346 WARN("SPACING",
4347 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4348 }
4349
4350# unnecessary space "type (* funcptr)(args...)"
4351 if (defined $post_pointer_space &&
4352 $post_pointer_space =~ /^\s/) {
4353 WARN("SPACING",
4354 "Unnecessary space before function pointer name\n" . $herecurr);
4355 }
4356
4357# unnecessary space "type (*funcptr )(args...)"
4358 if (defined $post_funcname_space &&
4359 $post_funcname_space =~ /^\s/) {
4360 WARN("SPACING",
4361 "Unnecessary space after function pointer name\n" . $herecurr);
4362 }
4363
4364# unnecessary space "type (*funcptr) (args...)"
4365 if (defined $pre_args_space &&
4366 $pre_args_space =~ /^\s/) {
4367 WARN("SPACING",
4368 "Unnecessary space before function pointer arguments\n" . $herecurr);
4369 }
4370
4371 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004372 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004373 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004374 }
4375 }
4376
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004377# check for spacing round square brackets; allowed:
4378# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004379# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4380# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004381 while ($line =~ /(.*?\s)\[/g) {
4382 my ($where, $prefix) = ($-[1], $1);
4383 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004384 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004385 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004386 if (ERROR("BRACKET_SPACE",
4387 "space prohibited before open square bracket '['\n" . $herecurr) &&
4388 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004389 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004390 s/^(\+.*?)\s+\[/$1\[/;
4391 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004392 }
4393 }
4394
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004395# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004396 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004397 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004398 my $ctx_before = substr($line, 0, $-[1]);
4399 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004400
4401 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004402 if ($name =~ /^(?:
4403 if|for|while|switch|return|case|
4404 volatile|__volatile__|
4405 __attribute__|format|__extension__|
4406 asm|__asm__)$/x)
4407 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004408 # cpp #define statements have non-optional spaces, ie
4409 # if there is a space between the name and the open
4410 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004411 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004412
4413 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004414 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004415
4416 # If this whole things ends with a type its most
4417 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004418 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004419
4420 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004421 if (WARN("SPACING",
4422 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4423 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004424 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004425 s/\b$name\s+\(/$name\(/;
4426 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004427 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004428 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004429
Andy Whitcroft653d4872007-06-23 17:16:34 -07004430# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004431 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004432 my $fixed_line = "";
4433 my $line_fixed = 0;
4434
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004435 my $ops = qr{
4436 <<=|>>=|<=|>=|==|!=|
4437 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4438 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004439 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004440 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004441 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004442 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004443
4444## print("element count: <" . $#elements . ">\n");
4445## foreach my $el (@elements) {
4446## print("el: <$el>\n");
4447## }
4448
4449 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004450 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004451
Joe Perches3705ce52013-07-03 15:05:31 -07004452 foreach my $el (@elements) {
4453 push(@fix_elements, substr($rawline, $off, length($el)));
4454 $off += length($el);
4455 }
4456
4457 $off = 0;
4458
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004459 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004460 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004461
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004462 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004463
4464 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4465
4466## print("n: <$n> good: <$good>\n");
4467
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004468 $off += length($elements[$n]);
4469
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004470 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004471 my $ca = substr($opline, 0, $off);
4472 my $cc = '';
4473 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4474 $cc = substr($opline, $off + length($elements[$n + 1]));
4475 }
4476 my $cb = "$ca$;$cc";
4477
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004478 my $a = '';
4479 $a = 'V' if ($elements[$n] ne '');
4480 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004481 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004482 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4483 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004484 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004485
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004486 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004487
4488 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004489 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004490 $c = 'V' if ($elements[$n + 2] ne '');
4491 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004492 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004493 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4494 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004495 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004496 } else {
4497 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004498 }
4499
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004500 my $ctx = "${a}x${c}";
4501
4502 my $at = "(ctx:$ctx)";
4503
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004504 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004505 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004506
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004507 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004508 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004509
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004510 # Get the full operator variant.
4511 my $opv = $op . substr($curr_vars, $off, 1);
4512
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004513 # Ignore operators passed as parameters.
4514 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004515 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004516
Andy Whitcroftcf655042008-03-04 14:28:20 -08004517# # Ignore comments
4518# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004519
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004520 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004521 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004522 if ($ctx !~ /.x[WEBC]/ &&
4523 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004524 if (ERROR("SPACING",
4525 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004526 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004527 $line_fixed = 1;
4528 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004529 }
4530
4531 # // is a comment
4532 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004533
Joe Perchesb00e4812014-04-03 14:49:33 -07004534 # : when part of a bitfield
4535 } elsif ($opv eq ':B') {
4536 # skip the bitfield test for now
4537
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004538 # No spaces for:
4539 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004540 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004541 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004542 if (ERROR("SPACING",
4543 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004544 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004545 if (defined $fix_elements[$n + 2]) {
4546 $fix_elements[$n + 2] =~ s/^\s+//;
4547 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004548 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004549 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004550 }
4551
Joe Perches23810972014-12-10 15:51:32 -08004552 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004553 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004554 my $rtrim_before = 0;
4555 my $space_after = 0;
4556 if ($ctx =~ /Wx./) {
4557 if (ERROR("SPACING",
4558 "space prohibited before that '$op' $at\n" . $hereptr)) {
4559 $line_fixed = 1;
4560 $rtrim_before = 1;
4561 }
4562 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004563 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004564 if (ERROR("SPACING",
4565 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004566 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004567 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004568 $space_after = 1;
4569 }
4570 }
4571 if ($rtrim_before || $space_after) {
4572 if ($rtrim_before) {
4573 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4574 } else {
4575 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4576 }
4577 if ($space_after) {
4578 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004579 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004580 }
4581
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004582 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004583 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004584 #warn "'*' is part of type\n";
4585
4586 # unary operators should have a space before and
4587 # none after. May be left adjacent to another
4588 # unary operator, or a cast
4589 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004590 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004591 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004592 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004593 if (ERROR("SPACING",
4594 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004595 if ($n != $last_after + 2) {
4596 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4597 $line_fixed = 1;
4598 }
Joe Perches3705ce52013-07-03 15:05:31 -07004599 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004600 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004601 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004602 # A unary '*' may be const
4603
4604 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004605 if (ERROR("SPACING",
4606 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004607 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004608 if (defined $fix_elements[$n + 2]) {
4609 $fix_elements[$n + 2] =~ s/^\s+//;
4610 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004611 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004612 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004613 }
4614
4615 # unary ++ and unary -- are allowed no space on one side.
4616 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004617 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004618 if (ERROR("SPACING",
4619 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004620 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004621 $line_fixed = 1;
4622 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004623 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004624 if ($ctx =~ /Wx[BE]/ ||
4625 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004626 if (ERROR("SPACING",
4627 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004628 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004629 $line_fixed = 1;
4630 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004631 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004632 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004633 if (ERROR("SPACING",
4634 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004635 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004636 if (defined $fix_elements[$n + 2]) {
4637 $fix_elements[$n + 2] =~ s/^\s+//;
4638 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004639 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004640 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004641 }
4642
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004643 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004644 } elsif ($op eq '<<' or $op eq '>>' or
4645 $op eq '&' or $op eq '^' or $op eq '|' or
4646 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004647 $op eq '*' or $op eq '/' or
4648 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004649 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004650 if ($check) {
4651 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4652 if (CHK("SPACING",
4653 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4654 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4655 $fix_elements[$n + 2] =~ s/^\s+//;
4656 $line_fixed = 1;
4657 }
4658 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4659 if (CHK("SPACING",
4660 "space preferred before that '$op' $at\n" . $hereptr)) {
4661 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4662 $line_fixed = 1;
4663 }
4664 }
4665 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004666 if (ERROR("SPACING",
4667 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004668 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4669 if (defined $fix_elements[$n + 2]) {
4670 $fix_elements[$n + 2] =~ s/^\s+//;
4671 }
Joe Perches3705ce52013-07-03 15:05:31 -07004672 $line_fixed = 1;
4673 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004674 }
4675
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004676 # A colon needs no spaces before when it is
4677 # terminating a case value or a label.
4678 } elsif ($opv eq ':C' || $opv eq ':L') {
4679 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004680 if (ERROR("SPACING",
4681 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004682 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004683 $line_fixed = 1;
4684 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004685 }
4686
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004687 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004688 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004689 my $ok = 0;
4690
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004691 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004692 if (($op eq '<' &&
4693 $cc =~ /^\S+\@\S+>/) ||
4694 ($op eq '>' &&
4695 $ca =~ /<\S+\@\S+$/))
4696 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07004697 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004698 }
4699
Joe Perchese0df7e12015-04-16 12:44:53 -07004700 # for asm volatile statements
4701 # ignore a colon with another
4702 # colon immediately before or after
4703 if (($op eq ':') &&
4704 ($ca =~ /:$/ || $cc =~ /^:/)) {
4705 $ok = 1;
4706 }
4707
Joe Perches84731622013-11-12 15:10:05 -08004708 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004709 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004710 my $msg_level = \&ERROR;
4711 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004712
Jean Delvare0675a8f2017-09-08 16:16:07 -07004713 if (&{$msg_level}("SPACING",
4714 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004715 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4716 if (defined $fix_elements[$n + 2]) {
4717 $fix_elements[$n + 2] =~ s/^\s+//;
4718 }
Joe Perches3705ce52013-07-03 15:05:31 -07004719 $line_fixed = 1;
4720 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004721 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004722 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004723 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004724
4725## print("n: <$n> GOOD: <$good>\n");
4726
4727 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004728 }
Joe Perches3705ce52013-07-03 15:05:31 -07004729
4730 if (($#elements % 2) == 0) {
4731 $fixed_line = $fixed_line . $fix_elements[$#elements];
4732 }
4733
Joe Perches194f66f2014-08-06 16:11:03 -07004734 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4735 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004736 }
4737
4738
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004739 }
4740
Joe Perches786b6322013-07-03 15:05:32 -07004741# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004742 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004743 if (WARN("SPACING",
4744 "space prohibited before semicolon\n" . $herecurr) &&
4745 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004746 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004747 s/^(\+.*\S)\s+;/$1;/;
4748 }
4749 }
4750
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004751# check for multiple assignments
4752 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004753 CHK("MULTIPLE_ASSIGNMENTS",
4754 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004755 }
4756
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004757## # check for multiple declarations, allowing for a function declaration
4758## # continuation.
4759## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4760## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4761##
4762## # Remove any bracketed sections to ensure we do not
4763## # falsly report the parameters of functions.
4764## my $ln = $line;
4765## while ($ln =~ s/\([^\(\)]*\)//g) {
4766## }
4767## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004768## WARN("MULTIPLE_DECLARATION",
4769## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004770## }
4771## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004772
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004773#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004774 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004775 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004776 if (ERROR("SPACING",
4777 "space required before the open brace '{'\n" . $herecurr) &&
4778 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004779 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004780 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004781 }
4782
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004783## # check for blank lines before declarations
4784## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4785## $prevrawline =~ /^.\s*$/) {
4786## WARN("SPACING",
4787## "No blank lines before declarations\n" . $hereprev);
4788## }
4789##
4790
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004791# closing brace should have a space following it when it has anything
4792# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07004793 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004794 if (ERROR("SPACING",
4795 "space required after that close brace '}'\n" . $herecurr) &&
4796 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004797 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004798 s/}((?!(?:,|;|\)))\S)/} $1/;
4799 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004800 }
4801
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004802# check spacing on square brackets
4803 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004804 if (ERROR("SPACING",
4805 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4806 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004807 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004808 s/\[\s+/\[/;
4809 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004810 }
4811 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004812 if (ERROR("SPACING",
4813 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4814 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004815 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004816 s/\s+\]/\]/;
4817 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004818 }
4819
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004820# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004821 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4822 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004823 if (ERROR("SPACING",
4824 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4825 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004826 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004827 s/\(\s+/\(/;
4828 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004829 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004830 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004831 $line !~ /for\s*\(.*;\s+\)/ &&
4832 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004833 if (ERROR("SPACING",
4834 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4835 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004836 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004837 s/\s+\)/\)/;
4838 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004839 }
4840
Joe Perchese2826fd2014-08-06 16:10:48 -07004841# check unnecessary parentheses around addressof/dereference single $Lvals
4842# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4843
4844 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004845 my $var = $1;
4846 if (CHK("UNNECESSARY_PARENTHESES",
4847 "Unnecessary parentheses around $var\n" . $herecurr) &&
4848 $fix) {
4849 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4850 }
4851 }
4852
4853# check for unnecessary parentheses around function pointer uses
4854# ie: (foo->bar)(); should be foo->bar();
4855# but not "if (foo->bar) (" to avoid some false positives
4856 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4857 my $var = $2;
4858 if (CHK("UNNECESSARY_PARENTHESES",
4859 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4860 $fix) {
4861 my $var2 = deparenthesize($var);
4862 $var2 =~ s/\s//g;
4863 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4864 }
4865 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004866
Joe Perches63b7c732017-09-08 16:16:01 -07004867# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004868# when !drivers/staging or command-line uses --strict
4869 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004870 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004871 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4872 my $if_stat = $1;
4873 my $test = substr($2, 1, -1);
4874 my $herectx;
4875 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4876 my $match = $1;
4877 # avoid parentheses around potential macro args
4878 next if ($match =~ /^\s*\w+\s*$/);
4879 if (!defined($herectx)) {
4880 $herectx = $here . "\n";
4881 my $cnt = statement_rawlines($if_stat);
4882 for (my $n = 0; $n < $cnt; $n++) {
4883 my $rl = raw_line($linenr, $n);
4884 $herectx .= $rl . "\n";
4885 last if $rl =~ /^[ \+].*\{/;
4886 }
4887 }
4888 CHK("UNNECESSARY_PARENTHESES",
4889 "Unnecessary parentheses around '$match'\n" . $herectx);
4890 }
4891 }
4892
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004893#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004894 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004895 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004896 if (WARN("INDENTED_LABEL",
4897 "labels should not be indented\n" . $herecurr) &&
4898 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004899 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004900 s/^(.)\s+/$1/;
4901 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004902 }
4903
Joe Perches5b9553a2014-04-03 14:49:21 -07004904# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004905 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004906 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004907 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004908 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4909 my $value = $1;
4910 $value = deparenthesize($value);
4911 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4912 ERROR("RETURN_PARENTHESES",
4913 "return is not a function, parentheses are not required\n" . $herecurr);
4914 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004915 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004916 ERROR("SPACING",
4917 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004918 }
4919 }
Joe Perches507e5142013-11-12 15:10:13 -08004920
Joe Perchesb43ae212014-06-23 13:22:07 -07004921# unnecessary return in a void function
4922# at end-of-function, with the previous line a single leading tab, then return;
4923# and the line before that not a goto label target like "out:"
4924 if ($sline =~ /^[ \+]}\s*$/ &&
4925 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4926 $linenr >= 3 &&
4927 $lines[$linenr - 3] =~ /^[ +]/ &&
4928 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004929 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004930 "void function return statements are not generally useful\n" . $hereprev);
4931 }
Joe Perches9819cf22014-06-04 16:12:09 -07004932
Joe Perches189248d2014-01-23 15:54:47 -08004933# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004934 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004935 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4936 my $openparens = $1;
4937 my $count = $openparens =~ tr@\(@\(@;
4938 my $msg = "";
4939 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4940 my $comp = $4; #Not $1 because of $LvalOrFunc
4941 $msg = " - maybe == should be = ?" if ($comp eq "==");
4942 WARN("UNNECESSARY_PARENTHESES",
4943 "Unnecessary parentheses$msg\n" . $herecurr);
4944 }
4945 }
4946
Joe Perchesc5595fa2015-09-09 15:37:58 -07004947# comparisons with a constant or upper case identifier on the left
4948# avoid cases like "foo + BAR < baz"
4949# only fix matches surrounded by parentheses to avoid incorrect
4950# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004951 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004952 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4953 my $lead = $1;
4954 my $const = $2;
4955 my $comp = $3;
4956 my $to = $4;
4957 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004958 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004959 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4960 WARN("CONSTANT_COMPARISON",
4961 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4962 $fix) {
4963 if ($comp eq "<") {
4964 $newcomp = ">";
4965 } elsif ($comp eq "<=") {
4966 $newcomp = ">=";
4967 } elsif ($comp eq ">") {
4968 $newcomp = "<";
4969 } elsif ($comp eq ">=") {
4970 $newcomp = "<=";
4971 }
4972 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4973 }
4974 }
4975
Joe Perchesf34e4a42015-04-16 12:44:19 -07004976# Return of what appears to be an errno should normally be negative
4977 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004978 my $name = $1;
4979 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004980 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004981 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004982 }
4983 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004984
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004985# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004986 if ($line =~ /\b(if|while|for|switch)\(/) {
4987 if (ERROR("SPACING",
4988 "space required before the open parenthesis '('\n" . $herecurr) &&
4989 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004990 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004991 s/\b(if|while|for|switch)\(/$1 \(/;
4992 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004993 }
4994
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004995# Check for illegal assignment in if conditional -- and check for trailing
4996# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004997 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004998 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4999 ctx_statement_block($linenr, $realcnt, 0)
5000 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005001 my ($stat_next) = ctx_statement_block($line_nr_next,
5002 $remain_next, $off_next);
5003 $stat_next =~ s/\n./\n /g;
5004 ##print "stat<$stat> stat_next<$stat_next>\n";
5005
5006 if ($stat_next =~ /^\s*while\b/) {
5007 # If the statement carries leading newlines,
5008 # then count those as offsets.
5009 my ($whitespace) =
5010 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5011 my $offset =
5012 statement_rawlines($whitespace) - 1;
5013
5014 $suppress_whiletrailers{$line_nr_next +
5015 $offset} = 1;
5016 }
5017 }
5018 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005019 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005020 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005021 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005022
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005023 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005024 ERROR("ASSIGN_IN_IF",
5025 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005026 }
5027
5028 # Find out what is on the end of the line after the
5029 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005030 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005031 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005032 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005033 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5034 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005035 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005036 # Find out how long the conditional actually is.
5037 my @newlines = ($c =~ /\n/gs);
5038 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005039 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005040
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005041 $stat_real = raw_line($linenr, $cond_lines)
5042 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005043 if (defined($stat_real) && $cond_lines > 1) {
5044 $stat_real = "[...]\n$stat_real";
5045 }
5046
Joe Perches000d1cc12011-07-25 17:13:25 -07005047 ERROR("TRAILING_STATEMENTS",
5048 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005049 }
5050 }
5051
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005052# Check for bitwise tests written as boolean
5053 if ($line =~ /
5054 (?:
5055 (?:\[|\(|\&\&|\|\|)
5056 \s*0[xX][0-9]+\s*
5057 (?:\&\&|\|\|)
5058 |
5059 (?:\&\&|\|\|)
5060 \s*0[xX][0-9]+\s*
5061 (?:\&\&|\|\||\)|\])
5062 )/x)
5063 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005064 WARN("HEXADECIMAL_BOOLEAN_TEST",
5065 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005066 }
5067
Andy Whitcroft8905a672007-11-28 16:21:06 -08005068# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005069 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5070 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005071 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005072 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005073 ERROR("TRAILING_STATEMENTS",
5074 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005075 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005076 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005077# if should not continue a brace
5078 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005079 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005080 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005081 $herecurr);
5082 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005083# case and default should not have general statements after them
5084 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5085 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005086 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005087 \s*return\s+
5088 )/xg)
5089 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005090 ERROR("TRAILING_STATEMENTS",
5091 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005092 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005093
5094 # Check for }<nl>else {, these must be at the same
5095 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005096 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5097 $previndent == $indent) {
5098 if (ERROR("ELSE_AFTER_BRACE",
5099 "else should follow close brace '}'\n" . $hereprev) &&
5100 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5101 fix_delete_line($fixlinenr - 1, $prevrawline);
5102 fix_delete_line($fixlinenr, $rawline);
5103 my $fixedline = $prevrawline;
5104 $fixedline =~ s/}\s*$//;
5105 if ($fixedline !~ /^\+\s*$/) {
5106 fix_insert_line($fixlinenr, $fixedline);
5107 }
5108 $fixedline = $rawline;
5109 $fixedline =~ s/^(.\s*)else/$1} else/;
5110 fix_insert_line($fixlinenr, $fixedline);
5111 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005112 }
5113
Joe Perches8b8856f2014-08-06 16:11:14 -07005114 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5115 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005116 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5117
5118 # Find out what is on the end of the line after the
5119 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005120 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005121 $s =~ s/\n.*//g;
5122
5123 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005124 if (ERROR("WHILE_AFTER_BRACE",
5125 "while should follow close brace '}'\n" . $hereprev) &&
5126 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5127 fix_delete_line($fixlinenr - 1, $prevrawline);
5128 fix_delete_line($fixlinenr, $rawline);
5129 my $fixedline = $prevrawline;
5130 my $trailing = $rawline;
5131 $trailing =~ s/^\+//;
5132 $trailing = trim($trailing);
5133 $fixedline =~ s/}\s*$/} $trailing/;
5134 fix_insert_line($fixlinenr, $fixedline);
5135 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005136 }
5137 }
5138
Joe Perches95e2c602013-07-03 15:05:20 -07005139#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005140 while ($line =~ m{($Constant|$Lval)}g) {
5141 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005142
Joe Perches95e2c602013-07-03 15:05:20 -07005143#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005144 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005145 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005146#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005147 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005148#Ignore SI style variants like nS, mV and dB
5149#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5150 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005151#Ignore some three character SI units explicitly, like MiB and KHz
5152 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005153 while ($var =~ m{($Ident)}g) {
5154 my $word = $1;
5155 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005156 if ($check) {
5157 seed_camelcase_includes();
5158 if (!$file && !$camelcase_file_seeded) {
5159 seed_camelcase_file($realfile);
5160 $camelcase_file_seeded = 1;
5161 }
5162 }
Joe Perches7e781f62013-09-11 14:23:55 -07005163 if (!defined $camelcase{$word}) {
5164 $camelcase{$word} = 1;
5165 CHK("CAMELCASE",
5166 "Avoid CamelCase: <$word>\n" . $herecurr);
5167 }
Joe Perches34456862013-07-03 15:05:34 -07005168 }
Joe Perches323c1262012-12-17 16:02:07 -08005169 }
5170 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005171
5172#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005173 if ($line =~ /\#\s*define.*\\\s+$/) {
5174 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5175 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5176 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005177 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005178 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005179 }
5180
Fabian Frederick0e212e02015-04-16 12:44:25 -07005181# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5182# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005183 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005184 my $file = "$1.h";
5185 my $checkfile = "include/linux/$file";
5186 if (-f "$root/$checkfile" &&
5187 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005188 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005189 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005190 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5191 if ($asminclude > 0) {
5192 if ($realfile =~ m{^arch/}) {
5193 CHK("ARCH_INCLUDE_LINUX",
5194 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5195 } else {
5196 WARN("INCLUDE_LINUX",
5197 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5198 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005199 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005200 }
5201 }
5202
Andy Whitcroft653d4872007-06-23 17:16:34 -07005203# multi-statement macros should be enclosed in a do while loop, grab the
5204# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005205# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005206 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5207 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005208 my $ln = $linenr;
5209 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005210 my ($off, $dstat, $dcond, $rest);
5211 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005212 my $has_flow_statement = 0;
5213 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005214 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005215 ctx_statement_block($linenr, $realcnt, 0);
5216 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005217 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005218 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005219
Joe Perches08a28432014-10-13 15:51:55 -07005220 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005221 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005222
Joe Perchesf59b64b2016-10-11 13:52:08 -07005223 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5224 my $define_args = $1;
5225 my $define_stmt = $dstat;
5226 my @def_args = ();
5227
5228 if (defined $define_args && $define_args ne "") {
5229 $define_args = substr($define_args, 1, length($define_args) - 2);
5230 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005231 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005232 @def_args = split(",", $define_args);
5233 }
5234
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005235 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005236 $dstat =~ s/\\\n.//g;
5237 $dstat =~ s/^\s*//s;
5238 $dstat =~ s/\s*$//s;
5239
5240 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005241 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5242 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005243 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005244 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005245 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005246
Antonio Borneo342d3d22020-04-06 20:11:01 -07005247 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005248 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5249 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005250 {
5251 }
5252
Joe Perches42e15292016-03-15 14:58:01 -07005253 # Make asm volatile uses seem like a generic function
5254 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5255
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005256 my $exceptions = qr{
5257 $Declare|
5258 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005259 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005260 DECLARE_PER_CPU|
5261 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005262 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005263 union|
5264 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005265 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005266 ^\"|\"$|
5267 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005268 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005269 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005270
5271 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005272 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005273 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005274
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005275 if ($dstat ne '' &&
5276 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5277 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005278 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005279 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005280 $dstat !~ /$exceptions/ &&
5281 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005282 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005283 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005284 $dstat !~ /^for\s*$Constant$/ && # for (...)
5285 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5286 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005287 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005288 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005289 {
Joe Perchese7955562017-05-08 15:55:48 -07005290 if ($dstat =~ /^\s*if\b/) {
5291 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5292 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5293 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005294 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5295 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5296 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005297 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005298 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005299 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005300
5301 }
Joe Perches52076492016-10-11 13:52:14 -07005302
5303 # Make $define_stmt single line, comment-free, etc
5304 my @stmt_array = split('\n', $define_stmt);
5305 my $first = 1;
5306 $define_stmt = "";
5307 foreach my $l (@stmt_array) {
5308 $l =~ s/\\$//;
5309 if ($first) {
5310 $define_stmt = $l;
5311 $first = 0;
5312 } elsif ($l =~ /^[\+ ]/) {
5313 $define_stmt .= substr($l, 1);
5314 }
5315 }
5316 $define_stmt =~ s/$;//g;
5317 $define_stmt =~ s/\s+/ /g;
5318 $define_stmt = trim($define_stmt);
5319
Joe Perchesf59b64b2016-10-11 13:52:08 -07005320# check if any macro arguments are reused (ignore '...' and 'type')
5321 foreach my $arg (@def_args) {
5322 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005323 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005324 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005325 $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 -07005326 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5327 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005328 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005329 if ($use_cnt > 1) {
5330 CHK("MACRO_ARG_REUSE",
5331 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005332 }
5333# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005334 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005335 ((defined($1) && $1 ne ',') ||
5336 (defined($2) && $2 ne ','))) {
5337 CHK("MACRO_ARG_PRECEDENCE",
5338 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005339 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005340 }
Joe Perches5023d342012-12-17 16:01:47 -08005341
Joe Perches08a28432014-10-13 15:51:55 -07005342# check for macros with flow control, but without ## concatenation
5343# ## concatenation is commonly a macro that defines a function so ignore those
5344 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005345 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005346 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005347
Joe Perches08a28432014-10-13 15:51:55 -07005348 WARN("MACRO_WITH_FLOW_CONTROL",
5349 "Macros with flow control statements should be avoided\n" . "$herectx");
5350 }
5351
Joe Perches481eb482012-12-17 16:01:56 -08005352# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005353
5354 } else {
5355 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005356 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5357 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005358 $line =~ /^\+.*\\$/) {
5359 WARN("LINE_CONTINUATIONS",
5360 "Avoid unnecessary line continuations\n" . $herecurr);
5361 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005362 }
5363
Joe Perchesb13edf72012-07-30 14:41:24 -07005364# do {} while (0) macro tests:
5365# single-statement macros do not need to be enclosed in do while (0) loop,
5366# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005367 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005368 $realfile !~ m@/vmlinux.lds.h$@ &&
5369 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5370 my $ln = $linenr;
5371 my $cnt = $realcnt;
5372 my ($off, $dstat, $dcond, $rest);
5373 my $ctx = '';
5374 ($dstat, $dcond, $ln, $cnt, $off) =
5375 ctx_statement_block($linenr, $realcnt, 0);
5376 $ctx = $dstat;
5377
5378 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005379 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005380
5381 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5382 my $stmts = $2;
5383 my $semis = $3;
5384
5385 $ctx =~ s/\n*$//;
5386 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005387 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005388
Joe Perchesac8e97f2012-08-21 16:15:53 -07005389 if (($stmts =~ tr/;/;/) == 1 &&
5390 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005391 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5392 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5393 }
5394 if (defined $semis && $semis ne "") {
5395 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5396 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5397 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005398 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5399 $ctx =~ s/\n*$//;
5400 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005401 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005402
5403 WARN("TRAILING_SEMICOLON",
5404 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005405 }
5406 }
5407
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005408# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005409 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5410 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005411 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005412 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005413 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5414 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005415 my @allowed = ();
5416 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005417 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005418 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005419 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005420 for my $chunk (@chunks) {
5421 my ($cond, $block) = @{$chunk};
5422
Andy Whitcroft773647a2008-03-28 14:15:58 -07005423 # If the condition carries leading newlines, then count those as offsets.
5424 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5425 my $offset = statement_rawlines($whitespace) - 1;
5426
Joe Perchesaad4f612012-03-23 15:02:19 -07005427 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005428 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5429
5430 # We have looked at and allowed this specific line.
5431 $suppress_ifbraces{$ln + $offset} = 1;
5432
5433 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005434 $ln += statement_rawlines($block) - 1;
5435
Andy Whitcroft773647a2008-03-28 14:15:58 -07005436 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005437
5438 $seen++ if ($block =~ /^\s*{/);
5439
Joe Perchesaad4f612012-03-23 15:02:19 -07005440 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005441 if (statement_lines($cond) > 1) {
5442 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005443 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005444 }
5445 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005446 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005447 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005448 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005449 if (statement_block_size($block) > 1) {
5450 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005451 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005452 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005453 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005454 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005455 if ($seen) {
5456 my $sum_allowed = 0;
5457 foreach (@allowed) {
5458 $sum_allowed += $_;
5459 }
5460 if ($sum_allowed == 0) {
5461 WARN("BRACES",
5462 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5463 } elsif ($sum_allowed != $allow &&
5464 $seen != $allow) {
5465 CHK("BRACES",
5466 "braces {} should be used on all arms of this statement\n" . $herectx);
5467 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005468 }
5469 }
5470 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005471 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005472 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005473 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005474
Andy Whitcroftcf655042008-03-04 14:28:20 -08005475 # Check the pre-context.
5476 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5477 #print "APW: ALLOWED: pre<$1>\n";
5478 $allowed = 1;
5479 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005480
5481 my ($level, $endln, @chunks) =
5482 ctx_statement_full($linenr, $realcnt, $-[0]);
5483
Andy Whitcroftcf655042008-03-04 14:28:20 -08005484 # Check the condition.
5485 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005486 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005487 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005488 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005489 }
5490 if (statement_lines($cond) > 1) {
5491 #print "APW: ALLOWED: cond<$cond>\n";
5492 $allowed = 1;
5493 }
5494 if ($block =~/\b(?:if|for|while)\b/) {
5495 #print "APW: ALLOWED: block<$block>\n";
5496 $allowed = 1;
5497 }
5498 if (statement_block_size($block) > 1) {
5499 #print "APW: ALLOWED: lines block<$block>\n";
5500 $allowed = 1;
5501 }
5502 # Check the post-context.
5503 if (defined $chunks[1]) {
5504 my ($cond, $block) = @{$chunks[1]};
5505 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005506 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005507 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005508 if ($block =~ /^\s*\{/) {
5509 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5510 $allowed = 1;
5511 }
5512 }
5513 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005514 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005515 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005516
Joe Perches000d1cc12011-07-25 17:13:25 -07005517 WARN("BRACES",
5518 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005519 }
5520 }
5521
Joe Perchese4c5bab2017-02-24 15:01:41 -08005522# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005523 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5524 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005525 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5526 }
5527
Joe Perches0979ae62012-12-17 16:01:59 -08005528# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005529 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005530 if (CHK("BRACES",
5531 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5532 $fix && $prevrawline =~ /^\+/) {
5533 fix_delete_line($fixlinenr - 1, $prevrawline);
5534 }
Joe Perches0979ae62012-12-17 16:01:59 -08005535 }
Joe Perches77b9a532013-07-03 15:05:29 -07005536 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005537 if (CHK("BRACES",
5538 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5539 $fix) {
5540 fix_delete_line($fixlinenr, $rawline);
5541 }
Joe Perches0979ae62012-12-17 16:01:59 -08005542 }
5543
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005544# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005545 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5546 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005547 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005548 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005549 }
5550
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005551# Check for user-visible strings broken across lines, which breaks the ability
5552# to grep for the string. Make exceptions when the previous string ends in a
5553# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5554# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005555 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005556 $prevline =~ /"\s*$/ &&
5557 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5558 if (WARN("SPLIT_STRING",
5559 "quoted string split across lines\n" . $hereprev) &&
5560 $fix &&
5561 $prevrawline =~ /^\+.*"\s*$/ &&
5562 $last_coalesced_string_linenr != $linenr - 1) {
5563 my $extracted_string = get_quoted_string($line, $rawline);
5564 my $comma_close = "";
5565 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5566 $comma_close = $1;
5567 }
5568
5569 fix_delete_line($fixlinenr - 1, $prevrawline);
5570 fix_delete_line($fixlinenr, $rawline);
5571 my $fixedline = $prevrawline;
5572 $fixedline =~ s/"\s*$//;
5573 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5574 fix_insert_line($fixlinenr - 1, $fixedline);
5575 $fixedline = $rawline;
5576 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5577 if ($fixedline !~ /\+\s*$/) {
5578 fix_insert_line($fixlinenr, $fixedline);
5579 }
5580 $last_coalesced_string_linenr = $linenr;
5581 }
5582 }
5583
5584# check for missing a space in a string concatenation
5585 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5586 WARN('MISSING_SPACE',
5587 "break quoted strings at a space character\n" . $hereprev);
5588 }
5589
Joe Perchese4b7d302017-05-08 15:55:51 -07005590# check for an embedded function name in a string when the function is known
5591# This does not work very well for -f --file checking as it depends on patch
5592# context providing the function name or a single line form for in-file
5593# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005594 if ($line =~ /^\+.*$String/ &&
5595 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005596 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5597 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005598 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005599 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005600 }
5601
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005602# check for spaces before a quoted newline
5603 if ($rawline =~ /^.*\".*\s\\n/) {
5604 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5605 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5606 $fix) {
5607 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5608 }
5609
5610 }
5611
Joe Perchesf17dba42014-10-13 15:51:51 -07005612# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005613 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5614 if (CHK("CONCATENATED_STRING",
5615 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5616 $fix) {
5617 while ($line =~ /($String)/g) {
5618 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5619 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5620 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5621 }
5622 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005623 }
5624
Joe Perches90ad30e2014-12-10 15:51:59 -08005625# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005626 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005627 if (WARN("STRING_FRAGMENTS",
5628 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5629 $fix) {
5630 while ($line =~ /($String)(?=\s*")/g) {
5631 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5632 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5633 }
5634 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005635 }
5636
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005637# check for non-standard and hex prefixed decimal printf formats
5638 my $show_L = 1; #don't show the same defect twice
5639 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005640 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005641 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005642 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005643 # check for %L
5644 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005645 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005646 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5647 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005648 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005649 # check for %Z
5650 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5651 WARN("PRINTF_Z",
5652 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5653 $show_Z = 0;
5654 }
5655 # check for 0x<decimal>
5656 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5657 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005658 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5659 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005660 }
5661
5662# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005663 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005664 WARN("LINE_CONTINUATIONS",
5665 "Avoid line continuations in quoted strings\n" . $herecurr);
5666 }
5667
Andy Whitcroft00df3442007-06-08 13:47:06 -07005668# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005669 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005670 WARN("IF_0",
5671 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5672 }
5673
5674# warn about #if 1
5675 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5676 WARN("IF_1",
5677 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005678 }
5679
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005680# check for needless "if (<foo>) fn(<foo>)" uses
5681 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005682 my $tested = quotemeta($1);
5683 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5684 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5685 my $func = $1;
5686 if (WARN('NEEDLESS_IF',
5687 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5688 $fix) {
5689 my $do_fix = 1;
5690 my $leading_tabs = "";
5691 my $new_leading_tabs = "";
5692 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5693 $leading_tabs = $1;
5694 } else {
5695 $do_fix = 0;
5696 }
5697 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5698 $new_leading_tabs = $1;
5699 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5700 $do_fix = 0;
5701 }
5702 } else {
5703 $do_fix = 0;
5704 }
5705 if ($do_fix) {
5706 fix_delete_line($fixlinenr - 1, $prevrawline);
5707 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5708 }
5709 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005710 }
5711 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005712
Joe Perchesebfdc402014-08-06 16:10:27 -07005713# check for unnecessary "Out of Memory" messages
5714 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5715 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5716 (defined $1 || defined $3) &&
5717 $linenr > 3) {
5718 my $testval = $2;
5719 my $testline = $lines[$linenr - 3];
5720
5721 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5722# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5723
Joe Perchese29a70f2019-03-07 16:28:35 -08005724 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5725 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005726 WARN("OOM_MESSAGE",
5727 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5728 }
5729 }
5730
Joe Perchesf78d98f2014-10-13 15:52:01 -07005731# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005732 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005733 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5734 my $level = $1;
5735 if (WARN("UNNECESSARY_KERN_LEVEL",
5736 "Possible unnecessary $level\n" . $herecurr) &&
5737 $fix) {
5738 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5739 }
5740 }
5741
Joe Perches45c55e92017-02-24 15:01:31 -08005742# check for logging continuations
5743 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5744 WARN("LOGGING_CONTINUATION",
5745 "Avoid logging continuation uses where feasible\n" . $herecurr);
5746 }
5747
Joe Perchesabb08a52014-12-10 15:51:46 -08005748# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005749 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005750 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5751 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5752 WARN("MASK_THEN_SHIFT",
5753 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5754 }
5755
Joe Perchesb75ac612014-12-10 15:52:02 -08005756# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005757 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005758 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5759 my $val = $1;
5760 my $equal = "!";
5761 $equal = "" if ($4 eq "!=");
5762 if (CHK("COMPARISON_TO_NULL",
5763 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5764 $fix) {
5765 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5766 }
5767 }
5768 }
5769
Joe Perches8716de32013-09-11 14:24:05 -07005770# check for bad placement of section $InitAttribute (e.g.: __initdata)
5771 if ($line =~ /(\b$InitAttribute\b)/) {
5772 my $attr = $1;
5773 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5774 my $ptr = $1;
5775 my $var = $2;
5776 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5777 ERROR("MISPLACED_INIT",
5778 "$attr should be placed after $var\n" . $herecurr)) ||
5779 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5780 WARN("MISPLACED_INIT",
5781 "$attr should be placed after $var\n" . $herecurr))) &&
5782 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005783 $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 -07005784 }
5785 }
5786 }
5787
Joe Perchese970b8842013-11-12 15:10:10 -08005788# check for $InitAttributeData (ie: __initdata) with const
5789 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5790 my $attr = $1;
5791 $attr =~ /($InitAttributePrefix)(.*)/;
5792 my $attr_prefix = $1;
5793 my $attr_type = $2;
5794 if (ERROR("INIT_ATTRIBUTE",
5795 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5796 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005797 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005798 s/$InitAttributeData/${attr_prefix}initconst/;
5799 }
5800 }
5801
5802# check for $InitAttributeConst (ie: __initconst) without const
5803 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5804 my $attr = $1;
5805 if (ERROR("INIT_ATTRIBUTE",
5806 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5807 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005808 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005809 /(^\+\s*(?:static\s+))/;
5810 $lead = rtrim($1);
5811 $lead = "$lead " if ($lead !~ /^\+$/);
5812 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005813 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005814 }
5815 }
5816
Joe Perchesc17893c2015-04-16 12:44:42 -07005817# check for __read_mostly with const non-pointer (should just be const)
5818 if ($line =~ /\b__read_mostly\b/ &&
5819 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5820 if (ERROR("CONST_READ_MOSTLY",
5821 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5822 $fix) {
5823 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5824 }
5825 }
5826
Joe Perchesfbdb8132014-04-03 14:49:14 -07005827# don't use __constant_<foo> functions outside of include/uapi/
5828 if ($realfile !~ m@^include/uapi/@ &&
5829 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5830 my $constant_func = $1;
5831 my $func = $constant_func;
5832 $func =~ s/^__constant_//;
5833 if (WARN("CONSTANT_CONVERSION",
5834 "$constant_func should be $func\n" . $herecurr) &&
5835 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005836 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005837 }
5838 }
5839
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005840# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005841 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005842 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005843 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005844 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005845 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005846 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07005847 }
5848 if ($delay > 2000) {
5849 WARN("LONG_UDELAY",
5850 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005851 }
5852 }
5853
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005854# warn about unexpectedly long msleep's
5855 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5856 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005857 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005858 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005859 }
5860 }
5861
Joe Perches36ec1932013-07-03 15:05:25 -07005862# check for comparisons of jiffies
5863 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5864 WARN("JIFFIES_COMPARISON",
5865 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5866 }
5867
Joe Perches9d7a34a2013-07-03 15:05:26 -07005868# check for comparisons of get_jiffies_64()
5869 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5870 WARN("JIFFIES_COMPARISON",
5871 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5872 }
5873
Andy Whitcroft00df3442007-06-08 13:47:06 -07005874# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005875# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005876# print "#ifdef in C files should be avoided\n";
5877# print "$herecurr";
5878# $clean = 0;
5879# }
5880
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005881# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005882 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005883 if (ERROR("SPACING",
5884 "exactly one space required after that #$1\n" . $herecurr) &&
5885 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005886 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005887 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5888 }
5889
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005890 }
5891
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005892# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005893 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5894 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005895 my $which = $1;
5896 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005897 CHK("UNCOMMENTED_DEFINITION",
5898 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005899 }
5900 }
5901# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005902
5903 my $barriers = qr{
5904 mb|
5905 rmb|
5906 wmb|
5907 read_barrier_depends
5908 }x;
5909 my $barrier_stems = qr{
5910 mb__before_atomic|
5911 mb__after_atomic|
5912 store_release|
5913 load_acquire|
5914 store_mb|
5915 (?:$barriers)
5916 }x;
5917 my $all_barriers = qr{
5918 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005919 smp_(?:$barrier_stems)|
5920 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005921 }x;
5922
5923 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005924 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005925 WARN("MEMORY_BARRIER",
5926 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005927 }
5928 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005929
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005930 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5931
5932 if ($realfile !~ m@^include/asm-generic/@ &&
5933 $realfile !~ m@/barrier\.h$@ &&
5934 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5935 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5936 WARN("MEMORY_BARRIER",
5937 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5938 }
5939
Joe Perchescb426e92015-06-25 15:02:46 -07005940# check for waitqueue_active without a comment.
5941 if ($line =~ /\bwaitqueue_active\s*\(/) {
5942 if (!ctx_has_comment($first_line, $linenr)) {
5943 WARN("WAITQUEUE_ACTIVE",
5944 "waitqueue_active without comment\n" . $herecurr);
5945 }
5946 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005947
Marco Elver5099a722020-04-01 12:17:14 +02005948# check for data_race without a comment.
5949 if ($line =~ /\bdata_race\s*\(/) {
5950 if (!ctx_has_comment($first_line, $linenr)) {
5951 WARN("DATA_RACE",
5952 "data_race without comment\n" . $herecurr);
5953 }
5954 }
5955
Paul E. McKenney91db2592017-11-27 09:37:35 -08005956# check for smp_read_barrier_depends and read_barrier_depends
5957 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5958 WARN("READ_BARRIER_DEPENDS",
5959 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5960 }
5961
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005962# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005963 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005964 CHK("ARCH_DEFINES",
5965 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005966 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005967
Joe Perches596ed452017-07-12 14:37:02 -07005968# check that the storage class is not after a type
5969 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005970 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005971 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5972 }
5973# Check that the storage class is at the beginning of a declaration
5974 if ($line =~ /\b$Storage\b/ &&
5975 $line !~ /^.\s*$Storage/ &&
5976 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5977 $1 !~ /[\,\)]\s*$/) {
5978 WARN("STORAGE_CLASS",
5979 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005980 }
5981
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005982# check the location of the inline attribute, that it is between
5983# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005984 if ($line =~ /\b$Type\s+$Inline\b/ ||
5985 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005986 ERROR("INLINE_LOCATION",
5987 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005988 }
5989
Andy Whitcroft8905a672007-11-28 16:21:06 -08005990# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005991 if ($realfile !~ m@\binclude/uapi/@ &&
5992 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005993 if (WARN("INLINE",
5994 "plain inline is preferred over $1\n" . $herecurr) &&
5995 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005996 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005997
5998 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005999 }
6000
Joe Perches3d130fd2011-01-12 17:00:00 -08006001# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08006002 if ($realfile !~ m@\binclude/uapi/@ &&
6003 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006004 WARN("PREFER_PACKED",
6005 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08006006 }
6007
Joe Perches39b7e282011-07-25 17:13:24 -07006008# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08006009 if ($realfile !~ m@\binclude/uapi/@ &&
6010 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006011 WARN("PREFER_ALIGNED",
6012 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07006013 }
6014
Joe Perches462811d2019-09-25 16:46:44 -07006015# Check for __attribute__ section, prefer __section
6016 if ($realfile !~ m@\binclude/uapi/@ &&
6017 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
6018 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6019 my $new = substr($old, 1, -1);
6020 if (WARN("PREFER_SECTION",
6021 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6022 $fix) {
6023 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6024 }
6025 }
6026
Joe Perches5f14d3b2012-01-10 15:09:52 -08006027# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08006028 if ($realfile !~ m@\binclude/uapi/@ &&
6029 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006030 if (WARN("PREFER_PRINTF",
6031 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6032 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006033 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006034
6035 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08006036 }
6037
Joe Perches6061d942012-03-23 15:02:16 -07006038# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08006039 if ($realfile !~ m@\binclude/uapi/@ &&
6040 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006041 if (WARN("PREFER_SCANF",
6042 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6043 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006044 $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 -07006045 }
Joe Perches6061d942012-03-23 15:02:16 -07006046 }
6047
Joe Perches619a9082014-12-10 15:51:35 -08006048# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006049 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006050 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6051 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6052 $line =~ /\b__weak\b/)) {
6053 ERROR("WEAK_DECLARATION",
6054 "Using weak declarations can have unintended link defects\n" . $herecurr);
6055 }
6056
Tomas Winklerfd39f902016-12-12 16:46:34 -08006057# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006058 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006059 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006060 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6061 my $type = $1;
6062 if ($type =~ /\b($typeC99Typedefs)\b/) {
6063 $type = $1;
6064 my $kernel_type = 'u';
6065 $kernel_type = 's' if ($type =~ /^_*[si]/);
6066 $type =~ /(\d+)/;
6067 $kernel_type .= $1;
6068 if (CHK("PREFER_KERNEL_TYPES",
6069 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6070 $fix) {
6071 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6072 }
6073 }
6074 }
6075
Joe Perches938224b2016-01-20 14:59:15 -08006076# check for cast of C90 native int or longer types constants
6077 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6078 my $cast = $1;
6079 my $const = $2;
6080 if (WARN("TYPECAST_INT_CONSTANT",
6081 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6082 $fix) {
6083 my $suffix = "";
6084 my $newconst = $const;
6085 $newconst =~ s/${Int_type}$//;
6086 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6087 if ($cast =~ /\blong\s+long\b/) {
6088 $suffix .= 'LL';
6089 } elsif ($cast =~ /\blong\b/) {
6090 $suffix .= 'L';
6091 }
6092 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6093 }
6094 }
6095
Joe Perches8f53a9b2010-03-05 13:43:48 -08006096# check for sizeof(&)
6097 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006098 WARN("SIZEOF_ADDRESS",
6099 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006100 }
6101
Joe Perches66c80b62012-07-30 14:41:22 -07006102# check for sizeof without parenthesis
6103 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006104 if (WARN("SIZEOF_PARENTHESIS",
6105 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6106 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006107 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006108 }
Joe Perches66c80b62012-07-30 14:41:22 -07006109 }
6110
Joe Perches88982fe2012-12-17 16:02:00 -08006111# check for struct spinlock declarations
6112 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6113 WARN("USE_SPINLOCK_T",
6114 "struct spinlock should be spinlock_t\n" . $herecurr);
6115 }
6116
Joe Perchesa6962d72013-04-29 16:18:13 -07006117# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006118 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006119 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006120 $fmt =~ s/%%//g;
6121 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006122 if (WARN("PREFER_SEQ_PUTS",
6123 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6124 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006125 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006126 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006127 }
6128 }
6129
Joe Perches478b1792018-04-10 16:33:34 -07006130# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006131 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006132 defined $stat &&
6133 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6134 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006135 my $stat_real;
6136
Joe Perches0b523762017-05-08 15:55:36 -07006137 my $lc = $stat =~ tr@\n@@;
6138 $lc = $lc + $linenr;
6139 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006140 my $specifier;
6141 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006142 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006143 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006144 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6145 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006146
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006147 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006148 $specifier = $1;
6149 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006150 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006151 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006152 ($extension eq "f" &&
6153 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006154 $bad_specifier = $specifier;
6155 last;
6156 }
6157 if ($extension eq "x" && !defined($stat_real)) {
6158 if (!defined($stat_real)) {
6159 $stat_real = get_stat_real($linenr, $lc);
6160 }
6161 WARN("VSPRINTF_SPECIFIER_PX",
6162 "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");
6163 }
6164 }
6165 if ($bad_specifier ne "") {
6166 my $stat_real = get_stat_real($linenr, $lc);
6167 my $ext_type = "Invalid";
6168 my $use = "";
6169 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006170 $use = " - use %pS instead";
6171 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6172 }
6173
6174 WARN("VSPRINTF_POINTER_EXTENSION",
6175 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6176 }
Joe Perches0b523762017-05-08 15:55:36 -07006177 }
6178 }
6179
Andy Whitcroft554e1652012-01-10 15:09:57 -08006180# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006181 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006182 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006183 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006184
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006185 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006186 my $ms_val = $7;
6187 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006188
Andy Whitcroft554e1652012-01-10 15:09:57 -08006189 if ($ms_size =~ /^(0x|)0$/i) {
6190 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006191 "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 -08006192 } elsif ($ms_size =~ /^(0x|)1$/i) {
6193 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006194 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6195 }
6196 }
6197
Joe Perches98a9bba2014-01-23 15:54:52 -08006198# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006199# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006200# defined $stat &&
6201# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6202# if (WARN("PREFER_ETHER_ADDR_COPY",
6203# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6204# $fix) {
6205# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6206# }
6207# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006208
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006209# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006210# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006211# defined $stat &&
6212# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6213# WARN("PREFER_ETHER_ADDR_EQUAL",
6214# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6215# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006216
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006217# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6218# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006219# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006220# defined $stat &&
6221# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6222#
6223# my $ms_val = $7;
6224#
6225# if ($ms_val =~ /^(?:0x|)0+$/i) {
6226# if (WARN("PREFER_ETH_ZERO_ADDR",
6227# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6228# $fix) {
6229# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6230# }
6231# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6232# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6233# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6234# $fix) {
6235# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6236# }
6237# }
6238# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006239
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006240# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006241 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006242 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006243 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006244 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006245 my $call = $1;
6246 my $cast1 = deparenthesize($2);
6247 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006248 my $cast2 = deparenthesize($7);
6249 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006250 my $cast;
6251
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006252 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006253 $cast = "$cast1 or $cast2";
6254 } elsif ($cast1 ne "") {
6255 $cast = $cast1;
6256 } else {
6257 $cast = $cast2;
6258 }
6259 WARN("MINMAX",
6260 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006261 }
6262 }
6263
Joe Perches4a273192012-07-30 14:41:20 -07006264# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006265 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006266 defined $stat &&
6267 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6268 my $min = $1;
6269 my $max = $7;
6270 if ($min eq $max) {
6271 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006272 "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 -07006273 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6274 $min > $max) {
6275 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006276 "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 -07006277 }
6278 }
6279
Joe Perches823b7942013-11-12 15:10:15 -08006280# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006281 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006282 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006283 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006284 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6285 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6286 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6287 my $lc = $stat =~ tr@\n@@;
6288 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006289 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006290 WARN("NAKED_SSCANF",
6291 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6292 }
6293
Joe Perchesafc819a2014-06-04 16:12:08 -07006294# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006295 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006296 defined $stat &&
6297 $line =~ /\bsscanf\b/) {
6298 my $lc = $stat =~ tr@\n@@;
6299 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006300 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006301 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6302 my $format = $6;
6303 my $count = $format =~ tr@%@%@;
6304 if ($count == 1 &&
6305 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6306 WARN("SSCANF_TO_KSTRTO",
6307 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6308 }
6309 }
6310 }
6311
Joe Perches70dc8a42013-09-11 14:23:58 -07006312# check for new externs in .h files.
6313 if ($realfile =~ /\.h$/ &&
6314 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006315 if (CHK("AVOID_EXTERNS",
6316 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006317 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006318 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006319 }
6320 }
6321
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006322# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006323 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006324 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006325 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006326 my $function_name = $1;
6327 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006328
6329 my $s = $stat;
6330 if (defined $cond) {
6331 substr($s, 0, length($cond), '');
6332 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006333 if ($s =~ /^\s*;/ &&
6334 $function_name ne 'uninitialized_var')
6335 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006336 WARN("AVOID_EXTERNS",
6337 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006338 }
6339
6340 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006341 WARN("FUNCTION_ARGUMENTS",
6342 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006343 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006344
6345 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6346 $stat =~ /^.\s*extern\s+/)
6347 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006348 WARN("AVOID_EXTERNS",
6349 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006350 }
6351
Joe Perchesa0ad7592017-07-10 15:52:19 -07006352# check for function declarations that have arguments without identifier names
Joe Perches16b7f3c2020-04-06 20:11:17 -07006353# while avoiding uninitialized_var(x)
Joe Perchesa0ad7592017-07-10 15:52:19 -07006354 if (defined $stat &&
Joe Perches16b7f3c2020-04-06 20:11:17 -07006355 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:($Ident)|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6356 (!defined($1) ||
6357 (defined($1) && $1 ne "uninitialized_var")) &&
6358 $2 ne "void") {
6359 my $args = trim($2);
Joe Perchesca0d8922016-10-11 13:52:16 -07006360 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6361 my $arg = trim($1);
Joe Perches16b7f3c2020-04-06 20:11:17 -07006362 if ($arg =~ /^$Type$/ &&
6363 $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006364 WARN("FUNCTION_ARGUMENTS",
6365 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6366 }
6367 }
6368 }
6369
Joe Perchesa0ad7592017-07-10 15:52:19 -07006370# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006371 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006372 defined $stat &&
6373 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6374 $context_function = $1;
6375
6376# check for multiline function definition with misplaced open brace
6377 my $ok = 0;
6378 my $cnt = statement_rawlines($stat);
6379 my $herectx = $here . "\n";
6380 for (my $n = 0; $n < $cnt; $n++) {
6381 my $rl = raw_line($linenr, $n);
6382 $herectx .= $rl . "\n";
6383 $ok = 1 if ($rl =~ /^[ \+]\{/);
6384 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6385 last if $rl =~ /^[ \+].*\{/;
6386 }
6387 if (!$ok) {
6388 ERROR("OPEN_BRACE",
6389 "open brace '{' following function definitions go on the next line\n" . $herectx);
6390 }
6391 }
6392
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006393# checks for new __setup's
6394 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6395 my $name = $1;
6396
6397 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006398 CHK("UNDOCUMENTED_SETUP",
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07006399 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006400 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006401 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006402
Joe Perchese29a70f2019-03-07 16:28:35 -08006403# check for pointless casting of alloc functions
6404 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006405 WARN("UNNECESSARY_CASTS",
6406 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006407 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006408
Joe Perchesa640d252013-07-03 15:05:21 -07006409# alloc style
6410# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006411 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006412 $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 -07006413 CHK("ALLOC_SIZEOF_STRUCT",
6414 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6415 }
6416
Joe Perches60a55362014-06-04 16:12:07 -07006417# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006418 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006419 defined $stat &&
6420 $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 -07006421 my $oldfunc = $3;
6422 my $a1 = $4;
6423 my $a2 = $10;
6424 my $newfunc = "kmalloc_array";
6425 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006426 my $r1 = $a1;
6427 my $r2 = $a2;
6428 if ($a1 =~ /^sizeof\s*\S/) {
6429 $r1 = $a2;
6430 $r2 = $a1;
6431 }
6432 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6433 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006434 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006435 my $herectx = get_stat_here($linenr, $cnt, $here);
6436
Joe Perches60a55362014-06-04 16:12:07 -07006437 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006438 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6439 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006440 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006441 $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 -07006442 }
6443 }
6444 }
6445
Joe Perches972fdea2013-04-29 16:18:12 -07006446# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006447 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006448 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6449 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006450 WARN("KREALLOC_ARG_REUSE",
6451 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6452 }
6453
Joe Perches5ce59ae2013-02-21 16:44:18 -08006454# check for alloc argument mismatch
6455 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6456 WARN("ALLOC_ARRAY_ARGS",
6457 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6458 }
6459
Joe Perchescaf2a542011-01-12 16:59:56 -08006460# check for multiple semicolons
6461 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006462 if (WARN("ONE_SEMICOLON",
6463 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6464 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006465 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006466 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006467 }
6468
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006469# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6470 if ($realfile !~ m@^include/uapi/@ &&
6471 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006472 my $ull = "";
6473 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6474 if (CHK("BIT_MACRO",
6475 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6476 $fix) {
6477 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6478 }
6479 }
6480
Joe Perches2d632742016-05-20 17:04:00 -07006481# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6482 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6483 my $config = $1;
6484 if (WARN("PREFER_IS_ENABLED",
6485 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6486 $fix) {
6487 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6488 }
6489 }
6490
Joe Perchese81f2392014-08-06 16:11:25 -07006491# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006492 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6493 my $has_break = 0;
6494 my $has_statement = 0;
6495 my $count = 0;
6496 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006497 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006498 $prevline--;
6499 my $rline = $rawlines[$prevline - 1];
6500 my $fline = $lines[$prevline - 1];
6501 last if ($fline =~ /^\@\@/);
6502 next if ($fline =~ /^\-/);
6503 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6504 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6505 next if ($fline =~ /^.[\s$;]*$/);
6506 $has_statement = 1;
6507 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006508 $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 -08006509 }
6510 if (!$has_break && $has_statement) {
6511 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006512 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006513 }
6514 }
6515
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006516# check for /* fallthrough */ like comment, prefer fallthrough;
6517 my @fallthroughs = (
6518 'fallthrough',
6519 '@fallthrough@',
6520 'lint -fallthrough[ \t]*',
6521 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6522 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6523 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6524 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6525 );
6526 if ($raw_comment ne '') {
6527 foreach my $ft (@fallthroughs) {
6528 if ($raw_comment =~ /$ft/) {
6529 my $msg_level = \&WARN;
6530 $msg_level = \&CHK if ($file);
6531 &{$msg_level}("PREFER_FALLTHROUGH",
6532 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6533 last;
6534 }
6535 }
6536 }
6537
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006538# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006539 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006540 defined $stat &&
6541 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006542 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006543 my $herectx = get_stat_here($linenr, $cnt, $here);
6544
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006545 WARN("DEFAULT_NO_BREAK",
6546 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006547 }
6548
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006549# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006550 if ($line =~ /\b__FUNCTION__\b/) {
6551 if (WARN("USE_FUNC",
6552 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6553 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006554 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006555 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006556 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006557
Joe Perches62ec8182015-02-13 14:38:18 -08006558# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6559 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6560 ERROR("DATE_TIME",
6561 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6562 }
6563
Joe Perches2c924882012-03-23 15:02:20 -07006564# check for use of yield()
6565 if ($line =~ /\byield\s*\(\s*\)/) {
6566 WARN("YIELD",
6567 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6568 }
6569
Joe Perches179f8f42013-07-03 15:05:30 -07006570# check for comparisons against true and false
6571 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6572 my $lead = $1;
6573 my $arg = $2;
6574 my $test = $3;
6575 my $otype = $4;
6576 my $trail = $5;
6577 my $op = "!";
6578
6579 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6580
6581 my $type = lc($otype);
6582 if ($type =~ /^(?:true|false)$/) {
6583 if (("$test" eq "==" && "$type" eq "true") ||
6584 ("$test" eq "!=" && "$type" eq "false")) {
6585 $op = "";
6586 }
6587
6588 CHK("BOOL_COMPARISON",
6589 "Using comparison to $otype is error prone\n" . $herecurr);
6590
6591## maybe suggesting a correct construct would better
6592## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6593
6594 }
6595 }
6596
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006597# check for semaphores initialized locked
6598 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006599 WARN("CONSIDER_COMPLETION",
6600 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006601 }
Joe Perches6712d852012-03-23 15:02:20 -07006602
Joe Perches67d0a072011-10-31 17:13:10 -07006603# recommend kstrto* over simple_strto* and strict_strto*
6604 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006605 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006606 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006607 }
Joe Perches6712d852012-03-23 15:02:20 -07006608
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006609# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006610 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006611 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006612 "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 -07006613 }
Joe Perches6712d852012-03-23 15:02:20 -07006614
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006615# check for spin_is_locked(), suggest lockdep instead
6616 if ($line =~ /\bspin_is_locked\(/) {
6617 WARN("USE_LOCKDEP",
6618 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6619 }
6620
Joe Perches9189c7e2018-09-07 15:26:18 -07006621# check for deprecated apis
6622 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6623 my $deprecated_api = $1;
6624 my $new_api = $deprecated_apis{$deprecated_api};
6625 WARN("DEPRECATED_API",
6626 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6627 }
6628
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006629# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006630# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006631 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006632 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006633 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006634 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006635 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006636
6637# use of NR_CPUS is usually wrong
6638# ignore definitions of NR_CPUS and usage to define arrays as likely right
6639 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006640 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6641 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006642 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6643 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6644 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006645 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006646 WARN("NR_CPUS",
6647 "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 -07006648 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006649
Joe Perches52ea8502013-11-12 15:10:09 -08006650# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6651 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6652 ERROR("DEFINE_ARCH_HAS",
6653 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6654 }
6655
Joe Perchesacd93622015-02-13 14:38:38 -08006656# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006657 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006658 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6659 WARN("LIKELY_MISUSE",
6660 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6661 }
6662
Denis Efremovde3f1862019-09-25 16:49:25 -07006663# nested likely/unlikely calls
6664 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6665 WARN("LIKELY_MISUSE",
6666 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6667 }
6668
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006669# whine mightly about in_atomic
6670 if ($line =~ /\bin_atomic\s*\(/) {
6671 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006672 ERROR("IN_ATOMIC",
6673 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006674 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006675 WARN("IN_ATOMIC",
6676 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006677 }
6678 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006679
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006680# check for mutex_trylock_recursive usage
6681 if ($line =~ /mutex_trylock_recursive/) {
6682 ERROR("LOCKING",
6683 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6684 }
6685
Peter Zijlstra1704f472010-03-19 01:37:42 +01006686# check for lockdep_set_novalidate_class
6687 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6688 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6689 if ($realfile !~ m@^kernel/lockdep@ &&
6690 $realfile !~ m@^include/linux/lockdep@ &&
6691 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006692 ERROR("LOCKDEP",
6693 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006694 }
6695 }
Dave Jones88f88312011-01-12 16:59:59 -08006696
Joe Perchesb392c642015-04-16 12:44:16 -07006697 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6698 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006699 WARN("EXPORTED_WORLD_WRITABLE",
6700 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006701 }
Joe Perches24358802014-04-03 14:49:13 -07006702
Joe Perches00180462018-02-06 15:38:55 -08006703# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6704# and whether or not function naming is typical and if
6705# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006706 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006707 defined $stat &&
6708 $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*\)/) {
6709 my $var = $1;
6710 my $perms = $2;
6711 my $show = $3;
6712 my $store = $4;
6713 my $octal_perms = perms_to_octal($perms);
6714 if ($show =~ /^${var}_show$/ &&
6715 $store =~ /^${var}_store$/ &&
6716 $octal_perms eq "0644") {
6717 if (WARN("DEVICE_ATTR_RW",
6718 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6719 $fix) {
6720 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6721 }
6722 } elsif ($show =~ /^${var}_show$/ &&
6723 $store =~ /^NULL$/ &&
6724 $octal_perms eq "0444") {
6725 if (WARN("DEVICE_ATTR_RO",
6726 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6727 $fix) {
6728 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6729 }
6730 } elsif ($show =~ /^NULL$/ &&
6731 $store =~ /^${var}_store$/ &&
6732 $octal_perms eq "0200") {
6733 if (WARN("DEVICE_ATTR_WO",
6734 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6735 $fix) {
6736 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6737 }
6738 } elsif ($octal_perms eq "0644" ||
6739 $octal_perms eq "0444" ||
6740 $octal_perms eq "0200") {
6741 my $newshow = "$show";
6742 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6743 my $newstore = $store;
6744 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6745 my $rename = "";
6746 if ($show ne $newshow) {
6747 $rename .= " '$show' to '$newshow'";
6748 }
6749 if ($store ne $newstore) {
6750 $rename .= " '$store' to '$newstore'";
6751 }
6752 WARN("DEVICE_ATTR_FUNCTIONS",
6753 "Consider renaming function(s)$rename\n" . $herecurr);
6754 } else {
6755 WARN("DEVICE_ATTR_PERMS",
6756 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6757 }
6758 }
6759
Joe Perches515a2352014-04-03 14:49:24 -07006760# Mode permission misuses where it seems decimal should be octal
6761# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006762# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6763# specific definition of not visible in sysfs.
6764# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6765# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006766 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006767 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006768 $line =~ /$mode_perms_search/) {
6769 foreach my $entry (@mode_permission_funcs) {
6770 my $func = $entry->[0];
6771 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006772
Joe Perches459cf0a2016-10-11 13:52:19 -07006773 my $lc = $stat =~ tr@\n@@;
6774 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006775 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006776
Joe Perches515a2352014-04-03 14:49:24 -07006777 my $skip_args = "";
6778 if ($arg_pos > 1) {
6779 $arg_pos--;
6780 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6781 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006782 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006783 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006784 my $val = $1;
6785 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006786 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6787 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6788 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006789 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006790 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006791 }
6792 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006793 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006794 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006795 }
Joe Perches24358802014-04-03 14:49:13 -07006796 }
6797 }
6798 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006799
Joe Perches459cf0a2016-10-11 13:52:19 -07006800# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006801 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006802 my $oval = $1;
6803 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006804 if (WARN("SYMBOLIC_PERMS",
6805 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6806 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006807 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006808 }
6809 }
6810
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006811# validate content of MODULE_LICENSE against list from include/linux/module.h
6812 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6813 my $extracted_string = get_quoted_string($line, $rawline);
6814 my $valid_licenses = qr{
6815 GPL|
6816 GPL\ v2|
6817 GPL\ and\ additional\ rights|
6818 Dual\ BSD/GPL|
6819 Dual\ MIT/GPL|
6820 Dual\ MPL/GPL|
6821 Proprietary
6822 }x;
6823 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6824 WARN("MODULE_LICENSE",
6825 "unknown module license " . $extracted_string . "\n" . $herecurr);
6826 }
6827 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07006828
6829# check for sysctl duplicate constants
6830 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6831 WARN("DUPLICATED_SYSCTL_CONST",
6832 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6833 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006834 }
6835
6836 # If we have no input at all, then there is nothing to report on
6837 # so just keep quiet.
6838 if ($#rawlines == -1) {
6839 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006840 }
6841
Andy Whitcroft8905a672007-11-28 16:21:06 -08006842 # In mailback mode only produce a report in the negative, for
6843 # things that appear to be patches.
6844 if ($mailback && ($clean == 1 || !$is_patch)) {
6845 exit(0);
6846 }
6847
6848 # This is not a patch, and we are are in 'no-patch' mode so
6849 # just keep quiet.
6850 if (!$chk_patch && !$is_patch) {
6851 exit(0);
6852 }
6853
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006854 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006855 ERROR("NOT_UNIFIED_DIFF",
6856 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006857 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006858 if ($is_patch && $has_commit_log && $chk_signoff) {
6859 if ($signoff == 0) {
6860 ERROR("MISSING_SIGN_OFF",
6861 "Missing Signed-off-by: line(s)\n");
6862 } elsif (!$authorsignoff) {
6863 WARN("NO_AUTHOR_SIGN_OFF",
6864 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6865 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006866 }
6867
Andy Whitcroft8905a672007-11-28 16:21:06 -08006868 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006869 if ($summary && !($clean == 1 && $quiet == 1)) {
6870 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006871 print "total: $cnt_error errors, $cnt_warn warnings, " .
6872 (($check)? "$cnt_chk checks, " : "") .
6873 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006874 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006875
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006876 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006877 # If there were any defects found and not already fixing them
6878 if (!$clean and !$fix) {
6879 print << "EOM"
6880
6881NOTE: For some of the reported defects, checkpatch may be able to
6882 mechanically convert to the typical style using --fix or --fix-inplace.
6883EOM
6884 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006885 # If there were whitespace errors which cleanpatch can fix
6886 # then suggest that.
6887 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006888 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006889 print << "EOM"
6890
6891NOTE: Whitespace errors detected.
6892 You may wish to use scripts/cleanpatch or scripts/cleanfile
6893EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006894 }
6895 }
6896
Joe Perchesd752fcc2014-08-06 16:11:05 -07006897 if ($clean == 0 && $fix &&
6898 ("@rawlines" ne "@fixed" ||
6899 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006900 my $newfile = $filename;
6901 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006902 my $linecount = 0;
6903 my $f;
6904
Joe Perchesd752fcc2014-08-06 16:11:05 -07006905 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6906
Joe Perches3705ce52013-07-03 15:05:31 -07006907 open($f, '>', $newfile)
6908 or die "$P: Can't open $newfile for write\n";
6909 foreach my $fixed_line (@fixed) {
6910 $linecount++;
6911 if ($file) {
6912 if ($linecount > 3) {
6913 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006914 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006915 }
6916 } else {
6917 print $f $fixed_line . "\n";
6918 }
6919 }
6920 close($f);
6921
6922 if (!$quiet) {
6923 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006924
Joe Perches3705ce52013-07-03 15:05:31 -07006925Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6926
6927Do _NOT_ trust the results written to this file.
6928Do _NOT_ submit these changes without inspecting them for correctness.
6929
6930This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6931No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006932EOM
6933 }
6934 }
6935
Joe Perchesd8469f12015-06-25 15:03:00 -07006936 if ($quiet == 0) {
6937 print "\n";
6938 if ($clean == 1) {
6939 print "$vname has no obvious style problems and is ready for submission.\n";
6940 } else {
6941 print "$vname has style problems, please review.\n";
6942 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006943 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006944 return $clean;
6945}