blob: 1abddf8fe56ea166b0c327574a3d463d08beeef4 [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";
Quentin Monnetced69da12020-08-11 18:35:13 -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
Quentin Monnetced69da12020-08-11 18:35:13 -0700759 $$wordsRef .= '|' if (defined $$wordsRef);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700760 $$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
Quentin Monnetced69da12020-08-11 18:35:13 -0700769my $const_structs;
770if (show_type("CONST_STRUCT")) {
771 read_words(\$const_structs, $conststructsfile)
772 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
773}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700774
Quentin Monnetced69da12020-08-11 18:35:13 -0700775if (defined($typedefsfile)) {
776 my $typeOtherTypedefs;
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700777 read_words(\$typeOtherTypedefs, $typedefsfile)
778 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
Quentin Monnetced69da12020-08-11 18:35:13 -0700779 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700780}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700781
Andy Whitcroft8905a672007-11-28 16:21:06 -0800782sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700783 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
784 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700785 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700786 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700787 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700788 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700789 (?:$typeTypedefs\b)|
790 (?:${all}\b)
791 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800792 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700793 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800794 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800795 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700796 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700797 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800798 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700799 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800800 }x;
Joe Perches18130872014-08-06 16:11:22 -0700801 $NonptrTypeMisordered = qr{
802 (?:$Modifier\s+|const\s+)*
803 (?:
804 (?:${Misordered}\b)
805 )
806 (?:\s+$Modifier|\s+const)*
807 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700808 $NonptrTypeWithAttr = qr{
809 (?:$Modifier\s+|const\s+)*
810 (?:
811 (?:typeof|__typeof__)\s*\([^\)]*\)|
812 (?:$typeTypedefs\b)|
813 (?:${allWithAttr}\b)
814 )
815 (?:\s+$Modifier|\s+const)*
816 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800817 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700818 $NonptrType
Antonio Borneo7b184962020-04-06 20:11:04 -0700819 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700820 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800821 }x;
Joe Perches18130872014-08-06 16:11:22 -0700822 $TypeMisordered = qr{
823 $NonptrTypeMisordered
Antonio Borneo7b184962020-04-06 20:11:04 -0700824 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Perches18130872014-08-06 16:11:22 -0700825 (?:\s+$Inline|\s+$Modifier)*
826 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700827 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700828 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800829}
830build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700831
Joe Perches7d2367a2011-07-25 17:13:22 -0700832our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700833
834# Using $balanced_parens, $LvalOrFunc, or $FuncArg
835# requires at least perl version v5.10.0
836# Any use must be runtime checked with $^V
837
838our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700839our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800840our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700841
Joe Perchesf8422302014-08-06 16:11:31 -0700842our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700843 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700844 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700845 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700846)};
847
Joe Perches7d2367a2011-07-25 17:13:22 -0700848sub deparenthesize {
849 my ($string) = @_;
850 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700851
852 while ($string =~ /^\s*\(.*\)\s*$/) {
853 $string =~ s@^\s*\(\s*@@;
854 $string =~ s@\s*\)\s*$@@;
855 }
856
Joe Perches7d2367a2011-07-25 17:13:22 -0700857 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700858
Joe Perches7d2367a2011-07-25 17:13:22 -0700859 return $string;
860}
861
Joe Perches34456862013-07-03 15:05:34 -0700862sub seed_camelcase_file {
863 my ($file) = @_;
864
865 return if (!(-f $file));
866
867 local $/;
868
869 open(my $include_file, '<', "$file")
870 or warn "$P: Can't read '$file' $!\n";
871 my $text = <$include_file>;
872 close($include_file);
873
874 my @lines = split('\n', $text);
875
876 foreach my $line (@lines) {
877 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
878 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
879 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800880 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
881 $camelcase{$1} = 1;
882 } 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 -0700883 $camelcase{$1} = 1;
884 }
885 }
886}
887
Joe Perchescd28b112019-12-04 16:52:09 -0800888our %maintained_status = ();
889
Joe Perches85b0ee12016-10-11 13:51:44 -0700890sub is_maintained_obsolete {
891 my ($filename) = @_;
892
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800893 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700894
Joe Perchescd28b112019-12-04 16:52:09 -0800895 if (!exists($maintained_status{$filename})) {
896 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
897 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700898
Joe Perchescd28b112019-12-04 16:52:09 -0800899 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700900}
901
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700902sub is_SPDX_License_valid {
903 my ($license) = @_;
904
Joe Perches56294112018-08-21 21:58:04 -0700905 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700906
Joe Perches56294112018-08-21 21:58:04 -0700907 my $root_path = abs_path($root);
908 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700909 return 0 if ($status ne "");
910 return 1;
911}
912
Joe Perches34456862013-07-03 15:05:34 -0700913my $camelcase_seeded = 0;
914sub seed_camelcase_includes {
915 return if ($camelcase_seeded);
916
917 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700918 my $camelcase_cache = "";
919 my @include_files = ();
920
921 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700922
Richard Genoud3645e322014-02-10 14:25:32 -0800923 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700924 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -0700925 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700926 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700927 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700928 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700929 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700930 @include_files = split('\n', $files);
931 foreach my $file (@include_files) {
932 my $date = POSIX::strftime("%Y%m%d%H%M",
933 localtime((stat $file)[9]));
934 $last_mod_date = $date if ($last_mod_date < $date);
935 }
936 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700937 }
Joe Perchesc707a812013-07-08 16:00:43 -0700938
939 if ($camelcase_cache ne "" && -f $camelcase_cache) {
940 open(my $camelcase_file, '<', "$camelcase_cache")
941 or warn "$P: Can't read '$camelcase_cache' $!\n";
942 while (<$camelcase_file>) {
943 chomp;
944 $camelcase{$_} = 1;
945 }
946 close($camelcase_file);
947
948 return;
949 }
950
Richard Genoud3645e322014-02-10 14:25:32 -0800951 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700952 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700953 @include_files = split('\n', $files);
954 }
955
Joe Perches34456862013-07-03 15:05:34 -0700956 foreach my $file (@include_files) {
957 seed_camelcase_file($file);
958 }
Joe Perches351b2a12013-07-03 15:05:36 -0700959
Joe Perchesc707a812013-07-08 16:00:43 -0700960 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700961 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700962 open(my $camelcase_file, '>', "$camelcase_cache")
963 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700964 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
965 print $camelcase_file ("$_\n");
966 }
967 close($camelcase_file);
968 }
Joe Perches34456862013-07-03 15:05:34 -0700969}
970
Joe Perchesd311cd42014-08-06 16:10:57 -0700971sub git_commit_info {
972 my ($commit, $id, $desc) = @_;
973
974 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
975
Joe Perchesdbbf8692019-09-25 16:46:52 -0700976 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -0700977 $output =~ s/^\s*//gm;
978 my @lines = split("\n", $output);
979
Joe Perches0d7835f2015-02-13 14:38:35 -0800980 return ($id, $desc) if ($#lines < 0);
981
Sean Christopherson5a7f4452019-09-25 16:46:49 -0700982 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -0700983# Maybe one day convert this block of bash into something that returns
984# all matching commit ids, but it's very slow...
985#
986# echo "checking commits $1..."
987# git rev-list --remotes | grep -i "^$1" |
988# while read line ; do
989# git log --format='%H %s' -1 $line |
990# echo "commit $(cut -c 1-12,41-)"
991# done
992 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700993 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700994 } else {
995 $id = substr($lines[0], 0, 12);
996 $desc = substr($lines[0], 41);
997 }
998
999 return ($id, $desc);
1000}
1001
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001002$chk_signoff = 0 if ($file);
1003
Andy Whitcroft00df3442007-06-08 13:47:06 -07001004my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001005my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001006my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001007my @fixed_inserted = ();
1008my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001009my $fixlinenr = -1;
1010
Du, Changbin4a593c32016-05-20 17:04:16 -07001011# If input is git commits, extract all commits from the commit expressions.
1012# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1013die "$P: No git repository found\n" if ($git && !-e ".git");
1014
1015if ($git) {
1016 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001017 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001018 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001019 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1020 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001021 } elsif ($commit_expr =~ m/\.\./) {
1022 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001023 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001024 $git_range = "-1 $commit_expr";
1025 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001026 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001027 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001028 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1029 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001030 my $sha1 = $1;
1031 my $subject = $2;
1032 unshift(@commits, $sha1);
1033 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001034 }
1035 }
1036 die "$P: no git commits after extraction!\n" if (@commits == 0);
1037 @ARGV = @commits;
1038}
1039
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001040my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001041$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001042for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001043 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -07001044 if ($git) {
1045 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1046 die "$P: $filename: git format-patch failed - $!\n";
1047 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001048 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001049 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001050 } elsif ($filename eq '-') {
1051 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001052 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001053 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001054 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001055 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001056 if ($filename eq '-') {
1057 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001058 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001059 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001060 } else {
1061 $vname = $filename;
1062 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001063 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001064 chomp;
1065 push(@rawlines, $_);
Geert Uytterhoevenc7f574d2020-06-04 16:50:43 -07001066 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001067 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001068 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001069
1070 if ($#ARGV > 0 && $quiet == 0) {
1071 print '-' x length($vname) . "\n";
1072 print "$vname\n";
1073 print '-' x length($vname) . "\n";
1074 }
1075
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001076 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001077 $exit = 1;
1078 }
1079 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001080 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001081 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001082 @fixed_inserted = ();
1083 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001084 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001085 @modifierListFile = ();
1086 @typeListFile = ();
1087 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001088}
1089
Joe Perchesd8469f12015-06-25 15:03:00 -07001090if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001091 hash_show_words(\%use_type, "Used");
1092 hash_show_words(\%ignore_type, "Ignored");
1093
Joe Perches5b579802018-08-21 21:57:33 -07001094 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001095 print << "EOM"
1096
1097NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001098 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001099EOM
1100 }
1101 if ($exit) {
1102 print << "EOM"
1103
1104NOTE: If any of the errors are false positives, please report
1105 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1106EOM
1107 }
1108}
1109
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001110exit($exit);
1111
1112sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001113 my ($root) = @_;
1114
1115 my @tree_check = (
1116 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1117 "README", "Documentation", "arch", "include", "drivers",
1118 "fs", "init", "ipc", "kernel", "lib", "scripts",
1119 );
1120
1121 foreach my $check (@tree_check) {
1122 if (! -e $root . '/' . $check) {
1123 return 0;
1124 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001125 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001126 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001127}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001128
Joe Perches20112472011-07-25 17:13:23 -07001129sub parse_email {
1130 my ($formatted_email) = @_;
1131
1132 my $name = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001133 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001134 my $address = "";
1135 my $comment = "";
1136
1137 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1138 $name = $1;
1139 $address = $2;
1140 $comment = $3 if defined $3;
1141 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1142 $address = $1;
1143 $comment = $2 if defined $2;
1144 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1145 $address = $1;
1146 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001147 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001148 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001149 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001150 $name =~ s/^\"|\"$//g;
1151 # If there's a name left after stripping spaces and
1152 # leading quotes, and the address doesn't have both
1153 # leading and trailing angle brackets, the address
1154 # is invalid. ie:
1155 # "joe smith joe@smith.com" bad
1156 # "joe smith <joe@smith.com" bad
1157 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1158 $name = "";
1159 $address = "";
1160 $comment = "";
1161 }
1162 }
1163
Joe Perches3705ce52013-07-03 15:05:31 -07001164 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001165 $name =~ s/^\"|\"$//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001166 $name =~ s/(\s*\([^\)]+\))\s*//;
1167 if (defined($1)) {
1168 $name_comment = trim($1);
1169 }
Joe Perches3705ce52013-07-03 15:05:31 -07001170 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001171 $address =~ s/^\<|\>$//g;
1172
1173 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1174 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1175 $name = "\"$name\"";
1176 }
1177
Joe Perchesdfa05c22020-04-06 20:10:48 -07001178 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001179}
1180
1181sub format_email {
1182 my ($name, $address) = @_;
1183
1184 my $formatted_email;
1185
Joe Perches3705ce52013-07-03 15:05:31 -07001186 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001187 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001188 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001189
1190 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1191 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1192 $name = "\"$name\"";
1193 }
1194
1195 if ("$name" eq "") {
1196 $formatted_email = "$address";
1197 } else {
1198 $formatted_email = "$name <$address>";
1199 }
1200
1201 return $formatted_email;
1202}
1203
Joe Perchesdfa05c22020-04-06 20:10:48 -07001204sub reformat_email {
1205 my ($email) = @_;
1206
1207 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1208 return format_email($email_name, $email_address);
1209}
1210
1211sub same_email_addresses {
1212 my ($email1, $email2) = @_;
1213
1214 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1215 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1216
1217 return $email1_name eq $email2_name &&
1218 $email1_address eq $email2_address;
1219}
1220
Joe Perchesd311cd42014-08-06 16:10:57 -07001221sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001222 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001223
Joe Perchesbd474ca2014-08-06 16:11:10 -07001224 foreach my $path (split(/:/, $ENV{PATH})) {
1225 if (-e "$path/$bin") {
1226 return "$path/$bin";
1227 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001228 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001229
Joe Perchesbd474ca2014-08-06 16:11:10 -07001230 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001231}
1232
Joe Perches000d1cc12011-07-25 17:13:25 -07001233sub which_conf {
1234 my ($conf) = @_;
1235
1236 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1237 if (-e "$path/$conf") {
1238 return "$path/$conf";
1239 }
1240 }
1241
1242 return "";
1243}
1244
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001245sub expand_tabs {
1246 my ($str) = @_;
1247
1248 my $res = '';
1249 my $n = 0;
1250 for my $c (split(//, $str)) {
1251 if ($c eq "\t") {
1252 $res .= ' ';
1253 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001254 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001255 $res .= ' ';
1256 }
1257 next;
1258 }
1259 $res .= $c;
1260 $n++;
1261 }
1262
1263 return $res;
1264}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001265sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001266 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001267 return $res;
1268}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001269
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001270sub line_stats {
1271 my ($line) = @_;
1272
1273 # Drop the diff line leader and expand tabs
1274 $line =~ s/^.//;
1275 $line = expand_tabs($line);
1276
1277 # Pick the indent from the front of the line.
1278 my ($white) = ($line =~ /^(\s*)/);
1279
1280 return (length($line), length($white));
1281}
1282
Andy Whitcroft773647a2008-03-28 14:15:58 -07001283my $sanitise_quote = '';
1284
1285sub sanitise_line_reset {
1286 my ($in_comment) = @_;
1287
1288 if ($in_comment) {
1289 $sanitise_quote = '*/';
1290 } else {
1291 $sanitise_quote = '';
1292 }
1293}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001294sub sanitise_line {
1295 my ($line) = @_;
1296
1297 my $res = '';
1298 my $l = '';
1299
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001300 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001301 my $off = 0;
1302 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001303
Andy Whitcroft773647a2008-03-28 14:15:58 -07001304 # Always copy over the diff marker.
1305 $res = substr($line, 0, 1);
1306
1307 for ($off = 1; $off < length($line); $off++) {
1308 $c = substr($line, $off, 1);
1309
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001310 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001311 # and end, all to $;.
1312 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1313 $sanitise_quote = '*/';
1314
1315 substr($res, $off, 2, "$;$;");
1316 $off++;
1317 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001318 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001319 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001320 $sanitise_quote = '';
1321 substr($res, $off, 2, "$;$;");
1322 $off++;
1323 next;
1324 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001325 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1326 $sanitise_quote = '//';
1327
1328 substr($res, $off, 2, $sanitise_quote);
1329 $off++;
1330 next;
1331 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001332
1333 # A \ in a string means ignore the next character.
1334 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1335 $c eq "\\") {
1336 substr($res, $off, 2, 'XX');
1337 $off++;
1338 next;
1339 }
1340 # Regular quotes.
1341 if ($c eq "'" || $c eq '"') {
1342 if ($sanitise_quote eq '') {
1343 $sanitise_quote = $c;
1344
1345 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001346 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001347 } elsif ($sanitise_quote eq $c) {
1348 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001349 }
1350 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001351
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001352 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001353 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1354 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001355 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1356 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001357 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1358 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001359 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001360 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001361 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001362 }
1363
Daniel Walker113f04a2009-09-21 17:04:35 -07001364 if ($sanitise_quote eq '//') {
1365 $sanitise_quote = '';
1366 }
1367
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001368 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001369 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001370 my $clean = 'X' x length($1);
1371 $res =~ s@\<.*\>@<$clean>@;
1372
1373 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001374 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001375 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001376 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001377 }
1378
Joe Perchesdadf6802016-08-02 14:04:33 -07001379 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1380 my $match = $1;
1381 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1382 }
1383
Andy Whitcroft00df3442007-06-08 13:47:06 -07001384 return $res;
1385}
1386
Joe Perchesa6962d72013-04-29 16:18:13 -07001387sub get_quoted_string {
1388 my ($line, $rawline) = @_;
1389
Joe Perches478b1792018-04-10 16:33:34 -07001390 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001391 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001392 return substr($rawline, $-[0], $+[0] - $-[0]);
1393}
1394
Andy Whitcroft8905a672007-11-28 16:21:06 -08001395sub ctx_statement_block {
1396 my ($linenr, $remain, $off) = @_;
1397 my $line = $linenr - 1;
1398 my $blk = '';
1399 my $soff = $off;
1400 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001401 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001402
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001403 my $loff = 0;
1404
Andy Whitcroft8905a672007-11-28 16:21:06 -08001405 my $type = '';
1406 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001407 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001408 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001409 my $c;
1410 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001411
1412 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001413 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001414 @stack = (['', 0]) if ($#stack == -1);
1415
Andy Whitcroft773647a2008-03-28 14:15:58 -07001416 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001417 # If we are about to drop off the end, pull in more
1418 # context.
1419 if ($off >= $len) {
1420 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001421 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001422 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001423 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001424 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001425 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001426 $len = length($blk);
1427 $line++;
1428 last;
1429 }
1430 # Bail if there is no further context.
1431 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001432 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001433 last;
1434 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001435 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1436 $level++;
1437 $type = '#';
1438 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001439 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001440 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001441 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001442 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001443
Andy Whitcroft773647a2008-03-28 14:15:58 -07001444 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001445
1446 # Handle nested #if/#else.
1447 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1448 push(@stack, [ $type, $level ]);
1449 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1450 ($type, $level) = @{$stack[$#stack - 1]};
1451 } elsif ($remainder =~ /^#\s*endif\b/) {
1452 ($type, $level) = @{pop(@stack)};
1453 }
1454
Andy Whitcroft8905a672007-11-28 16:21:06 -08001455 # Statement ends at the ';' or a close '}' at the
1456 # outermost level.
1457 if ($level == 0 && $c eq ';') {
1458 last;
1459 }
1460
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001461 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001462 if ($level == 0 && $coff_set == 0 &&
1463 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1464 $remainder =~ /^(else)(?:\s|{)/ &&
1465 $remainder !~ /^else\s+if\b/) {
1466 $coff = $off + length($1) - 1;
1467 $coff_set = 1;
1468 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1469 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001470 }
1471
Andy Whitcroft8905a672007-11-28 16:21:06 -08001472 if (($type eq '' || $type eq '(') && $c eq '(') {
1473 $level++;
1474 $type = '(';
1475 }
1476 if ($type eq '(' && $c eq ')') {
1477 $level--;
1478 $type = ($level != 0)? '(' : '';
1479
1480 if ($level == 0 && $coff < $soff) {
1481 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001482 $coff_set = 1;
1483 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001484 }
1485 }
1486 if (($type eq '' || $type eq '{') && $c eq '{') {
1487 $level++;
1488 $type = '{';
1489 }
1490 if ($type eq '{' && $c eq '}') {
1491 $level--;
1492 $type = ($level != 0)? '{' : '';
1493
1494 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001495 if (substr($blk, $off + 1, 1) eq ';') {
1496 $off++;
1497 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001498 last;
1499 }
1500 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001501 # Preprocessor commands end at the newline unless escaped.
1502 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1503 $level--;
1504 $type = '';
1505 $off++;
1506 last;
1507 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001508 $off++;
1509 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001510 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001511 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001512 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001513 $line++;
1514 $remain--;
1515 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001516
1517 my $statement = substr($blk, $soff, $off - $soff + 1);
1518 my $condition = substr($blk, $soff, $coff - $soff + 1);
1519
1520 #warn "STATEMENT<$statement>\n";
1521 #warn "CONDITION<$condition>\n";
1522
Andy Whitcroft773647a2008-03-28 14:15:58 -07001523 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001524
1525 return ($statement, $condition,
1526 $line, $remain + 1, $off - $loff + 1, $level);
1527}
1528
Andy Whitcroftcf655042008-03-04 14:28:20 -08001529sub statement_lines {
1530 my ($stmt) = @_;
1531
1532 # Strip the diff line prefixes and rip blank lines at start and end.
1533 $stmt =~ s/(^|\n)./$1/g;
1534 $stmt =~ s/^\s*//;
1535 $stmt =~ s/\s*$//;
1536
1537 my @stmt_lines = ($stmt =~ /\n/g);
1538
1539 return $#stmt_lines + 2;
1540}
1541
1542sub statement_rawlines {
1543 my ($stmt) = @_;
1544
1545 my @stmt_lines = ($stmt =~ /\n/g);
1546
1547 return $#stmt_lines + 2;
1548}
1549
1550sub statement_block_size {
1551 my ($stmt) = @_;
1552
1553 $stmt =~ s/(^|\n)./$1/g;
1554 $stmt =~ s/^\s*{//;
1555 $stmt =~ s/}\s*$//;
1556 $stmt =~ s/^\s*//;
1557 $stmt =~ s/\s*$//;
1558
1559 my @stmt_lines = ($stmt =~ /\n/g);
1560 my @stmt_statements = ($stmt =~ /;/g);
1561
1562 my $stmt_lines = $#stmt_lines + 2;
1563 my $stmt_statements = $#stmt_statements + 1;
1564
1565 if ($stmt_lines > $stmt_statements) {
1566 return $stmt_lines;
1567 } else {
1568 return $stmt_statements;
1569 }
1570}
1571
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001572sub ctx_statement_full {
1573 my ($linenr, $remain, $off) = @_;
1574 my ($statement, $condition, $level);
1575
1576 my (@chunks);
1577
Andy Whitcroftcf655042008-03-04 14:28:20 -08001578 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001579 ($statement, $condition, $linenr, $remain, $off, $level) =
1580 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001581 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001582 push(@chunks, [ $condition, $statement ]);
1583 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1584 return ($level, $linenr, @chunks);
1585 }
1586
1587 # Pull in the following conditional/block pairs and see if they
1588 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001589 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001590 ($statement, $condition, $linenr, $remain, $off, $level) =
1591 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001592 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001593 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001594 #print "C: push\n";
1595 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001596 }
1597
1598 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001599}
1600
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001601sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001602 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001603 my $line;
1604 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001605 my $blk = '';
1606 my @o;
1607 my @c;
1608 my @res = ();
1609
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001610 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001611 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001612 for ($line = $start; $remain > 0; $line++) {
1613 next if ($rawlines[$line] =~ /^-/);
1614 $remain--;
1615
1616 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001617
1618 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001619 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001620 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001621 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001622 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001623 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001624 $level = pop(@stack);
1625 }
1626
Andy Whitcroft01464f32010-10-26 14:23:19 -07001627 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001628 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1629 if ($off > 0) {
1630 $off--;
1631 next;
1632 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001633
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001634 if ($c eq $close && $level > 0) {
1635 $level--;
1636 last if ($level == 0);
1637 } elsif ($c eq $open) {
1638 $level++;
1639 }
1640 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001641
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001642 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001643 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001644 }
1645
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001646 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001647 }
1648
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001649 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001650}
1651sub ctx_block_outer {
1652 my ($linenr, $remain) = @_;
1653
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001654 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1655 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001656}
1657sub ctx_block {
1658 my ($linenr, $remain) = @_;
1659
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001660 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1661 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001662}
1663sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001664 my ($linenr, $remain, $off) = @_;
1665
1666 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1667 return @r;
1668}
1669sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001670 my ($linenr, $remain) = @_;
1671
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001672 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001673}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001674sub ctx_statement_level {
1675 my ($linenr, $remain, $off) = @_;
1676
1677 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1678}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001679
1680sub ctx_locate_comment {
1681 my ($first_line, $end_line) = @_;
1682
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001683 # If c99 comment on the current line, or the line before or after
1684 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1685 return $current_comment if (defined $current_comment);
1686 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1687 return $current_comment if (defined $current_comment);
1688 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1689 return $current_comment if (defined $current_comment);
1690
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001691 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001692 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001693 return $current_comment if (defined $current_comment);
1694
1695 # Look through the context and try and figure out if there is a
1696 # comment.
1697 my $in_comment = 0;
1698 $current_comment = '';
1699 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001700 my $line = $rawlines[$linenr - 1];
1701 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001702 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1703 $in_comment = 1;
1704 }
1705 if ($line =~ m@/\*@) {
1706 $in_comment = 1;
1707 }
1708 if (!$in_comment && $current_comment ne '') {
1709 $current_comment = '';
1710 }
1711 $current_comment .= $line . "\n" if ($in_comment);
1712 if ($line =~ m@\*/@) {
1713 $in_comment = 0;
1714 }
1715 }
1716
1717 chomp($current_comment);
1718 return($current_comment);
1719}
1720sub ctx_has_comment {
1721 my ($first_line, $end_line) = @_;
1722 my $cmt = ctx_locate_comment($first_line, $end_line);
1723
Andy Whitcroft00df3442007-06-08 13:47:06 -07001724 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001725 ##print "CMMT: $cmt\n";
1726
1727 return ($cmt ne '');
1728}
1729
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001730sub raw_line {
1731 my ($linenr, $cnt) = @_;
1732
1733 my $offset = $linenr - 1;
1734 $cnt++;
1735
1736 my $line;
1737 while ($cnt) {
1738 $line = $rawlines[$offset++];
1739 next if (defined($line) && $line =~ /^-/);
1740 $cnt--;
1741 }
1742
1743 return $line;
1744}
1745
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001746sub get_stat_real {
1747 my ($linenr, $lc) = @_;
1748
1749 my $stat_real = raw_line($linenr, 0);
1750 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1751 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1752 }
1753
1754 return $stat_real;
1755}
1756
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001757sub get_stat_here {
1758 my ($linenr, $cnt, $here) = @_;
1759
1760 my $herectx = $here . "\n";
1761 for (my $n = 0; $n < $cnt; $n++) {
1762 $herectx .= raw_line($linenr, $n) . "\n";
1763 }
1764
1765 return $herectx;
1766}
1767
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001768sub cat_vet {
1769 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001770 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001771
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001772 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001773 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1774 $res .= $1;
1775 if ($2 ne '') {
1776 $coded = sprintf("^%c", unpack('C', $2) + 64);
1777 $res .= $coded;
1778 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001779 }
1780 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001781
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001782 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001783}
1784
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001785my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001786my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001787my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001788my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001789
1790sub annotate_reset {
1791 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001792 $av_pending = '_';
1793 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001794 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001795}
1796
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001797sub annotate_values {
1798 my ($stream, $type) = @_;
1799
1800 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001801 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001802 my $cur = $stream;
1803
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001804 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001805
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001806 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001807 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001808 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001809 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001810 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001811 print "WS($1)\n" if ($dbg_values > 1);
1812 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001813 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001814 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001815 }
1816
Florian Micklerc023e4732011-01-12 16:59:58 -08001817 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001818 print "CAST($1)\n" if ($dbg_values > 1);
1819 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001820 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001821
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001822 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001823 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001824 $type = 'T';
1825
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001826 } elsif ($cur =~ /^($Modifier)\s*/) {
1827 print "MODIFIER($1)\n" if ($dbg_values > 1);
1828 $type = 'T';
1829
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001830 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001831 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001832 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001833 push(@av_paren_type, $type);
1834 if ($2 ne '') {
1835 $av_pending = 'N';
1836 }
1837 $type = 'E';
1838
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001839 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001840 print "UNDEF($1)\n" if ($dbg_values > 1);
1841 $av_preprocessor = 1;
1842 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001843
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001844 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001845 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001846 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001847
1848 push(@av_paren_type, $type);
1849 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001850 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001851
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001852 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001853 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1854 $av_preprocessor = 1;
1855
1856 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1857
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001858 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001859
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001860 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001861 print "PRE_END($1)\n" if ($dbg_values > 1);
1862
1863 $av_preprocessor = 1;
1864
1865 # Assume all arms of the conditional end as this
1866 # one does, and continue as if the #endif was not here.
1867 pop(@av_paren_type);
1868 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001869 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001870
1871 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001872 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001873
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001874 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1875 print "ATTR($1)\n" if ($dbg_values > 1);
1876 $av_pending = $type;
1877 $type = 'N';
1878
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001879 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001880 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001881 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001882 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001883 }
1884 $type = 'N';
1885
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001886 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001887 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001888 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001889 $type = 'N';
1890
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001891 } elsif ($cur =~/^(case)/o) {
1892 print "CASE($1)\n" if ($dbg_values > 1);
1893 $av_pend_colon = 'C';
1894 $type = 'N';
1895
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001896 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001897 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001898 $type = 'N';
1899
1900 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001901 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001902 push(@av_paren_type, $av_pending);
1903 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001904 $type = 'N';
1905
1906 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001907 my $new_type = pop(@av_paren_type);
1908 if ($new_type ne '_') {
1909 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001910 print "PAREN('$1') -> $type\n"
1911 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001912 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001913 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001914 }
1915
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001916 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001917 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001918 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001919 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001920
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001921 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1922 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001923 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001924 } elsif ($type eq 'E') {
1925 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001926 }
1927 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1928 $type = 'V';
1929
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001930 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001931 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001932 $type = 'V';
1933
1934 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001935 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001936 $type = 'N';
1937
Andy Whitcroftcf655042008-03-04 14:28:20 -08001938 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001939 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001940 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001941 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001942
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001943 } elsif ($cur =~/^(,)/) {
1944 print "COMMA($1)\n" if ($dbg_values > 1);
1945 $type = 'C';
1946
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001947 } elsif ($cur =~ /^(\?)/o) {
1948 print "QUESTION($1)\n" if ($dbg_values > 1);
1949 $type = 'N';
1950
1951 } elsif ($cur =~ /^(:)/o) {
1952 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1953
1954 substr($var, length($res), 1, $av_pend_colon);
1955 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1956 $type = 'E';
1957 } else {
1958 $type = 'N';
1959 }
1960 $av_pend_colon = 'O';
1961
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001962 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001963 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001964 $type = 'N';
1965
Andy Whitcroft0d413862008-10-15 22:02:16 -07001966 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001967 my $variant;
1968
1969 print "OPV($1)\n" if ($dbg_values > 1);
1970 if ($type eq 'V') {
1971 $variant = 'B';
1972 } else {
1973 $variant = 'U';
1974 }
1975
1976 substr($var, length($res), 1, $variant);
1977 $type = 'N';
1978
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001979 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001980 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001981 if ($1 ne '++' && $1 ne '--') {
1982 $type = 'N';
1983 }
1984
1985 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001986 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001987 }
1988 if (defined $1) {
1989 $cur = substr($cur, length($1));
1990 $res .= $type x length($1);
1991 }
1992 }
1993
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001994 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001995}
1996
Andy Whitcroft8905a672007-11-28 16:21:06 -08001997sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001998 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001999 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002000 ^(?:
2001 $Modifier|
2002 $Storage|
2003 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002004 DEFINE_\S+
2005 )$|
2006 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002007 goto|
2008 return|
2009 case|
2010 else|
2011 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002012 do|
2013 \#|
2014 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002015 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002016 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002017 )}x;
2018 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2019 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002020 # Check for modifiers.
2021 $possible =~ s/\s*$Storage\s*//g;
2022 $possible =~ s/\s*$Sparse\s*//g;
2023 if ($possible =~ /^\s*$/) {
2024
2025 } elsif ($possible =~ /\s/) {
2026 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002027 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002028 if ($modifier !~ $notPermitted) {
2029 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002030 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002031 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002032 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002033
2034 } else {
2035 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002036 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002037 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002038 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002039 } else {
2040 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002041 }
2042}
2043
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002044my $prefix = '';
2045
Joe Perches000d1cc12011-07-25 17:13:25 -07002046sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002047 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002048
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002049 $type =~ tr/[a-z]/[A-Z]/;
2050
Joe Perchescbec18a2014-04-03 14:49:19 -07002051 return defined $use_type{$type} if (scalar keys %use_type > 0);
2052
2053 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002054}
2055
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002056sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002057 my ($level, $type, $msg) = @_;
2058
2059 if (!show_type($type) ||
2060 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002061 return 0;
2062 }
Joe Perches57230292015-06-25 15:03:03 -07002063 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002064 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002065 if ($level eq 'ERROR') {
2066 $output .= RED;
2067 } elsif ($level eq 'WARNING') {
2068 $output .= YELLOW;
2069 } else {
2070 $output .= GREEN;
2071 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002072 }
Joe Perches57230292015-06-25 15:03:03 -07002073 $output .= $prefix . $level . ':';
2074 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002075 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002076 $output .= "$type:";
2077 }
John Brooks737c0762017-07-10 15:52:24 -07002078 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002079 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002080
2081 if ($showfile) {
2082 my @lines = split("\n", $output, -1);
2083 splice(@lines, 1, 1);
2084 $output = join("\n", @lines);
2085 }
Joe Perches57230292015-06-25 15:03:03 -07002086 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002087
Joe Perches57230292015-06-25 15:03:03 -07002088 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002089
2090 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002091}
Joe Perchescbec18a2014-04-03 14:49:19 -07002092
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002093sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002094 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002095}
Joe Perches000d1cc12011-07-25 17:13:25 -07002096
Joe Perchesd752fcc2014-08-06 16:11:05 -07002097sub fixup_current_range {
2098 my ($lineRef, $offset, $length) = @_;
2099
2100 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2101 my $o = $1;
2102 my $l = $2;
2103 my $no = $o + $offset;
2104 my $nl = $l + $length;
2105 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2106 }
2107}
2108
2109sub fix_inserted_deleted_lines {
2110 my ($linesRef, $insertedRef, $deletedRef) = @_;
2111
2112 my $range_last_linenr = 0;
2113 my $delta_offset = 0;
2114
2115 my $old_linenr = 0;
2116 my $new_linenr = 0;
2117
2118 my $next_insert = 0;
2119 my $next_delete = 0;
2120
2121 my @lines = ();
2122
2123 my $inserted = @{$insertedRef}[$next_insert++];
2124 my $deleted = @{$deletedRef}[$next_delete++];
2125
2126 foreach my $old_line (@{$linesRef}) {
2127 my $save_line = 1;
2128 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002129 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002130 $delta_offset = 0;
2131 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2132 $range_last_linenr = $new_linenr;
2133 fixup_current_range(\$line, $delta_offset, 0);
2134 }
2135
2136 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2137 $deleted = @{$deletedRef}[$next_delete++];
2138 $save_line = 0;
2139 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2140 }
2141
2142 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2143 push(@lines, ${$inserted}{'LINE'});
2144 $inserted = @{$insertedRef}[$next_insert++];
2145 $new_linenr++;
2146 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2147 }
2148
2149 if ($save_line) {
2150 push(@lines, $line);
2151 $new_linenr++;
2152 }
2153
2154 $old_linenr++;
2155 }
2156
2157 return @lines;
2158}
2159
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002160sub fix_insert_line {
2161 my ($linenr, $line) = @_;
2162
2163 my $inserted = {
2164 LINENR => $linenr,
2165 LINE => $line,
2166 };
2167 push(@fixed_inserted, $inserted);
2168}
2169
2170sub fix_delete_line {
2171 my ($linenr, $line) = @_;
2172
2173 my $deleted = {
2174 LINENR => $linenr,
2175 LINE => $line,
2176 };
2177
2178 push(@fixed_deleted, $deleted);
2179}
2180
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002181sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002182 my ($type, $msg) = @_;
2183
2184 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002185 our $clean = 0;
2186 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002187 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002188 }
Joe Perches3705ce52013-07-03 15:05:31 -07002189 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002190}
2191sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002192 my ($type, $msg) = @_;
2193
2194 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002195 our $clean = 0;
2196 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002197 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002198 }
Joe Perches3705ce52013-07-03 15:05:31 -07002199 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002200}
2201sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002202 my ($type, $msg) = @_;
2203
2204 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002205 our $clean = 0;
2206 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002207 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002208 }
Joe Perches3705ce52013-07-03 15:05:31 -07002209 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002210}
2211
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002212sub check_absolute_file {
2213 my ($absolute, $herecurr) = @_;
2214 my $file = $absolute;
2215
2216 ##print "absolute<$absolute>\n";
2217
2218 # See if any suffix of this path is a path within the tree.
2219 while ($file =~ s@^[^/]*/@@) {
2220 if (-f "$root/$file") {
2221 ##print "file<$file>\n";
2222 last;
2223 }
2224 }
2225 if (! -f _) {
2226 return 0;
2227 }
2228
2229 # It is, so see if the prefix is acceptable.
2230 my $prefix = $absolute;
2231 substr($prefix, -length($file)) = '';
2232
2233 ##print "prefix<$prefix>\n";
2234 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002235 WARN("USE_RELATIVE_PATH",
2236 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002237 }
2238}
2239
Joe Perches3705ce52013-07-03 15:05:31 -07002240sub trim {
2241 my ($string) = @_;
2242
Joe Perchesb34c6482013-09-11 14:24:01 -07002243 $string =~ s/^\s+|\s+$//g;
2244
2245 return $string;
2246}
2247
2248sub ltrim {
2249 my ($string) = @_;
2250
2251 $string =~ s/^\s+//;
2252
2253 return $string;
2254}
2255
2256sub rtrim {
2257 my ($string) = @_;
2258
2259 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002260
2261 return $string;
2262}
2263
Joe Perches52ea8502013-11-12 15:10:09 -08002264sub string_find_replace {
2265 my ($string, $find, $replace) = @_;
2266
2267 $string =~ s/$find/$replace/g;
2268
2269 return $string;
2270}
2271
Joe Perches3705ce52013-07-03 15:05:31 -07002272sub tabify {
2273 my ($leading) = @_;
2274
Antonio Borneo713a09d2020-04-06 20:11:07 -07002275 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002276 my $max_spaces_before_tab = $source_indent - 1;
2277 my $spaces_to_tab = " " x $source_indent;
2278
2279 #convert leading spaces to tabs
2280 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2281 #Remove spaces before a tab
2282 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2283
2284 return "$leading";
2285}
2286
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002287sub pos_last_openparen {
2288 my ($line) = @_;
2289
2290 my $pos = 0;
2291
2292 my $opens = $line =~ tr/\(/\(/;
2293 my $closes = $line =~ tr/\)/\)/;
2294
2295 my $last_openparen = 0;
2296
2297 if (($opens == 0) || ($closes >= $opens)) {
2298 return -1;
2299 }
2300
2301 my $len = length($line);
2302
2303 for ($pos = 0; $pos < $len; $pos++) {
2304 my $string = substr($line, $pos);
2305 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2306 $pos += length($1) - 1;
2307 } elsif (substr($line, $pos, 1) eq '(') {
2308 $last_openparen = $pos;
2309 } elsif (index($string, '(') == -1) {
2310 last;
2311 }
2312 }
2313
Joe Perches91cb5192014-04-03 14:49:32 -07002314 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002315}
2316
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002317sub get_raw_comment {
2318 my ($line, $rawline) = @_;
2319 my $comment = '';
2320
2321 for my $i (0 .. (length($line) - 1)) {
2322 if (substr($line, $i, 1) eq "$;") {
2323 $comment .= substr($rawline, $i, 1);
2324 }
2325 }
2326
2327 return $comment;
2328}
2329
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002330sub process {
2331 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002332
2333 my $linenr=0;
2334 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002335 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002336 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002337 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002338
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002339 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002340 my $indent;
2341 my $previndent=0;
2342 my $stashindent=0;
2343
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002344 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002345 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002346 my $author = '';
2347 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002348 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002349 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002350 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002351 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002352 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002353 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002354 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002355 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002356 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002357 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002358 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002359 my $non_utf8_charset = 0;
2360
Joe Perches365dd4e2014-08-06 16:10:42 -07002361 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002362 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002363
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002364 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002365 our $cnt_lines = 0;
2366 our $cnt_error = 0;
2367 our $cnt_warn = 0;
2368 our $cnt_chk = 0;
2369
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002370 # Trace the real file/line as we go.
2371 my $realfile = '';
2372 my $realline = 0;
2373 my $realcnt = 0;
2374 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002375 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002376 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002377 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002378 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002379 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002380
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002381 my $prev_values = 'E';
2382
2383 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002384 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002385 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002386 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002387 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002388
Joe Perches7e51f192013-09-11 14:23:57 -07002389 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002390
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002391 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002392 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002393 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002394 my @setup_docs = ();
2395 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002396
Joe Perchesd8b07712013-11-12 15:10:06 -08002397 my $camelcase_file_seeded = 0;
2398
Rob Herring9f3a8992018-04-10 16:33:13 -07002399 my $checklicenseline = 1;
2400
Andy Whitcroft773647a2008-03-28 14:15:58 -07002401 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002402 my $line;
2403 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002404 $linenr++;
2405 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002406
Joe Perches3705ce52013-07-03 15:05:31 -07002407 push(@fixed, $rawline) if ($fix);
2408
Andy Whitcroft773647a2008-03-28 14:15:58 -07002409 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002410 $setup_docs = 0;
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07002411 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002412 $setup_docs = 1;
2413 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002414 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002415 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002416 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002417 $realline=$1-1;
2418 if (defined $2) {
2419 $realcnt=$3+1;
2420 } else {
2421 $realcnt=1+1;
2422 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002423 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002424
2425 # Guestimate if this is a continuing comment. Run
2426 # the context looking for a comment "edge". If this
2427 # edge is a close comment then we must be in a comment
2428 # at context start.
2429 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002430 my $cnt = $realcnt;
2431 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2432 next if (defined $rawlines[$ln - 1] &&
2433 $rawlines[$ln - 1] =~ /^-/);
2434 $cnt--;
2435 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002436 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002437 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2438 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2439 ($edge) = $1;
2440 last;
2441 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002442 }
2443 if (defined $edge && $edge eq '*/') {
2444 $in_comment = 1;
2445 }
2446
2447 # Guestimate if this is a continuing comment. If this
2448 # is the start of a diff block and this line starts
2449 # ' *' then it is very likely a comment.
2450 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002451 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002452 {
2453 $in_comment = 1;
2454 }
2455
2456 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2457 sanitise_line_reset($in_comment);
2458
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002459 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002460 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002461 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002462 $line = sanitise_line($rawline);
2463 }
2464 push(@lines, $line);
2465
2466 if ($realcnt > 1) {
2467 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2468 } else {
2469 $realcnt = 0;
2470 }
2471
2472 #print "==>$rawline\n";
2473 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002474
2475 if ($setup_docs && $line =~ /^\+/) {
2476 push(@setup_docs, $line);
2477 }
2478 }
2479
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002480 $prefix = '';
2481
Andy Whitcroft773647a2008-03-28 14:15:58 -07002482 $realcnt = 0;
2483 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002484 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002485 foreach my $line (@lines) {
2486 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002487 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002488 my $sline = $line; #copy of $line
2489 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002490
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002491 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002492 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002493
Joe Perches12c253a2018-06-07 17:10:58 -07002494# check if it's a mode change, rename or start of a patch
2495 if (!$in_commit_log &&
2496 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2497 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2498 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2499 $is_patch = 1;
2500 }
2501
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002502#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002503 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002504 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2505 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002506 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002507 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002508 $realline=$1-1;
2509 if (defined $2) {
2510 $realcnt=$3+1;
2511 } else {
2512 $realcnt=1+1;
2513 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002514 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002515 $prev_values = 'E';
2516
Andy Whitcroft773647a2008-03-28 14:15:58 -07002517 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002518 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002519 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002520 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002521 if ($context =~ /\b(\w+)\s*\(/) {
2522 $context_function = $1;
2523 } else {
2524 undef $context_function;
2525 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002526 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002527
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002528# track the line number as we move through the hunk, note that
2529# new versions of GNU diff omit the leading space on completely
2530# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002531 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002532 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002533 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002534
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002535 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002536 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002537
2538 # Track the previous line.
2539 ($prevline, $stashline) = ($stashline, $line);
2540 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002541 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2542
Andy Whitcroft773647a2008-03-28 14:15:58 -07002543 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002544
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002545 } elsif ($realcnt == 1) {
2546 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002547 }
2548
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002549 my $hunk_line = ($realcnt != 0);
2550
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002551 $here = "#$linenr: " if (!$file);
2552 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002553
Joe Perches2ac73b42014-06-04 16:12:05 -07002554 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002555 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002556 if ($line =~ /^diff --git.*?(\S+)$/) {
2557 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002558 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002559 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002560 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002561 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002562 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002563 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002564 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002565
2566 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002567 if (!$file && $tree && $p1_prefix ne '' &&
2568 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002569 WARN("PATCH_PREFIX",
2570 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002571 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002572
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002573 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002574 ERROR("MODIFIED_INCLUDE_ASM",
2575 "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 -07002576 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002577 $found_file = 1;
2578 }
2579
Joe Perches34d88152015-06-25 15:03:05 -07002580#make up the handle for any error we report on this line
2581 if ($showfile) {
2582 $prefix = "$realfile:$realline: "
2583 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002584 if ($file) {
2585 $prefix = "$filename:$realline: ";
2586 } else {
2587 $prefix = "$filename:$linenr: ";
2588 }
Joe Perches34d88152015-06-25 15:03:05 -07002589 }
2590
Joe Perches2ac73b42014-06-04 16:12:05 -07002591 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002592 if (is_maintained_obsolete($realfile)) {
2593 WARN("OBSOLETE",
2594 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2595 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002596 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002597 $check = 1;
2598 } else {
2599 $check = $check_orig;
2600 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002601 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002602
2603 if ($realfile !~ /^MAINTAINERS/) {
2604 my $last_binding_patch = $is_binding_patch;
2605
2606 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2607
2608 if (($last_binding_patch != -1) &&
2609 ($last_binding_patch ^ $is_binding_patch)) {
2610 WARN("DT_SPLIT_BINDING_PATCH",
Mauro Carvalho Chehab858e6842020-04-15 16:45:25 +02002611 "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 -07002612 }
2613 }
2614
Andy Whitcroft773647a2008-03-28 14:15:58 -07002615 next;
2616 }
2617
Randy Dunlap389834b2007-06-08 13:47:03 -07002618 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002619
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002620 my $hereline = "$here\n$rawline\n";
2621 my $herecurr = "$here\n$rawline\n";
2622 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002623
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002624 $cnt_lines++ if ($realcnt != 0);
2625
Joe Perches490b2922018-08-21 21:58:01 -07002626# Verify the existence of a commit log if appropriate
2627# 2 is used because a $signature is counted in $commit_log_lines
2628 if ($in_commit_log) {
2629 if ($line !~ /^\s*$/) {
2630 $commit_log_lines++; #could be a $signature
2631 }
2632 } elsif ($has_commit_log && $commit_log_lines < 2) {
2633 WARN("COMMIT_MESSAGE",
2634 "Missing commit description - Add an appropriate one\n");
2635 $commit_log_lines = 2; #warn only once
2636 }
2637
Joe Perchese518e9a2015-06-25 15:03:27 -07002638# Check if the commit log has what seems like a diff which can confuse patch
2639 if ($in_commit_log && !$commit_log_has_diff &&
2640 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2641 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2642 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2643 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2644 ERROR("DIFF_IN_COMMIT_MSG",
2645 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2646 $commit_log_has_diff = 1;
2647 }
2648
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002649# Check for incorrect file permissions
2650 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2651 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002652 if ($realfile !~ m@scripts/@ &&
2653 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002654 ERROR("EXECUTE_PERMISSIONS",
2655 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002656 }
2657 }
2658
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002659# Check the patch for a From:
2660 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2661 $author = $1;
2662 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2663 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002664 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002665 }
2666
Joe Perches20112472011-07-25 17:13:23 -07002667# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002668 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002669 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002670 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002671 if ($author ne '') {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002672 if (same_email_addresses($1, $author)) {
2673 $authorsignoff = 1;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002674 }
2675 }
Joe Perches20112472011-07-25 17:13:23 -07002676 }
2677
Joe Perches44d303e2020-04-06 20:11:10 -07002678# Check for patch separator
2679 if ($line =~ /^---$/) {
2680 $has_patch_separator = 1;
2681 $in_commit_log = 0;
2682 }
2683
Joe Perchese0d975b2014-12-10 15:51:49 -08002684# Check if MAINTAINERS is being updated. If so, there's probably no need to
2685# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2686 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2687 $reported_maintainer_file = 1;
2688 }
2689
Joe Perches20112472011-07-25 17:13:23 -07002690# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002691 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002692 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002693 my $space_before = $1;
2694 my $sign_off = $2;
2695 my $space_after = $3;
2696 my $email = $4;
2697 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2698
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002699 if ($sign_off !~ /$signature_tags/) {
2700 WARN("BAD_SIGN_OFF",
2701 "Non-standard signature: $sign_off\n" . $herecurr);
2702 }
Joe Perches20112472011-07-25 17:13:23 -07002703 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002704 if (WARN("BAD_SIGN_OFF",
2705 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2706 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002707 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002708 "$ucfirst_sign_off $email";
2709 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002710 }
Joe Perches20112472011-07-25 17:13:23 -07002711 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002712 if (WARN("BAD_SIGN_OFF",
2713 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2714 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002715 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002716 "$ucfirst_sign_off $email";
2717 }
2718
Joe Perches20112472011-07-25 17:13:23 -07002719 }
2720 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002721 if (WARN("BAD_SIGN_OFF",
2722 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2723 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002724 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002725 "$ucfirst_sign_off $email";
2726 }
Joe Perches20112472011-07-25 17:13:23 -07002727 }
2728
Joe Perchesdfa05c22020-04-06 20:10:48 -07002729 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Joe Perches20112472011-07-25 17:13:23 -07002730 my $suggested_email = format_email(($email_name, $email_address));
2731 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002732 ERROR("BAD_SIGN_OFF",
2733 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002734 } else {
2735 my $dequoted = $suggested_email;
2736 $dequoted =~ s/^"//;
2737 $dequoted =~ s/" </ </;
2738 # Don't force email to have quotes
2739 # Allow just an angle bracketed address
Joe Perchesdfa05c22020-04-06 20:10:48 -07002740 if (!same_email_addresses($email, $suggested_email)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002741 WARN("BAD_SIGN_OFF",
2742 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002743 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002744 }
Joe Perches7e51f192013-09-11 14:23:57 -07002745
2746# Check for duplicate signatures
2747 my $sig_nospace = $line;
2748 $sig_nospace =~ s/\s//g;
2749 $sig_nospace = lc($sig_nospace);
2750 if (defined $signatures{$sig_nospace}) {
2751 WARN("BAD_SIGN_OFF",
2752 "Duplicate signature\n" . $herecurr);
2753 } else {
2754 $signatures{$sig_nospace} = 1;
2755 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002756
2757# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2758 if ($sign_off =~ /^co-developed-by:$/i) {
2759 if ($email eq $author) {
2760 WARN("BAD_SIGN_OFF",
2761 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2762 }
2763 if (!defined $lines[$linenr]) {
2764 WARN("BAD_SIGN_OFF",
2765 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2766 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2767 WARN("BAD_SIGN_OFF",
2768 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2769 } elsif ($1 ne $email) {
2770 WARN("BAD_SIGN_OFF",
2771 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2772 }
2773 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002774 }
2775
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002776# Check email subject for common tools that don't need to be mentioned
2777 if ($in_header_lines &&
2778 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2779 WARN("EMAIL_SUBJECT",
2780 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2781 }
2782
Joe Perches44d303e2020-04-06 20:11:10 -07002783# Check for Gerrit Change-Ids not in any patch context
2784 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002785 ERROR("GERRIT_CHANGE_ID",
Joe Perches44d303e2020-04-06 20:11:10 -07002786 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002787 }
2788
Joe Perches369c8dd2015-11-06 16:31:34 -08002789# Check if the commit log is in a possible stack dump
2790 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2791 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2792 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2793 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07002794 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2795 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2796 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2797 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08002798 $commit_log_possible_stack_dump = 1;
2799 }
2800
Joe Perches2a076f42015-04-16 12:44:28 -07002801# Check for line lengths > 75 in commit log, warn once
2802 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002803 length($line) > 75 &&
2804 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2805 # file delta changes
2806 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2807 # filename then :
2808 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2809 # A Fixes: or Link: line
2810 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002811 WARN("COMMIT_LOG_LONG_LINE",
2812 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2813 $commit_log_long_line = 1;
2814 }
2815
Joe Perchesbf4daf12015-09-09 15:37:50 -07002816# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002817 if ($in_commit_log && $commit_log_possible_stack_dump &&
2818 $line =~ /^\s*$/) {
2819 $commit_log_possible_stack_dump = 0;
2820 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002821
Joe Perches0d7835f2015-02-13 14:38:35 -08002822# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002823 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07002824 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002825 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002826 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002827 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002828 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2829 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002830 my $init_char = "c";
2831 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002832 my $short = 1;
2833 my $long = 0;
2834 my $case = 1;
2835 my $space = 1;
2836 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002837 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002838 my $id = '0123456789ab';
2839 my $orig_desc = "commit description";
2840 my $description = "";
2841
Joe Perchesfe043ea2015-09-09 15:37:25 -07002842 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2843 $init_char = $1;
2844 $orig_commit = lc($2);
2845 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2846 $orig_commit = lc($1);
2847 }
2848
Joe Perches0d7835f2015-02-13 14:38:35 -08002849 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2850 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2851 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2852 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2853 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2854 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002855 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002856 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2857 defined $rawlines[$linenr] &&
2858 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2859 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002860 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002861 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2862 defined $rawlines[$linenr] &&
2863 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2864 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2865 $orig_desc = $1;
2866 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2867 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002868 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002869 }
2870
2871 ($id, $description) = git_commit_info($orig_commit,
2872 $id, $orig_desc);
2873
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002874 if (defined($id) &&
2875 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002876 ERROR("GIT_COMMIT_ID",
2877 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2878 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002879 }
2880
Joe Perches13f19372014-08-06 16:10:59 -07002881# Check for added, moved or deleted files
2882 if (!$reported_maintainer_file && !$in_commit_log &&
2883 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2884 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2885 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2886 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002887 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002888 $reported_maintainer_file = 1;
2889 WARN("FILE_PATH_CHANGES",
2890 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2891 }
2892
Rob Herringe400edb2019-09-12 15:18:20 +01002893# Check for adding new DT bindings not in schema format
2894 if (!$in_commit_log &&
2895 ($line =~ /^new file mode\s*\d+\s*$/) &&
2896 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2897 WARN("DT_SCHEMA_BINDING_PATCH",
2898 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2899 }
2900
Andy Whitcroft00df3442007-06-08 13:47:06 -07002901# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002902 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002903 ERROR("CORRUPTED_PATCH",
2904 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002905 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002906 }
2907
2908# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2909 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002910 $rawline !~ m/^$UTF8*$/) {
2911 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2912
2913 my $blank = copy_spacing($rawline);
2914 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2915 my $hereptr = "$hereline$ptr\n";
2916
Joe Perches34d99212011-07-25 17:13:26 -07002917 CHK("INVALID_UTF8",
2918 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002919 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002920
Joe Perches15662b32011-10-31 17:13:12 -07002921# Check if it's the start of a commit log
2922# (not a header line and we haven't seen the patch filename)
2923 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002924 !($rawline =~ /^\s+(?:\S|$)/ ||
2925 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002926 $in_header_lines = 0;
2927 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002928 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002929 }
2930
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002931# Check if there is UTF-8 in a commit log when a mail header has explicitly
2932# declined it, i.e defined some charset where it is missing.
2933 if ($in_header_lines &&
2934 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2935 $1 !~ /utf-8/i) {
2936 $non_utf8_charset = 1;
2937 }
2938
2939 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002940 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002941 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002942 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2943 }
2944
Joe Perchesd6430f72016-12-12 16:46:28 -08002945# Check for absolute kernel paths in commit message
2946 if ($tree && $in_commit_log) {
2947 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2948 my $file = $1;
2949
2950 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2951 check_absolute_file($1, $herecurr)) {
2952 #
2953 } else {
2954 check_absolute_file($file, $herecurr);
2955 }
2956 }
2957 }
2958
Kees Cook66b47b42014-10-13 15:51:57 -07002959# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002960 if (defined($misspellings) &&
2961 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002962 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002963 my $typo = $1;
2964 my $typo_fix = $spelling_fix{lc($typo)};
2965 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2966 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002967 my $msg_level = \&WARN;
2968 $msg_level = \&CHK if ($file);
2969 if (&{$msg_level}("TYPO_SPELLING",
2970 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002971 $fix) {
2972 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2973 }
2974 }
2975 }
2976
Matteo Crocea8dd86b2019-09-25 16:46:38 -07002977# check for invalid commit id
2978 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2979 my $id;
2980 my $description;
2981 ($id, $description) = git_commit_info($2, undef, undef);
2982 if (!defined($id)) {
2983 WARN("UNKNOWN_COMMIT_ID",
2984 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2985 }
2986 }
2987
Andy Whitcroft306708542008-10-15 22:02:28 -07002988# ignore non-hunk lines and lines being removed
2989 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002990
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002991#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002992 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002993 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002994 if (ERROR("DOS_LINE_ENDINGS",
2995 "DOS line endings\n" . $herevet) &&
2996 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002997 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002998 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002999 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3000 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003001 if (ERROR("TRAILING_WHITESPACE",
3002 "trailing whitespace\n" . $herevet) &&
3003 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003004 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003005 }
3006
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003007 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003008 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07003009
Josh Triplett4783f892013-11-12 15:10:12 -08003010# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003011 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003012 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003013 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3014 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003015 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003016 my $msg_level = \&ERROR;
3017 $msg_level = \&CHK if ($file);
3018 &{$msg_level}("FSF_MAILING_ADDRESS",
3019 "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 -08003020 }
3021
Andi Kleen33549572010-05-24 14:33:29 -07003022# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003023# Only applies when adding the entry originally, after that we do not have
3024# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003025 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003026 # 'choice' is usually the last thing on the line (though
3027 # Kconfig supports named choices), so use a word boundary
3028 # (\b) rather than a whitespace character (\s)
3029 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003030 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003031 my $cnt = $realcnt;
3032 my $ln = $linenr + 1;
3033 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003034 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003035 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003036 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003037 $f = $lines[$ln - 1];
3038 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3039 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003040
3041 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003042 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003043
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003044 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003045 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01003046 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
3047 if ($lines[$ln - 1] =~ "---help---") {
3048 WARN("CONFIG_DESCRIPTION",
3049 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
3050 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003051 $length = -1;
3052 }
3053
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003054 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003055 $f =~ s/#.*//;
3056 $f =~ s/^\s+//;
3057 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003058
3059 # This only checks context lines in the patch
3060 # and so hopefully shouldn't trigger false
3061 # positives, even though some of these are
3062 # common words in help texts
3063 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3064 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003065 $is_end = 1;
3066 last;
3067 }
Andi Kleen33549572010-05-24 14:33:29 -07003068 $length++;
3069 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003070 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3071 WARN("CONFIG_DESCRIPTION",
3072 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3073 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003074 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003075 }
3076
Joe Perches7ccf41a2020-06-04 16:50:33 -07003077# check MAINTAINERS entries
3078 if ($realfile =~ /^MAINTAINERS$/) {
3079# check MAINTAINERS entries for the right form
3080 if ($rawline =~ /^\+[A-Z]:/ &&
3081 $rawline !~ /^\+[A-Z]:\t\S/) {
3082 if (WARN("MAINTAINERS_STYLE",
3083 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3084 $fix) {
3085 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3086 }
3087 }
3088# check MAINTAINERS entries for the right ordering too
3089 my $preferred_order = 'MRLSWQBCPTFXNK';
3090 if ($rawline =~ /^\+[A-Z]:/ &&
3091 $prevrawline =~ /^[\+ ][A-Z]:/) {
3092 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3093 my $cur = $1;
3094 my $curval = $2;
3095 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3096 my $prev = $1;
3097 my $prevval = $2;
3098 my $curindex = index($preferred_order, $cur);
3099 my $previndex = index($preferred_order, $prev);
3100 if ($curindex < 0) {
3101 WARN("MAINTAINERS_STYLE",
3102 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3103 } else {
3104 if ($previndex >= 0 && $curindex < $previndex) {
3105 WARN("MAINTAINERS_STYLE",
3106 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3107 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3108 ($prev eq 'X' && $cur eq 'X')) &&
3109 ($prevval cmp $curval) > 0) {
3110 WARN("MAINTAINERS_STYLE",
3111 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3112 }
3113 }
Joe Perches628f91a2017-07-10 15:52:07 -07003114 }
3115 }
3116
Christoph Jaeger327953e2015-02-13 14:38:29 -08003117# discourage the use of boolean for type definition attributes of Kconfig options
3118 if ($realfile =~ /Kconfig/ &&
3119 $line =~ /^\+\s*\bboolean\b/) {
3120 WARN("CONFIG_TYPE_BOOLEAN",
3121 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3122 }
3123
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003124 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3125 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3126 my $flag = $1;
3127 my $replacement = {
3128 'EXTRA_AFLAGS' => 'asflags-y',
3129 'EXTRA_CFLAGS' => 'ccflags-y',
3130 'EXTRA_CPPFLAGS' => 'cppflags-y',
3131 'EXTRA_LDFLAGS' => 'ldflags-y',
3132 };
3133
3134 WARN("DEPRECATED_VARIABLE",
3135 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3136 }
3137
Rob Herringbff5da42014-01-23 15:54:51 -08003138# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003139 if (defined $root &&
3140 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3141 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3142
Rob Herringbff5da42014-01-23 15:54:51 -08003143 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3144
Florian Vaussardcc933192014-04-03 14:49:27 -07003145 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003146 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003147
Rob Herringbff5da42014-01-23 15:54:51 -08003148 foreach my $compat (@compats) {
3149 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003150 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3151 my $compat3 = $compat;
3152 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3153 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003154 if ( $? >> 8 ) {
3155 WARN("UNDOCUMENTED_DT_STRING",
3156 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3157 }
3158
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003159 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3160 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003161 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003162 if ( $? >> 8 ) {
3163 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003164 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003165 }
3166 }
3167 }
3168
Rob Herring9f3a8992018-04-10 16:33:13 -07003169# check for using SPDX license tag at beginning of files
3170 if ($realline == $checklicenseline) {
3171 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3172 $checklicenseline = 2;
3173 } elsif ($rawline =~ /^\+/) {
3174 my $comment = "";
3175 if ($realfile =~ /\.(h|s|S)$/) {
3176 $comment = '/*';
3177 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3178 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003179 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003180 $comment = '#';
3181 } elsif ($realfile =~ /\.rst$/) {
3182 $comment = '..';
3183 }
3184
Joe Perchesfdf13692019-03-07 16:28:32 -08003185# check SPDX comment style for .[chsS] files
3186 if ($realfile =~ /\.[chsS]$/ &&
3187 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003188 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003189 WARN("SPDX_LICENSE_TAG",
3190 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3191 }
3192
Rob Herring9f3a8992018-04-10 16:33:13 -07003193 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003194 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3195 WARN("SPDX_LICENSE_TAG",
3196 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003197 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003198 my $spdx_license = $1;
3199 if (!is_SPDX_License_valid($spdx_license)) {
3200 WARN("SPDX_LICENSE_TAG",
3201 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3202 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003203 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3204 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3205 my $msg_level = \&WARN;
3206 $msg_level = \&CHK if ($file);
3207 if (&{$msg_level}("SPDX_LICENSE_TAG",
3208
3209 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3210 $fix) {
3211 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3212 }
3213 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003214 }
3215 }
3216 }
3217
Andy Whitcroft5368df22008-10-15 22:02:27 -07003218# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003219 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07003220
Joe Perchesa8da38a2019-03-07 16:28:42 -08003221# check for using SPDX-License-Identifier on the wrong line number
3222 if ($realline != $checklicenseline &&
3223 $rawline =~ /\bSPDX-License-Identifier:/ &&
3224 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3225 WARN("SPDX_LICENSE_TAG",
3226 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3227 }
3228
Joe Perches47e0c882015-06-25 15:02:57 -07003229# line length limit (with some exclusions)
3230#
3231# There are a few types of lines that may extend beyond $max_line_length:
3232# logging functions like pr_info that end in a string
3233# lines with a single string
3234# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003235# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003236#
3237# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003238# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003239# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3240# LONG_LINE all other lines longer than $max_line_length
3241#
3242# if LONG_LINE is ignored, the other 2 types are also ignored
3243#
3244
Joe Perchesb4749e92015-07-17 16:24:01 -07003245 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003246 my $msg_type = "LONG_LINE";
3247
3248 # Check the allowed long line types first
3249
3250 # logging functions that end in a string that starts
3251 # before $max_line_length
3252 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3253 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3254 $msg_type = "";
3255
3256 # lines with only strings (w/ possible termination)
3257 # #defines with only strings
3258 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3259 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3260 $msg_type = "";
3261
Joe Perchescc147502017-11-17 15:28:44 -08003262 # More special cases
3263 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3264 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003265 $msg_type = "";
3266
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003267 # URL ($rawline is used in case the URL is in a comment)
3268 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3269 $msg_type = "";
3270
Joe Perches47e0c882015-06-25 15:02:57 -07003271 # Otherwise set the alternate message types
3272
3273 # a comment starts before $max_line_length
3274 } elsif ($line =~ /($;[\s$;]*)$/ &&
3275 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3276 $msg_type = "LONG_LINE_COMMENT"
3277
3278 # a quoted string starts before $max_line_length
3279 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3280 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3281 $msg_type = "LONG_LINE_STRING"
3282 }
3283
3284 if ($msg_type ne "" &&
3285 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003286 my $msg_level = \&WARN;
3287 $msg_level = \&CHK if ($file);
3288 &{$msg_level}($msg_type,
3289 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003290 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003291 }
3292
Andy Whitcroft8905a672007-11-28 16:21:06 -08003293# check for adding lines without a newline.
3294 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003295 WARN("MISSING_EOF_NEWLINE",
3296 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003297 }
3298
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003299# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003300 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003301
3302# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003303# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003304 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3305 $rawline =~ /^\+\s* \s*/) {
3306 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003307 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003308 if (ERROR("CODE_INDENT",
3309 "code indent should use tabs where possible\n" . $herevet) &&
3310 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003311 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003312 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003313 }
3314
Alberto Panizzo08e44362010-03-05 13:43:54 -08003315# check for space before tabs.
3316 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3317 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003318 if (WARN("SPACE_BEFORE_TAB",
3319 "please, no space before tabs\n" . $herevet) &&
3320 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003321 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003322 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003323 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003324 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003325 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003326 }
3327
Joe Perches6a487212018-04-10 16:34:04 -07003328# check for assignments on the start of a line
3329 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3330 CHK("ASSIGNMENT_CONTINUATIONS",
3331 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3332 }
3333
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003334# check for && or || at the start of a line
3335 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3336 CHK("LOGICAL_CONTINUATIONS",
3337 "Logical continuations should be on the previous line\n" . $hereprev);
3338 }
3339
Joe Perchesa91e8992016-05-20 17:04:05 -07003340# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003341 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003342 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003343 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003344 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003345 if (WARN("TABSTOP",
3346 "Statements should start on a tabstop\n" . $herecurr) &&
3347 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003348 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003349 }
3350 }
3351 }
3352
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003353# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003354 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003355 $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 -07003356 $prevline =~ /^\+(\t*)(.*)$/;
3357 my $oldindent = $1;
3358 my $rest = $2;
3359
3360 my $pos = pos_last_openparen($rest);
3361 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003362 $line =~ /^(\+| )([ \t]*)/;
3363 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003364
3365 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003366 "\t" x ($pos / $tabsize) .
3367 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003368 my $goodspaceindent = $oldindent . " " x $pos;
3369
3370 if ($newindent ne $goodtabindent &&
3371 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003372
3373 if (CHK("PARENTHESIS_ALIGNMENT",
3374 "Alignment should match open parenthesis\n" . $hereprev) &&
3375 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003376 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003377 s/^\+[ \t]*/\+$goodtabindent/;
3378 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003379 }
3380 }
3381 }
3382
Joe Perches6ab3a972015-04-16 12:44:05 -07003383# check for space after cast like "(int) foo" or "(struct foo) bar"
3384# avoid checking a few false positives:
3385# "sizeof(<type>)" or "__alignof__(<type>)"
3386# function pointer declarations like "(*foo)(int) = bar;"
3387# structure definitions like "(struct foo) { 0 };"
3388# multiline macros that define functions
3389# known attributes or the __attribute__ keyword
3390 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3391 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003392 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003393 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003394 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003395 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003396 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003397 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003398 }
3399
Joe Perches86406b12015-09-09 15:37:41 -07003400# Block comment styles
3401# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003402 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003403 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003404 $rawline =~ /^\+[ \t]*\*/ &&
3405 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003406 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3407 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3408 }
3409
Joe Perches86406b12015-09-09 15:37:41 -07003410# Block comments use * on subsequent lines
3411 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3412 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003413 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003414 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003415 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003416 WARN("BLOCK_COMMENT_STYLE",
3417 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003418 }
3419
Joe Perches86406b12015-09-09 15:37:41 -07003420# Block comments use */ on trailing lines
3421 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003422 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3423 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3424 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003425 WARN("BLOCK_COMMENT_STYLE",
3426 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003427 }
3428
Joe Perches08eb9b82016-10-11 13:51:50 -07003429# Block comment * alignment
3430 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003431 $line =~ /^\+[ \t]*$;/ && #leading comment
3432 $rawline =~ /^\+[ \t]*\*/ && #leading *
3433 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003434 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003435 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3436 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003437 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003438 if (defined($1)) {
3439 $oldindent = expand_tabs($1);
3440 } else {
3441 $prevrawline =~ m@^\+(.*/?)\*@;
3442 $oldindent = expand_tabs($1);
3443 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003444 $rawline =~ m@^\+([ \t]*)\*@;
3445 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003446 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003447 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003448 WARN("BLOCK_COMMENT_STYLE",
3449 "Block comments should align the * on each line\n" . $hereprev);
3450 }
3451 }
3452
Joe Perches7f619192014-08-06 16:10:39 -07003453# check for missing blank lines after struct/union declarations
3454# with exceptions for various attributes and macros
3455 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3456 $line =~ /^\+/ &&
3457 !($line =~ /^\+\s*$/ ||
3458 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3459 $line =~ /^\+\s*MODULE_/i ||
3460 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3461 $line =~ /^\+[a-z_]*init/ ||
3462 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3463 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003464 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003465 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003466 if (CHK("LINE_SPACING",
3467 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3468 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003469 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003470 }
Joe Perches7f619192014-08-06 16:10:39 -07003471 }
3472
Joe Perches365dd4e2014-08-06 16:10:42 -07003473# check for multiple consecutive blank lines
3474 if ($prevline =~ /^[\+ ]\s*$/ &&
3475 $line =~ /^\+\s*$/ &&
3476 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003477 if (CHK("LINE_SPACING",
3478 "Please don't use multiple blank lines\n" . $hereprev) &&
3479 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003480 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003481 }
3482
Joe Perches365dd4e2014-08-06 16:10:42 -07003483 $last_blank_line = $linenr;
3484 }
3485
Joe Perches3b617e32014-04-03 14:49:28 -07003486# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003487 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3488 # actual declarations
3489 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003490 # function pointer declarations
3491 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003492 # foo bar; where foo is some local typedef or #define
3493 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3494 # known declaration macros
3495 $prevline =~ /^\+\s+$declaration_macros/) &&
3496 # for "else if" which can look like "$Ident $Ident"
3497 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3498 # other possible extensions of declaration lines
3499 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3500 # not starting a section or a macro "\" extended line
3501 $prevline =~ /(?:\{\s*|\\)$/) &&
3502 # looks like a declaration
3503 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003504 # function pointer declarations
3505 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003506 # foo bar; where foo is some local typedef or #define
3507 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3508 # known declaration macros
3509 $sline =~ /^\+\s+$declaration_macros/ ||
3510 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003511 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003512 # start or end of block or continuation of declaration
3513 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3514 # bitfield continuation
3515 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3516 # other possible extensions of declaration lines
3517 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3518 # indentation of previous and current line are the same
3519 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003520 if (WARN("LINE_SPACING",
3521 "Missing a blank line after declarations\n" . $hereprev) &&
3522 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003523 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003524 }
Joe Perches3b617e32014-04-03 14:49:28 -07003525 }
3526
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003527# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003528# Exceptions:
3529# 1) within comments
3530# 2) indented preprocessor commands
3531# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003532 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003533 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003534 if (WARN("LEADING_SPACE",
3535 "please, no spaces at the start of a line\n" . $herevet) &&
3536 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003537 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003538 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003539 }
3540
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003541# check we are in a valid C source file if not then ignore this hunk
3542 next if ($realfile !~ /\.(h|c)$/);
3543
Joe Perches5751a242017-11-17 15:28:52 -08003544# check for unusual line ending [ or (
3545 if ($line =~ /^\+.*([\[\(])\s*$/) {
3546 CHK("OPEN_ENDED_LINE",
3547 "Lines should not end with a '$1'\n" . $herecurr);
3548 }
3549
Joe Perches4dbed762017-05-08 15:55:39 -07003550# check if this appears to be the start function declaration, save the name
3551 if ($sline =~ /^\+\{\s*$/ &&
3552 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3553 $context_function = $1;
3554 }
3555
3556# check if this appears to be the end of function declaration
3557 if ($sline =~ /^\+\}\s*$/) {
3558 undef $context_function;
3559 }
3560
Joe Perches032a4c02014-08-06 16:10:29 -07003561# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003562# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003563# if the previous line is a break or return and is indented 1 tab more...
3564 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3565 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003566 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3567 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3568 defined $lines[$linenr] &&
3569 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003570 WARN("UNNECESSARY_ELSE",
3571 "else is not generally useful after a break or return\n" . $hereprev);
3572 }
3573 }
3574
Joe Perchesc00df192014-08-06 16:11:01 -07003575# check indentation of a line with a break;
3576# if the previous line is a goto or return and is indented the same # of tabs
3577 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3578 my $tabs = $1;
3579 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3580 WARN("UNNECESSARY_BREAK",
3581 "break is not useful after a goto or return\n" . $hereprev);
3582 }
3583 }
3584
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003585# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003586 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003587 WARN("CVS_KEYWORD",
3588 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003589 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003590
Joe Perches56e77d72013-02-21 16:44:14 -08003591# check for old HOTPLUG __dev<foo> section markings
3592 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3593 WARN("HOTPLUG_SECTION",
3594 "Using $1 is unnecessary\n" . $herecurr);
3595 }
3596
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003597# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003598 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3599 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003600#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003601 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003602 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003603 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003604 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003605 $stat =~ s/\n./\n /g;
3606 $cond =~ s/\n./\n /g;
3607
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003608#print "linenr<$linenr> <$stat>\n";
3609 # If this statement has no statement boundaries within
3610 # it there is no point in retrying a statement scan
3611 # until we hit end of it.
3612 my $frag = $stat; $frag =~ s/;+\s*$//;
3613 if ($frag !~ /(?:{|;)/) {
3614#print "skip<$line_nr_next>\n";
3615 $suppress_statement = $line_nr_next;
3616 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003617
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003618 # Find the real next line.
3619 $realline_next = $line_nr_next;
3620 if (defined $realline_next &&
3621 (!defined $lines[$realline_next - 1] ||
3622 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3623 $realline_next++;
3624 }
3625
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003626 my $s = $stat;
3627 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003628
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003629 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003630 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003631
3632 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003633 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003634
Andy Whitcroft463f2862009-09-21 17:04:34 -07003635 } elsif ($s =~ /^.\s*else\b/s) {
3636
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003637 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003638 } 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 -07003639 my $type = $1;
3640 $type =~ s/\s+/ /g;
3641 possible($type, "A:" . $s);
3642
Andy Whitcroft8905a672007-11-28 16:21:06 -08003643 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003644 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003645 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003646 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003647
3648 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003649 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003650 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003651 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003652
3653 # Check for any sort of function declaration.
3654 # int foo(something bar, other baz);
3655 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003656 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 -08003657 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003658
Andy Whitcroftcf655042008-03-04 14:28:20 -08003659 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003660 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003661 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003662
Andy Whitcroft8905a672007-11-28 16:21:06 -08003663 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003664 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003665
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003666 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003667 }
3668 }
3669 }
3670
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003671 }
3672
Andy Whitcroft653d4872007-06-23 17:16:34 -07003673#
3674# Checks which may be anchored in the context.
3675#
3676
3677# Check for switch () and associated case and default
3678# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003679 if ($line=~/\bswitch\s*\(.*\)/) {
3680 my $err = '';
3681 my $sep = '';
3682 my @ctx = ctx_block_outer($linenr, $realcnt);
3683 shift(@ctx);
3684 for my $ctx (@ctx) {
3685 my ($clen, $cindent) = line_stats($ctx);
3686 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3687 $indent != $cindent) {
3688 $err .= "$sep$ctx\n";
3689 $sep = '';
3690 } else {
3691 $sep = "[...]\n";
3692 }
3693 }
3694 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003695 ERROR("SWITCH_CASE_INDENT_LEVEL",
3696 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003697 }
3698 }
3699
3700# if/while/etc brace do not go on next line, unless defining a do while loop,
3701# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003702 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 -07003703 my $pre_ctx = "$1$2";
3704
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003705 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003706
3707 if ($line =~ /^\+\t{6,}/) {
3708 WARN("DEEP_INDENTATION",
3709 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3710 }
3711
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003712 my $ctx_cnt = $realcnt - $#ctx - 1;
3713 my $ctx = join("\n", @ctx);
3714
Andy Whitcroft548596d2008-07-23 21:29:01 -07003715 my $ctx_ln = $linenr;
3716 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003717
Andy Whitcroft548596d2008-07-23 21:29:01 -07003718 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3719 defined $lines[$ctx_ln - 1] &&
3720 $lines[$ctx_ln - 1] =~ /^-/)) {
3721 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3722 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003723 $ctx_ln++;
3724 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003725
Andy Whitcroft53210162008-07-23 21:29:03 -07003726 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3727 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003728
Joe Perchesd752fcc2014-08-06 16:11:05 -07003729 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003730 ERROR("OPEN_BRACE",
3731 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003732 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003733 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003734 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3735 $ctx =~ /\)\s*\;\s*$/ &&
3736 defined $lines[$ctx_ln - 1])
3737 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003738 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3739 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003740 WARN("TRAILING_SEMICOLON",
3741 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003742 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003743 }
3744 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003745 }
3746
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003747# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003748 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 -08003749 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3750 ctx_statement_block($linenr, $realcnt, 0)
3751 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003752 my ($s, $c) = ($stat, $cond);
3753
3754 substr($s, 0, length($c), '');
3755
Joe Perches9f5af482015-09-09 15:37:30 -07003756 # remove inline comments
3757 $s =~ s/$;/ /g;
3758 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003759
3760 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003761 my @newlines = ($c =~ /\n/gs);
3762 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003763
Joe Perches9f5af482015-09-09 15:37:30 -07003764 # Make sure we remove the line prefixes as we have
3765 # none on the first line, and are going to readd them
3766 # where necessary.
3767 $s =~ s/\n./\n/gs;
3768 while ($s =~ /\n\s+\\\n/) {
3769 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3770 }
3771
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003772 # We want to check the first line inside the block
3773 # starting at the end of the conditional, so remove:
3774 # 1) any blank line termination
3775 # 2) any opening brace { on end of the line
3776 # 3) any do (...) {
3777 my $continuation = 0;
3778 my $check = 0;
3779 $s =~ s/^.*\bdo\b//;
3780 $s =~ s/^\s*{//;
3781 if ($s =~ s/^\s*\\//) {
3782 $continuation = 1;
3783 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003784 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003785 $check = 1;
3786 $cond_lines++;
3787 }
3788
3789 # Also ignore a loop construct at the end of a
3790 # preprocessor statement.
3791 if (($prevline =~ /^.\s*#\s*define\s/ ||
3792 $prevline =~ /\\\s*$/) && $continuation == 0) {
3793 $check = 0;
3794 }
3795
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003796 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003797 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003798 while ($cond_ptr != $cond_lines) {
3799 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003800
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003801 # If we see an #else/#elif then the code
3802 # is not linear.
3803 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3804 $check = 0;
3805 }
3806
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003807 # Ignore:
3808 # 1) blank lines, they should be at 0,
3809 # 2) preprocessor lines, and
3810 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003811 if ($continuation ||
3812 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003813 $s =~ /^\s*#\s*?/ ||
3814 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003815 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003816 if ($s =~ s/^.*?\n//) {
3817 $cond_lines++;
3818 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003819 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003820 }
3821
3822 my (undef, $sindent) = line_stats("+" . $s);
3823 my $stat_real = raw_line($linenr, $cond_lines);
3824
3825 # Check if either of these lines are modified, else
3826 # this is not this patch's fault.
3827 if (!defined($stat_real) ||
3828 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3829 $check = 0;
3830 }
3831 if (defined($stat_real) && $cond_lines > 1) {
3832 $stat_real = "[...]\n$stat_real";
3833 }
3834
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003835 #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 -07003836
Joe Perches9f5af482015-09-09 15:37:30 -07003837 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07003838 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07003839 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003840 ($sindent == $indent &&
3841 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07003842 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003843 WARN("SUSPECT_CODE_INDENT",
3844 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003845 }
3846 }
3847
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003848 # Track the 'values' across context and added lines.
3849 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003850 my ($curr_values, $curr_vars) =
3851 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003852 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003853 if ($dbg_values) {
3854 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003855 print "$linenr > .$outline\n";
3856 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003857 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003858 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003859 $prev_values = substr($curr_values, -1);
3860
Andy Whitcroft00df3442007-06-08 13:47:06 -07003861#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003862 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003863
Joe Perches11ca40a2016-12-12 16:46:31 -08003864# check for dereferences that span multiple lines
3865 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3866 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3867 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3868 my $ref = $1;
3869 $line =~ /^.\s*($Lval)/;
3870 $ref .= $1;
3871 $ref =~ s/\s//g;
3872 WARN("MULTILINE_DEREFERENCE",
3873 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3874 }
3875
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003876# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003877 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 -07003878 my $type = $1;
3879 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003880 $var = "" if (!defined $var);
3881 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003882 my $sign = $1;
3883 my $pointer = $2;
3884
3885 $pointer = "" if (!defined $pointer);
3886
3887 if (WARN("UNSPECIFIED_INT",
3888 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3889 $fix) {
3890 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003891 my $comp_pointer = $pointer;
3892 $comp_pointer =~ s/\s//g;
3893 $decl .= $comp_pointer;
3894 $decl = rtrim($decl) if ($var eq "");
3895 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003896 }
3897 }
3898 }
3899
Andy Whitcroft653d4872007-06-23 17:16:34 -07003900# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003901 if ($dbg_type) {
3902 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003903 ERROR("TEST_TYPE",
3904 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003905 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003906 ERROR("TEST_NOT_TYPE",
3907 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003908 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003909 next;
3910 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003911# TEST: allow direct testing of the attribute matcher.
3912 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003913 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003914 ERROR("TEST_ATTR",
3915 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003916 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003917 ERROR("TEST_NOT_ATTR",
3918 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003919 }
3920 next;
3921 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003922
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003923# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003924 if ($line =~ /^.\s*{/ &&
3925 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003926 if (ERROR("OPEN_BRACE",
3927 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003928 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3929 fix_delete_line($fixlinenr - 1, $prevrawline);
3930 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003931 my $fixedline = $prevrawline;
3932 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003933 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003934 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003935 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003936 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003937 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003938 }
3939
Andy Whitcroft653d4872007-06-23 17:16:34 -07003940#
3941# Checks which are anchored on the added line.
3942#
3943
3944# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003945 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003946 my $path = $1;
3947 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003948 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003949 "malformed #include filename\n" . $herecurr);
3950 }
3951 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3952 ERROR("UAPI_INCLUDE",
3953 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003954 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003955 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003956
3957# no C99 // comments
3958 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003959 if (ERROR("C99_COMMENTS",
3960 "do not use C99 // comments\n" . $herecurr) &&
3961 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003962 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003963 if ($line =~ /\/\/(.*)$/) {
3964 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003965 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003966 }
3967 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003968 }
3969 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003970 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003971 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003972
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003973# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3974# the whole statement.
3975#print "APW <$lines[$realline_next - 1]>\n";
3976 if (defined $realline_next &&
3977 exists $lines[$realline_next - 1] &&
3978 !defined $suppress_export{$realline_next} &&
3979 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3980 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003981 # Handle definitions which produce identifiers with
3982 # a prefix:
3983 # XXX(foo);
3984 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003985 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003986 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003987 $name =~ /^${Ident}_$2/) {
3988#print "FOO C name<$name>\n";
3989 $suppress_export{$realline_next} = 1;
3990
3991 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003992 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003993 ^.DEFINE_$Ident\(\Q$name\E\)|
3994 ^.DECLARE_$Ident\(\Q$name\E\)|
3995 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003996 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3997 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003998 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003999#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4000 $suppress_export{$realline_next} = 2;
4001 } else {
4002 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004003 }
4004 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004005 if (!defined $suppress_export{$linenr} &&
4006 $prevline =~ /^.\s*$/ &&
4007 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4008 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4009#print "FOO B <$lines[$linenr - 1]>\n";
4010 $suppress_export{$linenr} = 2;
4011 }
4012 if (defined $suppress_export{$linenr} &&
4013 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004014 WARN("EXPORT_SYMBOL",
4015 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004016 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004017
Joe Eloff5150bda2010-08-09 17:21:00 -07004018# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004019 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004020 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004021 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004022 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004023 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004024 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004025 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004026# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004027 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004028 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004029 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004030 $herecurr) &&
4031 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004032 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004033 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004034 }
4035
Joe Perches18130872014-08-06 16:11:22 -07004036# check for misordered declarations of char/short/int/long with signed/unsigned
4037 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4038 my $tmp = trim($1);
4039 WARN("MISORDERED_TYPE",
4040 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4041 }
4042
Joe Perches809e0822018-08-21 21:58:12 -07004043# check for unnecessary <signed> int declarations of short/long/long long
4044 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4045 my $type = trim($1);
4046 next if ($type !~ /\bint\b/);
4047 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4048 my $new_type = $type;
4049 $new_type =~ s/\b\s*int\s*\b/ /;
4050 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4051 $new_type =~ s/^const\s+//;
4052 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4053 $new_type = "const $new_type" if ($type =~ /^const\b/);
4054 $new_type =~ s/\s+/ /g;
4055 $new_type = trim($new_type);
4056 if (WARN("UNNECESSARY_INT",
4057 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4058 $fix) {
4059 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4060 }
4061 }
4062
Joe Perchescb710ec2010-10-26 14:23:20 -07004063# check for static const char * arrays.
4064 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004065 WARN("STATIC_CONST_CHAR_ARRAY",
4066 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004067 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004068 }
4069
4070# check for initialized const char arrays that should be static const
4071 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4072 if (WARN("STATIC_CONST_CHAR_ARRAY",
4073 "const array should probably be static const\n" . $herecurr) &&
4074 $fix) {
4075 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4076 }
4077 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004078
4079# check for static char foo[] = "bar" declarations.
4080 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004081 WARN("STATIC_CONST_CHAR_ARRAY",
4082 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004083 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004084 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004085
Joe Perchesab7e23f2015-04-16 12:44:22 -07004086# check for const <foo> const where <foo> is not a pointer or array type
4087 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4088 my $found = $1;
4089 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4090 WARN("CONST_CONST",
4091 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4092 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4093 WARN("CONST_CONST",
4094 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4095 }
4096 }
4097
Joe Perches9b0fa602014-04-03 14:49:18 -07004098# check for non-global char *foo[] = {"bar", ...} declarations.
4099 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4100 WARN("STATIC_CONST_CHAR_ARRAY",
4101 "char * array declaration might be better as static const\n" .
4102 $herecurr);
4103 }
4104
Joe Perchesb598b672015-04-16 12:44:36 -07004105# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4106 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4107 my $array = $1;
4108 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4109 my $array_div = $1;
4110 if (WARN("ARRAY_SIZE",
4111 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4112 $fix) {
4113 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4114 }
4115 }
4116 }
4117
Joe Perchesb36190c2014-01-27 17:07:18 -08004118# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004119 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004120 if (ERROR("FUNCTION_WITHOUT_ARGS",
4121 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4122 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004123 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004124 }
4125 }
4126
Andy Whitcroft653d4872007-06-23 17:16:34 -07004127# check for new typedefs, only function parameters and sparse annotations
4128# make sense.
4129 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004130 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004131 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004132 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004133 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004134 WARN("NEW_TYPEDEFS",
4135 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004136 }
4137
4138# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004139 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004140 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4141 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004142 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004143
Andy Whitcroft65863862009-01-06 14:41:21 -08004144 # Should start with a space.
4145 $to =~ s/^(\S)/ $1/;
4146 # Should not end with a space.
4147 $to =~ s/\s+$//;
4148 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004149 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004150 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004151
Joe Perches3705ce52013-07-03 15:05:31 -07004152## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004153 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004154 if (ERROR("POINTER_LOCATION",
4155 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4156 $fix) {
4157 my $sub_from = $ident;
4158 my $sub_to = $ident;
4159 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004160 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004161 s@\Q$sub_from\E@$sub_to@;
4162 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004163 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004164 }
4165 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4166 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004167 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004168
Andy Whitcroft65863862009-01-06 14:41:21 -08004169 # Should start with a space.
4170 $to =~ s/^(\S)/ $1/;
4171 # Should not end with a space.
4172 $to =~ s/\s+$//;
4173 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004174 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004175 }
4176 # Modifiers should have spaces.
4177 $to =~ s/(\b$Modifier$)/$1 /;
4178
Joe Perches3705ce52013-07-03 15:05:31 -07004179## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004180 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004181 if (ERROR("POINTER_LOCATION",
4182 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4183 $fix) {
4184
4185 my $sub_from = $match;
4186 my $sub_to = $match;
4187 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004188 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004189 s@\Q$sub_from\E@$sub_to@;
4190 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004191 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004192 }
4193
Joe Perches9d3e3c72015-09-09 15:37:27 -07004194# avoid BUG() or BUG_ON()
4195 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004196 my $msg_level = \&WARN;
4197 $msg_level = \&CHK if ($file);
4198 &{$msg_level}("AVOID_BUG",
4199 "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 -07004200 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004201
Joe Perches9d3e3c72015-09-09 15:37:27 -07004202# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004203 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004204 WARN("LINUX_VERSION_CODE",
4205 "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 -08004206 }
4207
Joe Perches17441222011-06-15 15:08:17 -07004208# check for uses of printk_ratelimit
4209 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004210 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004211 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004212 }
4213
Joe Percheseeef5732017-11-17 15:28:41 -08004214# printk should use KERN_* levels
4215 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4216 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4217 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004218 }
4219
Joe Perches243f3802012-05-31 16:26:09 -07004220 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4221 my $orig = $1;
4222 my $level = lc($orig);
4223 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004224 my $level2 = $level;
4225 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004226 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004227 "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 -07004228 }
4229
Joe Perchesdc139312013-02-21 16:44:13 -08004230 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4231 my $orig = $1;
4232 my $level = lc($orig);
4233 $level = "warn" if ($level eq "warning");
4234 $level = "dbg" if ($level eq "debug");
4235 WARN("PREFER_DEV_LEVEL",
4236 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4237 }
4238
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004239# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4240# number of false positives, but assembly files are not checked, so at
4241# least the arch entry code will not trigger this warning.
4242 if ($line =~ /\bENOSYS\b/) {
4243 WARN("ENOSYS",
4244 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4245 }
4246
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004247# ENOTSUPP is not a standard error code and should be avoided in new patches.
4248# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4249# Similarly to ENOSYS warning a small number of false positives is expected.
4250 if (!$file && $line =~ /\bENOTSUPP\b/) {
4251 if (WARN("ENOTSUPP",
4252 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4253 $fix) {
4254 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4255 }
4256 }
4257
Andy Whitcroft653d4872007-06-23 17:16:34 -07004258# function brace can't be on same line, except for #defines of do while,
4259# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004260 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004261 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4262 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4263 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004264 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004265 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004266 $fix) {
4267 fix_delete_line($fixlinenr, $rawline);
4268 my $fixed_line = $rawline;
4269 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4270 my $line1 = $1;
4271 my $line2 = $2;
4272 fix_insert_line($fixlinenr, ltrim($line1));
4273 fix_insert_line($fixlinenr, "\+{");
4274 if ($line2 !~ /^\s*$/) {
4275 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4276 }
4277 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004278 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004279
Andy Whitcroft8905a672007-11-28 16:21:06 -08004280# open braces for enum, union and struct go on the same line.
4281 if ($line =~ /^.\s*{/ &&
4282 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004283 if (ERROR("OPEN_BRACE",
4284 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4285 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4286 fix_delete_line($fixlinenr - 1, $prevrawline);
4287 fix_delete_line($fixlinenr, $rawline);
4288 my $fixedline = rtrim($prevrawline) . " {";
4289 fix_insert_line($fixlinenr, $fixedline);
4290 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004291 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004292 if ($fixedline !~ /^\+\s*$/) {
4293 fix_insert_line($fixlinenr, $fixedline);
4294 }
4295 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004296 }
4297
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004298# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004299 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4300 if (WARN("SPACING",
4301 "missing space after $1 definition\n" . $herecurr) &&
4302 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004303 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004304 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4305 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004306 }
4307
Joe Perches31070b52014-01-23 15:54:49 -08004308# Function pointer declarations
4309# check spacing between type, funcptr, and args
4310# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004311 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004312 my $declare = $1;
4313 my $pre_pointer_space = $2;
4314 my $post_pointer_space = $3;
4315 my $funcname = $4;
4316 my $post_funcname_space = $5;
4317 my $pre_args_space = $6;
4318
Joe Perches91f72e92014-04-03 14:49:12 -07004319# the $Declare variable will capture all spaces after the type
4320# so check it for a missing trailing missing space but pointer return types
4321# don't need a space so don't warn for those.
4322 my $post_declare_space = "";
4323 if ($declare =~ /(\s+)$/) {
4324 $post_declare_space = $1;
4325 $declare = rtrim($declare);
4326 }
4327 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004328 WARN("SPACING",
4329 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004330 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004331 }
4332
4333# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004334# This test is not currently implemented because these declarations are
4335# equivalent to
4336# int foo(int bar, ...)
4337# and this is form shouldn't/doesn't generate a checkpatch warning.
4338#
4339# elsif ($declare =~ /\s{2,}$/) {
4340# WARN("SPACING",
4341# "Multiple spaces after return type\n" . $herecurr);
4342# }
Joe Perches31070b52014-01-23 15:54:49 -08004343
4344# unnecessary space "type ( *funcptr)(args...)"
4345 if (defined $pre_pointer_space &&
4346 $pre_pointer_space =~ /^\s/) {
4347 WARN("SPACING",
4348 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4349 }
4350
4351# unnecessary space "type (* funcptr)(args...)"
4352 if (defined $post_pointer_space &&
4353 $post_pointer_space =~ /^\s/) {
4354 WARN("SPACING",
4355 "Unnecessary space before function pointer name\n" . $herecurr);
4356 }
4357
4358# unnecessary space "type (*funcptr )(args...)"
4359 if (defined $post_funcname_space &&
4360 $post_funcname_space =~ /^\s/) {
4361 WARN("SPACING",
4362 "Unnecessary space after function pointer name\n" . $herecurr);
4363 }
4364
4365# unnecessary space "type (*funcptr) (args...)"
4366 if (defined $pre_args_space &&
4367 $pre_args_space =~ /^\s/) {
4368 WARN("SPACING",
4369 "Unnecessary space before function pointer arguments\n" . $herecurr);
4370 }
4371
4372 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004373 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004374 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004375 }
4376 }
4377
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004378# check for spacing round square brackets; allowed:
4379# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004380# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4381# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004382 while ($line =~ /(.*?\s)\[/g) {
4383 my ($where, $prefix) = ($-[1], $1);
4384 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004385 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004386 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004387 if (ERROR("BRACKET_SPACE",
4388 "space prohibited before open square bracket '['\n" . $herecurr) &&
4389 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004390 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004391 s/^(\+.*?)\s+\[/$1\[/;
4392 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004393 }
4394 }
4395
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004396# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004397 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004398 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004399 my $ctx_before = substr($line, 0, $-[1]);
4400 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004401
4402 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004403 if ($name =~ /^(?:
4404 if|for|while|switch|return|case|
4405 volatile|__volatile__|
4406 __attribute__|format|__extension__|
4407 asm|__asm__)$/x)
4408 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004409 # cpp #define statements have non-optional spaces, ie
4410 # if there is a space between the name and the open
4411 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004412 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004413
4414 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004415 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004416
4417 # If this whole things ends with a type its most
4418 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004419 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004420
4421 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004422 if (WARN("SPACING",
4423 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4424 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004425 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004426 s/\b$name\s+\(/$name\(/;
4427 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004428 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004429 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004430
Andy Whitcroft653d4872007-06-23 17:16:34 -07004431# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004432 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004433 my $fixed_line = "";
4434 my $line_fixed = 0;
4435
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004436 my $ops = qr{
4437 <<=|>>=|<=|>=|==|!=|
4438 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4439 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004440 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004441 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004442 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004443 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004444
4445## print("element count: <" . $#elements . ">\n");
4446## foreach my $el (@elements) {
4447## print("el: <$el>\n");
4448## }
4449
4450 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004451 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004452
Joe Perches3705ce52013-07-03 15:05:31 -07004453 foreach my $el (@elements) {
4454 push(@fix_elements, substr($rawline, $off, length($el)));
4455 $off += length($el);
4456 }
4457
4458 $off = 0;
4459
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004460 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004461 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004462
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004463 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004464
4465 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4466
4467## print("n: <$n> good: <$good>\n");
4468
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004469 $off += length($elements[$n]);
4470
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004471 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004472 my $ca = substr($opline, 0, $off);
4473 my $cc = '';
4474 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4475 $cc = substr($opline, $off + length($elements[$n + 1]));
4476 }
4477 my $cb = "$ca$;$cc";
4478
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004479 my $a = '';
4480 $a = 'V' if ($elements[$n] ne '');
4481 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004482 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004483 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4484 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004485 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004486
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004487 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004488
4489 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004490 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004491 $c = 'V' if ($elements[$n + 2] ne '');
4492 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004493 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004494 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4495 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004496 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004497 } else {
4498 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004499 }
4500
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004501 my $ctx = "${a}x${c}";
4502
4503 my $at = "(ctx:$ctx)";
4504
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004505 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004506 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004507
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004508 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004509 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004510
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004511 # Get the full operator variant.
4512 my $opv = $op . substr($curr_vars, $off, 1);
4513
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004514 # Ignore operators passed as parameters.
4515 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004516 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004517
Andy Whitcroftcf655042008-03-04 14:28:20 -08004518# # Ignore comments
4519# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004520
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004521 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004522 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004523 if ($ctx !~ /.x[WEBC]/ &&
4524 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004525 if (ERROR("SPACING",
4526 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004527 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004528 $line_fixed = 1;
4529 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004530 }
4531
4532 # // is a comment
4533 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004534
Joe Perchesb00e4812014-04-03 14:49:33 -07004535 # : when part of a bitfield
4536 } elsif ($opv eq ':B') {
4537 # skip the bitfield test for now
4538
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004539 # No spaces for:
4540 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004541 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004542 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004543 if (ERROR("SPACING",
4544 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004545 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004546 if (defined $fix_elements[$n + 2]) {
4547 $fix_elements[$n + 2] =~ s/^\s+//;
4548 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004549 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004550 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004551 }
4552
Joe Perches23810972014-12-10 15:51:32 -08004553 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004554 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004555 my $rtrim_before = 0;
4556 my $space_after = 0;
4557 if ($ctx =~ /Wx./) {
4558 if (ERROR("SPACING",
4559 "space prohibited before that '$op' $at\n" . $hereptr)) {
4560 $line_fixed = 1;
4561 $rtrim_before = 1;
4562 }
4563 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004564 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004565 if (ERROR("SPACING",
4566 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004567 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004568 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004569 $space_after = 1;
4570 }
4571 }
4572 if ($rtrim_before || $space_after) {
4573 if ($rtrim_before) {
4574 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4575 } else {
4576 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4577 }
4578 if ($space_after) {
4579 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004580 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004581 }
4582
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004583 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004584 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004585 #warn "'*' is part of type\n";
4586
4587 # unary operators should have a space before and
4588 # none after. May be left adjacent to another
4589 # unary operator, or a cast
4590 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004591 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004592 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004593 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004594 if (ERROR("SPACING",
4595 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004596 if ($n != $last_after + 2) {
4597 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4598 $line_fixed = 1;
4599 }
Joe Perches3705ce52013-07-03 15:05:31 -07004600 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004601 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004602 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004603 # A unary '*' may be const
4604
4605 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004606 if (ERROR("SPACING",
4607 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004608 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004609 if (defined $fix_elements[$n + 2]) {
4610 $fix_elements[$n + 2] =~ s/^\s+//;
4611 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004612 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004613 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004614 }
4615
4616 # unary ++ and unary -- are allowed no space on one side.
4617 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004618 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004619 if (ERROR("SPACING",
4620 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004621 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004622 $line_fixed = 1;
4623 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004624 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004625 if ($ctx =~ /Wx[BE]/ ||
4626 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004627 if (ERROR("SPACING",
4628 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004629 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004630 $line_fixed = 1;
4631 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004632 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004633 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004634 if (ERROR("SPACING",
4635 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004636 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004637 if (defined $fix_elements[$n + 2]) {
4638 $fix_elements[$n + 2] =~ s/^\s+//;
4639 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004640 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004641 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004642 }
4643
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004644 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004645 } elsif ($op eq '<<' or $op eq '>>' or
4646 $op eq '&' or $op eq '^' or $op eq '|' or
4647 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004648 $op eq '*' or $op eq '/' or
4649 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004650 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004651 if ($check) {
4652 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4653 if (CHK("SPACING",
4654 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4655 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4656 $fix_elements[$n + 2] =~ s/^\s+//;
4657 $line_fixed = 1;
4658 }
4659 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4660 if (CHK("SPACING",
4661 "space preferred before that '$op' $at\n" . $hereptr)) {
4662 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4663 $line_fixed = 1;
4664 }
4665 }
4666 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004667 if (ERROR("SPACING",
4668 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004669 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4670 if (defined $fix_elements[$n + 2]) {
4671 $fix_elements[$n + 2] =~ s/^\s+//;
4672 }
Joe Perches3705ce52013-07-03 15:05:31 -07004673 $line_fixed = 1;
4674 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004675 }
4676
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004677 # A colon needs no spaces before when it is
4678 # terminating a case value or a label.
4679 } elsif ($opv eq ':C' || $opv eq ':L') {
4680 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004681 if (ERROR("SPACING",
4682 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004683 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004684 $line_fixed = 1;
4685 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004686 }
4687
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004688 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004689 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004690 my $ok = 0;
4691
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004692 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004693 if (($op eq '<' &&
4694 $cc =~ /^\S+\@\S+>/) ||
4695 ($op eq '>' &&
4696 $ca =~ /<\S+\@\S+$/))
4697 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07004698 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004699 }
4700
Joe Perchese0df7e12015-04-16 12:44:53 -07004701 # for asm volatile statements
4702 # ignore a colon with another
4703 # colon immediately before or after
4704 if (($op eq ':') &&
4705 ($ca =~ /:$/ || $cc =~ /^:/)) {
4706 $ok = 1;
4707 }
4708
Joe Perches84731622013-11-12 15:10:05 -08004709 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004710 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004711 my $msg_level = \&ERROR;
4712 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004713
Jean Delvare0675a8f2017-09-08 16:16:07 -07004714 if (&{$msg_level}("SPACING",
4715 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004716 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4717 if (defined $fix_elements[$n + 2]) {
4718 $fix_elements[$n + 2] =~ s/^\s+//;
4719 }
Joe Perches3705ce52013-07-03 15:05:31 -07004720 $line_fixed = 1;
4721 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004722 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004723 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004724 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004725
4726## print("n: <$n> GOOD: <$good>\n");
4727
4728 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004729 }
Joe Perches3705ce52013-07-03 15:05:31 -07004730
4731 if (($#elements % 2) == 0) {
4732 $fixed_line = $fixed_line . $fix_elements[$#elements];
4733 }
4734
Joe Perches194f66f2014-08-06 16:11:03 -07004735 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4736 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004737 }
4738
4739
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004740 }
4741
Joe Perches786b6322013-07-03 15:05:32 -07004742# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004743 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004744 if (WARN("SPACING",
4745 "space prohibited before semicolon\n" . $herecurr) &&
4746 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004747 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004748 s/^(\+.*\S)\s+;/$1;/;
4749 }
4750 }
4751
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004752# check for multiple assignments
4753 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004754 CHK("MULTIPLE_ASSIGNMENTS",
4755 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004756 }
4757
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004758## # check for multiple declarations, allowing for a function declaration
4759## # continuation.
4760## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4761## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4762##
4763## # Remove any bracketed sections to ensure we do not
4764## # falsly report the parameters of functions.
4765## my $ln = $line;
4766## while ($ln =~ s/\([^\(\)]*\)//g) {
4767## }
4768## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004769## WARN("MULTIPLE_DECLARATION",
4770## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004771## }
4772## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004773
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004774#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004775 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004776 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004777 if (ERROR("SPACING",
4778 "space required before the open brace '{'\n" . $herecurr) &&
4779 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004780 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004781 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004782 }
4783
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004784## # check for blank lines before declarations
4785## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4786## $prevrawline =~ /^.\s*$/) {
4787## WARN("SPACING",
4788## "No blank lines before declarations\n" . $hereprev);
4789## }
4790##
4791
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004792# closing brace should have a space following it when it has anything
4793# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07004794 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004795 if (ERROR("SPACING",
4796 "space required after that close brace '}'\n" . $herecurr) &&
4797 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004798 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004799 s/}((?!(?:,|;|\)))\S)/} $1/;
4800 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004801 }
4802
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004803# check spacing on square brackets
4804 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004805 if (ERROR("SPACING",
4806 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4807 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004808 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004809 s/\[\s+/\[/;
4810 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004811 }
4812 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004813 if (ERROR("SPACING",
4814 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4815 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004816 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004817 s/\s+\]/\]/;
4818 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004819 }
4820
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004821# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004822 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4823 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004824 if (ERROR("SPACING",
4825 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4826 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004827 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004828 s/\(\s+/\(/;
4829 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004830 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004831 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004832 $line !~ /for\s*\(.*;\s+\)/ &&
4833 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004834 if (ERROR("SPACING",
4835 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4836 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004837 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004838 s/\s+\)/\)/;
4839 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004840 }
4841
Joe Perchese2826fd2014-08-06 16:10:48 -07004842# check unnecessary parentheses around addressof/dereference single $Lvals
4843# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4844
4845 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004846 my $var = $1;
4847 if (CHK("UNNECESSARY_PARENTHESES",
4848 "Unnecessary parentheses around $var\n" . $herecurr) &&
4849 $fix) {
4850 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4851 }
4852 }
4853
4854# check for unnecessary parentheses around function pointer uses
4855# ie: (foo->bar)(); should be foo->bar();
4856# but not "if (foo->bar) (" to avoid some false positives
4857 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4858 my $var = $2;
4859 if (CHK("UNNECESSARY_PARENTHESES",
4860 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4861 $fix) {
4862 my $var2 = deparenthesize($var);
4863 $var2 =~ s/\s//g;
4864 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4865 }
4866 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004867
Joe Perches63b7c732017-09-08 16:16:01 -07004868# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004869# when !drivers/staging or command-line uses --strict
4870 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004871 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004872 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4873 my $if_stat = $1;
4874 my $test = substr($2, 1, -1);
4875 my $herectx;
4876 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4877 my $match = $1;
4878 # avoid parentheses around potential macro args
4879 next if ($match =~ /^\s*\w+\s*$/);
4880 if (!defined($herectx)) {
4881 $herectx = $here . "\n";
4882 my $cnt = statement_rawlines($if_stat);
4883 for (my $n = 0; $n < $cnt; $n++) {
4884 my $rl = raw_line($linenr, $n);
4885 $herectx .= $rl . "\n";
4886 last if $rl =~ /^[ \+].*\{/;
4887 }
4888 }
4889 CHK("UNNECESSARY_PARENTHESES",
4890 "Unnecessary parentheses around '$match'\n" . $herectx);
4891 }
4892 }
4893
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004894#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004895 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004896 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004897 if (WARN("INDENTED_LABEL",
4898 "labels should not be indented\n" . $herecurr) &&
4899 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004900 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004901 s/^(.)\s+/$1/;
4902 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004903 }
4904
Joe Perches5b9553a2014-04-03 14:49:21 -07004905# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004906 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004907 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004908 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004909 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4910 my $value = $1;
4911 $value = deparenthesize($value);
4912 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4913 ERROR("RETURN_PARENTHESES",
4914 "return is not a function, parentheses are not required\n" . $herecurr);
4915 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004916 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004917 ERROR("SPACING",
4918 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004919 }
4920 }
Joe Perches507e5142013-11-12 15:10:13 -08004921
Joe Perchesb43ae212014-06-23 13:22:07 -07004922# unnecessary return in a void function
4923# at end-of-function, with the previous line a single leading tab, then return;
4924# and the line before that not a goto label target like "out:"
4925 if ($sline =~ /^[ \+]}\s*$/ &&
4926 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4927 $linenr >= 3 &&
4928 $lines[$linenr - 3] =~ /^[ +]/ &&
4929 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004930 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004931 "void function return statements are not generally useful\n" . $hereprev);
4932 }
Joe Perches9819cf22014-06-04 16:12:09 -07004933
Joe Perches189248d2014-01-23 15:54:47 -08004934# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004935 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004936 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4937 my $openparens = $1;
4938 my $count = $openparens =~ tr@\(@\(@;
4939 my $msg = "";
4940 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4941 my $comp = $4; #Not $1 because of $LvalOrFunc
4942 $msg = " - maybe == should be = ?" if ($comp eq "==");
4943 WARN("UNNECESSARY_PARENTHESES",
4944 "Unnecessary parentheses$msg\n" . $herecurr);
4945 }
4946 }
4947
Joe Perchesc5595fa2015-09-09 15:37:58 -07004948# comparisons with a constant or upper case identifier on the left
4949# avoid cases like "foo + BAR < baz"
4950# only fix matches surrounded by parentheses to avoid incorrect
4951# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004952 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004953 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4954 my $lead = $1;
4955 my $const = $2;
4956 my $comp = $3;
4957 my $to = $4;
4958 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004959 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004960 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4961 WARN("CONSTANT_COMPARISON",
4962 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4963 $fix) {
4964 if ($comp eq "<") {
4965 $newcomp = ">";
4966 } elsif ($comp eq "<=") {
4967 $newcomp = ">=";
4968 } elsif ($comp eq ">") {
4969 $newcomp = "<";
4970 } elsif ($comp eq ">=") {
4971 $newcomp = "<=";
4972 }
4973 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4974 }
4975 }
4976
Joe Perchesf34e4a42015-04-16 12:44:19 -07004977# Return of what appears to be an errno should normally be negative
4978 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004979 my $name = $1;
4980 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004981 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004982 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004983 }
4984 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004985
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004986# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004987 if ($line =~ /\b(if|while|for|switch)\(/) {
4988 if (ERROR("SPACING",
4989 "space required before the open parenthesis '('\n" . $herecurr) &&
4990 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004991 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004992 s/\b(if|while|for|switch)\(/$1 \(/;
4993 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004994 }
4995
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004996# Check for illegal assignment in if conditional -- and check for trailing
4997# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004998 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004999 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5000 ctx_statement_block($linenr, $realcnt, 0)
5001 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005002 my ($stat_next) = ctx_statement_block($line_nr_next,
5003 $remain_next, $off_next);
5004 $stat_next =~ s/\n./\n /g;
5005 ##print "stat<$stat> stat_next<$stat_next>\n";
5006
5007 if ($stat_next =~ /^\s*while\b/) {
5008 # If the statement carries leading newlines,
5009 # then count those as offsets.
5010 my ($whitespace) =
5011 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5012 my $offset =
5013 statement_rawlines($whitespace) - 1;
5014
5015 $suppress_whiletrailers{$line_nr_next +
5016 $offset} = 1;
5017 }
5018 }
5019 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005020 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005021 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005022 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005023
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005024 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches65b64b32020-08-11 18:35:10 -07005025 if (ERROR("ASSIGN_IN_IF",
5026 "do not use assignment in if condition\n" . $herecurr) &&
5027 $fix && $perl_version_ok) {
5028 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5029 my $space = $1;
5030 my $not = $2;
5031 my $statement = $3;
5032 my $assigned = $4;
5033 my $test = $8;
5034 my $against = $9;
5035 my $brace = $15;
5036 fix_delete_line($fixlinenr, $rawline);
5037 fix_insert_line($fixlinenr, "$space$statement;");
5038 my $newline = "${space}if (";
5039 $newline .= '!' if defined($not);
5040 $newline .= '(' if (defined $not && defined($test) && defined($against));
5041 $newline .= "$assigned";
5042 $newline .= " $test $against" if (defined($test) && defined($against));
5043 $newline .= ')' if (defined $not && defined($test) && defined($against));
5044 $newline .= ')';
5045 $newline .= " {" if (defined($brace));
5046 fix_insert_line($fixlinenr + 1, $newline);
5047 }
5048 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005049 }
5050
5051 # Find out what is on the end of the line after the
5052 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005053 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005054 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005055 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005056 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5057 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005058 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005059 # Find out how long the conditional actually is.
5060 my @newlines = ($c =~ /\n/gs);
5061 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005062 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005063
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005064 $stat_real = raw_line($linenr, $cond_lines)
5065 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005066 if (defined($stat_real) && $cond_lines > 1) {
5067 $stat_real = "[...]\n$stat_real";
5068 }
5069
Joe Perches000d1cc12011-07-25 17:13:25 -07005070 ERROR("TRAILING_STATEMENTS",
5071 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005072 }
5073 }
5074
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005075# Check for bitwise tests written as boolean
5076 if ($line =~ /
5077 (?:
5078 (?:\[|\(|\&\&|\|\|)
5079 \s*0[xX][0-9]+\s*
5080 (?:\&\&|\|\|)
5081 |
5082 (?:\&\&|\|\|)
5083 \s*0[xX][0-9]+\s*
5084 (?:\&\&|\|\||\)|\])
5085 )/x)
5086 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005087 WARN("HEXADECIMAL_BOOLEAN_TEST",
5088 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005089 }
5090
Andy Whitcroft8905a672007-11-28 16:21:06 -08005091# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005092 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5093 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005094 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005095 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005096 ERROR("TRAILING_STATEMENTS",
5097 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005098 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005099 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005100# if should not continue a brace
5101 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005102 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005103 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005104 $herecurr);
5105 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005106# case and default should not have general statements after them
5107 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5108 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005109 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005110 \s*return\s+
5111 )/xg)
5112 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005113 ERROR("TRAILING_STATEMENTS",
5114 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005115 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005116
5117 # Check for }<nl>else {, these must be at the same
5118 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005119 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5120 $previndent == $indent) {
5121 if (ERROR("ELSE_AFTER_BRACE",
5122 "else should follow close brace '}'\n" . $hereprev) &&
5123 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5124 fix_delete_line($fixlinenr - 1, $prevrawline);
5125 fix_delete_line($fixlinenr, $rawline);
5126 my $fixedline = $prevrawline;
5127 $fixedline =~ s/}\s*$//;
5128 if ($fixedline !~ /^\+\s*$/) {
5129 fix_insert_line($fixlinenr, $fixedline);
5130 }
5131 $fixedline = $rawline;
5132 $fixedline =~ s/^(.\s*)else/$1} else/;
5133 fix_insert_line($fixlinenr, $fixedline);
5134 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005135 }
5136
Joe Perches8b8856f2014-08-06 16:11:14 -07005137 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5138 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005139 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5140
5141 # Find out what is on the end of the line after the
5142 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005143 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005144 $s =~ s/\n.*//g;
5145
5146 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005147 if (ERROR("WHILE_AFTER_BRACE",
5148 "while should follow close brace '}'\n" . $hereprev) &&
5149 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5150 fix_delete_line($fixlinenr - 1, $prevrawline);
5151 fix_delete_line($fixlinenr, $rawline);
5152 my $fixedline = $prevrawline;
5153 my $trailing = $rawline;
5154 $trailing =~ s/^\+//;
5155 $trailing = trim($trailing);
5156 $fixedline =~ s/}\s*$/} $trailing/;
5157 fix_insert_line($fixlinenr, $fixedline);
5158 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005159 }
5160 }
5161
Joe Perches95e2c602013-07-03 15:05:20 -07005162#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005163 while ($line =~ m{($Constant|$Lval)}g) {
5164 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005165
Joe Perches95e2c602013-07-03 15:05:20 -07005166#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005167 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005168 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005169#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005170 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005171#Ignore SI style variants like nS, mV and dB
5172#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5173 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005174#Ignore some three character SI units explicitly, like MiB and KHz
5175 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005176 while ($var =~ m{($Ident)}g) {
5177 my $word = $1;
5178 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005179 if ($check) {
5180 seed_camelcase_includes();
5181 if (!$file && !$camelcase_file_seeded) {
5182 seed_camelcase_file($realfile);
5183 $camelcase_file_seeded = 1;
5184 }
5185 }
Joe Perches7e781f62013-09-11 14:23:55 -07005186 if (!defined $camelcase{$word}) {
5187 $camelcase{$word} = 1;
5188 CHK("CAMELCASE",
5189 "Avoid CamelCase: <$word>\n" . $herecurr);
5190 }
Joe Perches34456862013-07-03 15:05:34 -07005191 }
Joe Perches323c1262012-12-17 16:02:07 -08005192 }
5193 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005194
5195#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005196 if ($line =~ /\#\s*define.*\\\s+$/) {
5197 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5198 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5199 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005200 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005201 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005202 }
5203
Fabian Frederick0e212e02015-04-16 12:44:25 -07005204# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5205# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005206 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005207 my $file = "$1.h";
5208 my $checkfile = "include/linux/$file";
5209 if (-f "$root/$checkfile" &&
5210 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005211 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005212 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005213 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5214 if ($asminclude > 0) {
5215 if ($realfile =~ m{^arch/}) {
5216 CHK("ARCH_INCLUDE_LINUX",
5217 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5218 } else {
5219 WARN("INCLUDE_LINUX",
5220 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5221 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005222 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005223 }
5224 }
5225
Andy Whitcroft653d4872007-06-23 17:16:34 -07005226# multi-statement macros should be enclosed in a do while loop, grab the
5227# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005228# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005229 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5230 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005231 my $ln = $linenr;
5232 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005233 my ($off, $dstat, $dcond, $rest);
5234 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005235 my $has_flow_statement = 0;
5236 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005237 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005238 ctx_statement_block($linenr, $realcnt, 0);
5239 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005240 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005241 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005242
Joe Perches08a28432014-10-13 15:51:55 -07005243 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005244 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005245
Joe Perchesf59b64b2016-10-11 13:52:08 -07005246 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5247 my $define_args = $1;
5248 my $define_stmt = $dstat;
5249 my @def_args = ();
5250
5251 if (defined $define_args && $define_args ne "") {
5252 $define_args = substr($define_args, 1, length($define_args) - 2);
5253 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005254 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005255 @def_args = split(",", $define_args);
5256 }
5257
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005258 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005259 $dstat =~ s/\\\n.//g;
5260 $dstat =~ s/^\s*//s;
5261 $dstat =~ s/\s*$//s;
5262
5263 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005264 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5265 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005266 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005267 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005268 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005269
Antonio Borneo342d3d22020-04-06 20:11:01 -07005270 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005271 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5272 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005273 {
5274 }
5275
Joe Perches42e15292016-03-15 14:58:01 -07005276 # Make asm volatile uses seem like a generic function
5277 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5278
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005279 my $exceptions = qr{
5280 $Declare|
5281 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005282 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005283 DECLARE_PER_CPU|
5284 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005285 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005286 union|
5287 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005288 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005289 ^\"|\"$|
5290 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005291 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005292 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005293
5294 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005295 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005296 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005297
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005298 if ($dstat ne '' &&
5299 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5300 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005301 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005302 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005303 $dstat !~ /$exceptions/ &&
5304 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005305 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005306 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005307 $dstat !~ /^for\s*$Constant$/ && # for (...)
5308 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5309 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005310 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005311 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005312 {
Joe Perchese7955562017-05-08 15:55:48 -07005313 if ($dstat =~ /^\s*if\b/) {
5314 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5315 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5316 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005317 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5318 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5319 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005320 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005321 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005322 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005323
5324 }
Joe Perches52076492016-10-11 13:52:14 -07005325
5326 # Make $define_stmt single line, comment-free, etc
5327 my @stmt_array = split('\n', $define_stmt);
5328 my $first = 1;
5329 $define_stmt = "";
5330 foreach my $l (@stmt_array) {
5331 $l =~ s/\\$//;
5332 if ($first) {
5333 $define_stmt = $l;
5334 $first = 0;
5335 } elsif ($l =~ /^[\+ ]/) {
5336 $define_stmt .= substr($l, 1);
5337 }
5338 }
5339 $define_stmt =~ s/$;//g;
5340 $define_stmt =~ s/\s+/ /g;
5341 $define_stmt = trim($define_stmt);
5342
Joe Perchesf59b64b2016-10-11 13:52:08 -07005343# check if any macro arguments are reused (ignore '...' and 'type')
5344 foreach my $arg (@def_args) {
5345 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005346 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005347 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005348 $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 -07005349 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5350 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005351 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005352 if ($use_cnt > 1) {
5353 CHK("MACRO_ARG_REUSE",
5354 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005355 }
5356# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005357 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005358 ((defined($1) && $1 ne ',') ||
5359 (defined($2) && $2 ne ','))) {
5360 CHK("MACRO_ARG_PRECEDENCE",
5361 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005362 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005363 }
Joe Perches5023d342012-12-17 16:01:47 -08005364
Joe Perches08a28432014-10-13 15:51:55 -07005365# check for macros with flow control, but without ## concatenation
5366# ## concatenation is commonly a macro that defines a function so ignore those
5367 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005368 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005369 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005370
Joe Perches08a28432014-10-13 15:51:55 -07005371 WARN("MACRO_WITH_FLOW_CONTROL",
5372 "Macros with flow control statements should be avoided\n" . "$herectx");
5373 }
5374
Joe Perches481eb482012-12-17 16:01:56 -08005375# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005376
5377 } else {
5378 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005379 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5380 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005381 $line =~ /^\+.*\\$/) {
5382 WARN("LINE_CONTINUATIONS",
5383 "Avoid unnecessary line continuations\n" . $herecurr);
5384 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005385 }
5386
Joe Perchesb13edf72012-07-30 14:41:24 -07005387# do {} while (0) macro tests:
5388# single-statement macros do not need to be enclosed in do while (0) loop,
5389# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005390 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005391 $realfile !~ m@/vmlinux.lds.h$@ &&
5392 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5393 my $ln = $linenr;
5394 my $cnt = $realcnt;
5395 my ($off, $dstat, $dcond, $rest);
5396 my $ctx = '';
5397 ($dstat, $dcond, $ln, $cnt, $off) =
5398 ctx_statement_block($linenr, $realcnt, 0);
5399 $ctx = $dstat;
5400
5401 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005402 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005403
5404 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5405 my $stmts = $2;
5406 my $semis = $3;
5407
5408 $ctx =~ s/\n*$//;
5409 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005410 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005411
Joe Perchesac8e97f2012-08-21 16:15:53 -07005412 if (($stmts =~ tr/;/;/) == 1 &&
5413 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005414 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5415 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5416 }
5417 if (defined $semis && $semis ne "") {
5418 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5419 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5420 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005421 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5422 $ctx =~ s/\n*$//;
5423 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005424 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005425
5426 WARN("TRAILING_SEMICOLON",
5427 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005428 }
5429 }
5430
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005431# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005432 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5433 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005434 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005435 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005436 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5437 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005438 my @allowed = ();
5439 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005440 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005441 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005442 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005443 for my $chunk (@chunks) {
5444 my ($cond, $block) = @{$chunk};
5445
Andy Whitcroft773647a2008-03-28 14:15:58 -07005446 # If the condition carries leading newlines, then count those as offsets.
5447 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5448 my $offset = statement_rawlines($whitespace) - 1;
5449
Joe Perchesaad4f612012-03-23 15:02:19 -07005450 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005451 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5452
5453 # We have looked at and allowed this specific line.
5454 $suppress_ifbraces{$ln + $offset} = 1;
5455
5456 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005457 $ln += statement_rawlines($block) - 1;
5458
Andy Whitcroft773647a2008-03-28 14:15:58 -07005459 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005460
5461 $seen++ if ($block =~ /^\s*{/);
5462
Joe Perchesaad4f612012-03-23 15:02:19 -07005463 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005464 if (statement_lines($cond) > 1) {
5465 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005466 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005467 }
5468 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005469 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005470 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005471 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005472 if (statement_block_size($block) > 1) {
5473 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005474 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005475 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005476 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005477 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005478 if ($seen) {
5479 my $sum_allowed = 0;
5480 foreach (@allowed) {
5481 $sum_allowed += $_;
5482 }
5483 if ($sum_allowed == 0) {
5484 WARN("BRACES",
5485 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5486 } elsif ($sum_allowed != $allow &&
5487 $seen != $allow) {
5488 CHK("BRACES",
5489 "braces {} should be used on all arms of this statement\n" . $herectx);
5490 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005491 }
5492 }
5493 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005494 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005495 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005496 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005497
Andy Whitcroftcf655042008-03-04 14:28:20 -08005498 # Check the pre-context.
5499 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5500 #print "APW: ALLOWED: pre<$1>\n";
5501 $allowed = 1;
5502 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005503
5504 my ($level, $endln, @chunks) =
5505 ctx_statement_full($linenr, $realcnt, $-[0]);
5506
Andy Whitcroftcf655042008-03-04 14:28:20 -08005507 # Check the condition.
5508 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005509 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005510 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005511 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005512 }
5513 if (statement_lines($cond) > 1) {
5514 #print "APW: ALLOWED: cond<$cond>\n";
5515 $allowed = 1;
5516 }
5517 if ($block =~/\b(?:if|for|while)\b/) {
5518 #print "APW: ALLOWED: block<$block>\n";
5519 $allowed = 1;
5520 }
5521 if (statement_block_size($block) > 1) {
5522 #print "APW: ALLOWED: lines block<$block>\n";
5523 $allowed = 1;
5524 }
5525 # Check the post-context.
5526 if (defined $chunks[1]) {
5527 my ($cond, $block) = @{$chunks[1]};
5528 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005529 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005530 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005531 if ($block =~ /^\s*\{/) {
5532 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5533 $allowed = 1;
5534 }
5535 }
5536 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005537 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005538 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005539
Joe Perches000d1cc12011-07-25 17:13:25 -07005540 WARN("BRACES",
5541 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005542 }
5543 }
5544
Joe Perchese4c5bab2017-02-24 15:01:41 -08005545# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005546 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5547 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005548 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5549 }
5550
Joe Perches0979ae62012-12-17 16:01:59 -08005551# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005552 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005553 if (CHK("BRACES",
5554 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5555 $fix && $prevrawline =~ /^\+/) {
5556 fix_delete_line($fixlinenr - 1, $prevrawline);
5557 }
Joe Perches0979ae62012-12-17 16:01:59 -08005558 }
Joe Perches77b9a532013-07-03 15:05:29 -07005559 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005560 if (CHK("BRACES",
5561 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5562 $fix) {
5563 fix_delete_line($fixlinenr, $rawline);
5564 }
Joe Perches0979ae62012-12-17 16:01:59 -08005565 }
5566
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005567# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005568 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5569 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005570 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005571 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005572 }
5573
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005574# Check for user-visible strings broken across lines, which breaks the ability
5575# to grep for the string. Make exceptions when the previous string ends in a
5576# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5577# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005578 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005579 $prevline =~ /"\s*$/ &&
5580 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5581 if (WARN("SPLIT_STRING",
5582 "quoted string split across lines\n" . $hereprev) &&
5583 $fix &&
5584 $prevrawline =~ /^\+.*"\s*$/ &&
5585 $last_coalesced_string_linenr != $linenr - 1) {
5586 my $extracted_string = get_quoted_string($line, $rawline);
5587 my $comma_close = "";
5588 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5589 $comma_close = $1;
5590 }
5591
5592 fix_delete_line($fixlinenr - 1, $prevrawline);
5593 fix_delete_line($fixlinenr, $rawline);
5594 my $fixedline = $prevrawline;
5595 $fixedline =~ s/"\s*$//;
5596 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5597 fix_insert_line($fixlinenr - 1, $fixedline);
5598 $fixedline = $rawline;
5599 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5600 if ($fixedline !~ /\+\s*$/) {
5601 fix_insert_line($fixlinenr, $fixedline);
5602 }
5603 $last_coalesced_string_linenr = $linenr;
5604 }
5605 }
5606
5607# check for missing a space in a string concatenation
5608 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5609 WARN('MISSING_SPACE',
5610 "break quoted strings at a space character\n" . $hereprev);
5611 }
5612
Joe Perchese4b7d302017-05-08 15:55:51 -07005613# check for an embedded function name in a string when the function is known
5614# This does not work very well for -f --file checking as it depends on patch
5615# context providing the function name or a single line form for in-file
5616# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005617 if ($line =~ /^\+.*$String/ &&
5618 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005619 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5620 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005621 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005622 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005623 }
5624
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005625# check for spaces before a quoted newline
5626 if ($rawline =~ /^.*\".*\s\\n/) {
5627 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5628 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5629 $fix) {
5630 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5631 }
5632
5633 }
5634
Joe Perchesf17dba42014-10-13 15:51:51 -07005635# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005636 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5637 if (CHK("CONCATENATED_STRING",
5638 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5639 $fix) {
5640 while ($line =~ /($String)/g) {
5641 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5642 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5643 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5644 }
5645 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005646 }
5647
Joe Perches90ad30e2014-12-10 15:51:59 -08005648# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005649 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005650 if (WARN("STRING_FRAGMENTS",
5651 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5652 $fix) {
5653 while ($line =~ /($String)(?=\s*")/g) {
5654 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5655 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5656 }
5657 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005658 }
5659
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005660# check for non-standard and hex prefixed decimal printf formats
5661 my $show_L = 1; #don't show the same defect twice
5662 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005663 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005664 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005665 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005666 # check for %L
5667 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005668 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005669 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5670 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005671 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005672 # check for %Z
5673 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5674 WARN("PRINTF_Z",
5675 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5676 $show_Z = 0;
5677 }
5678 # check for 0x<decimal>
5679 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5680 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005681 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5682 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005683 }
5684
5685# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005686 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005687 WARN("LINE_CONTINUATIONS",
5688 "Avoid line continuations in quoted strings\n" . $herecurr);
5689 }
5690
Andy Whitcroft00df3442007-06-08 13:47:06 -07005691# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005692 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005693 WARN("IF_0",
5694 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5695 }
5696
5697# warn about #if 1
5698 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5699 WARN("IF_1",
5700 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005701 }
5702
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005703# check for needless "if (<foo>) fn(<foo>)" uses
5704 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005705 my $tested = quotemeta($1);
5706 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5707 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5708 my $func = $1;
5709 if (WARN('NEEDLESS_IF',
5710 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5711 $fix) {
5712 my $do_fix = 1;
5713 my $leading_tabs = "";
5714 my $new_leading_tabs = "";
5715 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5716 $leading_tabs = $1;
5717 } else {
5718 $do_fix = 0;
5719 }
5720 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5721 $new_leading_tabs = $1;
5722 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5723 $do_fix = 0;
5724 }
5725 } else {
5726 $do_fix = 0;
5727 }
5728 if ($do_fix) {
5729 fix_delete_line($fixlinenr - 1, $prevrawline);
5730 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5731 }
5732 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005733 }
5734 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005735
Joe Perchesebfdc402014-08-06 16:10:27 -07005736# check for unnecessary "Out of Memory" messages
5737 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5738 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5739 (defined $1 || defined $3) &&
5740 $linenr > 3) {
5741 my $testval = $2;
5742 my $testline = $lines[$linenr - 3];
5743
5744 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5745# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5746
Joe Perchese29a70f2019-03-07 16:28:35 -08005747 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5748 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005749 WARN("OOM_MESSAGE",
5750 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5751 }
5752 }
5753
Joe Perchesf78d98f2014-10-13 15:52:01 -07005754# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005755 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005756 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5757 my $level = $1;
5758 if (WARN("UNNECESSARY_KERN_LEVEL",
5759 "Possible unnecessary $level\n" . $herecurr) &&
5760 $fix) {
5761 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5762 }
5763 }
5764
Joe Perches45c55e92017-02-24 15:01:31 -08005765# check for logging continuations
5766 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5767 WARN("LOGGING_CONTINUATION",
5768 "Avoid logging continuation uses where feasible\n" . $herecurr);
5769 }
5770
Joe Perchesabb08a52014-12-10 15:51:46 -08005771# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005772 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005773 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5774 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5775 WARN("MASK_THEN_SHIFT",
5776 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5777 }
5778
Joe Perchesb75ac612014-12-10 15:52:02 -08005779# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005780 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005781 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5782 my $val = $1;
5783 my $equal = "!";
5784 $equal = "" if ($4 eq "!=");
5785 if (CHK("COMPARISON_TO_NULL",
5786 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5787 $fix) {
5788 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5789 }
5790 }
5791 }
5792
Joe Perches8716de32013-09-11 14:24:05 -07005793# check for bad placement of section $InitAttribute (e.g.: __initdata)
5794 if ($line =~ /(\b$InitAttribute\b)/) {
5795 my $attr = $1;
5796 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5797 my $ptr = $1;
5798 my $var = $2;
5799 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5800 ERROR("MISPLACED_INIT",
5801 "$attr should be placed after $var\n" . $herecurr)) ||
5802 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5803 WARN("MISPLACED_INIT",
5804 "$attr should be placed after $var\n" . $herecurr))) &&
5805 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005806 $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 -07005807 }
5808 }
5809 }
5810
Joe Perchese970b8842013-11-12 15:10:10 -08005811# check for $InitAttributeData (ie: __initdata) with const
5812 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5813 my $attr = $1;
5814 $attr =~ /($InitAttributePrefix)(.*)/;
5815 my $attr_prefix = $1;
5816 my $attr_type = $2;
5817 if (ERROR("INIT_ATTRIBUTE",
5818 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5819 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005820 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005821 s/$InitAttributeData/${attr_prefix}initconst/;
5822 }
5823 }
5824
5825# check for $InitAttributeConst (ie: __initconst) without const
5826 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5827 my $attr = $1;
5828 if (ERROR("INIT_ATTRIBUTE",
5829 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5830 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005831 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005832 /(^\+\s*(?:static\s+))/;
5833 $lead = rtrim($1);
5834 $lead = "$lead " if ($lead !~ /^\+$/);
5835 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005836 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005837 }
5838 }
5839
Joe Perchesc17893c2015-04-16 12:44:42 -07005840# check for __read_mostly with const non-pointer (should just be const)
5841 if ($line =~ /\b__read_mostly\b/ &&
5842 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5843 if (ERROR("CONST_READ_MOSTLY",
5844 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5845 $fix) {
5846 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5847 }
5848 }
5849
Joe Perchesfbdb8132014-04-03 14:49:14 -07005850# don't use __constant_<foo> functions outside of include/uapi/
5851 if ($realfile !~ m@^include/uapi/@ &&
5852 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5853 my $constant_func = $1;
5854 my $func = $constant_func;
5855 $func =~ s/^__constant_//;
5856 if (WARN("CONSTANT_CONVERSION",
5857 "$constant_func should be $func\n" . $herecurr) &&
5858 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005859 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005860 }
5861 }
5862
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005863# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005864 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005865 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005866 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005867 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005868 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005869 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07005870 }
5871 if ($delay > 2000) {
5872 WARN("LONG_UDELAY",
5873 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005874 }
5875 }
5876
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005877# warn about unexpectedly long msleep's
5878 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5879 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005880 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005881 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005882 }
5883 }
5884
Joe Perches36ec1932013-07-03 15:05:25 -07005885# check for comparisons of jiffies
5886 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5887 WARN("JIFFIES_COMPARISON",
5888 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5889 }
5890
Joe Perches9d7a34a2013-07-03 15:05:26 -07005891# check for comparisons of get_jiffies_64()
5892 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5893 WARN("JIFFIES_COMPARISON",
5894 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5895 }
5896
Andy Whitcroft00df3442007-06-08 13:47:06 -07005897# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005898# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005899# print "#ifdef in C files should be avoided\n";
5900# print "$herecurr";
5901# $clean = 0;
5902# }
5903
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005904# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005905 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005906 if (ERROR("SPACING",
5907 "exactly one space required after that #$1\n" . $herecurr) &&
5908 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005909 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005910 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5911 }
5912
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005913 }
5914
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005915# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005916 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5917 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005918 my $which = $1;
5919 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005920 CHK("UNCOMMENTED_DEFINITION",
5921 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005922 }
5923 }
5924# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005925
5926 my $barriers = qr{
5927 mb|
5928 rmb|
Will Deaconad83ec62019-11-07 14:49:00 +00005929 wmb
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005930 }x;
5931 my $barrier_stems = qr{
5932 mb__before_atomic|
5933 mb__after_atomic|
5934 store_release|
5935 load_acquire|
5936 store_mb|
5937 (?:$barriers)
5938 }x;
5939 my $all_barriers = qr{
5940 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005941 smp_(?:$barrier_stems)|
5942 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005943 }x;
5944
5945 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005946 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005947 WARN("MEMORY_BARRIER",
5948 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005949 }
5950 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005951
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005952 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5953
5954 if ($realfile !~ m@^include/asm-generic/@ &&
5955 $realfile !~ m@/barrier\.h$@ &&
5956 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5957 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5958 WARN("MEMORY_BARRIER",
5959 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5960 }
5961
Joe Perchescb426e92015-06-25 15:02:46 -07005962# check for waitqueue_active without a comment.
5963 if ($line =~ /\bwaitqueue_active\s*\(/) {
5964 if (!ctx_has_comment($first_line, $linenr)) {
5965 WARN("WAITQUEUE_ACTIVE",
5966 "waitqueue_active without comment\n" . $herecurr);
5967 }
5968 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005969
Marco Elver5099a722020-04-01 12:17:14 +02005970# check for data_race without a comment.
5971 if ($line =~ /\bdata_race\s*\(/) {
5972 if (!ctx_has_comment($first_line, $linenr)) {
5973 WARN("DATA_RACE",
5974 "data_race without comment\n" . $herecurr);
5975 }
5976 }
5977
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005978# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005979 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005980 CHK("ARCH_DEFINES",
5981 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005982 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005983
Joe Perches596ed452017-07-12 14:37:02 -07005984# check that the storage class is not after a type
5985 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005986 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005987 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5988 }
5989# Check that the storage class is at the beginning of a declaration
5990 if ($line =~ /\b$Storage\b/ &&
5991 $line !~ /^.\s*$Storage/ &&
5992 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5993 $1 !~ /[\,\)]\s*$/) {
5994 WARN("STORAGE_CLASS",
5995 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005996 }
5997
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005998# check the location of the inline attribute, that it is between
5999# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006000 if ($line =~ /\b$Type\s+$Inline\b/ ||
6001 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006002 ERROR("INLINE_LOCATION",
6003 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006004 }
6005
Andy Whitcroft8905a672007-11-28 16:21:06 -08006006# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08006007 if ($realfile !~ m@\binclude/uapi/@ &&
6008 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006009 if (WARN("INLINE",
6010 "plain inline is preferred over $1\n" . $herecurr) &&
6011 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006012 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006013
6014 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006015 }
6016
Joe Perches3d130fd2011-01-12 17:00:00 -08006017# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08006018 if ($realfile !~ m@\binclude/uapi/@ &&
6019 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006020 WARN("PREFER_PACKED",
6021 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08006022 }
6023
Joe Perches39b7e282011-07-25 17:13:24 -07006024# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08006025 if ($realfile !~ m@\binclude/uapi/@ &&
6026 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006027 WARN("PREFER_ALIGNED",
6028 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07006029 }
6030
Joe Perches462811d2019-09-25 16:46:44 -07006031# Check for __attribute__ section, prefer __section
6032 if ($realfile !~ m@\binclude/uapi/@ &&
6033 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
6034 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6035 my $new = substr($old, 1, -1);
6036 if (WARN("PREFER_SECTION",
6037 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6038 $fix) {
6039 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6040 }
6041 }
6042
Joe Perches5f14d3b2012-01-10 15:09:52 -08006043# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08006044 if ($realfile !~ m@\binclude/uapi/@ &&
6045 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006046 if (WARN("PREFER_PRINTF",
6047 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6048 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006049 $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 -07006050
6051 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08006052 }
6053
Joe Perches6061d942012-03-23 15:02:16 -07006054# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08006055 if ($realfile !~ m@\binclude/uapi/@ &&
6056 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006057 if (WARN("PREFER_SCANF",
6058 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6059 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006060 $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 -07006061 }
Joe Perches6061d942012-03-23 15:02:16 -07006062 }
6063
Joe Perches619a9082014-12-10 15:51:35 -08006064# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006065 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006066 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6067 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6068 $line =~ /\b__weak\b/)) {
6069 ERROR("WEAK_DECLARATION",
6070 "Using weak declarations can have unintended link defects\n" . $herecurr);
6071 }
6072
Tomas Winklerfd39f902016-12-12 16:46:34 -08006073# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006074 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006075 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006076 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6077 my $type = $1;
6078 if ($type =~ /\b($typeC99Typedefs)\b/) {
6079 $type = $1;
6080 my $kernel_type = 'u';
6081 $kernel_type = 's' if ($type =~ /^_*[si]/);
6082 $type =~ /(\d+)/;
6083 $kernel_type .= $1;
6084 if (CHK("PREFER_KERNEL_TYPES",
6085 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6086 $fix) {
6087 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6088 }
6089 }
6090 }
6091
Joe Perches938224b2016-01-20 14:59:15 -08006092# check for cast of C90 native int or longer types constants
6093 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6094 my $cast = $1;
6095 my $const = $2;
6096 if (WARN("TYPECAST_INT_CONSTANT",
6097 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6098 $fix) {
6099 my $suffix = "";
6100 my $newconst = $const;
6101 $newconst =~ s/${Int_type}$//;
6102 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6103 if ($cast =~ /\blong\s+long\b/) {
6104 $suffix .= 'LL';
6105 } elsif ($cast =~ /\blong\b/) {
6106 $suffix .= 'L';
6107 }
6108 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6109 }
6110 }
6111
Joe Perches8f53a9b2010-03-05 13:43:48 -08006112# check for sizeof(&)
6113 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006114 WARN("SIZEOF_ADDRESS",
6115 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006116 }
6117
Joe Perches66c80b62012-07-30 14:41:22 -07006118# check for sizeof without parenthesis
6119 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006120 if (WARN("SIZEOF_PARENTHESIS",
6121 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6122 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006123 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006124 }
Joe Perches66c80b62012-07-30 14:41:22 -07006125 }
6126
Joe Perches88982fe2012-12-17 16:02:00 -08006127# check for struct spinlock declarations
6128 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6129 WARN("USE_SPINLOCK_T",
6130 "struct spinlock should be spinlock_t\n" . $herecurr);
6131 }
6132
Joe Perchesa6962d72013-04-29 16:18:13 -07006133# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006134 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006135 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006136 $fmt =~ s/%%//g;
6137 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006138 if (WARN("PREFER_SEQ_PUTS",
6139 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6140 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006141 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006142 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006143 }
6144 }
6145
Joe Perches478b1792018-04-10 16:33:34 -07006146# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006147 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006148 defined $stat &&
6149 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6150 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006151 my $stat_real;
6152
Joe Perches0b523762017-05-08 15:55:36 -07006153 my $lc = $stat =~ tr@\n@@;
6154 $lc = $lc + $linenr;
6155 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006156 my $specifier;
6157 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006158 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006159 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006160 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6161 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006162
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006163 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006164 $specifier = $1;
6165 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006166 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006167 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006168 ($extension eq "f" &&
6169 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006170 $bad_specifier = $specifier;
6171 last;
6172 }
6173 if ($extension eq "x" && !defined($stat_real)) {
6174 if (!defined($stat_real)) {
6175 $stat_real = get_stat_real($linenr, $lc);
6176 }
6177 WARN("VSPRINTF_SPECIFIER_PX",
6178 "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");
6179 }
6180 }
6181 if ($bad_specifier ne "") {
6182 my $stat_real = get_stat_real($linenr, $lc);
6183 my $ext_type = "Invalid";
6184 my $use = "";
6185 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006186 $use = " - use %pS instead";
6187 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6188 }
6189
6190 WARN("VSPRINTF_POINTER_EXTENSION",
6191 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6192 }
Joe Perches0b523762017-05-08 15:55:36 -07006193 }
6194 }
6195
Andy Whitcroft554e1652012-01-10 15:09:57 -08006196# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006197 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006198 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006199 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006200
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006201 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006202 my $ms_val = $7;
6203 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006204
Andy Whitcroft554e1652012-01-10 15:09:57 -08006205 if ($ms_size =~ /^(0x|)0$/i) {
6206 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006207 "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 -08006208 } elsif ($ms_size =~ /^(0x|)1$/i) {
6209 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006210 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6211 }
6212 }
6213
Joe Perches98a9bba2014-01-23 15:54:52 -08006214# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006215# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006216# defined $stat &&
6217# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6218# if (WARN("PREFER_ETHER_ADDR_COPY",
6219# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6220# $fix) {
6221# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6222# }
6223# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006224
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006225# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006226# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006227# defined $stat &&
6228# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6229# WARN("PREFER_ETHER_ADDR_EQUAL",
6230# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6231# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006232
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006233# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6234# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006235# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006236# defined $stat &&
6237# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6238#
6239# my $ms_val = $7;
6240#
6241# if ($ms_val =~ /^(?:0x|)0+$/i) {
6242# if (WARN("PREFER_ETH_ZERO_ADDR",
6243# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6244# $fix) {
6245# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6246# }
6247# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6248# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6249# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6250# $fix) {
6251# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6252# }
6253# }
6254# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006255
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006256# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006257 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006258 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006259 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006260 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006261 my $call = $1;
6262 my $cast1 = deparenthesize($2);
6263 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006264 my $cast2 = deparenthesize($7);
6265 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006266 my $cast;
6267
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006268 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006269 $cast = "$cast1 or $cast2";
6270 } elsif ($cast1 ne "") {
6271 $cast = $cast1;
6272 } else {
6273 $cast = $cast2;
6274 }
6275 WARN("MINMAX",
6276 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006277 }
6278 }
6279
Joe Perches4a273192012-07-30 14:41:20 -07006280# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006281 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006282 defined $stat &&
6283 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6284 my $min = $1;
6285 my $max = $7;
6286 if ($min eq $max) {
6287 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006288 "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 -07006289 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6290 $min > $max) {
6291 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006292 "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 -07006293 }
6294 }
6295
Joe Perches823b7942013-11-12 15:10:15 -08006296# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006297 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006298 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006299 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006300 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6301 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6302 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6303 my $lc = $stat =~ tr@\n@@;
6304 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006305 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006306 WARN("NAKED_SSCANF",
6307 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6308 }
6309
Joe Perchesafc819a2014-06-04 16:12:08 -07006310# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006311 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006312 defined $stat &&
6313 $line =~ /\bsscanf\b/) {
6314 my $lc = $stat =~ tr@\n@@;
6315 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006316 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006317 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6318 my $format = $6;
6319 my $count = $format =~ tr@%@%@;
6320 if ($count == 1 &&
6321 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6322 WARN("SSCANF_TO_KSTRTO",
6323 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6324 }
6325 }
6326 }
6327
Joe Perches70dc8a42013-09-11 14:23:58 -07006328# check for new externs in .h files.
6329 if ($realfile =~ /\.h$/ &&
6330 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006331 if (CHK("AVOID_EXTERNS",
6332 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006333 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006334 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006335 }
6336 }
6337
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006338# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006339 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006340 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006341 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006342 my $function_name = $1;
6343 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006344
6345 my $s = $stat;
6346 if (defined $cond) {
6347 substr($s, 0, length($cond), '');
6348 }
Kees Cookd8b44b52020-06-03 14:18:09 -07006349 if ($s =~ /^\s*;/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006350 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006351 WARN("AVOID_EXTERNS",
6352 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006353 }
6354
6355 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006356 WARN("FUNCTION_ARGUMENTS",
6357 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006358 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006359
6360 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6361 $stat =~ /^.\s*extern\s+/)
6362 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006363 WARN("AVOID_EXTERNS",
6364 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006365 }
6366
Joe Perchesa0ad7592017-07-10 15:52:19 -07006367# check for function declarations that have arguments without identifier names
6368 if (defined $stat &&
Kees Cookd8b44b52020-06-03 14:18:09 -07006369 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6370 $1 ne "void") {
6371 my $args = trim($1);
Joe Perchesca0d8922016-10-11 13:52:16 -07006372 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6373 my $arg = trim($1);
Kees Cookd8b44b52020-06-03 14:18:09 -07006374 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006375 WARN("FUNCTION_ARGUMENTS",
6376 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6377 }
6378 }
6379 }
6380
Joe Perchesa0ad7592017-07-10 15:52:19 -07006381# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006382 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006383 defined $stat &&
6384 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6385 $context_function = $1;
6386
6387# check for multiline function definition with misplaced open brace
6388 my $ok = 0;
6389 my $cnt = statement_rawlines($stat);
6390 my $herectx = $here . "\n";
6391 for (my $n = 0; $n < $cnt; $n++) {
6392 my $rl = raw_line($linenr, $n);
6393 $herectx .= $rl . "\n";
6394 $ok = 1 if ($rl =~ /^[ \+]\{/);
6395 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6396 last if $rl =~ /^[ \+].*\{/;
6397 }
6398 if (!$ok) {
6399 ERROR("OPEN_BRACE",
6400 "open brace '{' following function definitions go on the next line\n" . $herectx);
6401 }
6402 }
6403
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006404# checks for new __setup's
6405 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6406 my $name = $1;
6407
6408 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006409 CHK("UNDOCUMENTED_SETUP",
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07006410 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006411 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006412 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006413
Joe Perchese29a70f2019-03-07 16:28:35 -08006414# check for pointless casting of alloc functions
6415 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006416 WARN("UNNECESSARY_CASTS",
6417 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006418 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006419
Joe Perchesa640d252013-07-03 15:05:21 -07006420# alloc style
6421# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006422 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006423 $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 -07006424 CHK("ALLOC_SIZEOF_STRUCT",
6425 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6426 }
6427
Joe Perches60a55362014-06-04 16:12:07 -07006428# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006429 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006430 defined $stat &&
6431 $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 -07006432 my $oldfunc = $3;
6433 my $a1 = $4;
6434 my $a2 = $10;
6435 my $newfunc = "kmalloc_array";
6436 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006437 my $r1 = $a1;
6438 my $r2 = $a2;
6439 if ($a1 =~ /^sizeof\s*\S/) {
6440 $r1 = $a2;
6441 $r2 = $a1;
6442 }
6443 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6444 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006445 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006446 my $herectx = get_stat_here($linenr, $cnt, $here);
6447
Joe Perches60a55362014-06-04 16:12:07 -07006448 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006449 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6450 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006451 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006452 $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 -07006453 }
6454 }
6455 }
6456
Joe Perches972fdea2013-04-29 16:18:12 -07006457# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006458 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006459 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6460 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006461 WARN("KREALLOC_ARG_REUSE",
6462 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6463 }
6464
Joe Perches5ce59ae2013-02-21 16:44:18 -08006465# check for alloc argument mismatch
6466 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6467 WARN("ALLOC_ARRAY_ARGS",
6468 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6469 }
6470
Joe Perchescaf2a542011-01-12 16:59:56 -08006471# check for multiple semicolons
6472 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006473 if (WARN("ONE_SEMICOLON",
6474 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6475 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006476 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006477 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006478 }
6479
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006480# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6481 if ($realfile !~ m@^include/uapi/@ &&
6482 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006483 my $ull = "";
6484 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6485 if (CHK("BIT_MACRO",
6486 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6487 $fix) {
6488 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6489 }
6490 }
6491
Joe Perches50161262020-08-11 18:35:07 -07006492# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
6493 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^CONFIG_/) {
6494 WARN("IS_ENABLED_CONFIG",
6495 "IS_ENABLED($1) is normally used as IS_ENABLED(CONFIG_$1)\n" . $herecurr);
6496 }
6497
Joe Perches2d632742016-05-20 17:04:00 -07006498# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6499 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6500 my $config = $1;
6501 if (WARN("PREFER_IS_ENABLED",
6502 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6503 $fix) {
6504 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6505 }
6506 }
6507
Joe Perchese81f2392014-08-06 16:11:25 -07006508# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006509 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6510 my $has_break = 0;
6511 my $has_statement = 0;
6512 my $count = 0;
6513 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006514 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006515 $prevline--;
6516 my $rline = $rawlines[$prevline - 1];
6517 my $fline = $lines[$prevline - 1];
6518 last if ($fline =~ /^\@\@/);
6519 next if ($fline =~ /^\-/);
6520 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6521 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6522 next if ($fline =~ /^.[\s$;]*$/);
6523 $has_statement = 1;
6524 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006525 $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 -08006526 }
6527 if (!$has_break && $has_statement) {
6528 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006529 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006530 }
6531 }
6532
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006533# check for /* fallthrough */ like comment, prefer fallthrough;
6534 my @fallthroughs = (
6535 'fallthrough',
6536 '@fallthrough@',
6537 'lint -fallthrough[ \t]*',
6538 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6539 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6540 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6541 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6542 );
6543 if ($raw_comment ne '') {
6544 foreach my $ft (@fallthroughs) {
6545 if ($raw_comment =~ /$ft/) {
6546 my $msg_level = \&WARN;
6547 $msg_level = \&CHK if ($file);
6548 &{$msg_level}("PREFER_FALLTHROUGH",
6549 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6550 last;
6551 }
6552 }
6553 }
6554
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006555# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006556 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006557 defined $stat &&
6558 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006559 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006560 my $herectx = get_stat_here($linenr, $cnt, $here);
6561
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006562 WARN("DEFAULT_NO_BREAK",
6563 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006564 }
6565
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006566# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006567 if ($line =~ /\b__FUNCTION__\b/) {
6568 if (WARN("USE_FUNC",
6569 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6570 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006571 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006572 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006573 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006574
Joe Perches62ec8182015-02-13 14:38:18 -08006575# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6576 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6577 ERROR("DATE_TIME",
6578 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6579 }
6580
Joe Perches2c924882012-03-23 15:02:20 -07006581# check for use of yield()
6582 if ($line =~ /\byield\s*\(\s*\)/) {
6583 WARN("YIELD",
6584 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6585 }
6586
Joe Perches179f8f42013-07-03 15:05:30 -07006587# check for comparisons against true and false
6588 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6589 my $lead = $1;
6590 my $arg = $2;
6591 my $test = $3;
6592 my $otype = $4;
6593 my $trail = $5;
6594 my $op = "!";
6595
6596 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6597
6598 my $type = lc($otype);
6599 if ($type =~ /^(?:true|false)$/) {
6600 if (("$test" eq "==" && "$type" eq "true") ||
6601 ("$test" eq "!=" && "$type" eq "false")) {
6602 $op = "";
6603 }
6604
6605 CHK("BOOL_COMPARISON",
6606 "Using comparison to $otype is error prone\n" . $herecurr);
6607
6608## maybe suggesting a correct construct would better
6609## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6610
6611 }
6612 }
6613
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006614# check for semaphores initialized locked
6615 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006616 WARN("CONSIDER_COMPLETION",
6617 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006618 }
Joe Perches6712d852012-03-23 15:02:20 -07006619
Joe Perches67d0a072011-10-31 17:13:10 -07006620# recommend kstrto* over simple_strto* and strict_strto*
6621 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006622 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006623 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006624 }
Joe Perches6712d852012-03-23 15:02:20 -07006625
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006626# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006627 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006628 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006629 "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 -07006630 }
Joe Perches6712d852012-03-23 15:02:20 -07006631
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006632# check for spin_is_locked(), suggest lockdep instead
6633 if ($line =~ /\bspin_is_locked\(/) {
6634 WARN("USE_LOCKDEP",
6635 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6636 }
6637
Joe Perches9189c7e2018-09-07 15:26:18 -07006638# check for deprecated apis
6639 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6640 my $deprecated_api = $1;
6641 my $new_api = $deprecated_apis{$deprecated_api};
6642 WARN("DEPRECATED_API",
6643 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6644 }
6645
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006646# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006647# and avoid what seem like struct definitions 'struct foo {'
Quentin Monnetced69da12020-08-11 18:35:13 -07006648 if (defined($const_structs) &&
6649 $line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006650 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006651 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006652 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006653 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006654
6655# use of NR_CPUS is usually wrong
6656# ignore definitions of NR_CPUS and usage to define arrays as likely right
6657 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006658 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6659 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006660 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6661 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6662 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006663 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006664 WARN("NR_CPUS",
6665 "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 -07006666 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006667
Joe Perches52ea8502013-11-12 15:10:09 -08006668# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6669 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6670 ERROR("DEFINE_ARCH_HAS",
6671 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6672 }
6673
Joe Perchesacd93622015-02-13 14:38:38 -08006674# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006675 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006676 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6677 WARN("LIKELY_MISUSE",
6678 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6679 }
6680
Denis Efremovde3f1862019-09-25 16:49:25 -07006681# nested likely/unlikely calls
6682 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6683 WARN("LIKELY_MISUSE",
6684 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6685 }
6686
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006687# whine mightly about in_atomic
6688 if ($line =~ /\bin_atomic\s*\(/) {
6689 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006690 ERROR("IN_ATOMIC",
6691 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006692 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006693 WARN("IN_ATOMIC",
6694 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006695 }
6696 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006697
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006698# check for mutex_trylock_recursive usage
6699 if ($line =~ /mutex_trylock_recursive/) {
6700 ERROR("LOCKING",
6701 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6702 }
6703
Peter Zijlstra1704f472010-03-19 01:37:42 +01006704# check for lockdep_set_novalidate_class
6705 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6706 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6707 if ($realfile !~ m@^kernel/lockdep@ &&
6708 $realfile !~ m@^include/linux/lockdep@ &&
6709 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006710 ERROR("LOCKDEP",
6711 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006712 }
6713 }
Dave Jones88f88312011-01-12 16:59:59 -08006714
Joe Perchesb392c642015-04-16 12:44:16 -07006715 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6716 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006717 WARN("EXPORTED_WORLD_WRITABLE",
6718 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006719 }
Joe Perches24358802014-04-03 14:49:13 -07006720
Joe Perches00180462018-02-06 15:38:55 -08006721# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6722# and whether or not function naming is typical and if
6723# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006724 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006725 defined $stat &&
6726 $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*\)/) {
6727 my $var = $1;
6728 my $perms = $2;
6729 my $show = $3;
6730 my $store = $4;
6731 my $octal_perms = perms_to_octal($perms);
6732 if ($show =~ /^${var}_show$/ &&
6733 $store =~ /^${var}_store$/ &&
6734 $octal_perms eq "0644") {
6735 if (WARN("DEVICE_ATTR_RW",
6736 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6737 $fix) {
6738 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6739 }
6740 } elsif ($show =~ /^${var}_show$/ &&
6741 $store =~ /^NULL$/ &&
6742 $octal_perms eq "0444") {
6743 if (WARN("DEVICE_ATTR_RO",
6744 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6745 $fix) {
6746 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6747 }
6748 } elsif ($show =~ /^NULL$/ &&
6749 $store =~ /^${var}_store$/ &&
6750 $octal_perms eq "0200") {
6751 if (WARN("DEVICE_ATTR_WO",
6752 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6753 $fix) {
6754 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6755 }
6756 } elsif ($octal_perms eq "0644" ||
6757 $octal_perms eq "0444" ||
6758 $octal_perms eq "0200") {
6759 my $newshow = "$show";
6760 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6761 my $newstore = $store;
6762 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6763 my $rename = "";
6764 if ($show ne $newshow) {
6765 $rename .= " '$show' to '$newshow'";
6766 }
6767 if ($store ne $newstore) {
6768 $rename .= " '$store' to '$newstore'";
6769 }
6770 WARN("DEVICE_ATTR_FUNCTIONS",
6771 "Consider renaming function(s)$rename\n" . $herecurr);
6772 } else {
6773 WARN("DEVICE_ATTR_PERMS",
6774 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6775 }
6776 }
6777
Joe Perches515a2352014-04-03 14:49:24 -07006778# Mode permission misuses where it seems decimal should be octal
6779# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006780# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6781# specific definition of not visible in sysfs.
6782# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6783# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006784 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006785 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006786 $line =~ /$mode_perms_search/) {
6787 foreach my $entry (@mode_permission_funcs) {
6788 my $func = $entry->[0];
6789 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006790
Joe Perches459cf0a2016-10-11 13:52:19 -07006791 my $lc = $stat =~ tr@\n@@;
6792 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006793 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006794
Joe Perches515a2352014-04-03 14:49:24 -07006795 my $skip_args = "";
6796 if ($arg_pos > 1) {
6797 $arg_pos--;
6798 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6799 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006800 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006801 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006802 my $val = $1;
6803 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006804 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6805 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6806 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006807 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006808 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006809 }
6810 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006811 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006812 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006813 }
Joe Perches24358802014-04-03 14:49:13 -07006814 }
6815 }
6816 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006817
Joe Perches459cf0a2016-10-11 13:52:19 -07006818# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006819 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006820 my $oval = $1;
6821 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006822 if (WARN("SYMBOLIC_PERMS",
6823 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6824 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006825 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006826 }
6827 }
6828
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006829# validate content of MODULE_LICENSE against list from include/linux/module.h
6830 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6831 my $extracted_string = get_quoted_string($line, $rawline);
6832 my $valid_licenses = qr{
6833 GPL|
6834 GPL\ v2|
6835 GPL\ and\ additional\ rights|
6836 Dual\ BSD/GPL|
6837 Dual\ MIT/GPL|
6838 Dual\ MPL/GPL|
6839 Proprietary
6840 }x;
6841 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6842 WARN("MODULE_LICENSE",
6843 "unknown module license " . $extracted_string . "\n" . $herecurr);
6844 }
6845 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07006846
6847# check for sysctl duplicate constants
6848 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6849 WARN("DUPLICATED_SYSCTL_CONST",
6850 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6851 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006852 }
6853
6854 # If we have no input at all, then there is nothing to report on
6855 # so just keep quiet.
6856 if ($#rawlines == -1) {
6857 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006858 }
6859
Andy Whitcroft8905a672007-11-28 16:21:06 -08006860 # In mailback mode only produce a report in the negative, for
6861 # things that appear to be patches.
6862 if ($mailback && ($clean == 1 || !$is_patch)) {
6863 exit(0);
6864 }
6865
6866 # This is not a patch, and we are are in 'no-patch' mode so
6867 # just keep quiet.
6868 if (!$chk_patch && !$is_patch) {
6869 exit(0);
6870 }
6871
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006872 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006873 ERROR("NOT_UNIFIED_DIFF",
6874 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006875 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006876 if ($is_patch && $has_commit_log && $chk_signoff) {
6877 if ($signoff == 0) {
6878 ERROR("MISSING_SIGN_OFF",
6879 "Missing Signed-off-by: line(s)\n");
6880 } elsif (!$authorsignoff) {
6881 WARN("NO_AUTHOR_SIGN_OFF",
6882 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6883 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006884 }
6885
Andy Whitcroft8905a672007-11-28 16:21:06 -08006886 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006887 if ($summary && !($clean == 1 && $quiet == 1)) {
6888 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006889 print "total: $cnt_error errors, $cnt_warn warnings, " .
6890 (($check)? "$cnt_chk checks, " : "") .
6891 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006892 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006893
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006894 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006895 # If there were any defects found and not already fixing them
6896 if (!$clean and !$fix) {
6897 print << "EOM"
6898
6899NOTE: For some of the reported defects, checkpatch may be able to
6900 mechanically convert to the typical style using --fix or --fix-inplace.
6901EOM
6902 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006903 # If there were whitespace errors which cleanpatch can fix
6904 # then suggest that.
6905 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006906 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006907 print << "EOM"
6908
6909NOTE: Whitespace errors detected.
6910 You may wish to use scripts/cleanpatch or scripts/cleanfile
6911EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006912 }
6913 }
6914
Joe Perchesd752fcc2014-08-06 16:11:05 -07006915 if ($clean == 0 && $fix &&
6916 ("@rawlines" ne "@fixed" ||
6917 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006918 my $newfile = $filename;
6919 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006920 my $linecount = 0;
6921 my $f;
6922
Joe Perchesd752fcc2014-08-06 16:11:05 -07006923 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6924
Joe Perches3705ce52013-07-03 15:05:31 -07006925 open($f, '>', $newfile)
6926 or die "$P: Can't open $newfile for write\n";
6927 foreach my $fixed_line (@fixed) {
6928 $linecount++;
6929 if ($file) {
6930 if ($linecount > 3) {
6931 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006932 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006933 }
6934 } else {
6935 print $f $fixed_line . "\n";
6936 }
6937 }
6938 close($f);
6939
6940 if (!$quiet) {
6941 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006942
Joe Perches3705ce52013-07-03 15:05:31 -07006943Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6944
6945Do _NOT_ trust the results written to this file.
6946Do _NOT_ submit these changes without inspecting them for correctness.
6947
6948This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6949No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006950EOM
6951 }
6952 }
6953
Joe Perchesd8469f12015-06-25 15:03:00 -07006954 if ($quiet == 0) {
6955 print "\n";
6956 if ($clean == 1) {
6957 print "$vname has no obvious style problems and is ready for submission.\n";
6958 } else {
6959 print "$vname has style problems, please review.\n";
6960 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006961 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006962 return $clean;
6963}