blob: 4223a9ac7059bf393a897a05948d5fcd49c46879 [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;
Jerome Forissier3e89ad82020-10-15 20:11:49 -070068my ${CONFIG_} = "CONFIG_";
Hannes Eder77f5b102009-09-21 17:04:37 -070069
70sub help {
71 my ($exitcode) = @_;
72
73 print << "EOM";
74Usage: $P [OPTION]... [FILE]...
75Version: $V
76
77Options:
78 -q, --quiet quiet
79 --no-tree run without a kernel tree
80 --no-signoff do not check for 'Signed-off-by' line
81 --patch treat FILE as patchfile (default)
82 --emacs emacs compile window format
83 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070084 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070085 -g, --git treat FILE as a single commit or git revision range
86 single git commit with:
87 <rev>
88 <rev>^
89 <rev>~n
90 multiple git commits with:
91 <rev1>..<rev2>
92 <rev1>...<rev2>
93 <rev>-<count>
94 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070095 -f, --file treat FILE as regular source file
96 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070097 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070098 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070099 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -0700100 --show-types show the specific message type in the output
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700101 --max-line-length=n set the maximum line length, (default $max_line_length)
102 if exceeded, warn on patches
103 requires --strict for use with --file
Vadim Bendebury56193272014-10-13 15:51:48 -0700104 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700105 --tab-size=n set the number of spaces for tab (default $tabsize)
Hannes Eder77f5b102009-09-21 17:04:37 -0700106 --root=PATH PATH to the kernel tree root
107 --no-summary suppress the per-file summary
108 --mailback only produce a report in case of warnings/errors
109 --summary-file include the filename in summary
110 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
111 'values', 'possible', 'type', and 'attr' (default
112 is all off)
113 --test-only=WORD report only warnings/errors containing WORD
114 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700115 --fix EXPERIMENTAL - may create horrible results
116 If correctable single-line errors exist, create
117 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
118 with potential errors corrected to the preferred
119 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800120 --fix-inplace EXPERIMENTAL - may create horrible results
121 Is the same as --fix, but overwrites the input
122 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700123 --ignore-perl-version override checking of perl version. expect
124 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700125 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700126 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700127 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700128 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700129 --color[=WHEN] Use colors 'always', 'never', or only when output
130 is a terminal ('auto'). Default is 'auto'.
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700131 --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default
132 ${CONFIG_})
Hannes Eder77f5b102009-09-21 17:04:37 -0700133 -h, --help, --version display this help and exit
134
135When FILE is - read standard input.
136EOM
137
138 exit($exitcode);
139}
140
Joe Perches3beb42e2016-05-20 17:04:14 -0700141sub uniq {
142 my %seen;
143 return grep { !$seen{$_}++ } @_;
144}
145
146sub list_types {
147 my ($exitcode) = @_;
148
149 my $count = 0;
150
151 local $/ = undef;
152
153 open(my $script, '<', abs_path($P)) or
154 die "$P: Can't read '$P' $!\n";
155
156 my $text = <$script>;
157 close($script);
158
159 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700160 # Also catch when type or level is passed through a variable
161 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700162 push (@types, $_);
163 }
164 @types = sort(uniq(@types));
165 print("#\tMessage type\n\n");
166 foreach my $type (@types) {
167 print(++$count . "\t" . $type . "\n");
168 }
169
170 exit($exitcode);
171}
172
Joe Perches000d1cc12011-07-25 17:13:25 -0700173my $conf = which_conf($configuration_file);
174if (-f $conf) {
175 my @conf_args;
176 open(my $conffile, '<', "$conf")
177 or warn "$P: Can't find a readable $configuration_file file $!\n";
178
179 while (<$conffile>) {
180 my $line = $_;
181
182 $line =~ s/\s*\n?$//g;
183 $line =~ s/^\s*//g;
184 $line =~ s/\s+/ /g;
185
186 next if ($line =~ m/^\s*#/);
187 next if ($line =~ m/^\s*$/);
188
189 my @words = split(" ", $line);
190 foreach my $word (@words) {
191 last if ($word =~ m/^#/);
192 push (@conf_args, $word);
193 }
194 }
195 close($conffile);
196 unshift(@ARGV, @conf_args) if @conf_args;
197}
198
John Brooks737c0762017-07-10 15:52:24 -0700199# Perl's Getopt::Long allows options to take optional arguments after a space.
200# Prevent --color by itself from consuming other arguments
201foreach (@ARGV) {
202 if ($_ eq "--color" || $_ eq "-color") {
203 $_ = "--color=$color";
204 }
205}
206
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700207GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700208 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700209 'tree!' => \$tree,
210 'signoff!' => \$chk_signoff,
211 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700212 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800213 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700214 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700215 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700216 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700217 'subjective!' => \$check,
218 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700219 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700220 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700221 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700222 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800223 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700224 'min-conf-desc-length=i' => \$min_conf_desc_length,
Antonio Borneo713a09d2020-04-06 20:11:07 -0700225 'tab-size=i' => \$tabsize,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700226 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800227 'summary!' => \$summary,
228 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800229 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700230 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800231 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700232 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800233 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700234 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700235 'codespell!' => \$codespell,
236 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700237 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700238 'color=s' => \$color,
239 'no-color' => \$color, #keep old behaviors of -nocolor
240 'nocolor' => \$color, #keep old behaviors of -nocolor
Jerome Forissier3e89ad82020-10-15 20:11:49 -0700241 'kconfig-prefix=s' => \${CONFIG_},
Hannes Eder77f5b102009-09-21 17:04:37 -0700242 'h|help' => \$help,
243 'version' => \$help
244) or help(1);
245
246help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700247
Joe Perches3beb42e2016-05-20 17:04:14 -0700248list_types(0) if ($list_types);
249
Joe Perches9624b8d2014-01-23 15:54:44 -0800250$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700251$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800252
Joe Perches32f30ca2020-06-04 16:50:40 -0700253die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
254
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700255my $exit = 0;
256
Joe Perches5b579802018-08-21 21:57:33 -0700257my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700258if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700259 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700260 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700261 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700262}
263
Allen Hubbe45107ff2016-08-02 14:04:47 -0700264#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700265if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700266 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700267}
268
John Brooks737c0762017-07-10 15:52:24 -0700269if ($color =~ /^[01]$/) {
270 $color = !$color;
271} elsif ($color =~ /^always$/i) {
272 $color = 1;
273} elsif ($color =~ /^never$/i) {
274 $color = 0;
275} elsif ($color =~ /^auto$/i) {
276 $color = (-t STDOUT);
277} else {
Joe Perches32f30ca2020-06-04 16:50:40 -0700278 die "$P: Invalid color mode: $color\n";
John Brooks737c0762017-07-10 15:52:24 -0700279}
280
Antonio Borneo713a09d2020-04-06 20:11:07 -0700281# skip TAB size 1 to avoid additional checks on $tabsize - 1
Joe Perches32f30ca2020-06-04 16:50:40 -0700282die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
Antonio Borneo713a09d2020-04-06 20:11:07 -0700283
Joe Perches91bfe482013-09-11 14:23:59 -0700284sub hash_save_array_words {
285 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700286
Joe Perches91bfe482013-09-11 14:23:59 -0700287 my @array = split(/,/, join(',', @$arrayRef));
288 foreach my $word (@array) {
289 $word =~ s/\s*\n?$//g;
290 $word =~ s/^\s*//g;
291 $word =~ s/\s+/ /g;
292 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700293
Joe Perches91bfe482013-09-11 14:23:59 -0700294 next if ($word =~ m/^\s*#/);
295 next if ($word =~ m/^\s*$/);
296
297 $hashRef->{$word}++;
298 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700299}
300
Joe Perches91bfe482013-09-11 14:23:59 -0700301sub hash_show_words {
302 my ($hashRef, $prefix) = @_;
303
Joe Perches3c816e42015-06-25 15:03:29 -0700304 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700305 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700306 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700307 print " $word";
308 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700309 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700310 }
311}
312
313hash_save_array_words(\%ignore_type, \@ignore);
314hash_save_array_words(\%use_type, \@use);
315
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800316my $dbg_values = 0;
317my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700318my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700319my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800320for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800321 ## no critic
322 eval "\${dbg_$key} = '$debug{$key}';";
323 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800324}
325
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700326my $rpt_cleaners = 0;
327
Andy Whitcroft8905a672007-11-28 16:21:06 -0800328if ($terse) {
329 $emacs = 1;
330 $quiet++;
331}
332
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700333if ($tree) {
334 if (defined $root) {
335 if (!top_of_kernel_tree($root)) {
336 die "$P: $root: --root does not point at a valid tree\n";
337 }
338 } else {
339 if (top_of_kernel_tree('.')) {
340 $root = '.';
341 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
342 top_of_kernel_tree($1)) {
343 $root = $1;
344 }
345 }
346
347 if (!defined $root) {
348 print "Must be run from the top-level dir. of a kernel tree\n";
349 exit(2);
350 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700351}
352
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700353my $emitted_corrupt = 0;
354
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700355our $Ident = qr{
356 [A-Za-z_][A-Za-z\d_]*
357 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
358 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700359our $Storage = qr{extern|static|asmlinkage};
360our $Sparse = qr{
361 __user|
362 __kernel|
363 __force|
364 __iomem|
365 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800366 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700367 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700368 __refconst|
369 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800370 __rcu|
371 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700372 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800373our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
374our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
375our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
376our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
377our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700378
Wolfram Sang52131292010-03-05 13:43:51 -0800379# Notes to $Attribute:
380# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700381our $Attribute = qr{
382 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700383 __percpu|
384 __nocast|
385 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200386 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700387 __packed__|
388 __packed2__|
389 __naked|
390 __maybe_unused|
391 __always_unused|
392 __noreturn|
393 __used|
394 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800395 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700396 __noclone|
397 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700398 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700399 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700400 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700401 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700402 ____cacheline_aligned|
403 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800404 ____cacheline_internodealigned_in_smp|
405 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700406 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700407our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700408our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700409our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
410our $Lval = qr{$Ident(?:$Member)*};
411
Joe Perches95e2c602013-07-03 15:05:20 -0700412our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
413our $Binary = qr{(?i)0b[01]+$Int_type?};
414our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
415our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700416our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800417our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800418our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
419our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
420our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800421our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700422our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800423our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700424our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700425our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700426our $Operators = qr{
427 <=|>=|==|!=|
428 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700429 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700430 }x;
431
Joe Perches91cb5192014-04-03 14:49:32 -0700432our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
433
Joe Perchesab7e23f2015-04-16 12:44:22 -0700434our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800435our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700436our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700437our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800438our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700439our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800440our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700441our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800442
Joe Perches15662b32011-10-31 17:13:12 -0700443our $NON_ASCII_UTF8 = qr{
444 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700445 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
446 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
447 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
448 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
449 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
450 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
451}x;
452
Joe Perches15662b32011-10-31 17:13:12 -0700453our $UTF8 = qr{
454 [\x09\x0A\x0D\x20-\x7E] # ASCII
455 | $NON_ASCII_UTF8
456}x;
457
Joe Perchese6176fa2015-06-25 15:02:49 -0700458our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800459our $typeOtherOSTypedefs = qr{(?x:
460 u_(?:char|short|int|long) | # bsd
461 u(?:nchar|short|int|long) # sysv
462)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700463our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700464 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700465 atomic_t
466)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700467our $typeTypedefs = qr{(?x:
468 $typeC99Typedefs\b|
469 $typeOtherOSTypedefs\b|
470 $typeKernelTypedefs\b
471)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700472
Joe Perches6d32f7a2015-11-06 16:31:37 -0800473our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
474
Joe Perches691e6692010-03-05 13:43:51 -0800475our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800476 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700477 (?:[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 -0800478 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700479 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700480 panic|
Joe Perches06668722013-11-12 15:10:07 -0800481 MODULE_[A-Z_]+|
482 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800483)};
484
Joe Perchese29a70f2019-03-07 16:28:35 -0800485our $allocFunctions = qr{(?x:
486 (?:(?:devm_)?
487 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
488 kstrdup(?:_const)? |
489 kmemdup(?:_nul)?) |
Christophe JAILLET461e1562020-04-20 18:13:58 -0700490 (?:\w+)?alloc_skb(?:_ip_align)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800491 # dev_alloc_skb/netdev_alloc_skb, et al
492 dma_alloc_coherent
493)};
494
Joe Perches20112472011-07-25 17:13:23 -0700495our $signature_tags = qr{(?xi:
496 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800497 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700498 Acked-by:|
499 Tested-by:|
500 Reviewed-by:|
501 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700502 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700503 To:|
504 Cc:
505)};
506
Joe Perches18130872014-08-06 16:11:22 -0700507our @typeListMisordered = (
508 qr{char\s+(?:un)?signed},
509 qr{int\s+(?:(?:un)?signed\s+)?short\s},
510 qr{int\s+short(?:\s+(?:un)?signed)},
511 qr{short\s+int(?:\s+(?:un)?signed)},
512 qr{(?:un)?signed\s+int\s+short},
513 qr{short\s+(?:un)?signed},
514 qr{long\s+int\s+(?:un)?signed},
515 qr{int\s+long\s+(?:un)?signed},
516 qr{long\s+(?:un)?signed\s+int},
517 qr{int\s+(?:un)?signed\s+long},
518 qr{int\s+(?:un)?signed},
519 qr{int\s+long\s+long\s+(?:un)?signed},
520 qr{long\s+long\s+int\s+(?:un)?signed},
521 qr{long\s+long\s+(?:un)?signed\s+int},
522 qr{long\s+long\s+(?:un)?signed},
523 qr{long\s+(?:un)?signed},
524);
525
Andy Whitcroft8905a672007-11-28 16:21:06 -0800526our @typeList = (
527 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700528 qr{(?:(?:un)?signed\s+)?char},
529 qr{(?:(?:un)?signed\s+)?short\s+int},
530 qr{(?:(?:un)?signed\s+)?short},
531 qr{(?:(?:un)?signed\s+)?int},
532 qr{(?:(?:un)?signed\s+)?long\s+int},
533 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
534 qr{(?:(?:un)?signed\s+)?long\s+long},
535 qr{(?:(?:un)?signed\s+)?long},
536 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800537 qr{float},
538 qr{double},
539 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800540 qr{struct\s+$Ident},
541 qr{union\s+$Ident},
542 qr{enum\s+$Ident},
543 qr{${Ident}_t},
544 qr{${Ident}_handler},
545 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700546 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800547);
Joe Perches938224b2016-01-20 14:59:15 -0800548
549our $C90_int_types = qr{(?x:
550 long\s+long\s+int\s+(?:un)?signed|
551 long\s+long\s+(?:un)?signed\s+int|
552 long\s+long\s+(?:un)?signed|
553 (?:(?:un)?signed\s+)?long\s+long\s+int|
554 (?:(?:un)?signed\s+)?long\s+long|
555 int\s+long\s+long\s+(?:un)?signed|
556 int\s+(?:(?:un)?signed\s+)?long\s+long|
557
558 long\s+int\s+(?:un)?signed|
559 long\s+(?:un)?signed\s+int|
560 long\s+(?:un)?signed|
561 (?:(?:un)?signed\s+)?long\s+int|
562 (?:(?:un)?signed\s+)?long|
563 int\s+long\s+(?:un)?signed|
564 int\s+(?:(?:un)?signed\s+)?long|
565
566 int\s+(?:un)?signed|
567 (?:(?:un)?signed\s+)?int
568)};
569
Alex Dowad485ff232015-06-25 15:02:52 -0700570our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700571our @typeListWithAttr = (
572 @typeList,
573 qr{struct\s+$InitAttribute\s+$Ident},
574 qr{union\s+$InitAttribute\s+$Ident},
575);
576
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700577our @modifierList = (
578 qr{fastcall},
579);
Alex Dowad485ff232015-06-25 15:02:52 -0700580our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800581
Joe Perches24358802014-04-03 14:49:13 -0700582our @mode_permission_funcs = (
583 ["module_param", 3],
584 ["module_param_(?:array|named|string)", 4],
585 ["module_param_array_named", 5],
586 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
587 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700588 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
589 ["IIO_DEV_ATTR_[A-Z_]+", 1],
590 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
591 ["SENSOR_TEMPLATE(?:_2|)", 3],
592 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700593);
594
Joe Perches1a3dcf22020-08-11 18:35:16 -0700595my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
596
Joe Perches515a2352014-04-03 14:49:24 -0700597#Create a search pattern for all these functions to speed up a loop below
598our $mode_perms_search = "";
599foreach my $entry (@mode_permission_funcs) {
600 $mode_perms_search .= '|' if ($mode_perms_search ne "");
601 $mode_perms_search .= $entry->[0];
602}
Joe Perches00180462018-02-06 15:38:55 -0800603$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700604
Joe Perches9189c7e2018-09-07 15:26:18 -0700605our %deprecated_apis = (
606 "synchronize_rcu_bh" => "synchronize_rcu",
607 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
608 "call_rcu_bh" => "call_rcu",
609 "rcu_barrier_bh" => "rcu_barrier",
610 "synchronize_sched" => "synchronize_rcu",
611 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
612 "call_rcu_sched" => "call_rcu",
613 "rcu_barrier_sched" => "rcu_barrier",
614 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
615 "cond_synchronize_sched" => "cond_synchronize_rcu",
616);
617
618#Create a search pattern for all these strings to speed up a loop below
619our $deprecated_apis_search = "";
620foreach my $entry (keys %deprecated_apis) {
621 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
622 $deprecated_apis_search .= $entry;
623}
624$deprecated_apis_search = "(?:${deprecated_apis_search})";
625
Joe Perchesb392c642015-04-16 12:44:16 -0700626our $mode_perms_world_writable = qr{
627 S_IWUGO |
628 S_IWOTH |
629 S_IRWXUGO |
630 S_IALLUGO |
631 0[0-7][0-7][2367]
632}x;
633
Joe Perchesf90774e2016-10-11 13:51:47 -0700634our %mode_permission_string_types = (
635 "S_IRWXU" => 0700,
636 "S_IRUSR" => 0400,
637 "S_IWUSR" => 0200,
638 "S_IXUSR" => 0100,
639 "S_IRWXG" => 0070,
640 "S_IRGRP" => 0040,
641 "S_IWGRP" => 0020,
642 "S_IXGRP" => 0010,
643 "S_IRWXO" => 0007,
644 "S_IROTH" => 0004,
645 "S_IWOTH" => 0002,
646 "S_IXOTH" => 0001,
647 "S_IRWXUGO" => 0777,
648 "S_IRUGO" => 0444,
649 "S_IWUGO" => 0222,
650 "S_IXUGO" => 0111,
651);
652
653#Create a search pattern for all these strings to speed up a loop below
654our $mode_perms_string_search = "";
655foreach my $entry (keys %mode_permission_string_types) {
656 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
657 $mode_perms_string_search .= $entry;
658}
Joe Perches00180462018-02-06 15:38:55 -0800659our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
660our $multi_mode_perms_string_search = qr{
661 ${single_mode_perms_string_search}
662 (?:\s*\|\s*${single_mode_perms_string_search})*
663}x;
664
665sub perms_to_octal {
666 my ($string) = @_;
667
668 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
669
670 my $val = "";
671 my $oval = "";
672 my $to = 0;
673 my $curpos = 0;
674 my $lastpos = 0;
675 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
676 $curpos = pos($string);
677 my $match = $2;
678 my $omatch = $1;
679 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
680 $lastpos = $curpos;
681 $to |= $mode_permission_string_types{$match};
682 $val .= '\s*\|\s*' if ($val ne "");
683 $val .= $match;
684 $oval .= $omatch;
685 }
686 $oval =~ s/^\s*\|\s*//;
687 $oval =~ s/\s*\|\s*$//;
688 return sprintf("%04o", $to);
689}
Joe Perchesf90774e2016-10-11 13:51:47 -0700690
Wolfram Sang7840a942010-08-09 17:20:57 -0700691our $allowed_asm_includes = qr{(?x:
692 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700693 memory|
694 time|
695 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700696)};
697# memory.h: ARM has a custom one
698
Kees Cook66b47b42014-10-13 15:51:57 -0700699# Load common spelling mistakes and build regular expression list.
700my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700701my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700702
Joe Perches36061e32014-12-10 15:51:43 -0800703if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800704 while (<$spelling>) {
705 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700706
Joe Perches36061e32014-12-10 15:51:43 -0800707 $line =~ s/\s*\n?$//g;
708 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700709
Joe Perches36061e32014-12-10 15:51:43 -0800710 next if ($line =~ m/^\s*#/);
711 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700712
Joe Perches36061e32014-12-10 15:51:43 -0800713 my ($suspect, $fix) = split(/\|\|/, $line);
714
Joe Perches36061e32014-12-10 15:51:43 -0800715 $spelling_fix{$suspect} = $fix;
716 }
717 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800718} else {
719 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700720}
Kees Cook66b47b42014-10-13 15:51:57 -0700721
Joe Perchesebfd7d62015-04-16 12:44:14 -0700722if ($codespell) {
723 if (open(my $spelling, '<', $codespellfile)) {
724 while (<$spelling>) {
725 my $line = $_;
726
727 $line =~ s/\s*\n?$//g;
728 $line =~ s/^\s*//g;
729
730 next if ($line =~ m/^\s*#/);
731 next if ($line =~ m/^\s*$/);
732 next if ($line =~ m/, disabled/i);
733
734 $line =~ s/,.*$//;
735
736 my ($suspect, $fix) = split(/->/, $line);
737
738 $spelling_fix{$suspect} = $fix;
739 }
740 close($spelling);
741 } else {
742 warn "No codespell typos will be found - file '$codespellfile': $!\n";
743 }
744}
745
746$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
747
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700748sub read_words {
749 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700750
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700751 if (open(my $words, '<', $file)) {
752 while (<$words>) {
753 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700754
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700755 $line =~ s/\s*\n?$//g;
756 $line =~ s/^\s*//g;
757
758 next if ($line =~ m/^\s*#/);
759 next if ($line =~ m/^\s*$/);
760 if ($line =~ /\s/) {
761 print("$file: '$line' invalid - ignored\n");
762 next;
763 }
764
Quentin Monnetced69da12020-08-11 18:35:13 -0700765 $$wordsRef .= '|' if (defined $$wordsRef);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700766 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700767 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700768 close($file);
769 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700770 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700771
772 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700773}
774
Quentin Monnetced69da12020-08-11 18:35:13 -0700775my $const_structs;
776if (show_type("CONST_STRUCT")) {
777 read_words(\$const_structs, $conststructsfile)
778 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
779}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700780
Quentin Monnetced69da12020-08-11 18:35:13 -0700781if (defined($typedefsfile)) {
782 my $typeOtherTypedefs;
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700783 read_words(\$typeOtherTypedefs, $typedefsfile)
784 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
Quentin Monnetced69da12020-08-11 18:35:13 -0700785 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700786}
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700787
Andy Whitcroft8905a672007-11-28 16:21:06 -0800788sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700789 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
790 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700791 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700792 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700793 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700794 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700795 (?:$typeTypedefs\b)|
796 (?:${all}\b)
797 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800798 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700799 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800800 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800801 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700802 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700803 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800804 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700805 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800806 }x;
Joe Perches18130872014-08-06 16:11:22 -0700807 $NonptrTypeMisordered = qr{
808 (?:$Modifier\s+|const\s+)*
809 (?:
810 (?:${Misordered}\b)
811 )
812 (?:\s+$Modifier|\s+const)*
813 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700814 $NonptrTypeWithAttr = qr{
815 (?:$Modifier\s+|const\s+)*
816 (?:
817 (?:typeof|__typeof__)\s*\([^\)]*\)|
818 (?:$typeTypedefs\b)|
819 (?:${allWithAttr}\b)
820 )
821 (?:\s+$Modifier|\s+const)*
822 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800823 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700824 $NonptrType
Antonio Borneo7b184962020-04-06 20:11:04 -0700825 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700826 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800827 }x;
Joe Perches18130872014-08-06 16:11:22 -0700828 $TypeMisordered = qr{
829 $NonptrTypeMisordered
Antonio Borneo7b184962020-04-06 20:11:04 -0700830 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Perches18130872014-08-06 16:11:22 -0700831 (?:\s+$Inline|\s+$Modifier)*
832 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700833 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700834 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800835}
836build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700837
Joe Perches7d2367a2011-07-25 17:13:22 -0700838our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700839
840# Using $balanced_parens, $LvalOrFunc, or $FuncArg
841# requires at least perl version v5.10.0
842# Any use must be runtime checked with $^V
843
844our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700845our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800846our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700847
Joe Perchesf8422302014-08-06 16:11:31 -0700848our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700849 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700850 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700851 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700852)};
853
Joe Perches7d2367a2011-07-25 17:13:22 -0700854sub deparenthesize {
855 my ($string) = @_;
856 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700857
858 while ($string =~ /^\s*\(.*\)\s*$/) {
859 $string =~ s@^\s*\(\s*@@;
860 $string =~ s@\s*\)\s*$@@;
861 }
862
Joe Perches7d2367a2011-07-25 17:13:22 -0700863 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700864
Joe Perches7d2367a2011-07-25 17:13:22 -0700865 return $string;
866}
867
Joe Perches34456862013-07-03 15:05:34 -0700868sub seed_camelcase_file {
869 my ($file) = @_;
870
871 return if (!(-f $file));
872
873 local $/;
874
875 open(my $include_file, '<', "$file")
876 or warn "$P: Can't read '$file' $!\n";
877 my $text = <$include_file>;
878 close($include_file);
879
880 my @lines = split('\n', $text);
881
882 foreach my $line (@lines) {
883 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
884 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
885 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800886 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
887 $camelcase{$1} = 1;
888 } 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 -0700889 $camelcase{$1} = 1;
890 }
891 }
892}
893
Joe Perchescd28b112019-12-04 16:52:09 -0800894our %maintained_status = ();
895
Joe Perches85b0ee12016-10-11 13:51:44 -0700896sub is_maintained_obsolete {
897 my ($filename) = @_;
898
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800899 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700900
Joe Perchescd28b112019-12-04 16:52:09 -0800901 if (!exists($maintained_status{$filename})) {
902 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
903 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700904
Joe Perchescd28b112019-12-04 16:52:09 -0800905 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700906}
907
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700908sub is_SPDX_License_valid {
909 my ($license) = @_;
910
Joe Perches56294112018-08-21 21:58:04 -0700911 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700912
Joe Perches56294112018-08-21 21:58:04 -0700913 my $root_path = abs_path($root);
914 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700915 return 0 if ($status ne "");
916 return 1;
917}
918
Joe Perches34456862013-07-03 15:05:34 -0700919my $camelcase_seeded = 0;
920sub seed_camelcase_includes {
921 return if ($camelcase_seeded);
922
923 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700924 my $camelcase_cache = "";
925 my @include_files = ();
926
927 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700928
Richard Genoud3645e322014-02-10 14:25:32 -0800929 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700930 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -0700931 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700932 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700933 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700934 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700935 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700936 @include_files = split('\n', $files);
937 foreach my $file (@include_files) {
938 my $date = POSIX::strftime("%Y%m%d%H%M",
939 localtime((stat $file)[9]));
940 $last_mod_date = $date if ($last_mod_date < $date);
941 }
942 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700943 }
Joe Perchesc707a812013-07-08 16:00:43 -0700944
945 if ($camelcase_cache ne "" && -f $camelcase_cache) {
946 open(my $camelcase_file, '<', "$camelcase_cache")
947 or warn "$P: Can't read '$camelcase_cache' $!\n";
948 while (<$camelcase_file>) {
949 chomp;
950 $camelcase{$_} = 1;
951 }
952 close($camelcase_file);
953
954 return;
955 }
956
Richard Genoud3645e322014-02-10 14:25:32 -0800957 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700958 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700959 @include_files = split('\n', $files);
960 }
961
Joe Perches34456862013-07-03 15:05:34 -0700962 foreach my $file (@include_files) {
963 seed_camelcase_file($file);
964 }
Joe Perches351b2a12013-07-03 15:05:36 -0700965
Joe Perchesc707a812013-07-08 16:00:43 -0700966 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700967 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700968 open(my $camelcase_file, '>', "$camelcase_cache")
969 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700970 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
971 print $camelcase_file ("$_\n");
972 }
973 close($camelcase_file);
974 }
Joe Perches34456862013-07-03 15:05:34 -0700975}
976
Joe Perchesf5f61322020-10-15 20:12:12 -0700977sub git_is_single_file {
978 my ($filename) = @_;
979
980 return 0 if ((which("git") eq "") || !(-e "$gitroot"));
981
982 my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
983 my $count = $output =~ tr/\n//;
984 return $count eq 1 && $output =~ m{^${filename}$};
985}
986
Joe Perchesd311cd42014-08-06 16:10:57 -0700987sub git_commit_info {
988 my ($commit, $id, $desc) = @_;
989
990 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
991
Joe Perchesdbbf8692019-09-25 16:46:52 -0700992 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -0700993 $output =~ s/^\s*//gm;
994 my @lines = split("\n", $output);
995
Joe Perches0d7835f2015-02-13 14:38:35 -0800996 return ($id, $desc) if ($#lines < 0);
997
Sean Christopherson5a7f4452019-09-25 16:46:49 -0700998 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -0700999# Maybe one day convert this block of bash into something that returns
1000# all matching commit ids, but it's very slow...
1001#
1002# echo "checking commits $1..."
1003# git rev-list --remotes | grep -i "^$1" |
1004# while read line ; do
1005# git log --format='%H %s' -1 $line |
1006# echo "commit $(cut -c 1-12,41-)"
1007# done
1008 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07001009 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -07001010 } else {
1011 $id = substr($lines[0], 0, 12);
1012 $desc = substr($lines[0], 41);
1013 }
1014
1015 return ($id, $desc);
1016}
1017
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001018$chk_signoff = 0 if ($file);
1019
Andy Whitcroft00df3442007-06-08 13:47:06 -07001020my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001021my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001022my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001023my @fixed_inserted = ();
1024my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001025my $fixlinenr = -1;
1026
Du, Changbin4a593c32016-05-20 17:04:16 -07001027# If input is git commits, extract all commits from the commit expressions.
1028# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1029die "$P: No git repository found\n" if ($git && !-e ".git");
1030
1031if ($git) {
1032 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001033 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001034 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001035 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1036 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001037 } elsif ($commit_expr =~ m/\.\./) {
1038 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001039 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001040 $git_range = "-1 $commit_expr";
1041 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001042 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001043 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001044 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1045 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001046 my $sha1 = $1;
1047 my $subject = $2;
1048 unshift(@commits, $sha1);
1049 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001050 }
1051 }
1052 die "$P: no git commits after extraction!\n" if (@commits == 0);
1053 @ARGV = @commits;
1054}
1055
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001056my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001057$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001058for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001059 my $FILE;
Joe Perchesf5f61322020-10-15 20:12:12 -07001060 my $is_git_file = git_is_single_file($filename);
1061 my $oldfile = $file;
1062 $file = 1 if ($is_git_file);
Du, Changbin4a593c32016-05-20 17:04:16 -07001063 if ($git) {
1064 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1065 die "$P: $filename: git format-patch failed - $!\n";
1066 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001067 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001068 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001069 } elsif ($filename eq '-') {
1070 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001071 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001072 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001073 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001074 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001075 if ($filename eq '-') {
1076 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001077 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001078 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001079 } else {
1080 $vname = $filename;
1081 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001082 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001083 chomp;
1084 push(@rawlines, $_);
Geert Uytterhoevenc7f574d2020-06-04 16:50:43 -07001085 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001086 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001087 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001088
1089 if ($#ARGV > 0 && $quiet == 0) {
1090 print '-' x length($vname) . "\n";
1091 print "$vname\n";
1092 print '-' x length($vname) . "\n";
1093 }
1094
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001095 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001096 $exit = 1;
1097 }
1098 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001099 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001100 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001101 @fixed_inserted = ();
1102 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001103 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001104 @modifierListFile = ();
1105 @typeListFile = ();
1106 build_types();
Joe Perchesf5f61322020-10-15 20:12:12 -07001107 $file = $oldfile if ($is_git_file);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001108}
1109
Joe Perchesd8469f12015-06-25 15:03:00 -07001110if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001111 hash_show_words(\%use_type, "Used");
1112 hash_show_words(\%ignore_type, "Ignored");
1113
Joe Perches5b579802018-08-21 21:57:33 -07001114 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001115 print << "EOM"
1116
1117NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001118 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001119EOM
1120 }
1121 if ($exit) {
1122 print << "EOM"
1123
1124NOTE: If any of the errors are false positives, please report
1125 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1126EOM
1127 }
1128}
1129
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001130exit($exit);
1131
1132sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001133 my ($root) = @_;
1134
1135 my @tree_check = (
1136 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1137 "README", "Documentation", "arch", "include", "drivers",
1138 "fs", "init", "ipc", "kernel", "lib", "scripts",
1139 );
1140
1141 foreach my $check (@tree_check) {
1142 if (! -e $root . '/' . $check) {
1143 return 0;
1144 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001145 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001146 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001147}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001148
Joe Perches20112472011-07-25 17:13:23 -07001149sub parse_email {
1150 my ($formatted_email) = @_;
1151
1152 my $name = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001153 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001154 my $address = "";
1155 my $comment = "";
1156
1157 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1158 $name = $1;
1159 $address = $2;
1160 $comment = $3 if defined $3;
1161 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1162 $address = $1;
1163 $comment = $2 if defined $2;
1164 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1165 $address = $1;
1166 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001167 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001168 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001169 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001170 $name =~ s/^\"|\"$//g;
1171 # If there's a name left after stripping spaces and
1172 # leading quotes, and the address doesn't have both
1173 # leading and trailing angle brackets, the address
1174 # is invalid. ie:
1175 # "joe smith joe@smith.com" bad
1176 # "joe smith <joe@smith.com" bad
1177 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1178 $name = "";
1179 $address = "";
1180 $comment = "";
1181 }
1182 }
1183
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001184 $comment = trim($comment);
Joe Perches3705ce52013-07-03 15:05:31 -07001185 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001186 $name =~ s/^\"|\"$//g;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001187 if ($name =~ s/(\s*\([^\)]+\))\s*//) {
Joe Perchesdfa05c22020-04-06 20:10:48 -07001188 $name_comment = trim($1);
1189 }
Joe Perches3705ce52013-07-03 15:05:31 -07001190 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001191 $address =~ s/^\<|\>$//g;
1192
1193 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1194 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1195 $name = "\"$name\"";
1196 }
1197
Joe Perchesdfa05c22020-04-06 20:10:48 -07001198 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001199}
1200
1201sub format_email {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001202 my ($name, $name_comment, $address, $comment) = @_;
Joe Perches20112472011-07-25 17:13:23 -07001203
1204 my $formatted_email;
1205
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001206 $name_comment = trim($name_comment);
1207 $comment = trim($comment);
Joe Perches3705ce52013-07-03 15:05:31 -07001208 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001209 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001210 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001211
1212 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1213 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1214 $name = "\"$name\"";
1215 }
1216
1217 if ("$name" eq "") {
1218 $formatted_email = "$address";
1219 } else {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001220 $formatted_email = "$name$name_comment <$address>";
Joe Perches20112472011-07-25 17:13:23 -07001221 }
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001222 $formatted_email .= "$comment";
Joe Perches20112472011-07-25 17:13:23 -07001223 return $formatted_email;
1224}
1225
Joe Perchesdfa05c22020-04-06 20:10:48 -07001226sub reformat_email {
1227 my ($email) = @_;
1228
1229 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001230 return format_email($email_name, $name_comment, $email_address, $comment);
Joe Perchesdfa05c22020-04-06 20:10:48 -07001231}
1232
1233sub same_email_addresses {
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001234 my ($email1, $email2, $match_comment) = @_;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001235
1236 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1237 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1238
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001239 if ($match_comment != 1) {
1240 return $email1_name eq $email2_name &&
1241 $email1_address eq $email2_address;
1242 }
Joe Perchesdfa05c22020-04-06 20:10:48 -07001243 return $email1_name eq $email2_name &&
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07001244 $email1_address eq $email2_address &&
1245 $name1_comment eq $name2_comment &&
1246 $comment1 eq $comment2;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001247}
1248
Joe Perchesd311cd42014-08-06 16:10:57 -07001249sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001250 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001251
Joe Perchesbd474ca2014-08-06 16:11:10 -07001252 foreach my $path (split(/:/, $ENV{PATH})) {
1253 if (-e "$path/$bin") {
1254 return "$path/$bin";
1255 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001256 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001257
Joe Perchesbd474ca2014-08-06 16:11:10 -07001258 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001259}
1260
Joe Perches000d1cc12011-07-25 17:13:25 -07001261sub which_conf {
1262 my ($conf) = @_;
1263
1264 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1265 if (-e "$path/$conf") {
1266 return "$path/$conf";
1267 }
1268 }
1269
1270 return "";
1271}
1272
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001273sub expand_tabs {
1274 my ($str) = @_;
1275
1276 my $res = '';
1277 my $n = 0;
1278 for my $c (split(//, $str)) {
1279 if ($c eq "\t") {
1280 $res .= ' ';
1281 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001282 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001283 $res .= ' ';
1284 }
1285 next;
1286 }
1287 $res .= $c;
1288 $n++;
1289 }
1290
1291 return $res;
1292}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001293sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001294 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001295 return $res;
1296}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001297
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001298sub line_stats {
1299 my ($line) = @_;
1300
1301 # Drop the diff line leader and expand tabs
1302 $line =~ s/^.//;
1303 $line = expand_tabs($line);
1304
1305 # Pick the indent from the front of the line.
1306 my ($white) = ($line =~ /^(\s*)/);
1307
1308 return (length($line), length($white));
1309}
1310
Andy Whitcroft773647a2008-03-28 14:15:58 -07001311my $sanitise_quote = '';
1312
1313sub sanitise_line_reset {
1314 my ($in_comment) = @_;
1315
1316 if ($in_comment) {
1317 $sanitise_quote = '*/';
1318 } else {
1319 $sanitise_quote = '';
1320 }
1321}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001322sub sanitise_line {
1323 my ($line) = @_;
1324
1325 my $res = '';
1326 my $l = '';
1327
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001328 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001329 my $off = 0;
1330 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001331
Andy Whitcroft773647a2008-03-28 14:15:58 -07001332 # Always copy over the diff marker.
1333 $res = substr($line, 0, 1);
1334
1335 for ($off = 1; $off < length($line); $off++) {
1336 $c = substr($line, $off, 1);
1337
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001338 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001339 # and end, all to $;.
1340 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1341 $sanitise_quote = '*/';
1342
1343 substr($res, $off, 2, "$;$;");
1344 $off++;
1345 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001346 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001347 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001348 $sanitise_quote = '';
1349 substr($res, $off, 2, "$;$;");
1350 $off++;
1351 next;
1352 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001353 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1354 $sanitise_quote = '//';
1355
1356 substr($res, $off, 2, $sanitise_quote);
1357 $off++;
1358 next;
1359 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001360
1361 # A \ in a string means ignore the next character.
1362 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1363 $c eq "\\") {
1364 substr($res, $off, 2, 'XX');
1365 $off++;
1366 next;
1367 }
1368 # Regular quotes.
1369 if ($c eq "'" || $c eq '"') {
1370 if ($sanitise_quote eq '') {
1371 $sanitise_quote = $c;
1372
1373 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001374 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001375 } elsif ($sanitise_quote eq $c) {
1376 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001377 }
1378 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001379
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001380 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001381 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1382 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001383 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1384 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001385 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1386 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001387 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001388 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001389 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001390 }
1391
Daniel Walker113f04a2009-09-21 17:04:35 -07001392 if ($sanitise_quote eq '//') {
1393 $sanitise_quote = '';
1394 }
1395
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001396 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001397 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001398 my $clean = 'X' x length($1);
1399 $res =~ s@\<.*\>@<$clean>@;
1400
1401 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001402 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001403 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001404 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001405 }
1406
Joe Perchesdadf6802016-08-02 14:04:33 -07001407 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1408 my $match = $1;
1409 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1410 }
1411
Andy Whitcroft00df3442007-06-08 13:47:06 -07001412 return $res;
1413}
1414
Joe Perchesa6962d72013-04-29 16:18:13 -07001415sub get_quoted_string {
1416 my ($line, $rawline) = @_;
1417
Joe Perches478b1792018-04-10 16:33:34 -07001418 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001419 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001420 return substr($rawline, $-[0], $+[0] - $-[0]);
1421}
1422
Andy Whitcroft8905a672007-11-28 16:21:06 -08001423sub ctx_statement_block {
1424 my ($linenr, $remain, $off) = @_;
1425 my $line = $linenr - 1;
1426 my $blk = '';
1427 my $soff = $off;
1428 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001429 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001430
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001431 my $loff = 0;
1432
Andy Whitcroft8905a672007-11-28 16:21:06 -08001433 my $type = '';
1434 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001435 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001436 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001437 my $c;
1438 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001439
1440 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001441 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001442 @stack = (['', 0]) if ($#stack == -1);
1443
Andy Whitcroft773647a2008-03-28 14:15:58 -07001444 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001445 # If we are about to drop off the end, pull in more
1446 # context.
1447 if ($off >= $len) {
1448 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001449 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001450 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001451 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001452 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001453 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001454 $len = length($blk);
1455 $line++;
1456 last;
1457 }
1458 # Bail if there is no further context.
1459 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001460 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001461 last;
1462 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001463 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1464 $level++;
1465 $type = '#';
1466 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001467 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001468 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001469 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001470 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001471
Andy Whitcroft773647a2008-03-28 14:15:58 -07001472 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001473
1474 # Handle nested #if/#else.
1475 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1476 push(@stack, [ $type, $level ]);
1477 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1478 ($type, $level) = @{$stack[$#stack - 1]};
1479 } elsif ($remainder =~ /^#\s*endif\b/) {
1480 ($type, $level) = @{pop(@stack)};
1481 }
1482
Andy Whitcroft8905a672007-11-28 16:21:06 -08001483 # Statement ends at the ';' or a close '}' at the
1484 # outermost level.
1485 if ($level == 0 && $c eq ';') {
1486 last;
1487 }
1488
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001489 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001490 if ($level == 0 && $coff_set == 0 &&
1491 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1492 $remainder =~ /^(else)(?:\s|{)/ &&
1493 $remainder !~ /^else\s+if\b/) {
1494 $coff = $off + length($1) - 1;
1495 $coff_set = 1;
1496 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1497 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001498 }
1499
Andy Whitcroft8905a672007-11-28 16:21:06 -08001500 if (($type eq '' || $type eq '(') && $c eq '(') {
1501 $level++;
1502 $type = '(';
1503 }
1504 if ($type eq '(' && $c eq ')') {
1505 $level--;
1506 $type = ($level != 0)? '(' : '';
1507
1508 if ($level == 0 && $coff < $soff) {
1509 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001510 $coff_set = 1;
1511 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001512 }
1513 }
1514 if (($type eq '' || $type eq '{') && $c eq '{') {
1515 $level++;
1516 $type = '{';
1517 }
1518 if ($type eq '{' && $c eq '}') {
1519 $level--;
1520 $type = ($level != 0)? '{' : '';
1521
1522 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001523 if (substr($blk, $off + 1, 1) eq ';') {
1524 $off++;
1525 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001526 last;
1527 }
1528 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001529 # Preprocessor commands end at the newline unless escaped.
1530 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1531 $level--;
1532 $type = '';
1533 $off++;
1534 last;
1535 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001536 $off++;
1537 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001538 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001539 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001540 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001541 $line++;
1542 $remain--;
1543 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001544
1545 my $statement = substr($blk, $soff, $off - $soff + 1);
1546 my $condition = substr($blk, $soff, $coff - $soff + 1);
1547
1548 #warn "STATEMENT<$statement>\n";
1549 #warn "CONDITION<$condition>\n";
1550
Andy Whitcroft773647a2008-03-28 14:15:58 -07001551 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001552
1553 return ($statement, $condition,
1554 $line, $remain + 1, $off - $loff + 1, $level);
1555}
1556
Andy Whitcroftcf655042008-03-04 14:28:20 -08001557sub statement_lines {
1558 my ($stmt) = @_;
1559
1560 # Strip the diff line prefixes and rip blank lines at start and end.
1561 $stmt =~ s/(^|\n)./$1/g;
1562 $stmt =~ s/^\s*//;
1563 $stmt =~ s/\s*$//;
1564
1565 my @stmt_lines = ($stmt =~ /\n/g);
1566
1567 return $#stmt_lines + 2;
1568}
1569
1570sub statement_rawlines {
1571 my ($stmt) = @_;
1572
1573 my @stmt_lines = ($stmt =~ /\n/g);
1574
1575 return $#stmt_lines + 2;
1576}
1577
1578sub statement_block_size {
1579 my ($stmt) = @_;
1580
1581 $stmt =~ s/(^|\n)./$1/g;
1582 $stmt =~ s/^\s*{//;
1583 $stmt =~ s/}\s*$//;
1584 $stmt =~ s/^\s*//;
1585 $stmt =~ s/\s*$//;
1586
1587 my @stmt_lines = ($stmt =~ /\n/g);
1588 my @stmt_statements = ($stmt =~ /;/g);
1589
1590 my $stmt_lines = $#stmt_lines + 2;
1591 my $stmt_statements = $#stmt_statements + 1;
1592
1593 if ($stmt_lines > $stmt_statements) {
1594 return $stmt_lines;
1595 } else {
1596 return $stmt_statements;
1597 }
1598}
1599
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001600sub ctx_statement_full {
1601 my ($linenr, $remain, $off) = @_;
1602 my ($statement, $condition, $level);
1603
1604 my (@chunks);
1605
Andy Whitcroftcf655042008-03-04 14:28:20 -08001606 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001607 ($statement, $condition, $linenr, $remain, $off, $level) =
1608 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001609 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001610 push(@chunks, [ $condition, $statement ]);
1611 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1612 return ($level, $linenr, @chunks);
1613 }
1614
1615 # Pull in the following conditional/block pairs and see if they
1616 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001617 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001618 ($statement, $condition, $linenr, $remain, $off, $level) =
1619 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001620 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001621 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001622 #print "C: push\n";
1623 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001624 }
1625
1626 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001627}
1628
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001629sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001630 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001631 my $line;
1632 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001633 my $blk = '';
1634 my @o;
1635 my @c;
1636 my @res = ();
1637
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001638 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001639 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001640 for ($line = $start; $remain > 0; $line++) {
1641 next if ($rawlines[$line] =~ /^-/);
1642 $remain--;
1643
1644 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001645
1646 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001647 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001648 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001649 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001650 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001651 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001652 $level = pop(@stack);
1653 }
1654
Andy Whitcroft01464f32010-10-26 14:23:19 -07001655 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001656 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1657 if ($off > 0) {
1658 $off--;
1659 next;
1660 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001661
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001662 if ($c eq $close && $level > 0) {
1663 $level--;
1664 last if ($level == 0);
1665 } elsif ($c eq $open) {
1666 $level++;
1667 }
1668 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001669
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001670 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001671 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001672 }
1673
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001674 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001675 }
1676
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001677 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001678}
1679sub ctx_block_outer {
1680 my ($linenr, $remain) = @_;
1681
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001682 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1683 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001684}
1685sub ctx_block {
1686 my ($linenr, $remain) = @_;
1687
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001688 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1689 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001690}
1691sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001692 my ($linenr, $remain, $off) = @_;
1693
1694 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1695 return @r;
1696}
1697sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001698 my ($linenr, $remain) = @_;
1699
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001700 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001701}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001702sub ctx_statement_level {
1703 my ($linenr, $remain, $off) = @_;
1704
1705 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1706}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001707
1708sub ctx_locate_comment {
1709 my ($first_line, $end_line) = @_;
1710
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001711 # If c99 comment on the current line, or the line before or after
1712 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1713 return $current_comment if (defined $current_comment);
1714 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1715 return $current_comment if (defined $current_comment);
1716 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1717 return $current_comment if (defined $current_comment);
1718
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001719 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001720 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001721 return $current_comment if (defined $current_comment);
1722
1723 # Look through the context and try and figure out if there is a
1724 # comment.
1725 my $in_comment = 0;
1726 $current_comment = '';
1727 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001728 my $line = $rawlines[$linenr - 1];
1729 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001730 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1731 $in_comment = 1;
1732 }
1733 if ($line =~ m@/\*@) {
1734 $in_comment = 1;
1735 }
1736 if (!$in_comment && $current_comment ne '') {
1737 $current_comment = '';
1738 }
1739 $current_comment .= $line . "\n" if ($in_comment);
1740 if ($line =~ m@\*/@) {
1741 $in_comment = 0;
1742 }
1743 }
1744
1745 chomp($current_comment);
1746 return($current_comment);
1747}
1748sub ctx_has_comment {
1749 my ($first_line, $end_line) = @_;
1750 my $cmt = ctx_locate_comment($first_line, $end_line);
1751
Andy Whitcroft00df3442007-06-08 13:47:06 -07001752 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001753 ##print "CMMT: $cmt\n";
1754
1755 return ($cmt ne '');
1756}
1757
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001758sub raw_line {
1759 my ($linenr, $cnt) = @_;
1760
1761 my $offset = $linenr - 1;
1762 $cnt++;
1763
1764 my $line;
1765 while ($cnt) {
1766 $line = $rawlines[$offset++];
1767 next if (defined($line) && $line =~ /^-/);
1768 $cnt--;
1769 }
1770
1771 return $line;
1772}
1773
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001774sub get_stat_real {
1775 my ($linenr, $lc) = @_;
1776
1777 my $stat_real = raw_line($linenr, 0);
1778 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1779 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1780 }
1781
1782 return $stat_real;
1783}
1784
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001785sub get_stat_here {
1786 my ($linenr, $cnt, $here) = @_;
1787
1788 my $herectx = $here . "\n";
1789 for (my $n = 0; $n < $cnt; $n++) {
1790 $herectx .= raw_line($linenr, $n) . "\n";
1791 }
1792
1793 return $herectx;
1794}
1795
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001796sub cat_vet {
1797 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001798 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001799
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001800 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001801 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1802 $res .= $1;
1803 if ($2 ne '') {
1804 $coded = sprintf("^%c", unpack('C', $2) + 64);
1805 $res .= $coded;
1806 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001807 }
1808 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001809
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001810 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001811}
1812
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001813my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001814my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001815my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001816my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001817
1818sub annotate_reset {
1819 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001820 $av_pending = '_';
1821 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001822 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001823}
1824
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001825sub annotate_values {
1826 my ($stream, $type) = @_;
1827
1828 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001829 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001830 my $cur = $stream;
1831
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001832 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001833
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001834 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001835 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001836 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001837 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001838 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001839 print "WS($1)\n" if ($dbg_values > 1);
1840 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001841 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001842 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001843 }
1844
Florian Micklerc023e4732011-01-12 16:59:58 -08001845 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001846 print "CAST($1)\n" if ($dbg_values > 1);
1847 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001848 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001849
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001850 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001851 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001852 $type = 'T';
1853
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001854 } elsif ($cur =~ /^($Modifier)\s*/) {
1855 print "MODIFIER($1)\n" if ($dbg_values > 1);
1856 $type = 'T';
1857
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001858 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001859 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001860 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001861 push(@av_paren_type, $type);
1862 if ($2 ne '') {
1863 $av_pending = 'N';
1864 }
1865 $type = 'E';
1866
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001867 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001868 print "UNDEF($1)\n" if ($dbg_values > 1);
1869 $av_preprocessor = 1;
1870 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001871
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001872 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001873 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001874 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001875
1876 push(@av_paren_type, $type);
1877 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001878 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001879
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001880 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001881 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1882 $av_preprocessor = 1;
1883
1884 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1885
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001886 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001887
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001888 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001889 print "PRE_END($1)\n" if ($dbg_values > 1);
1890
1891 $av_preprocessor = 1;
1892
1893 # Assume all arms of the conditional end as this
1894 # one does, and continue as if the #endif was not here.
1895 pop(@av_paren_type);
1896 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001897 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001898
1899 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001900 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001901
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001902 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1903 print "ATTR($1)\n" if ($dbg_values > 1);
1904 $av_pending = $type;
1905 $type = 'N';
1906
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001907 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001908 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001909 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001910 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001911 }
1912 $type = 'N';
1913
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001914 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001915 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001916 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001917 $type = 'N';
1918
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001919 } elsif ($cur =~/^(case)/o) {
1920 print "CASE($1)\n" if ($dbg_values > 1);
1921 $av_pend_colon = 'C';
1922 $type = 'N';
1923
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001924 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001925 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001926 $type = 'N';
1927
1928 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001929 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001930 push(@av_paren_type, $av_pending);
1931 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001932 $type = 'N';
1933
1934 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001935 my $new_type = pop(@av_paren_type);
1936 if ($new_type ne '_') {
1937 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001938 print "PAREN('$1') -> $type\n"
1939 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001940 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001941 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001942 }
1943
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001944 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001945 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001946 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001947 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001948
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001949 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1950 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001951 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001952 } elsif ($type eq 'E') {
1953 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001954 }
1955 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1956 $type = 'V';
1957
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001958 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001959 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001960 $type = 'V';
1961
1962 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001963 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001964 $type = 'N';
1965
Andy Whitcroftcf655042008-03-04 14:28:20 -08001966 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001967 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001968 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001969 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001970
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001971 } elsif ($cur =~/^(,)/) {
1972 print "COMMA($1)\n" if ($dbg_values > 1);
1973 $type = 'C';
1974
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001975 } elsif ($cur =~ /^(\?)/o) {
1976 print "QUESTION($1)\n" if ($dbg_values > 1);
1977 $type = 'N';
1978
1979 } elsif ($cur =~ /^(:)/o) {
1980 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1981
1982 substr($var, length($res), 1, $av_pend_colon);
1983 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1984 $type = 'E';
1985 } else {
1986 $type = 'N';
1987 }
1988 $av_pend_colon = 'O';
1989
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001990 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001991 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001992 $type = 'N';
1993
Andy Whitcroft0d413862008-10-15 22:02:16 -07001994 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001995 my $variant;
1996
1997 print "OPV($1)\n" if ($dbg_values > 1);
1998 if ($type eq 'V') {
1999 $variant = 'B';
2000 } else {
2001 $variant = 'U';
2002 }
2003
2004 substr($var, length($res), 1, $variant);
2005 $type = 'N';
2006
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002007 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002008 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002009 if ($1 ne '++' && $1 ne '--') {
2010 $type = 'N';
2011 }
2012
2013 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002014 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002015 }
2016 if (defined $1) {
2017 $cur = substr($cur, length($1));
2018 $res .= $type x length($1);
2019 }
2020 }
2021
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002022 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002023}
2024
Andy Whitcroft8905a672007-11-28 16:21:06 -08002025sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002026 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002027 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002028 ^(?:
2029 $Modifier|
2030 $Storage|
2031 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002032 DEFINE_\S+
2033 )$|
2034 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002035 goto|
2036 return|
2037 case|
2038 else|
2039 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002040 do|
2041 \#|
2042 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002043 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002044 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002045 )}x;
2046 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2047 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002048 # Check for modifiers.
2049 $possible =~ s/\s*$Storage\s*//g;
2050 $possible =~ s/\s*$Sparse\s*//g;
2051 if ($possible =~ /^\s*$/) {
2052
2053 } elsif ($possible =~ /\s/) {
2054 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002055 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002056 if ($modifier !~ $notPermitted) {
2057 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002058 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002059 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002060 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002061
2062 } else {
2063 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002064 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002065 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002066 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002067 } else {
2068 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002069 }
2070}
2071
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002072my $prefix = '';
2073
Joe Perches000d1cc12011-07-25 17:13:25 -07002074sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002075 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002076
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002077 $type =~ tr/[a-z]/[A-Z]/;
2078
Joe Perchescbec18a2014-04-03 14:49:19 -07002079 return defined $use_type{$type} if (scalar keys %use_type > 0);
2080
2081 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002082}
2083
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002084sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002085 my ($level, $type, $msg) = @_;
2086
2087 if (!show_type($type) ||
2088 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002089 return 0;
2090 }
Joe Perches57230292015-06-25 15:03:03 -07002091 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002092 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002093 if ($level eq 'ERROR') {
2094 $output .= RED;
2095 } elsif ($level eq 'WARNING') {
2096 $output .= YELLOW;
2097 } else {
2098 $output .= GREEN;
2099 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002100 }
Joe Perches57230292015-06-25 15:03:03 -07002101 $output .= $prefix . $level . ':';
2102 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002103 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002104 $output .= "$type:";
2105 }
John Brooks737c0762017-07-10 15:52:24 -07002106 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002107 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002108
2109 if ($showfile) {
2110 my @lines = split("\n", $output, -1);
2111 splice(@lines, 1, 1);
2112 $output = join("\n", @lines);
2113 }
Joe Perches57230292015-06-25 15:03:03 -07002114 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002115
Joe Perches57230292015-06-25 15:03:03 -07002116 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002117
2118 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002119}
Joe Perchescbec18a2014-04-03 14:49:19 -07002120
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002121sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002122 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002123}
Joe Perches000d1cc12011-07-25 17:13:25 -07002124
Joe Perchesd752fcc2014-08-06 16:11:05 -07002125sub fixup_current_range {
2126 my ($lineRef, $offset, $length) = @_;
2127
2128 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2129 my $o = $1;
2130 my $l = $2;
2131 my $no = $o + $offset;
2132 my $nl = $l + $length;
2133 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2134 }
2135}
2136
2137sub fix_inserted_deleted_lines {
2138 my ($linesRef, $insertedRef, $deletedRef) = @_;
2139
2140 my $range_last_linenr = 0;
2141 my $delta_offset = 0;
2142
2143 my $old_linenr = 0;
2144 my $new_linenr = 0;
2145
2146 my $next_insert = 0;
2147 my $next_delete = 0;
2148
2149 my @lines = ();
2150
2151 my $inserted = @{$insertedRef}[$next_insert++];
2152 my $deleted = @{$deletedRef}[$next_delete++];
2153
2154 foreach my $old_line (@{$linesRef}) {
2155 my $save_line = 1;
2156 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002157 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002158 $delta_offset = 0;
2159 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2160 $range_last_linenr = $new_linenr;
2161 fixup_current_range(\$line, $delta_offset, 0);
2162 }
2163
2164 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2165 $deleted = @{$deletedRef}[$next_delete++];
2166 $save_line = 0;
2167 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2168 }
2169
2170 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2171 push(@lines, ${$inserted}{'LINE'});
2172 $inserted = @{$insertedRef}[$next_insert++];
2173 $new_linenr++;
2174 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2175 }
2176
2177 if ($save_line) {
2178 push(@lines, $line);
2179 $new_linenr++;
2180 }
2181
2182 $old_linenr++;
2183 }
2184
2185 return @lines;
2186}
2187
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002188sub fix_insert_line {
2189 my ($linenr, $line) = @_;
2190
2191 my $inserted = {
2192 LINENR => $linenr,
2193 LINE => $line,
2194 };
2195 push(@fixed_inserted, $inserted);
2196}
2197
2198sub fix_delete_line {
2199 my ($linenr, $line) = @_;
2200
2201 my $deleted = {
2202 LINENR => $linenr,
2203 LINE => $line,
2204 };
2205
2206 push(@fixed_deleted, $deleted);
2207}
2208
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002209sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002210 my ($type, $msg) = @_;
2211
2212 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002213 our $clean = 0;
2214 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002215 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002216 }
Joe Perches3705ce52013-07-03 15:05:31 -07002217 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002218}
2219sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002220 my ($type, $msg) = @_;
2221
2222 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002223 our $clean = 0;
2224 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002225 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002226 }
Joe Perches3705ce52013-07-03 15:05:31 -07002227 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002228}
2229sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002230 my ($type, $msg) = @_;
2231
2232 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002233 our $clean = 0;
2234 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002235 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002236 }
Joe Perches3705ce52013-07-03 15:05:31 -07002237 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002238}
2239
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002240sub check_absolute_file {
2241 my ($absolute, $herecurr) = @_;
2242 my $file = $absolute;
2243
2244 ##print "absolute<$absolute>\n";
2245
2246 # See if any suffix of this path is a path within the tree.
2247 while ($file =~ s@^[^/]*/@@) {
2248 if (-f "$root/$file") {
2249 ##print "file<$file>\n";
2250 last;
2251 }
2252 }
2253 if (! -f _) {
2254 return 0;
2255 }
2256
2257 # It is, so see if the prefix is acceptable.
2258 my $prefix = $absolute;
2259 substr($prefix, -length($file)) = '';
2260
2261 ##print "prefix<$prefix>\n";
2262 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002263 WARN("USE_RELATIVE_PATH",
2264 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002265 }
2266}
2267
Joe Perches3705ce52013-07-03 15:05:31 -07002268sub trim {
2269 my ($string) = @_;
2270
Joe Perchesb34c6482013-09-11 14:24:01 -07002271 $string =~ s/^\s+|\s+$//g;
2272
2273 return $string;
2274}
2275
2276sub ltrim {
2277 my ($string) = @_;
2278
2279 $string =~ s/^\s+//;
2280
2281 return $string;
2282}
2283
2284sub rtrim {
2285 my ($string) = @_;
2286
2287 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002288
2289 return $string;
2290}
2291
Joe Perches52ea8502013-11-12 15:10:09 -08002292sub string_find_replace {
2293 my ($string, $find, $replace) = @_;
2294
2295 $string =~ s/$find/$replace/g;
2296
2297 return $string;
2298}
2299
Joe Perches3705ce52013-07-03 15:05:31 -07002300sub tabify {
2301 my ($leading) = @_;
2302
Antonio Borneo713a09d2020-04-06 20:11:07 -07002303 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002304 my $max_spaces_before_tab = $source_indent - 1;
2305 my $spaces_to_tab = " " x $source_indent;
2306
2307 #convert leading spaces to tabs
2308 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2309 #Remove spaces before a tab
2310 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2311
2312 return "$leading";
2313}
2314
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002315sub pos_last_openparen {
2316 my ($line) = @_;
2317
2318 my $pos = 0;
2319
2320 my $opens = $line =~ tr/\(/\(/;
2321 my $closes = $line =~ tr/\)/\)/;
2322
2323 my $last_openparen = 0;
2324
2325 if (($opens == 0) || ($closes >= $opens)) {
2326 return -1;
2327 }
2328
2329 my $len = length($line);
2330
2331 for ($pos = 0; $pos < $len; $pos++) {
2332 my $string = substr($line, $pos);
2333 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2334 $pos += length($1) - 1;
2335 } elsif (substr($line, $pos, 1) eq '(') {
2336 $last_openparen = $pos;
2337 } elsif (index($string, '(') == -1) {
2338 last;
2339 }
2340 }
2341
Joe Perches91cb5192014-04-03 14:49:32 -07002342 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002343}
2344
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002345sub get_raw_comment {
2346 my ($line, $rawline) = @_;
2347 my $comment = '';
2348
2349 for my $i (0 .. (length($line) - 1)) {
2350 if (substr($line, $i, 1) eq "$;") {
2351 $comment .= substr($rawline, $i, 1);
2352 }
2353 }
2354
2355 return $comment;
2356}
2357
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002358sub process {
2359 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002360
2361 my $linenr=0;
2362 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002363 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002364 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002365 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002366
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002367 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002368 my $indent;
2369 my $previndent=0;
2370 my $stashindent=0;
2371
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002372 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002373 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002374 my $author = '';
2375 my $authorsignoff = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002376 my $author_sob = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002377 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002378 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002379 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002380 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002381 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002382 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002383 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002384 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002385 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002386 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002387 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002388 my $non_utf8_charset = 0;
2389
Joe Perches365dd4e2014-08-06 16:10:42 -07002390 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002391 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002392
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002393 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002394 our $cnt_lines = 0;
2395 our $cnt_error = 0;
2396 our $cnt_warn = 0;
2397 our $cnt_chk = 0;
2398
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002399 # Trace the real file/line as we go.
2400 my $realfile = '';
2401 my $realline = 0;
2402 my $realcnt = 0;
2403 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002404 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002405 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002406 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002407 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002408 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002409
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002410 my $prev_values = 'E';
2411
2412 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002413 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002414 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002415 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002416 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002417
Joe Perches7e51f192013-09-11 14:23:57 -07002418 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002419
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002420 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002421 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002422 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002423 my @setup_docs = ();
2424 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002425
Joe Perchesd8b07712013-11-12 15:10:06 -08002426 my $camelcase_file_seeded = 0;
2427
Rob Herring9f3a8992018-04-10 16:33:13 -07002428 my $checklicenseline = 1;
2429
Andy Whitcroft773647a2008-03-28 14:15:58 -07002430 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002431 my $line;
2432 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002433 $linenr++;
2434 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002435
Joe Perches3705ce52013-07-03 15:05:31 -07002436 push(@fixed, $rawline) if ($fix);
2437
Andy Whitcroft773647a2008-03-28 14:15:58 -07002438 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002439 $setup_docs = 0;
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07002440 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002441 $setup_docs = 1;
2442 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002443 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002444 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002445 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002446 $realline=$1-1;
2447 if (defined $2) {
2448 $realcnt=$3+1;
2449 } else {
2450 $realcnt=1+1;
2451 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002452 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002453
2454 # Guestimate if this is a continuing comment. Run
2455 # the context looking for a comment "edge". If this
2456 # edge is a close comment then we must be in a comment
2457 # at context start.
2458 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002459 my $cnt = $realcnt;
2460 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2461 next if (defined $rawlines[$ln - 1] &&
2462 $rawlines[$ln - 1] =~ /^-/);
2463 $cnt--;
2464 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002465 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002466 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2467 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2468 ($edge) = $1;
2469 last;
2470 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002471 }
2472 if (defined $edge && $edge eq '*/') {
2473 $in_comment = 1;
2474 }
2475
2476 # Guestimate if this is a continuing comment. If this
2477 # is the start of a diff block and this line starts
2478 # ' *' then it is very likely a comment.
2479 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002480 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002481 {
2482 $in_comment = 1;
2483 }
2484
2485 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2486 sanitise_line_reset($in_comment);
2487
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002488 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002489 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002490 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002491 $line = sanitise_line($rawline);
2492 }
2493 push(@lines, $line);
2494
2495 if ($realcnt > 1) {
2496 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2497 } else {
2498 $realcnt = 0;
2499 }
2500
2501 #print "==>$rawline\n";
2502 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002503
2504 if ($setup_docs && $line =~ /^\+/) {
2505 push(@setup_docs, $line);
2506 }
2507 }
2508
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002509 $prefix = '';
2510
Andy Whitcroft773647a2008-03-28 14:15:58 -07002511 $realcnt = 0;
2512 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002513 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002514 foreach my $line (@lines) {
2515 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002516 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002517 my $sline = $line; #copy of $line
2518 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002519
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002520 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002521 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002522
Joe Perches12c253a2018-06-07 17:10:58 -07002523# check if it's a mode change, rename or start of a patch
2524 if (!$in_commit_log &&
2525 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2526 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2527 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2528 $is_patch = 1;
2529 }
2530
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002531#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002532 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002533 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2534 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002535 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002536 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002537 $realline=$1-1;
2538 if (defined $2) {
2539 $realcnt=$3+1;
2540 } else {
2541 $realcnt=1+1;
2542 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002543 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002544 $prev_values = 'E';
2545
Andy Whitcroft773647a2008-03-28 14:15:58 -07002546 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002547 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002548 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002549 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002550 if ($context =~ /\b(\w+)\s*\(/) {
2551 $context_function = $1;
2552 } else {
2553 undef $context_function;
2554 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002555 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002556
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002557# track the line number as we move through the hunk, note that
2558# new versions of GNU diff omit the leading space on completely
2559# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002560 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002561 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002562 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002563
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002564 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002565 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002566
2567 # Track the previous line.
2568 ($prevline, $stashline) = ($stashline, $line);
2569 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002570 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2571
Andy Whitcroft773647a2008-03-28 14:15:58 -07002572 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002573
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002574 } elsif ($realcnt == 1) {
2575 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002576 }
2577
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002578 my $hunk_line = ($realcnt != 0);
2579
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002580 $here = "#$linenr: " if (!$file);
2581 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002582
Joe Perches2ac73b42014-06-04 16:12:05 -07002583 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002584 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002585 if ($line =~ /^diff --git.*?(\S+)$/) {
2586 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002587 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002588 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002589 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002590 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002591 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002592 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002593 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002594
2595 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002596 if (!$file && $tree && $p1_prefix ne '' &&
2597 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002598 WARN("PATCH_PREFIX",
2599 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002600 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002601
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002602 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002603 ERROR("MODIFIED_INCLUDE_ASM",
2604 "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 -07002605 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002606 $found_file = 1;
2607 }
2608
Joe Perches34d88152015-06-25 15:03:05 -07002609#make up the handle for any error we report on this line
2610 if ($showfile) {
2611 $prefix = "$realfile:$realline: "
2612 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002613 if ($file) {
2614 $prefix = "$filename:$realline: ";
2615 } else {
2616 $prefix = "$filename:$linenr: ";
2617 }
Joe Perches34d88152015-06-25 15:03:05 -07002618 }
2619
Joe Perches2ac73b42014-06-04 16:12:05 -07002620 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002621 if (is_maintained_obsolete($realfile)) {
2622 WARN("OBSOLETE",
2623 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2624 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002625 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002626 $check = 1;
2627 } else {
2628 $check = $check_orig;
2629 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002630 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002631
2632 if ($realfile !~ /^MAINTAINERS/) {
2633 my $last_binding_patch = $is_binding_patch;
2634
2635 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2636
2637 if (($last_binding_patch != -1) &&
2638 ($last_binding_patch ^ $is_binding_patch)) {
2639 WARN("DT_SPLIT_BINDING_PATCH",
Mauro Carvalho Chehab858e6842020-04-15 16:45:25 +02002640 "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 -07002641 }
2642 }
2643
Andy Whitcroft773647a2008-03-28 14:15:58 -07002644 next;
2645 }
2646
Randy Dunlap389834b2007-06-08 13:47:03 -07002647 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002648
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002649 my $hereline = "$here\n$rawline\n";
2650 my $herecurr = "$here\n$rawline\n";
2651 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002652
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002653 $cnt_lines++ if ($realcnt != 0);
2654
Joe Perches490b2922018-08-21 21:58:01 -07002655# Verify the existence of a commit log if appropriate
2656# 2 is used because a $signature is counted in $commit_log_lines
2657 if ($in_commit_log) {
2658 if ($line !~ /^\s*$/) {
2659 $commit_log_lines++; #could be a $signature
2660 }
2661 } elsif ($has_commit_log && $commit_log_lines < 2) {
2662 WARN("COMMIT_MESSAGE",
2663 "Missing commit description - Add an appropriate one\n");
2664 $commit_log_lines = 2; #warn only once
2665 }
2666
Joe Perchese518e9a2015-06-25 15:03:27 -07002667# Check if the commit log has what seems like a diff which can confuse patch
2668 if ($in_commit_log && !$commit_log_has_diff &&
Mrinal Pandey13e45412020-09-04 16:35:52 -07002669 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2670 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
Joe Perchese518e9a2015-06-25 15:03:27 -07002671 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2672 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2673 ERROR("DIFF_IN_COMMIT_MSG",
2674 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2675 $commit_log_has_diff = 1;
2676 }
2677
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002678# Check for incorrect file permissions
2679 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2680 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002681 if ($realfile !~ m@scripts/@ &&
2682 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002683 ERROR("EXECUTE_PERMISSIONS",
2684 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002685 }
2686 }
2687
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002688# Check the patch for a From:
2689 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2690 $author = $1;
Dwaipayan Raye7f929f2020-10-15 20:12:15 -07002691 my $curline = $linenr;
2692 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2693 $author .= $1;
2694 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002695 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2696 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002697 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002698 }
2699
Joe Perches20112472011-07-25 17:13:23 -07002700# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002701 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002702 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002703 $in_commit_log = 0;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002704 if ($author ne '' && $authorsignoff != 1) {
2705 if (same_email_addresses($1, $author, 1)) {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002706 $authorsignoff = 1;
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002707 } else {
2708 my $ctx = $1;
2709 my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
2710 my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
2711
2712 if ($email_address eq $author_address && $email_name eq $author_name) {
2713 $author_sob = $ctx;
2714 $authorsignoff = 2;
2715 } elsif ($email_address eq $author_address) {
2716 $author_sob = $ctx;
2717 $authorsignoff = 3;
2718 } elsif ($email_name eq $author_name) {
2719 $author_sob = $ctx;
2720 $authorsignoff = 4;
2721
2722 my $address1 = $email_address;
2723 my $address2 = $author_address;
2724
2725 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
2726 $address1 = "$1$2";
2727 }
2728 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
2729 $address2 = "$1$2";
2730 }
2731 if ($address1 eq $address2) {
2732 $authorsignoff = 5;
2733 }
2734 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002735 }
2736 }
Joe Perches20112472011-07-25 17:13:23 -07002737 }
2738
Joe Perches44d303e2020-04-06 20:11:10 -07002739# Check for patch separator
2740 if ($line =~ /^---$/) {
2741 $has_patch_separator = 1;
2742 $in_commit_log = 0;
2743 }
2744
Joe Perchese0d975b2014-12-10 15:51:49 -08002745# Check if MAINTAINERS is being updated. If so, there's probably no need to
2746# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2747 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2748 $reported_maintainer_file = 1;
2749 }
2750
Joe Perches20112472011-07-25 17:13:23 -07002751# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002752 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002753 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002754 my $space_before = $1;
2755 my $sign_off = $2;
2756 my $space_after = $3;
2757 my $email = $4;
2758 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2759
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002760 if ($sign_off !~ /$signature_tags/) {
2761 WARN("BAD_SIGN_OFF",
2762 "Non-standard signature: $sign_off\n" . $herecurr);
2763 }
Joe Perches20112472011-07-25 17:13:23 -07002764 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002765 if (WARN("BAD_SIGN_OFF",
2766 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2767 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002768 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002769 "$ucfirst_sign_off $email";
2770 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002771 }
Joe Perches20112472011-07-25 17:13:23 -07002772 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002773 if (WARN("BAD_SIGN_OFF",
2774 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2775 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002776 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002777 "$ucfirst_sign_off $email";
2778 }
2779
Joe Perches20112472011-07-25 17:13:23 -07002780 }
2781 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002782 if (WARN("BAD_SIGN_OFF",
2783 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2784 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002785 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002786 "$ucfirst_sign_off $email";
2787 }
Joe Perches20112472011-07-25 17:13:23 -07002788 }
2789
Joe Perchesdfa05c22020-04-06 20:10:48 -07002790 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002791 my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
Joe Perches20112472011-07-25 17:13:23 -07002792 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002793 ERROR("BAD_SIGN_OFF",
2794 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002795 } else {
2796 my $dequoted = $suggested_email;
2797 $dequoted =~ s/^"//;
2798 $dequoted =~ s/" </ </;
2799 # Don't force email to have quotes
2800 # Allow just an angle bracketed address
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002801 if (!same_email_addresses($email, $suggested_email, 0)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002802 WARN("BAD_SIGN_OFF",
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07002803 "email address '$email' might be better as '$suggested_email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002804 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002805 }
Joe Perches7e51f192013-09-11 14:23:57 -07002806
2807# Check for duplicate signatures
2808 my $sig_nospace = $line;
2809 $sig_nospace =~ s/\s//g;
2810 $sig_nospace = lc($sig_nospace);
2811 if (defined $signatures{$sig_nospace}) {
2812 WARN("BAD_SIGN_OFF",
2813 "Duplicate signature\n" . $herecurr);
2814 } else {
2815 $signatures{$sig_nospace} = 1;
2816 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002817
2818# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2819 if ($sign_off =~ /^co-developed-by:$/i) {
2820 if ($email eq $author) {
2821 WARN("BAD_SIGN_OFF",
2822 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2823 }
2824 if (!defined $lines[$linenr]) {
2825 WARN("BAD_SIGN_OFF",
2826 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2827 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2828 WARN("BAD_SIGN_OFF",
2829 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2830 } elsif ($1 ne $email) {
2831 WARN("BAD_SIGN_OFF",
2832 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2833 }
2834 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002835 }
2836
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002837# Check email subject for common tools that don't need to be mentioned
2838 if ($in_header_lines &&
2839 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2840 WARN("EMAIL_SUBJECT",
2841 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2842 }
2843
Joe Perches44d303e2020-04-06 20:11:10 -07002844# Check for Gerrit Change-Ids not in any patch context
2845 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002846 ERROR("GERRIT_CHANGE_ID",
Joe Perches44d303e2020-04-06 20:11:10 -07002847 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002848 }
2849
Joe Perches369c8dd2015-11-06 16:31:34 -08002850# Check if the commit log is in a possible stack dump
2851 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2852 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2853 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2854 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07002855 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2856 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2857 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2858 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08002859 $commit_log_possible_stack_dump = 1;
2860 }
2861
Joe Perches2a076f42015-04-16 12:44:28 -07002862# Check for line lengths > 75 in commit log, warn once
2863 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002864 length($line) > 75 &&
2865 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2866 # file delta changes
2867 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2868 # filename then :
2869 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2870 # A Fixes: or Link: line
2871 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002872 WARN("COMMIT_LOG_LONG_LINE",
2873 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2874 $commit_log_long_line = 1;
2875 }
2876
Joe Perchesbf4daf12015-09-09 15:37:50 -07002877# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002878 if ($in_commit_log && $commit_log_possible_stack_dump &&
2879 $line =~ /^\s*$/) {
2880 $commit_log_possible_stack_dump = 0;
2881 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002882
Joe Perches0d7835f2015-02-13 14:38:35 -08002883# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002884 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07002885 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002886 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002887 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002888 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002889 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2890 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002891 my $init_char = "c";
2892 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002893 my $short = 1;
2894 my $long = 0;
2895 my $case = 1;
2896 my $space = 1;
2897 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002898 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002899 my $id = '0123456789ab';
2900 my $orig_desc = "commit description";
2901 my $description = "";
2902
Joe Perchesfe043ea2015-09-09 15:37:25 -07002903 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2904 $init_char = $1;
2905 $orig_commit = lc($2);
2906 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2907 $orig_commit = lc($1);
2908 }
2909
Joe Perches0d7835f2015-02-13 14:38:35 -08002910 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2911 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2912 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2913 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2914 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2915 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002916 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002917 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2918 defined $rawlines[$linenr] &&
2919 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2920 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002921 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002922 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2923 defined $rawlines[$linenr] &&
2924 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2925 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2926 $orig_desc = $1;
2927 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2928 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002929 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002930 }
2931
2932 ($id, $description) = git_commit_info($orig_commit,
2933 $id, $orig_desc);
2934
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002935 if (defined($id) &&
2936 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002937 ERROR("GIT_COMMIT_ID",
2938 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2939 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002940 }
2941
Joe Perches13f19372014-08-06 16:10:59 -07002942# Check for added, moved or deleted files
2943 if (!$reported_maintainer_file && !$in_commit_log &&
2944 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2945 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2946 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2947 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002948 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002949 $reported_maintainer_file = 1;
2950 WARN("FILE_PATH_CHANGES",
2951 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2952 }
2953
Rob Herringe400edb2019-09-12 15:18:20 +01002954# Check for adding new DT bindings not in schema format
2955 if (!$in_commit_log &&
2956 ($line =~ /^new file mode\s*\d+\s*$/) &&
2957 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2958 WARN("DT_SCHEMA_BINDING_PATCH",
2959 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2960 }
2961
Andy Whitcroft00df3442007-06-08 13:47:06 -07002962# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002963 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002964 ERROR("CORRUPTED_PATCH",
2965 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002966 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002967 }
2968
2969# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2970 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002971 $rawline !~ m/^$UTF8*$/) {
2972 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2973
2974 my $blank = copy_spacing($rawline);
2975 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2976 my $hereptr = "$hereline$ptr\n";
2977
Joe Perches34d99212011-07-25 17:13:26 -07002978 CHK("INVALID_UTF8",
2979 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002980 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002981
Joe Perches15662b32011-10-31 17:13:12 -07002982# Check if it's the start of a commit log
2983# (not a header line and we haven't seen the patch filename)
2984 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002985 !($rawline =~ /^\s+(?:\S|$)/ ||
2986 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002987 $in_header_lines = 0;
2988 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002989 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002990 }
2991
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002992# Check if there is UTF-8 in a commit log when a mail header has explicitly
2993# declined it, i.e defined some charset where it is missing.
2994 if ($in_header_lines &&
2995 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2996 $1 !~ /utf-8/i) {
2997 $non_utf8_charset = 1;
2998 }
2999
3000 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07003001 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07003002 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07003003 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3004 }
3005
Joe Perchesd6430f72016-12-12 16:46:28 -08003006# Check for absolute kernel paths in commit message
3007 if ($tree && $in_commit_log) {
3008 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3009 my $file = $1;
3010
3011 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3012 check_absolute_file($1, $herecurr)) {
3013 #
3014 } else {
3015 check_absolute_file($file, $herecurr);
3016 }
3017 }
3018 }
3019
Kees Cook66b47b42014-10-13 15:51:57 -07003020# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07003021 if (defined($misspellings) &&
3022 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07003023 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07003024 my $typo = $1;
3025 my $typo_fix = $spelling_fix{lc($typo)};
3026 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3027 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07003028 my $msg_level = \&WARN;
3029 $msg_level = \&CHK if ($file);
3030 if (&{$msg_level}("TYPO_SPELLING",
3031 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07003032 $fix) {
3033 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3034 }
3035 }
3036 }
3037
Matteo Crocea8dd86b2019-09-25 16:46:38 -07003038# check for invalid commit id
3039 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3040 my $id;
3041 my $description;
3042 ($id, $description) = git_commit_info($2, undef, undef);
3043 if (!defined($id)) {
3044 WARN("UNKNOWN_COMMIT_ID",
3045 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3046 }
3047 }
3048
Joe Perches310cd062020-10-15 20:11:52 -07003049# check for repeated words separated by a single space
3050 if ($rawline =~ /^\+/ || $in_commit_log) {
3051 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3052
3053 my $first = $1;
3054 my $second = $2;
3055
3056 if ($first =~ /(?:struct|union|enum)/) {
3057 pos($rawline) += length($first) + length($second) + 1;
3058 next;
3059 }
3060
3061 next if ($first ne $second);
3062 next if ($first eq 'long');
3063
3064 if (WARN("REPEATED_WORD",
3065 "Possible repeated word: '$first'\n" . $herecurr) &&
3066 $fix) {
3067 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3068 }
3069 }
3070
3071 # if it's a repeated word on consecutive lines in a comment block
3072 if ($prevline =~ /$;+\s*$/ &&
3073 $prevrawline =~ /($word_pattern)\s*$/) {
3074 my $last_word = $1;
3075 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3076 if (WARN("REPEATED_WORD",
3077 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3078 $fix) {
3079 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3080 }
3081 }
3082 }
3083 }
3084
Andy Whitcroft306708542008-10-15 22:02:28 -07003085# ignore non-hunk lines and lines being removed
3086 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003087
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003088#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003089 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003090 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07003091 if (ERROR("DOS_LINE_ENDINGS",
3092 "DOS line endings\n" . $herevet) &&
3093 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003094 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003095 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003096 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3097 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003098 if (ERROR("TRAILING_WHITESPACE",
3099 "trailing whitespace\n" . $herevet) &&
3100 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003101 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003102 }
3103
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003104 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003105 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07003106
Josh Triplett4783f892013-11-12 15:10:12 -08003107# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003108 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003109 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003110 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3111 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003112 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003113 my $msg_level = \&ERROR;
3114 $msg_level = \&CHK if ($file);
3115 &{$msg_level}("FSF_MAILING_ADDRESS",
3116 "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 -08003117 }
3118
Andi Kleen33549572010-05-24 14:33:29 -07003119# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003120# Only applies when adding the entry originally, after that we do not have
3121# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003122 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003123 # 'choice' is usually the last thing on the line (though
3124 # Kconfig supports named choices), so use a word boundary
3125 # (\b) rather than a whitespace character (\s)
3126 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003127 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003128 my $cnt = $realcnt;
3129 my $ln = $linenr + 1;
3130 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003131 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003132 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003133 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003134 $f = $lines[$ln - 1];
3135 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3136 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003137
3138 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003139 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003140
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003141 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003142 $is_start = 1;
Masahiro Yamada22a4ac02020-06-17 12:02:20 +09003143 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003144 $length = -1;
3145 }
3146
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003147 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003148 $f =~ s/#.*//;
3149 $f =~ s/^\s+//;
3150 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003151
3152 # This only checks context lines in the patch
3153 # and so hopefully shouldn't trigger false
3154 # positives, even though some of these are
3155 # common words in help texts
3156 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3157 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003158 $is_end = 1;
3159 last;
3160 }
Andi Kleen33549572010-05-24 14:33:29 -07003161 $length++;
3162 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003163 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3164 WARN("CONFIG_DESCRIPTION",
3165 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3166 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003167 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003168 }
3169
Joe Perches7ccf41a2020-06-04 16:50:33 -07003170# check MAINTAINERS entries
3171 if ($realfile =~ /^MAINTAINERS$/) {
3172# check MAINTAINERS entries for the right form
3173 if ($rawline =~ /^\+[A-Z]:/ &&
3174 $rawline !~ /^\+[A-Z]:\t\S/) {
3175 if (WARN("MAINTAINERS_STYLE",
3176 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3177 $fix) {
3178 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3179 }
3180 }
3181# check MAINTAINERS entries for the right ordering too
3182 my $preferred_order = 'MRLSWQBCPTFXNK';
3183 if ($rawline =~ /^\+[A-Z]:/ &&
3184 $prevrawline =~ /^[\+ ][A-Z]:/) {
3185 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3186 my $cur = $1;
3187 my $curval = $2;
3188 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3189 my $prev = $1;
3190 my $prevval = $2;
3191 my $curindex = index($preferred_order, $cur);
3192 my $previndex = index($preferred_order, $prev);
3193 if ($curindex < 0) {
3194 WARN("MAINTAINERS_STYLE",
3195 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3196 } else {
3197 if ($previndex >= 0 && $curindex < $previndex) {
3198 WARN("MAINTAINERS_STYLE",
3199 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3200 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3201 ($prev eq 'X' && $cur eq 'X')) &&
3202 ($prevval cmp $curval) > 0) {
3203 WARN("MAINTAINERS_STYLE",
3204 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3205 }
3206 }
Joe Perches628f91a2017-07-10 15:52:07 -07003207 }
3208 }
3209
Christoph Jaeger327953e2015-02-13 14:38:29 -08003210# discourage the use of boolean for type definition attributes of Kconfig options
3211 if ($realfile =~ /Kconfig/ &&
3212 $line =~ /^\+\s*\bboolean\b/) {
3213 WARN("CONFIG_TYPE_BOOLEAN",
3214 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3215 }
3216
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003217 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3218 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3219 my $flag = $1;
3220 my $replacement = {
3221 'EXTRA_AFLAGS' => 'asflags-y',
3222 'EXTRA_CFLAGS' => 'ccflags-y',
3223 'EXTRA_CPPFLAGS' => 'cppflags-y',
3224 'EXTRA_LDFLAGS' => 'ldflags-y',
3225 };
3226
3227 WARN("DEPRECATED_VARIABLE",
3228 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3229 }
3230
Rob Herringbff5da42014-01-23 15:54:51 -08003231# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003232 if (defined $root &&
3233 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3234 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3235
Rob Herringbff5da42014-01-23 15:54:51 -08003236 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3237
Florian Vaussardcc933192014-04-03 14:49:27 -07003238 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003239 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003240
Rob Herringbff5da42014-01-23 15:54:51 -08003241 foreach my $compat (@compats) {
3242 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003243 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3244 my $compat3 = $compat;
3245 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3246 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003247 if ( $? >> 8 ) {
3248 WARN("UNDOCUMENTED_DT_STRING",
3249 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3250 }
3251
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003252 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3253 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003254 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003255 if ( $? >> 8 ) {
3256 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003257 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003258 }
3259 }
3260 }
3261
Rob Herring9f3a8992018-04-10 16:33:13 -07003262# check for using SPDX license tag at beginning of files
3263 if ($realline == $checklicenseline) {
3264 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3265 $checklicenseline = 2;
3266 } elsif ($rawline =~ /^\+/) {
3267 my $comment = "";
3268 if ($realfile =~ /\.(h|s|S)$/) {
3269 $comment = '/*';
3270 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3271 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003272 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003273 $comment = '#';
3274 } elsif ($realfile =~ /\.rst$/) {
3275 $comment = '..';
3276 }
3277
Joe Perchesfdf13692019-03-07 16:28:32 -08003278# check SPDX comment style for .[chsS] files
3279 if ($realfile =~ /\.[chsS]$/ &&
3280 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003281 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003282 WARN("SPDX_LICENSE_TAG",
3283 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3284 }
3285
Rob Herring9f3a8992018-04-10 16:33:13 -07003286 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003287 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3288 WARN("SPDX_LICENSE_TAG",
3289 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003290 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003291 my $spdx_license = $1;
3292 if (!is_SPDX_License_valid($spdx_license)) {
3293 WARN("SPDX_LICENSE_TAG",
3294 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3295 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003296 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3297 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3298 my $msg_level = \&WARN;
3299 $msg_level = \&CHK if ($file);
3300 if (&{$msg_level}("SPDX_LICENSE_TAG",
3301
3302 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3303 $fix) {
3304 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3305 }
3306 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003307 }
3308 }
3309 }
3310
Joe Perchesa0154cd2020-10-15 20:12:19 -07003311# check for embedded filenames
3312 if ($rawline =~ /^\+.*\Q$realfile\E/) {
3313 WARN("EMBEDDED_FILENAME",
3314 "It's generally not useful to have the filename in the file\n" . $herecurr);
3315 }
3316
Andy Whitcroft5368df202008-10-15 22:02:27 -07003317# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003318 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003319
Joe Perchesa8da38a2019-03-07 16:28:42 -08003320# check for using SPDX-License-Identifier on the wrong line number
3321 if ($realline != $checklicenseline &&
3322 $rawline =~ /\bSPDX-License-Identifier:/ &&
3323 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3324 WARN("SPDX_LICENSE_TAG",
3325 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3326 }
3327
Joe Perches47e0c882015-06-25 15:02:57 -07003328# line length limit (with some exclusions)
3329#
3330# There are a few types of lines that may extend beyond $max_line_length:
3331# logging functions like pr_info that end in a string
3332# lines with a single string
3333# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003334# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003335#
3336# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003337# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003338# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3339# LONG_LINE all other lines longer than $max_line_length
3340#
3341# if LONG_LINE is ignored, the other 2 types are also ignored
3342#
3343
Joe Perchesb4749e92015-07-17 16:24:01 -07003344 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003345 my $msg_type = "LONG_LINE";
3346
3347 # Check the allowed long line types first
3348
3349 # logging functions that end in a string that starts
3350 # before $max_line_length
3351 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3352 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3353 $msg_type = "";
3354
3355 # lines with only strings (w/ possible termination)
3356 # #defines with only strings
3357 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3358 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3359 $msg_type = "";
3360
Joe Perchescc147502017-11-17 15:28:44 -08003361 # More special cases
3362 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3363 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003364 $msg_type = "";
3365
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003366 # URL ($rawline is used in case the URL is in a comment)
3367 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3368 $msg_type = "";
3369
Joe Perches47e0c882015-06-25 15:02:57 -07003370 # Otherwise set the alternate message types
3371
3372 # a comment starts before $max_line_length
3373 } elsif ($line =~ /($;[\s$;]*)$/ &&
3374 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3375 $msg_type = "LONG_LINE_COMMENT"
3376
3377 # a quoted string starts before $max_line_length
3378 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3379 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3380 $msg_type = "LONG_LINE_STRING"
3381 }
3382
3383 if ($msg_type ne "" &&
3384 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003385 my $msg_level = \&WARN;
3386 $msg_level = \&CHK if ($file);
3387 &{$msg_level}($msg_type,
3388 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003389 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003390 }
3391
Andy Whitcroft8905a672007-11-28 16:21:06 -08003392# check for adding lines without a newline.
3393 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003394 WARN("MISSING_EOF_NEWLINE",
3395 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003396 }
3397
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003398# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003399 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003400
3401# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003402# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003403 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3404 $rawline =~ /^\+\s* \s*/) {
3405 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003406 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003407 if (ERROR("CODE_INDENT",
3408 "code indent should use tabs where possible\n" . $herevet) &&
3409 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003410 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003411 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003412 }
3413
Alberto Panizzo08e44362010-03-05 13:43:54 -08003414# check for space before tabs.
3415 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3416 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003417 if (WARN("SPACE_BEFORE_TAB",
3418 "please, no space before tabs\n" . $herevet) &&
3419 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003420 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003421 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003422 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003423 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003424 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003425 }
3426
Joe Perches6a487212018-04-10 16:34:04 -07003427# check for assignments on the start of a line
3428 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3429 CHK("ASSIGNMENT_CONTINUATIONS",
3430 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3431 }
3432
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003433# check for && or || at the start of a line
3434 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3435 CHK("LOGICAL_CONTINUATIONS",
3436 "Logical continuations should be on the previous line\n" . $hereprev);
3437 }
3438
Joe Perchesa91e8992016-05-20 17:04:05 -07003439# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003440 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003441 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003442 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003443 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003444 if (WARN("TABSTOP",
3445 "Statements should start on a tabstop\n" . $herecurr) &&
3446 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003447 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003448 }
3449 }
3450 }
3451
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003452# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003453 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003454 $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 -07003455 $prevline =~ /^\+(\t*)(.*)$/;
3456 my $oldindent = $1;
3457 my $rest = $2;
3458
3459 my $pos = pos_last_openparen($rest);
3460 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003461 $line =~ /^(\+| )([ \t]*)/;
3462 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003463
3464 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003465 "\t" x ($pos / $tabsize) .
3466 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003467 my $goodspaceindent = $oldindent . " " x $pos;
3468
3469 if ($newindent ne $goodtabindent &&
3470 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003471
3472 if (CHK("PARENTHESIS_ALIGNMENT",
3473 "Alignment should match open parenthesis\n" . $hereprev) &&
3474 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003475 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003476 s/^\+[ \t]*/\+$goodtabindent/;
3477 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003478 }
3479 }
3480 }
3481
Joe Perches6ab3a972015-04-16 12:44:05 -07003482# check for space after cast like "(int) foo" or "(struct foo) bar"
3483# avoid checking a few false positives:
3484# "sizeof(<type>)" or "__alignof__(<type>)"
3485# function pointer declarations like "(*foo)(int) = bar;"
3486# structure definitions like "(struct foo) { 0 };"
3487# multiline macros that define functions
3488# known attributes or the __attribute__ keyword
3489 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3490 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003491 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003492 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003493 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003494 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003495 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003496 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003497 }
3498
Joe Perches86406b12015-09-09 15:37:41 -07003499# Block comment styles
3500# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003501 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003502 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003503 $rawline =~ /^\+[ \t]*\*/ &&
Łukasz Stelmachc70735c2020-10-15 20:12:25 -07003504 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
Joe Perches05880602012-10-04 17:13:35 -07003505 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3506 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3507 }
3508
Joe Perches86406b12015-09-09 15:37:41 -07003509# Block comments use * on subsequent lines
3510 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3511 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003512 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003513 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003514 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003515 WARN("BLOCK_COMMENT_STYLE",
3516 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003517 }
3518
Joe Perches86406b12015-09-09 15:37:41 -07003519# Block comments use */ on trailing lines
3520 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003521 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3522 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3523 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003524 WARN("BLOCK_COMMENT_STYLE",
3525 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003526 }
3527
Joe Perches08eb9b82016-10-11 13:51:50 -07003528# Block comment * alignment
3529 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003530 $line =~ /^\+[ \t]*$;/ && #leading comment
3531 $rawline =~ /^\+[ \t]*\*/ && #leading *
3532 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003533 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003534 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3535 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003536 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003537 if (defined($1)) {
3538 $oldindent = expand_tabs($1);
3539 } else {
3540 $prevrawline =~ m@^\+(.*/?)\*@;
3541 $oldindent = expand_tabs($1);
3542 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003543 $rawline =~ m@^\+([ \t]*)\*@;
3544 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003545 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003546 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003547 WARN("BLOCK_COMMENT_STYLE",
3548 "Block comments should align the * on each line\n" . $hereprev);
3549 }
3550 }
3551
Joe Perches7f619192014-08-06 16:10:39 -07003552# check for missing blank lines after struct/union declarations
3553# with exceptions for various attributes and macros
3554 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3555 $line =~ /^\+/ &&
3556 !($line =~ /^\+\s*$/ ||
3557 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3558 $line =~ /^\+\s*MODULE_/i ||
3559 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3560 $line =~ /^\+[a-z_]*init/ ||
3561 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3562 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003563 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003564 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003565 if (CHK("LINE_SPACING",
3566 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3567 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003568 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003569 }
Joe Perches7f619192014-08-06 16:10:39 -07003570 }
3571
Joe Perches365dd4e2014-08-06 16:10:42 -07003572# check for multiple consecutive blank lines
3573 if ($prevline =~ /^[\+ ]\s*$/ &&
3574 $line =~ /^\+\s*$/ &&
3575 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003576 if (CHK("LINE_SPACING",
3577 "Please don't use multiple blank lines\n" . $hereprev) &&
3578 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003579 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003580 }
3581
Joe Perches365dd4e2014-08-06 16:10:42 -07003582 $last_blank_line = $linenr;
3583 }
3584
Joe Perches3b617e32014-04-03 14:49:28 -07003585# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003586 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3587 # actual declarations
3588 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003589 # function pointer declarations
3590 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003591 # foo bar; where foo is some local typedef or #define
3592 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3593 # known declaration macros
3594 $prevline =~ /^\+\s+$declaration_macros/) &&
3595 # for "else if" which can look like "$Ident $Ident"
3596 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3597 # other possible extensions of declaration lines
3598 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3599 # not starting a section or a macro "\" extended line
3600 $prevline =~ /(?:\{\s*|\\)$/) &&
3601 # looks like a declaration
3602 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003603 # function pointer declarations
3604 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003605 # foo bar; where foo is some local typedef or #define
3606 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3607 # known declaration macros
3608 $sline =~ /^\+\s+$declaration_macros/ ||
3609 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003610 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003611 # start or end of block or continuation of declaration
3612 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3613 # bitfield continuation
3614 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3615 # other possible extensions of declaration lines
3616 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3617 # indentation of previous and current line are the same
3618 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003619 if (WARN("LINE_SPACING",
3620 "Missing a blank line after declarations\n" . $hereprev) &&
3621 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003622 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003623 }
Joe Perches3b617e32014-04-03 14:49:28 -07003624 }
3625
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003626# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003627# Exceptions:
3628# 1) within comments
3629# 2) indented preprocessor commands
3630# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003631 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003632 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003633 if (WARN("LEADING_SPACE",
3634 "please, no spaces at the start of a line\n" . $herevet) &&
3635 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003636 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003637 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003638 }
3639
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003640# check we are in a valid C source file if not then ignore this hunk
3641 next if ($realfile !~ /\.(h|c)$/);
3642
Joe Perches5751a242017-11-17 15:28:52 -08003643# check for unusual line ending [ or (
3644 if ($line =~ /^\+.*([\[\(])\s*$/) {
3645 CHK("OPEN_ENDED_LINE",
3646 "Lines should not end with a '$1'\n" . $herecurr);
3647 }
3648
Joe Perches4dbed762017-05-08 15:55:39 -07003649# check if this appears to be the start function declaration, save the name
3650 if ($sline =~ /^\+\{\s*$/ &&
3651 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3652 $context_function = $1;
3653 }
3654
3655# check if this appears to be the end of function declaration
3656 if ($sline =~ /^\+\}\s*$/) {
3657 undef $context_function;
3658 }
3659
Joe Perches032a4c02014-08-06 16:10:29 -07003660# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003661# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003662# if the previous line is a break or return and is indented 1 tab more...
3663 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3664 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003665 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3666 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3667 defined $lines[$linenr] &&
3668 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003669 WARN("UNNECESSARY_ELSE",
3670 "else is not generally useful after a break or return\n" . $hereprev);
3671 }
3672 }
3673
Joe Perchesc00df192014-08-06 16:11:01 -07003674# check indentation of a line with a break;
3675# if the previous line is a goto or return and is indented the same # of tabs
3676 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3677 my $tabs = $1;
3678 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3679 WARN("UNNECESSARY_BREAK",
3680 "break is not useful after a goto or return\n" . $hereprev);
3681 }
3682 }
3683
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003684# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003685 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003686 WARN("CVS_KEYWORD",
3687 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003688 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003689
Joe Perches56e77d72013-02-21 16:44:14 -08003690# check for old HOTPLUG __dev<foo> section markings
3691 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3692 WARN("HOTPLUG_SECTION",
3693 "Using $1 is unnecessary\n" . $herecurr);
3694 }
3695
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003696# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003697 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3698 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003699#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003700 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003701 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003702 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003703 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003704 $stat =~ s/\n./\n /g;
3705 $cond =~ s/\n./\n /g;
3706
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003707#print "linenr<$linenr> <$stat>\n";
3708 # If this statement has no statement boundaries within
3709 # it there is no point in retrying a statement scan
3710 # until we hit end of it.
3711 my $frag = $stat; $frag =~ s/;+\s*$//;
3712 if ($frag !~ /(?:{|;)/) {
3713#print "skip<$line_nr_next>\n";
3714 $suppress_statement = $line_nr_next;
3715 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003716
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003717 # Find the real next line.
3718 $realline_next = $line_nr_next;
3719 if (defined $realline_next &&
3720 (!defined $lines[$realline_next - 1] ||
3721 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3722 $realline_next++;
3723 }
3724
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003725 my $s = $stat;
3726 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003727
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003728 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003729 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003730
3731 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003732 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003733
Andy Whitcroft463f2862009-09-21 17:04:34 -07003734 } elsif ($s =~ /^.\s*else\b/s) {
3735
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003736 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003737 } 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 -07003738 my $type = $1;
3739 $type =~ s/\s+/ /g;
3740 possible($type, "A:" . $s);
3741
Andy Whitcroft8905a672007-11-28 16:21:06 -08003742 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003743 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003744 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003745 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003746
3747 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003748 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003749 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003750 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003751
3752 # Check for any sort of function declaration.
3753 # int foo(something bar, other baz);
3754 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003755 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 -08003756 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003757
Andy Whitcroftcf655042008-03-04 14:28:20 -08003758 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003759 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003760 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003761
Andy Whitcroft8905a672007-11-28 16:21:06 -08003762 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003763 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003764
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003765 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003766 }
3767 }
3768 }
3769
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003770 }
3771
Andy Whitcroft653d4872007-06-23 17:16:34 -07003772#
3773# Checks which may be anchored in the context.
3774#
3775
3776# Check for switch () and associated case and default
3777# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003778 if ($line=~/\bswitch\s*\(.*\)/) {
3779 my $err = '';
3780 my $sep = '';
3781 my @ctx = ctx_block_outer($linenr, $realcnt);
3782 shift(@ctx);
3783 for my $ctx (@ctx) {
3784 my ($clen, $cindent) = line_stats($ctx);
3785 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3786 $indent != $cindent) {
3787 $err .= "$sep$ctx\n";
3788 $sep = '';
3789 } else {
3790 $sep = "[...]\n";
3791 }
3792 }
3793 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003794 ERROR("SWITCH_CASE_INDENT_LEVEL",
3795 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003796 }
3797 }
3798
3799# if/while/etc brace do not go on next line, unless defining a do while loop,
3800# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003801 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 -07003802 my $pre_ctx = "$1$2";
3803
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003804 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003805
3806 if ($line =~ /^\+\t{6,}/) {
3807 WARN("DEEP_INDENTATION",
3808 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3809 }
3810
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003811 my $ctx_cnt = $realcnt - $#ctx - 1;
3812 my $ctx = join("\n", @ctx);
3813
Andy Whitcroft548596d2008-07-23 21:29:01 -07003814 my $ctx_ln = $linenr;
3815 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003816
Andy Whitcroft548596d2008-07-23 21:29:01 -07003817 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3818 defined $lines[$ctx_ln - 1] &&
3819 $lines[$ctx_ln - 1] =~ /^-/)) {
3820 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3821 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003822 $ctx_ln++;
3823 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003824
Andy Whitcroft53210162008-07-23 21:29:03 -07003825 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3826 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003827
Joe Perchesd752fcc2014-08-06 16:11:05 -07003828 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003829 ERROR("OPEN_BRACE",
3830 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003831 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003832 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003833 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3834 $ctx =~ /\)\s*\;\s*$/ &&
3835 defined $lines[$ctx_ln - 1])
3836 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003837 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3838 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003839 WARN("TRAILING_SEMICOLON",
3840 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003841 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003842 }
3843 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003844 }
3845
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003846# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003847 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 -08003848 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3849 ctx_statement_block($linenr, $realcnt, 0)
3850 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003851 my ($s, $c) = ($stat, $cond);
3852
3853 substr($s, 0, length($c), '');
3854
Joe Perches9f5af482015-09-09 15:37:30 -07003855 # remove inline comments
3856 $s =~ s/$;/ /g;
3857 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003858
3859 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003860 my @newlines = ($c =~ /\n/gs);
3861 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003862
Joe Perches9f5af482015-09-09 15:37:30 -07003863 # Make sure we remove the line prefixes as we have
3864 # none on the first line, and are going to readd them
3865 # where necessary.
3866 $s =~ s/\n./\n/gs;
3867 while ($s =~ /\n\s+\\\n/) {
3868 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3869 }
3870
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003871 # We want to check the first line inside the block
3872 # starting at the end of the conditional, so remove:
3873 # 1) any blank line termination
3874 # 2) any opening brace { on end of the line
3875 # 3) any do (...) {
3876 my $continuation = 0;
3877 my $check = 0;
3878 $s =~ s/^.*\bdo\b//;
3879 $s =~ s/^\s*{//;
3880 if ($s =~ s/^\s*\\//) {
3881 $continuation = 1;
3882 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003883 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003884 $check = 1;
3885 $cond_lines++;
3886 }
3887
3888 # Also ignore a loop construct at the end of a
3889 # preprocessor statement.
3890 if (($prevline =~ /^.\s*#\s*define\s/ ||
3891 $prevline =~ /\\\s*$/) && $continuation == 0) {
3892 $check = 0;
3893 }
3894
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003895 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003896 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003897 while ($cond_ptr != $cond_lines) {
3898 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003899
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003900 # If we see an #else/#elif then the code
3901 # is not linear.
3902 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3903 $check = 0;
3904 }
3905
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003906 # Ignore:
3907 # 1) blank lines, they should be at 0,
3908 # 2) preprocessor lines, and
3909 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003910 if ($continuation ||
3911 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003912 $s =~ /^\s*#\s*?/ ||
3913 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003914 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003915 if ($s =~ s/^.*?\n//) {
3916 $cond_lines++;
3917 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003918 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003919 }
3920
3921 my (undef, $sindent) = line_stats("+" . $s);
3922 my $stat_real = raw_line($linenr, $cond_lines);
3923
3924 # Check if either of these lines are modified, else
3925 # this is not this patch's fault.
3926 if (!defined($stat_real) ||
3927 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3928 $check = 0;
3929 }
3930 if (defined($stat_real) && $cond_lines > 1) {
3931 $stat_real = "[...]\n$stat_real";
3932 }
3933
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003934 #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 -07003935
Joe Perches9f5af482015-09-09 15:37:30 -07003936 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07003937 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07003938 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003939 ($sindent == $indent &&
3940 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07003941 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003942 WARN("SUSPECT_CODE_INDENT",
3943 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003944 }
3945 }
3946
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003947 # Track the 'values' across context and added lines.
3948 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003949 my ($curr_values, $curr_vars) =
3950 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003951 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003952 if ($dbg_values) {
3953 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003954 print "$linenr > .$outline\n";
3955 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003956 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003957 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003958 $prev_values = substr($curr_values, -1);
3959
Andy Whitcroft00df3442007-06-08 13:47:06 -07003960#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003961 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003962
Joe Perches99ca38c2020-10-15 20:12:09 -07003963# check for self assignments used to avoid compiler warnings
3964# e.g.: int foo = foo, *bar = NULL;
3965# struct foo bar = *(&(bar));
3966 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
3967 my $var = $1;
3968 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
3969 WARN("SELF_ASSIGNMENT",
3970 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
3971 }
3972 }
3973
Joe Perches11ca40a2016-12-12 16:46:31 -08003974# check for dereferences that span multiple lines
3975 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3976 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3977 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3978 my $ref = $1;
3979 $line =~ /^.\s*($Lval)/;
3980 $ref .= $1;
3981 $ref =~ s/\s//g;
3982 WARN("MULTILINE_DEREFERENCE",
3983 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3984 }
3985
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003986# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003987 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 -07003988 my $type = $1;
3989 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003990 $var = "" if (!defined $var);
3991 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003992 my $sign = $1;
3993 my $pointer = $2;
3994
3995 $pointer = "" if (!defined $pointer);
3996
3997 if (WARN("UNSPECIFIED_INT",
3998 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3999 $fix) {
4000 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07004001 my $comp_pointer = $pointer;
4002 $comp_pointer =~ s/\s//g;
4003 $decl .= $comp_pointer;
4004 $decl = rtrim($decl) if ($var eq "");
4005 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07004006 }
4007 }
4008 }
4009
Andy Whitcroft653d4872007-06-23 17:16:34 -07004010# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07004011 if ($dbg_type) {
4012 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004013 ERROR("TEST_TYPE",
4014 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004015 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004016 ERROR("TEST_NOT_TYPE",
4017 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07004018 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004019 next;
4020 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004021# TEST: allow direct testing of the attribute matcher.
4022 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004023 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004024 ERROR("TEST_ATTR",
4025 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08004026 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004027 ERROR("TEST_NOT_ATTR",
4028 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07004029 }
4030 next;
4031 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004032
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004033# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07004034 if ($line =~ /^.\s*{/ &&
4035 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07004036 if (ERROR("OPEN_BRACE",
4037 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004038 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4039 fix_delete_line($fixlinenr - 1, $prevrawline);
4040 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004041 my $fixedline = $prevrawline;
4042 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004043 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004044 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004045 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004046 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004047 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004048 }
4049
Andy Whitcroft653d4872007-06-23 17:16:34 -07004050#
4051# Checks which are anchored on the added line.
4052#
4053
4054# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004055 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07004056 my $path = $1;
4057 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004058 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08004059 "malformed #include filename\n" . $herecurr);
4060 }
4061 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4062 ERROR("UAPI_INCLUDE",
4063 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004064 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004065 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004066
4067# no C99 // comments
4068 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07004069 if (ERROR("C99_COMMENTS",
4070 "do not use C99 // comments\n" . $herecurr) &&
4071 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004072 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07004073 if ($line =~ /\/\/(.*)$/) {
4074 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07004075 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07004076 }
4077 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004078 }
4079 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004080 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004081 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004082
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004083# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4084# the whole statement.
4085#print "APW <$lines[$realline_next - 1]>\n";
4086 if (defined $realline_next &&
4087 exists $lines[$realline_next - 1] &&
4088 !defined $suppress_export{$realline_next} &&
4089 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4090 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004091 # Handle definitions which produce identifiers with
4092 # a prefix:
4093 # XXX(foo);
4094 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004095 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08004096 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004097 $name =~ /^${Ident}_$2/) {
4098#print "FOO C name<$name>\n";
4099 $suppress_export{$realline_next} = 1;
4100
4101 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004102 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07004103 ^.DEFINE_$Ident\(\Q$name\E\)|
4104 ^.DECLARE_$Ident\(\Q$name\E\)|
4105 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004106 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4107 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07004108 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004109#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4110 $suppress_export{$realline_next} = 2;
4111 } else {
4112 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004113 }
4114 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004115 if (!defined $suppress_export{$linenr} &&
4116 $prevline =~ /^.\s*$/ &&
4117 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4118 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4119#print "FOO B <$lines[$linenr - 1]>\n";
4120 $suppress_export{$linenr} = 2;
4121 }
4122 if (defined $suppress_export{$linenr} &&
4123 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004124 WARN("EXPORT_SYMBOL",
4125 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004126 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004127
Joe Eloff5150bda2010-08-09 17:21:00 -07004128# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004129 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004130 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004131 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004132 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004133 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004134 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004135 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004136# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004137 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004138 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004139 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004140 $herecurr) &&
4141 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004142 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004143 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004144 }
4145
Joe Perches18130872014-08-06 16:11:22 -07004146# check for misordered declarations of char/short/int/long with signed/unsigned
4147 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4148 my $tmp = trim($1);
4149 WARN("MISORDERED_TYPE",
4150 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4151 }
4152
Joe Perches809e0822018-08-21 21:58:12 -07004153# check for unnecessary <signed> int declarations of short/long/long long
4154 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4155 my $type = trim($1);
4156 next if ($type !~ /\bint\b/);
4157 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4158 my $new_type = $type;
4159 $new_type =~ s/\b\s*int\s*\b/ /;
4160 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4161 $new_type =~ s/^const\s+//;
4162 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4163 $new_type = "const $new_type" if ($type =~ /^const\b/);
4164 $new_type =~ s/\s+/ /g;
4165 $new_type = trim($new_type);
4166 if (WARN("UNNECESSARY_INT",
4167 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4168 $fix) {
4169 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4170 }
4171 }
4172
Joe Perchescb710ec2010-10-26 14:23:20 -07004173# check for static const char * arrays.
4174 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004175 WARN("STATIC_CONST_CHAR_ARRAY",
4176 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004177 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004178 }
4179
4180# check for initialized const char arrays that should be static const
4181 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4182 if (WARN("STATIC_CONST_CHAR_ARRAY",
4183 "const array should probably be static const\n" . $herecurr) &&
4184 $fix) {
4185 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4186 }
4187 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004188
4189# check for static char foo[] = "bar" declarations.
4190 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004191 WARN("STATIC_CONST_CHAR_ARRAY",
4192 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004193 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004194 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004195
Joe Perchesab7e23f2015-04-16 12:44:22 -07004196# check for const <foo> const where <foo> is not a pointer or array type
4197 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4198 my $found = $1;
4199 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4200 WARN("CONST_CONST",
4201 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4202 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4203 WARN("CONST_CONST",
4204 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4205 }
4206 }
4207
Joe Perches9b0fa602014-04-03 14:49:18 -07004208# check for non-global char *foo[] = {"bar", ...} declarations.
4209 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4210 WARN("STATIC_CONST_CHAR_ARRAY",
4211 "char * array declaration might be better as static const\n" .
4212 $herecurr);
4213 }
4214
Joe Perchesb598b672015-04-16 12:44:36 -07004215# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4216 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4217 my $array = $1;
4218 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4219 my $array_div = $1;
4220 if (WARN("ARRAY_SIZE",
4221 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4222 $fix) {
4223 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4224 }
4225 }
4226 }
4227
Joe Perchesb36190c2014-01-27 17:07:18 -08004228# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004229 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004230 if (ERROR("FUNCTION_WITHOUT_ARGS",
4231 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4232 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004233 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004234 }
4235 }
4236
Andy Whitcroft653d4872007-06-23 17:16:34 -07004237# check for new typedefs, only function parameters and sparse annotations
4238# make sense.
4239 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004240 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004241 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004242 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004243 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004244 WARN("NEW_TYPEDEFS",
4245 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004246 }
4247
4248# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004249 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004250 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4251 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004252 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004253
Andy Whitcroft65863862009-01-06 14:41:21 -08004254 # Should start with a space.
4255 $to =~ s/^(\S)/ $1/;
4256 # Should not end with a space.
4257 $to =~ s/\s+$//;
4258 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004259 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004260 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004261
Joe Perches3705ce52013-07-03 15:05:31 -07004262## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004263 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004264 if (ERROR("POINTER_LOCATION",
4265 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4266 $fix) {
4267 my $sub_from = $ident;
4268 my $sub_to = $ident;
4269 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004270 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004271 s@\Q$sub_from\E@$sub_to@;
4272 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004273 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004274 }
4275 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4276 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004277 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004278
Andy Whitcroft65863862009-01-06 14:41:21 -08004279 # Should start with a space.
4280 $to =~ s/^(\S)/ $1/;
4281 # Should not end with a space.
4282 $to =~ s/\s+$//;
4283 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004284 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004285 }
4286 # Modifiers should have spaces.
4287 $to =~ s/(\b$Modifier$)/$1 /;
4288
Joe Perches3705ce52013-07-03 15:05:31 -07004289## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004290 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004291 if (ERROR("POINTER_LOCATION",
4292 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4293 $fix) {
4294
4295 my $sub_from = $match;
4296 my $sub_to = $match;
4297 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004298 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004299 s@\Q$sub_from\E@$sub_to@;
4300 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004301 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004302 }
4303
Joe Perches9d3e3c72015-09-09 15:37:27 -07004304# avoid BUG() or BUG_ON()
4305 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004306 my $msg_level = \&WARN;
4307 $msg_level = \&CHK if ($file);
4308 &{$msg_level}("AVOID_BUG",
4309 "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 -07004310 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004311
Joe Perches9d3e3c72015-09-09 15:37:27 -07004312# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004313 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004314 WARN("LINUX_VERSION_CODE",
4315 "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 -08004316 }
4317
Joe Perches17441222011-06-15 15:08:17 -07004318# check for uses of printk_ratelimit
4319 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004320 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004321 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004322 }
4323
Joe Percheseeef5732017-11-17 15:28:41 -08004324# printk should use KERN_* levels
4325 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4326 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4327 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004328 }
4329
Joe Perches243f3802012-05-31 16:26:09 -07004330 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4331 my $orig = $1;
4332 my $level = lc($orig);
4333 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004334 my $level2 = $level;
4335 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004336 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004337 "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 -07004338 }
4339
Joe Perchesdc139312013-02-21 16:44:13 -08004340 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4341 my $orig = $1;
4342 my $level = lc($orig);
4343 $level = "warn" if ($level eq "warning");
4344 $level = "dbg" if ($level eq "debug");
4345 WARN("PREFER_DEV_LEVEL",
4346 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4347 }
4348
Nicolas Boichat8020b252020-10-15 20:12:02 -07004349# trace_printk should not be used in production code.
4350 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4351 WARN("TRACE_PRINTK",
4352 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4353 }
4354
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004355# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4356# number of false positives, but assembly files are not checked, so at
4357# least the arch entry code will not trigger this warning.
4358 if ($line =~ /\bENOSYS\b/) {
4359 WARN("ENOSYS",
4360 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4361 }
4362
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004363# ENOTSUPP is not a standard error code and should be avoided in new patches.
4364# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4365# Similarly to ENOSYS warning a small number of false positives is expected.
4366 if (!$file && $line =~ /\bENOTSUPP\b/) {
4367 if (WARN("ENOTSUPP",
4368 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4369 $fix) {
4370 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4371 }
4372 }
4373
Andy Whitcroft653d4872007-06-23 17:16:34 -07004374# function brace can't be on same line, except for #defines of do while,
4375# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004376 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004377 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4378 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4379 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004380 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004381 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004382 $fix) {
4383 fix_delete_line($fixlinenr, $rawline);
4384 my $fixed_line = $rawline;
4385 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4386 my $line1 = $1;
4387 my $line2 = $2;
4388 fix_insert_line($fixlinenr, ltrim($line1));
4389 fix_insert_line($fixlinenr, "\+{");
4390 if ($line2 !~ /^\s*$/) {
4391 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4392 }
4393 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004394 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004395
Andy Whitcroft8905a672007-11-28 16:21:06 -08004396# open braces for enum, union and struct go on the same line.
4397 if ($line =~ /^.\s*{/ &&
4398 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004399 if (ERROR("OPEN_BRACE",
4400 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4401 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4402 fix_delete_line($fixlinenr - 1, $prevrawline);
4403 fix_delete_line($fixlinenr, $rawline);
4404 my $fixedline = rtrim($prevrawline) . " {";
4405 fix_insert_line($fixlinenr, $fixedline);
4406 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004407 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004408 if ($fixedline !~ /^\+\s*$/) {
4409 fix_insert_line($fixlinenr, $fixedline);
4410 }
4411 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004412 }
4413
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004414# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004415 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4416 if (WARN("SPACING",
4417 "missing space after $1 definition\n" . $herecurr) &&
4418 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004419 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004420 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4421 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004422 }
4423
Joe Perches31070b52014-01-23 15:54:49 -08004424# Function pointer declarations
4425# check spacing between type, funcptr, and args
4426# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004427 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004428 my $declare = $1;
4429 my $pre_pointer_space = $2;
4430 my $post_pointer_space = $3;
4431 my $funcname = $4;
4432 my $post_funcname_space = $5;
4433 my $pre_args_space = $6;
4434
Joe Perches91f72e92014-04-03 14:49:12 -07004435# the $Declare variable will capture all spaces after the type
4436# so check it for a missing trailing missing space but pointer return types
4437# don't need a space so don't warn for those.
4438 my $post_declare_space = "";
4439 if ($declare =~ /(\s+)$/) {
4440 $post_declare_space = $1;
4441 $declare = rtrim($declare);
4442 }
4443 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004444 WARN("SPACING",
4445 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004446 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004447 }
4448
4449# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004450# This test is not currently implemented because these declarations are
4451# equivalent to
4452# int foo(int bar, ...)
4453# and this is form shouldn't/doesn't generate a checkpatch warning.
4454#
4455# elsif ($declare =~ /\s{2,}$/) {
4456# WARN("SPACING",
4457# "Multiple spaces after return type\n" . $herecurr);
4458# }
Joe Perches31070b52014-01-23 15:54:49 -08004459
4460# unnecessary space "type ( *funcptr)(args...)"
4461 if (defined $pre_pointer_space &&
4462 $pre_pointer_space =~ /^\s/) {
4463 WARN("SPACING",
4464 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4465 }
4466
4467# unnecessary space "type (* funcptr)(args...)"
4468 if (defined $post_pointer_space &&
4469 $post_pointer_space =~ /^\s/) {
4470 WARN("SPACING",
4471 "Unnecessary space before function pointer name\n" . $herecurr);
4472 }
4473
4474# unnecessary space "type (*funcptr )(args...)"
4475 if (defined $post_funcname_space &&
4476 $post_funcname_space =~ /^\s/) {
4477 WARN("SPACING",
4478 "Unnecessary space after function pointer name\n" . $herecurr);
4479 }
4480
4481# unnecessary space "type (*funcptr) (args...)"
4482 if (defined $pre_args_space &&
4483 $pre_args_space =~ /^\s/) {
4484 WARN("SPACING",
4485 "Unnecessary space before function pointer arguments\n" . $herecurr);
4486 }
4487
4488 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004489 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004490 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004491 }
4492 }
4493
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004494# check for spacing round square brackets; allowed:
4495# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004496# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4497# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004498 while ($line =~ /(.*?\s)\[/g) {
4499 my ($where, $prefix) = ($-[1], $1);
4500 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004501 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004502 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004503 if (ERROR("BRACKET_SPACE",
4504 "space prohibited before open square bracket '['\n" . $herecurr) &&
4505 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004506 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004507 s/^(\+.*?)\s+\[/$1\[/;
4508 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004509 }
4510 }
4511
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004512# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004513 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004514 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004515 my $ctx_before = substr($line, 0, $-[1]);
4516 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004517
4518 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004519 if ($name =~ /^(?:
4520 if|for|while|switch|return|case|
4521 volatile|__volatile__|
4522 __attribute__|format|__extension__|
4523 asm|__asm__)$/x)
4524 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004525 # cpp #define statements have non-optional spaces, ie
4526 # if there is a space between the name and the open
4527 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004528 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004529
4530 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004531 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004532
4533 # If this whole things ends with a type its most
4534 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004535 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004536
4537 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004538 if (WARN("SPACING",
4539 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4540 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004541 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004542 s/\b$name\s+\(/$name\(/;
4543 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004544 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004545 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004546
Andy Whitcroft653d4872007-06-23 17:16:34 -07004547# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004548 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004549 my $fixed_line = "";
4550 my $line_fixed = 0;
4551
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004552 my $ops = qr{
4553 <<=|>>=|<=|>=|==|!=|
4554 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4555 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004556 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004557 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004558 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004559 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004560
4561## print("element count: <" . $#elements . ">\n");
4562## foreach my $el (@elements) {
4563## print("el: <$el>\n");
4564## }
4565
4566 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004567 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004568
Joe Perches3705ce52013-07-03 15:05:31 -07004569 foreach my $el (@elements) {
4570 push(@fix_elements, substr($rawline, $off, length($el)));
4571 $off += length($el);
4572 }
4573
4574 $off = 0;
4575
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004576 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004577 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004578
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004579 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004580
4581 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4582
4583## print("n: <$n> good: <$good>\n");
4584
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004585 $off += length($elements[$n]);
4586
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004587 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004588 my $ca = substr($opline, 0, $off);
4589 my $cc = '';
4590 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4591 $cc = substr($opline, $off + length($elements[$n + 1]));
4592 }
4593 my $cb = "$ca$;$cc";
4594
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004595 my $a = '';
4596 $a = 'V' if ($elements[$n] ne '');
4597 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004598 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004599 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4600 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004601 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004602
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004603 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004604
4605 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004606 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004607 $c = 'V' if ($elements[$n + 2] ne '');
4608 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004609 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004610 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4611 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004612 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004613 } else {
4614 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004615 }
4616
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004617 my $ctx = "${a}x${c}";
4618
4619 my $at = "(ctx:$ctx)";
4620
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004621 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004622 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004623
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004624 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004625 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004626
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004627 # Get the full operator variant.
4628 my $opv = $op . substr($curr_vars, $off, 1);
4629
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004630 # Ignore operators passed as parameters.
4631 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004632 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004633
Andy Whitcroftcf655042008-03-04 14:28:20 -08004634# # Ignore comments
4635# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004636
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004637 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004638 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004639 if ($ctx !~ /.x[WEBC]/ &&
4640 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004641 if (ERROR("SPACING",
4642 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004643 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004644 $line_fixed = 1;
4645 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004646 }
4647
4648 # // is a comment
4649 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004650
Joe Perchesb00e4812014-04-03 14:49:33 -07004651 # : when part of a bitfield
4652 } elsif ($opv eq ':B') {
4653 # skip the bitfield test for now
4654
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004655 # No spaces for:
4656 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004657 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004658 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004659 if (ERROR("SPACING",
4660 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004661 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004662 if (defined $fix_elements[$n + 2]) {
4663 $fix_elements[$n + 2] =~ s/^\s+//;
4664 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004665 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004666 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004667 }
4668
Joe Perches23810972014-12-10 15:51:32 -08004669 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004670 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004671 my $rtrim_before = 0;
4672 my $space_after = 0;
4673 if ($ctx =~ /Wx./) {
4674 if (ERROR("SPACING",
4675 "space prohibited before that '$op' $at\n" . $hereptr)) {
4676 $line_fixed = 1;
4677 $rtrim_before = 1;
4678 }
4679 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004680 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004681 if (ERROR("SPACING",
4682 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004683 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004684 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004685 $space_after = 1;
4686 }
4687 }
4688 if ($rtrim_before || $space_after) {
4689 if ($rtrim_before) {
4690 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4691 } else {
4692 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4693 }
4694 if ($space_after) {
4695 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004696 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004697 }
4698
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004699 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004700 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004701 #warn "'*' is part of type\n";
4702
4703 # unary operators should have a space before and
4704 # none after. May be left adjacent to another
4705 # unary operator, or a cast
4706 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004707 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004708 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004709 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004710 if (ERROR("SPACING",
4711 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004712 if ($n != $last_after + 2) {
4713 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4714 $line_fixed = 1;
4715 }
Joe Perches3705ce52013-07-03 15:05:31 -07004716 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004717 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004718 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004719 # A unary '*' may be const
4720
4721 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004722 if (ERROR("SPACING",
4723 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004724 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004725 if (defined $fix_elements[$n + 2]) {
4726 $fix_elements[$n + 2] =~ s/^\s+//;
4727 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004728 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004729 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004730 }
4731
4732 # unary ++ and unary -- are allowed no space on one side.
4733 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004734 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004735 if (ERROR("SPACING",
4736 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004737 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004738 $line_fixed = 1;
4739 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004740 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004741 if ($ctx =~ /Wx[BE]/ ||
4742 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004743 if (ERROR("SPACING",
4744 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004745 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004746 $line_fixed = 1;
4747 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004748 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004749 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004750 if (ERROR("SPACING",
4751 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004752 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004753 if (defined $fix_elements[$n + 2]) {
4754 $fix_elements[$n + 2] =~ s/^\s+//;
4755 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004756 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004757 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004758 }
4759
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004760 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004761 } elsif ($op eq '<<' or $op eq '>>' or
4762 $op eq '&' or $op eq '^' or $op eq '|' or
4763 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004764 $op eq '*' or $op eq '/' or
4765 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004766 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004767 if ($check) {
4768 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4769 if (CHK("SPACING",
4770 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4771 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4772 $fix_elements[$n + 2] =~ s/^\s+//;
4773 $line_fixed = 1;
4774 }
4775 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4776 if (CHK("SPACING",
4777 "space preferred before that '$op' $at\n" . $hereptr)) {
4778 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4779 $line_fixed = 1;
4780 }
4781 }
4782 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004783 if (ERROR("SPACING",
4784 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004785 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4786 if (defined $fix_elements[$n + 2]) {
4787 $fix_elements[$n + 2] =~ s/^\s+//;
4788 }
Joe Perches3705ce52013-07-03 15:05:31 -07004789 $line_fixed = 1;
4790 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004791 }
4792
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004793 # A colon needs no spaces before when it is
4794 # terminating a case value or a label.
4795 } elsif ($opv eq ':C' || $opv eq ':L') {
4796 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004797 if (ERROR("SPACING",
4798 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004799 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004800 $line_fixed = 1;
4801 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004802 }
4803
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004804 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004805 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004806 my $ok = 0;
4807
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004808 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004809 if (($op eq '<' &&
4810 $cc =~ /^\S+\@\S+>/) ||
4811 ($op eq '>' &&
4812 $ca =~ /<\S+\@\S+$/))
4813 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07004814 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004815 }
4816
Joe Perchese0df7e12015-04-16 12:44:53 -07004817 # for asm volatile statements
4818 # ignore a colon with another
4819 # colon immediately before or after
4820 if (($op eq ':') &&
4821 ($ca =~ /:$/ || $cc =~ /^:/)) {
4822 $ok = 1;
4823 }
4824
Joe Perches84731622013-11-12 15:10:05 -08004825 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004826 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004827 my $msg_level = \&ERROR;
4828 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004829
Jean Delvare0675a8f2017-09-08 16:16:07 -07004830 if (&{$msg_level}("SPACING",
4831 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004832 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4833 if (defined $fix_elements[$n + 2]) {
4834 $fix_elements[$n + 2] =~ s/^\s+//;
4835 }
Joe Perches3705ce52013-07-03 15:05:31 -07004836 $line_fixed = 1;
4837 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004838 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004839 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004840 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004841
4842## print("n: <$n> GOOD: <$good>\n");
4843
4844 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004845 }
Joe Perches3705ce52013-07-03 15:05:31 -07004846
4847 if (($#elements % 2) == 0) {
4848 $fixed_line = $fixed_line . $fix_elements[$#elements];
4849 }
4850
Joe Perches194f66f2014-08-06 16:11:03 -07004851 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4852 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004853 }
4854
4855
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004856 }
4857
Joe Perches786b6322013-07-03 15:05:32 -07004858# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004859 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004860 if (WARN("SPACING",
4861 "space prohibited before semicolon\n" . $herecurr) &&
4862 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004863 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004864 s/^(\+.*\S)\s+;/$1;/;
4865 }
4866 }
4867
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004868# check for multiple assignments
4869 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004870 CHK("MULTIPLE_ASSIGNMENTS",
4871 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004872 }
4873
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004874## # check for multiple declarations, allowing for a function declaration
4875## # continuation.
4876## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4877## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4878##
4879## # Remove any bracketed sections to ensure we do not
4880## # falsly report the parameters of functions.
4881## my $ln = $line;
4882## while ($ln =~ s/\([^\(\)]*\)//g) {
4883## }
4884## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004885## WARN("MULTIPLE_DECLARATION",
4886## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004887## }
4888## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004889
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004890#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004891 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004892 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004893 if (ERROR("SPACING",
4894 "space required before the open brace '{'\n" . $herecurr) &&
4895 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004896 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004897 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004898 }
4899
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004900## # check for blank lines before declarations
4901## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4902## $prevrawline =~ /^.\s*$/) {
4903## WARN("SPACING",
4904## "No blank lines before declarations\n" . $hereprev);
4905## }
4906##
4907
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004908# closing brace should have a space following it when it has anything
4909# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07004910 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004911 if (ERROR("SPACING",
4912 "space required after that close brace '}'\n" . $herecurr) &&
4913 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004914 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004915 s/}((?!(?:,|;|\)))\S)/} $1/;
4916 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004917 }
4918
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004919# check spacing on square brackets
4920 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004921 if (ERROR("SPACING",
4922 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4923 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004924 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004925 s/\[\s+/\[/;
4926 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004927 }
4928 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004929 if (ERROR("SPACING",
4930 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4931 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004932 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004933 s/\s+\]/\]/;
4934 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004935 }
4936
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004937# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004938 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4939 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004940 if (ERROR("SPACING",
4941 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4942 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004943 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004944 s/\(\s+/\(/;
4945 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004946 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004947 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004948 $line !~ /for\s*\(.*;\s+\)/ &&
4949 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004950 if (ERROR("SPACING",
4951 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4952 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004953 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004954 s/\s+\)/\)/;
4955 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004956 }
4957
Joe Perchese2826fd2014-08-06 16:10:48 -07004958# check unnecessary parentheses around addressof/dereference single $Lvals
4959# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4960
4961 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004962 my $var = $1;
4963 if (CHK("UNNECESSARY_PARENTHESES",
4964 "Unnecessary parentheses around $var\n" . $herecurr) &&
4965 $fix) {
4966 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4967 }
4968 }
4969
4970# check for unnecessary parentheses around function pointer uses
4971# ie: (foo->bar)(); should be foo->bar();
4972# but not "if (foo->bar) (" to avoid some false positives
4973 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4974 my $var = $2;
4975 if (CHK("UNNECESSARY_PARENTHESES",
4976 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4977 $fix) {
4978 my $var2 = deparenthesize($var);
4979 $var2 =~ s/\s//g;
4980 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4981 }
4982 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004983
Joe Perches63b7c732017-09-08 16:16:01 -07004984# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004985# when !drivers/staging or command-line uses --strict
4986 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004987 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004988 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4989 my $if_stat = $1;
4990 my $test = substr($2, 1, -1);
4991 my $herectx;
4992 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4993 my $match = $1;
4994 # avoid parentheses around potential macro args
4995 next if ($match =~ /^\s*\w+\s*$/);
4996 if (!defined($herectx)) {
4997 $herectx = $here . "\n";
4998 my $cnt = statement_rawlines($if_stat);
4999 for (my $n = 0; $n < $cnt; $n++) {
5000 my $rl = raw_line($linenr, $n);
5001 $herectx .= $rl . "\n";
5002 last if $rl =~ /^[ \+].*\{/;
5003 }
5004 }
5005 CHK("UNNECESSARY_PARENTHESES",
5006 "Unnecessary parentheses around '$match'\n" . $herectx);
5007 }
5008 }
5009
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005010#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005011 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005012 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07005013 if (WARN("INDENTED_LABEL",
5014 "labels should not be indented\n" . $herecurr) &&
5015 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005016 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005017 s/^(.)\s+/$1/;
5018 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005019 }
5020
Joe Perches40873ab2020-10-15 20:11:56 -07005021# check if a statement with a comma should be two statements like:
5022# foo = bar(), /* comma should be semicolon */
5023# bar = baz();
5024 if (defined($stat) &&
5025 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5026 my $cnt = statement_rawlines($stat);
5027 my $herectx = get_stat_here($linenr, $cnt, $here);
5028 WARN("SUSPECT_COMMA_SEMICOLON",
5029 "Possible comma where semicolon could be used\n" . $herectx);
5030 }
5031
Joe Perches5b9553a2014-04-03 14:49:21 -07005032# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08005033 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005034 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07005035 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07005036 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5037 my $value = $1;
5038 $value = deparenthesize($value);
5039 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5040 ERROR("RETURN_PARENTHESES",
5041 "return is not a function, parentheses are not required\n" . $herecurr);
5042 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005043 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005044 ERROR("SPACING",
5045 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005046 }
5047 }
Joe Perches507e5142013-11-12 15:10:13 -08005048
Joe Perchesb43ae212014-06-23 13:22:07 -07005049# unnecessary return in a void function
5050# at end-of-function, with the previous line a single leading tab, then return;
5051# and the line before that not a goto label target like "out:"
5052 if ($sline =~ /^[ \+]}\s*$/ &&
5053 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5054 $linenr >= 3 &&
5055 $lines[$linenr - 3] =~ /^[ +]/ &&
5056 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07005057 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07005058 "void function return statements are not generally useful\n" . $hereprev);
5059 }
Joe Perches9819cf22014-06-04 16:12:09 -07005060
Joe Perches189248d2014-01-23 15:54:47 -08005061# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07005062 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08005063 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5064 my $openparens = $1;
5065 my $count = $openparens =~ tr@\(@\(@;
5066 my $msg = "";
5067 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5068 my $comp = $4; #Not $1 because of $LvalOrFunc
5069 $msg = " - maybe == should be = ?" if ($comp eq "==");
5070 WARN("UNNECESSARY_PARENTHESES",
5071 "Unnecessary parentheses$msg\n" . $herecurr);
5072 }
5073 }
5074
Joe Perchesc5595fa2015-09-09 15:37:58 -07005075# comparisons with a constant or upper case identifier on the left
5076# avoid cases like "foo + BAR < baz"
5077# only fix matches surrounded by parentheses to avoid incorrect
5078# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07005079 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005080 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5081 my $lead = $1;
5082 my $const = $2;
5083 my $comp = $3;
5084 my $to = $4;
5085 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07005086 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005087 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5088 WARN("CONSTANT_COMPARISON",
5089 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5090 $fix) {
5091 if ($comp eq "<") {
5092 $newcomp = ">";
5093 } elsif ($comp eq "<=") {
5094 $newcomp = ">=";
5095 } elsif ($comp eq ">") {
5096 $newcomp = "<";
5097 } elsif ($comp eq ">=") {
5098 $newcomp = "<=";
5099 }
5100 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5101 }
5102 }
5103
Joe Perchesf34e4a42015-04-16 12:44:19 -07005104# Return of what appears to be an errno should normally be negative
5105 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005106 my $name = $1;
5107 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07005108 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07005109 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005110 }
5111 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005112
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005113# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07005114 if ($line =~ /\b(if|while|for|switch)\(/) {
5115 if (ERROR("SPACING",
5116 "space required before the open parenthesis '('\n" . $herecurr) &&
5117 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005118 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005119 s/\b(if|while|for|switch)\(/$1 \(/;
5120 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005121 }
5122
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07005123# Check for illegal assignment in if conditional -- and check for trailing
5124# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005125 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08005126 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5127 ctx_statement_block($linenr, $realcnt, 0)
5128 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005129 my ($stat_next) = ctx_statement_block($line_nr_next,
5130 $remain_next, $off_next);
5131 $stat_next =~ s/\n./\n /g;
5132 ##print "stat<$stat> stat_next<$stat_next>\n";
5133
5134 if ($stat_next =~ /^\s*while\b/) {
5135 # If the statement carries leading newlines,
5136 # then count those as offsets.
5137 my ($whitespace) =
5138 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5139 my $offset =
5140 statement_rawlines($whitespace) - 1;
5141
5142 $suppress_whiletrailers{$line_nr_next +
5143 $offset} = 1;
5144 }
5145 }
5146 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005147 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005148 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005149 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005150
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005151 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches65b64b32020-08-11 18:35:10 -07005152 if (ERROR("ASSIGN_IN_IF",
5153 "do not use assignment in if condition\n" . $herecurr) &&
5154 $fix && $perl_version_ok) {
5155 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5156 my $space = $1;
5157 my $not = $2;
5158 my $statement = $3;
5159 my $assigned = $4;
5160 my $test = $8;
5161 my $against = $9;
5162 my $brace = $15;
5163 fix_delete_line($fixlinenr, $rawline);
5164 fix_insert_line($fixlinenr, "$space$statement;");
5165 my $newline = "${space}if (";
5166 $newline .= '!' if defined($not);
5167 $newline .= '(' if (defined $not && defined($test) && defined($against));
5168 $newline .= "$assigned";
5169 $newline .= " $test $against" if (defined($test) && defined($against));
5170 $newline .= ')' if (defined $not && defined($test) && defined($against));
5171 $newline .= ')';
5172 $newline .= " {" if (defined($brace));
5173 fix_insert_line($fixlinenr + 1, $newline);
5174 }
5175 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005176 }
5177
5178 # Find out what is on the end of the line after the
5179 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005180 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005181 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005182 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005183 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5184 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005185 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005186 # Find out how long the conditional actually is.
5187 my @newlines = ($c =~ /\n/gs);
5188 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005189 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005190
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005191 $stat_real = raw_line($linenr, $cond_lines)
5192 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005193 if (defined($stat_real) && $cond_lines > 1) {
5194 $stat_real = "[...]\n$stat_real";
5195 }
5196
Joe Perches000d1cc12011-07-25 17:13:25 -07005197 ERROR("TRAILING_STATEMENTS",
5198 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005199 }
5200 }
5201
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005202# Check for bitwise tests written as boolean
5203 if ($line =~ /
5204 (?:
5205 (?:\[|\(|\&\&|\|\|)
5206 \s*0[xX][0-9]+\s*
5207 (?:\&\&|\|\|)
5208 |
5209 (?:\&\&|\|\|)
5210 \s*0[xX][0-9]+\s*
5211 (?:\&\&|\|\||\)|\])
5212 )/x)
5213 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005214 WARN("HEXADECIMAL_BOOLEAN_TEST",
5215 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005216 }
5217
Andy Whitcroft8905a672007-11-28 16:21:06 -08005218# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005219 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5220 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005221 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005222 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005223 ERROR("TRAILING_STATEMENTS",
5224 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005225 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005226 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005227# if should not continue a brace
5228 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005229 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005230 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005231 $herecurr);
5232 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005233# case and default should not have general statements after them
5234 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5235 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005236 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005237 \s*return\s+
5238 )/xg)
5239 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005240 ERROR("TRAILING_STATEMENTS",
5241 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005242 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005243
5244 # Check for }<nl>else {, these must be at the same
5245 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005246 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5247 $previndent == $indent) {
5248 if (ERROR("ELSE_AFTER_BRACE",
5249 "else should follow close brace '}'\n" . $hereprev) &&
5250 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5251 fix_delete_line($fixlinenr - 1, $prevrawline);
5252 fix_delete_line($fixlinenr, $rawline);
5253 my $fixedline = $prevrawline;
5254 $fixedline =~ s/}\s*$//;
5255 if ($fixedline !~ /^\+\s*$/) {
5256 fix_insert_line($fixlinenr, $fixedline);
5257 }
5258 $fixedline = $rawline;
5259 $fixedline =~ s/^(.\s*)else/$1} else/;
5260 fix_insert_line($fixlinenr, $fixedline);
5261 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005262 }
5263
Joe Perches8b8856f2014-08-06 16:11:14 -07005264 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5265 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005266 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5267
5268 # Find out what is on the end of the line after the
5269 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005270 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005271 $s =~ s/\n.*//g;
5272
5273 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005274 if (ERROR("WHILE_AFTER_BRACE",
5275 "while should follow close brace '}'\n" . $hereprev) &&
5276 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5277 fix_delete_line($fixlinenr - 1, $prevrawline);
5278 fix_delete_line($fixlinenr, $rawline);
5279 my $fixedline = $prevrawline;
5280 my $trailing = $rawline;
5281 $trailing =~ s/^\+//;
5282 $trailing = trim($trailing);
5283 $fixedline =~ s/}\s*$/} $trailing/;
5284 fix_insert_line($fixlinenr, $fixedline);
5285 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005286 }
5287 }
5288
Joe Perches95e2c602013-07-03 15:05:20 -07005289#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005290 while ($line =~ m{($Constant|$Lval)}g) {
5291 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005292
Joe Perches95e2c602013-07-03 15:05:20 -07005293#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005294 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005295 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005296#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005297 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005298#Ignore SI style variants like nS, mV and dB
5299#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5300 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005301#Ignore some three character SI units explicitly, like MiB and KHz
5302 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005303 while ($var =~ m{($Ident)}g) {
5304 my $word = $1;
5305 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005306 if ($check) {
5307 seed_camelcase_includes();
5308 if (!$file && !$camelcase_file_seeded) {
5309 seed_camelcase_file($realfile);
5310 $camelcase_file_seeded = 1;
5311 }
5312 }
Joe Perches7e781f62013-09-11 14:23:55 -07005313 if (!defined $camelcase{$word}) {
5314 $camelcase{$word} = 1;
5315 CHK("CAMELCASE",
5316 "Avoid CamelCase: <$word>\n" . $herecurr);
5317 }
Joe Perches34456862013-07-03 15:05:34 -07005318 }
Joe Perches323c1262012-12-17 16:02:07 -08005319 }
5320 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005321
5322#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005323 if ($line =~ /\#\s*define.*\\\s+$/) {
5324 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5325 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5326 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005327 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005328 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005329 }
5330
Fabian Frederick0e212e02015-04-16 12:44:25 -07005331# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5332# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005333 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005334 my $file = "$1.h";
5335 my $checkfile = "include/linux/$file";
5336 if (-f "$root/$checkfile" &&
5337 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005338 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005339 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005340 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5341 if ($asminclude > 0) {
5342 if ($realfile =~ m{^arch/}) {
5343 CHK("ARCH_INCLUDE_LINUX",
5344 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5345 } else {
5346 WARN("INCLUDE_LINUX",
5347 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5348 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005349 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005350 }
5351 }
5352
Andy Whitcroft653d4872007-06-23 17:16:34 -07005353# multi-statement macros should be enclosed in a do while loop, grab the
5354# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005355# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07005356 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5357 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005358 my $ln = $linenr;
5359 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005360 my ($off, $dstat, $dcond, $rest);
5361 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005362 my $has_flow_statement = 0;
5363 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005364 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005365 ctx_statement_block($linenr, $realcnt, 0);
5366 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005367 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005368 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005369
Joe Perches08a28432014-10-13 15:51:55 -07005370 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005371 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005372
Joe Perchesf59b64b2016-10-11 13:52:08 -07005373 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5374 my $define_args = $1;
5375 my $define_stmt = $dstat;
5376 my @def_args = ();
5377
5378 if (defined $define_args && $define_args ne "") {
5379 $define_args = substr($define_args, 1, length($define_args) - 2);
5380 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005381 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005382 @def_args = split(",", $define_args);
5383 }
5384
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005385 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005386 $dstat =~ s/\\\n.//g;
5387 $dstat =~ s/^\s*//s;
5388 $dstat =~ s/\s*$//s;
5389
5390 # Flatten any parentheses and braces
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005391 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5392 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5393 $dstat =~ s/.\[[^\[\]]*\]/1u/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005394 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005395 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005396
Antonio Borneo342d3d22020-04-06 20:11:01 -07005397 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005398 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5399 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005400 {
5401 }
5402
Joe Perches42e15292016-03-15 14:58:01 -07005403 # Make asm volatile uses seem like a generic function
5404 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5405
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005406 my $exceptions = qr{
5407 $Declare|
5408 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005409 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005410 DECLARE_PER_CPU|
5411 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005412 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005413 union|
5414 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005415 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005416 ^\"|\"$|
5417 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005418 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005419 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005420
5421 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005422 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005423 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005424
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005425 if ($dstat ne '' &&
5426 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5427 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005428 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005429 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005430 $dstat !~ /$exceptions/ &&
5431 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005432 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005433 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Dwaipayan Ray2e44e802020-10-15 20:12:22 -07005434 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005435 $dstat !~ /^for\s*$Constant$/ && # for (...)
5436 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5437 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005438 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005439 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005440 {
Joe Perchese7955562017-05-08 15:55:48 -07005441 if ($dstat =~ /^\s*if\b/) {
5442 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5443 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5444 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005445 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5446 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5447 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005448 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005449 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005450 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005451
5452 }
Joe Perches52076492016-10-11 13:52:14 -07005453
5454 # Make $define_stmt single line, comment-free, etc
5455 my @stmt_array = split('\n', $define_stmt);
5456 my $first = 1;
5457 $define_stmt = "";
5458 foreach my $l (@stmt_array) {
5459 $l =~ s/\\$//;
5460 if ($first) {
5461 $define_stmt = $l;
5462 $first = 0;
5463 } elsif ($l =~ /^[\+ ]/) {
5464 $define_stmt .= substr($l, 1);
5465 }
5466 }
5467 $define_stmt =~ s/$;//g;
5468 $define_stmt =~ s/\s+/ /g;
5469 $define_stmt = trim($define_stmt);
5470
Joe Perchesf59b64b2016-10-11 13:52:08 -07005471# check if any macro arguments are reused (ignore '...' and 'type')
5472 foreach my $arg (@def_args) {
5473 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005474 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005475 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005476 $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 -07005477 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5478 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005479 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005480 if ($use_cnt > 1) {
5481 CHK("MACRO_ARG_REUSE",
5482 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005483 }
5484# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005485 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005486 ((defined($1) && $1 ne ',') ||
5487 (defined($2) && $2 ne ','))) {
5488 CHK("MACRO_ARG_PRECEDENCE",
5489 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005490 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005491 }
Joe Perches5023d342012-12-17 16:01:47 -08005492
Joe Perches08a28432014-10-13 15:51:55 -07005493# check for macros with flow control, but without ## concatenation
5494# ## concatenation is commonly a macro that defines a function so ignore those
5495 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005496 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005497 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005498
Joe Perches08a28432014-10-13 15:51:55 -07005499 WARN("MACRO_WITH_FLOW_CONTROL",
5500 "Macros with flow control statements should be avoided\n" . "$herectx");
5501 }
5502
Joe Perches481eb482012-12-17 16:01:56 -08005503# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005504
5505 } else {
5506 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005507 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5508 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005509 $line =~ /^\+.*\\$/) {
5510 WARN("LINE_CONTINUATIONS",
5511 "Avoid unnecessary line continuations\n" . $herecurr);
5512 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005513 }
5514
Joe Perchesb13edf72012-07-30 14:41:24 -07005515# do {} while (0) macro tests:
5516# single-statement macros do not need to be enclosed in do while (0) loop,
5517# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005518 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005519 $realfile !~ m@/vmlinux.lds.h$@ &&
5520 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5521 my $ln = $linenr;
5522 my $cnt = $realcnt;
5523 my ($off, $dstat, $dcond, $rest);
5524 my $ctx = '';
5525 ($dstat, $dcond, $ln, $cnt, $off) =
5526 ctx_statement_block($linenr, $realcnt, 0);
5527 $ctx = $dstat;
5528
5529 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005530 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005531
5532 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5533 my $stmts = $2;
5534 my $semis = $3;
5535
5536 $ctx =~ s/\n*$//;
5537 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005538 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005539
Joe Perchesac8e97f2012-08-21 16:15:53 -07005540 if (($stmts =~ tr/;/;/) == 1 &&
5541 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005542 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5543 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5544 }
5545 if (defined $semis && $semis ne "") {
5546 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5547 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5548 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005549 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5550 $ctx =~ s/\n*$//;
5551 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005552 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005553
5554 WARN("TRAILING_SEMICOLON",
5555 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005556 }
5557 }
5558
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005559# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005560 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5561 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005562 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005563 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005564 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5565 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005566 my @allowed = ();
5567 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005568 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005569 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005570 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005571 for my $chunk (@chunks) {
5572 my ($cond, $block) = @{$chunk};
5573
Andy Whitcroft773647a2008-03-28 14:15:58 -07005574 # If the condition carries leading newlines, then count those as offsets.
5575 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5576 my $offset = statement_rawlines($whitespace) - 1;
5577
Joe Perchesaad4f612012-03-23 15:02:19 -07005578 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005579 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5580
5581 # We have looked at and allowed this specific line.
5582 $suppress_ifbraces{$ln + $offset} = 1;
5583
5584 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005585 $ln += statement_rawlines($block) - 1;
5586
Andy Whitcroft773647a2008-03-28 14:15:58 -07005587 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005588
5589 $seen++ if ($block =~ /^\s*{/);
5590
Joe Perchesaad4f612012-03-23 15:02:19 -07005591 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005592 if (statement_lines($cond) > 1) {
5593 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005594 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005595 }
5596 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005597 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005598 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005599 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005600 if (statement_block_size($block) > 1) {
5601 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005602 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005603 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005604 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005605 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005606 if ($seen) {
5607 my $sum_allowed = 0;
5608 foreach (@allowed) {
5609 $sum_allowed += $_;
5610 }
5611 if ($sum_allowed == 0) {
5612 WARN("BRACES",
5613 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5614 } elsif ($sum_allowed != $allow &&
5615 $seen != $allow) {
5616 CHK("BRACES",
5617 "braces {} should be used on all arms of this statement\n" . $herectx);
5618 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005619 }
5620 }
5621 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005622 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005623 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005624 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005625
Andy Whitcroftcf655042008-03-04 14:28:20 -08005626 # Check the pre-context.
5627 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5628 #print "APW: ALLOWED: pre<$1>\n";
5629 $allowed = 1;
5630 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005631
5632 my ($level, $endln, @chunks) =
5633 ctx_statement_full($linenr, $realcnt, $-[0]);
5634
Andy Whitcroftcf655042008-03-04 14:28:20 -08005635 # Check the condition.
5636 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005637 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005638 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005639 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005640 }
5641 if (statement_lines($cond) > 1) {
5642 #print "APW: ALLOWED: cond<$cond>\n";
5643 $allowed = 1;
5644 }
5645 if ($block =~/\b(?:if|for|while)\b/) {
5646 #print "APW: ALLOWED: block<$block>\n";
5647 $allowed = 1;
5648 }
5649 if (statement_block_size($block) > 1) {
5650 #print "APW: ALLOWED: lines block<$block>\n";
5651 $allowed = 1;
5652 }
5653 # Check the post-context.
5654 if (defined $chunks[1]) {
5655 my ($cond, $block) = @{$chunks[1]};
5656 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005657 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005658 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005659 if ($block =~ /^\s*\{/) {
5660 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5661 $allowed = 1;
5662 }
5663 }
5664 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005665 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005666 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005667
Joe Perches000d1cc12011-07-25 17:13:25 -07005668 WARN("BRACES",
5669 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005670 }
5671 }
5672
Joe Perchese4c5bab2017-02-24 15:01:41 -08005673# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005674 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5675 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005676 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5677 }
5678
Joe Perches0979ae62012-12-17 16:01:59 -08005679# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005680 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005681 if (CHK("BRACES",
5682 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5683 $fix && $prevrawline =~ /^\+/) {
5684 fix_delete_line($fixlinenr - 1, $prevrawline);
5685 }
Joe Perches0979ae62012-12-17 16:01:59 -08005686 }
Joe Perches77b9a532013-07-03 15:05:29 -07005687 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005688 if (CHK("BRACES",
5689 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5690 $fix) {
5691 fix_delete_line($fixlinenr, $rawline);
5692 }
Joe Perches0979ae62012-12-17 16:01:59 -08005693 }
5694
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005695# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005696 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5697 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005698 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005699 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005700 }
5701
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005702# Check for user-visible strings broken across lines, which breaks the ability
5703# to grep for the string. Make exceptions when the previous string ends in a
5704# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5705# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005706 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005707 $prevline =~ /"\s*$/ &&
5708 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5709 if (WARN("SPLIT_STRING",
5710 "quoted string split across lines\n" . $hereprev) &&
5711 $fix &&
5712 $prevrawline =~ /^\+.*"\s*$/ &&
5713 $last_coalesced_string_linenr != $linenr - 1) {
5714 my $extracted_string = get_quoted_string($line, $rawline);
5715 my $comma_close = "";
5716 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5717 $comma_close = $1;
5718 }
5719
5720 fix_delete_line($fixlinenr - 1, $prevrawline);
5721 fix_delete_line($fixlinenr, $rawline);
5722 my $fixedline = $prevrawline;
5723 $fixedline =~ s/"\s*$//;
5724 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5725 fix_insert_line($fixlinenr - 1, $fixedline);
5726 $fixedline = $rawline;
5727 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5728 if ($fixedline !~ /\+\s*$/) {
5729 fix_insert_line($fixlinenr, $fixedline);
5730 }
5731 $last_coalesced_string_linenr = $linenr;
5732 }
5733 }
5734
5735# check for missing a space in a string concatenation
5736 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5737 WARN('MISSING_SPACE',
5738 "break quoted strings at a space character\n" . $hereprev);
5739 }
5740
Joe Perchese4b7d302017-05-08 15:55:51 -07005741# check for an embedded function name in a string when the function is known
5742# This does not work very well for -f --file checking as it depends on patch
5743# context providing the function name or a single line form for in-file
5744# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005745 if ($line =~ /^\+.*$String/ &&
5746 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005747 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5748 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005749 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005750 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005751 }
5752
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005753# check for spaces before a quoted newline
5754 if ($rawline =~ /^.*\".*\s\\n/) {
5755 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5756 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5757 $fix) {
5758 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5759 }
5760
5761 }
5762
Joe Perchesf17dba42014-10-13 15:51:51 -07005763# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005764 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5765 if (CHK("CONCATENATED_STRING",
5766 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5767 $fix) {
5768 while ($line =~ /($String)/g) {
5769 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5770 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5771 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5772 }
5773 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005774 }
5775
Joe Perches90ad30e2014-12-10 15:51:59 -08005776# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005777 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005778 if (WARN("STRING_FRAGMENTS",
5779 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5780 $fix) {
5781 while ($line =~ /($String)(?=\s*")/g) {
5782 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5783 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5784 }
5785 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005786 }
5787
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005788# check for non-standard and hex prefixed decimal printf formats
5789 my $show_L = 1; #don't show the same defect twice
5790 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005791 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005792 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005793 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005794 # check for %L
5795 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005796 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005797 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5798 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005799 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005800 # check for %Z
5801 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5802 WARN("PRINTF_Z",
5803 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5804 $show_Z = 0;
5805 }
5806 # check for 0x<decimal>
5807 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5808 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005809 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5810 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005811 }
5812
5813# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005814 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005815 WARN("LINE_CONTINUATIONS",
5816 "Avoid line continuations in quoted strings\n" . $herecurr);
5817 }
5818
Andy Whitcroft00df3442007-06-08 13:47:06 -07005819# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005820 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005821 WARN("IF_0",
5822 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5823 }
5824
5825# warn about #if 1
5826 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5827 WARN("IF_1",
5828 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005829 }
5830
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005831# check for needless "if (<foo>) fn(<foo>)" uses
5832 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005833 my $tested = quotemeta($1);
5834 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5835 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5836 my $func = $1;
5837 if (WARN('NEEDLESS_IF',
5838 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5839 $fix) {
5840 my $do_fix = 1;
5841 my $leading_tabs = "";
5842 my $new_leading_tabs = "";
5843 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5844 $leading_tabs = $1;
5845 } else {
5846 $do_fix = 0;
5847 }
5848 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5849 $new_leading_tabs = $1;
5850 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5851 $do_fix = 0;
5852 }
5853 } else {
5854 $do_fix = 0;
5855 }
5856 if ($do_fix) {
5857 fix_delete_line($fixlinenr - 1, $prevrawline);
5858 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5859 }
5860 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005861 }
5862 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005863
Joe Perchesebfdc402014-08-06 16:10:27 -07005864# check for unnecessary "Out of Memory" messages
5865 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5866 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5867 (defined $1 || defined $3) &&
5868 $linenr > 3) {
5869 my $testval = $2;
5870 my $testline = $lines[$linenr - 3];
5871
5872 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5873# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5874
Joe Perchese29a70f2019-03-07 16:28:35 -08005875 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5876 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005877 WARN("OOM_MESSAGE",
5878 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5879 }
5880 }
5881
Joe Perchesf78d98f2014-10-13 15:52:01 -07005882# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005883 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005884 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5885 my $level = $1;
5886 if (WARN("UNNECESSARY_KERN_LEVEL",
5887 "Possible unnecessary $level\n" . $herecurr) &&
5888 $fix) {
5889 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5890 }
5891 }
5892
Joe Perches45c55e92017-02-24 15:01:31 -08005893# check for logging continuations
5894 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5895 WARN("LOGGING_CONTINUATION",
5896 "Avoid logging continuation uses where feasible\n" . $herecurr);
5897 }
5898
Joe Perchesabb08a52014-12-10 15:51:46 -08005899# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005900 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005901 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5902 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5903 WARN("MASK_THEN_SHIFT",
5904 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5905 }
5906
Joe Perchesb75ac612014-12-10 15:52:02 -08005907# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005908 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005909 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5910 my $val = $1;
5911 my $equal = "!";
5912 $equal = "" if ($4 eq "!=");
5913 if (CHK("COMPARISON_TO_NULL",
5914 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5915 $fix) {
5916 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5917 }
5918 }
5919 }
5920
Joe Perches8716de32013-09-11 14:24:05 -07005921# check for bad placement of section $InitAttribute (e.g.: __initdata)
5922 if ($line =~ /(\b$InitAttribute\b)/) {
5923 my $attr = $1;
5924 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5925 my $ptr = $1;
5926 my $var = $2;
5927 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5928 ERROR("MISPLACED_INIT",
5929 "$attr should be placed after $var\n" . $herecurr)) ||
5930 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5931 WARN("MISPLACED_INIT",
5932 "$attr should be placed after $var\n" . $herecurr))) &&
5933 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005934 $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 -07005935 }
5936 }
5937 }
5938
Joe Perchese970b8842013-11-12 15:10:10 -08005939# check for $InitAttributeData (ie: __initdata) with const
5940 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5941 my $attr = $1;
5942 $attr =~ /($InitAttributePrefix)(.*)/;
5943 my $attr_prefix = $1;
5944 my $attr_type = $2;
5945 if (ERROR("INIT_ATTRIBUTE",
5946 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5947 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005948 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005949 s/$InitAttributeData/${attr_prefix}initconst/;
5950 }
5951 }
5952
5953# check for $InitAttributeConst (ie: __initconst) without const
5954 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5955 my $attr = $1;
5956 if (ERROR("INIT_ATTRIBUTE",
5957 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5958 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005959 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005960 /(^\+\s*(?:static\s+))/;
5961 $lead = rtrim($1);
5962 $lead = "$lead " if ($lead !~ /^\+$/);
5963 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005964 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005965 }
5966 }
5967
Joe Perchesc17893c2015-04-16 12:44:42 -07005968# check for __read_mostly with const non-pointer (should just be const)
5969 if ($line =~ /\b__read_mostly\b/ &&
5970 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5971 if (ERROR("CONST_READ_MOSTLY",
5972 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5973 $fix) {
5974 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5975 }
5976 }
5977
Joe Perchesfbdb8132014-04-03 14:49:14 -07005978# don't use __constant_<foo> functions outside of include/uapi/
5979 if ($realfile !~ m@^include/uapi/@ &&
5980 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5981 my $constant_func = $1;
5982 my $func = $constant_func;
5983 $func =~ s/^__constant_//;
5984 if (WARN("CONSTANT_CONVERSION",
5985 "$constant_func should be $func\n" . $herecurr) &&
5986 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005987 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005988 }
5989 }
5990
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005991# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005992 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005993 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005994 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005995 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005996 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005997 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07005998 }
5999 if ($delay > 2000) {
6000 WARN("LONG_UDELAY",
6001 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07006002 }
6003 }
6004
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006005# warn about unexpectedly long msleep's
6006 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6007 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006008 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006009 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07006010 }
6011 }
6012
Joe Perches36ec1932013-07-03 15:05:25 -07006013# check for comparisons of jiffies
6014 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6015 WARN("JIFFIES_COMPARISON",
6016 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6017 }
6018
Joe Perches9d7a34a2013-07-03 15:05:26 -07006019# check for comparisons of get_jiffies_64()
6020 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6021 WARN("JIFFIES_COMPARISON",
6022 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6023 }
6024
Andy Whitcroft00df3442007-06-08 13:47:06 -07006025# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006026# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07006027# print "#ifdef in C files should be avoided\n";
6028# print "$herecurr";
6029# $clean = 0;
6030# }
6031
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006032# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006033 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07006034 if (ERROR("SPACING",
6035 "exactly one space required after that #$1\n" . $herecurr) &&
6036 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006037 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07006038 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6039 }
6040
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006041 }
6042
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006043# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006044 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6045 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006046 my $which = $1;
6047 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006048 CHK("UNCOMMENTED_DEFINITION",
6049 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006050 }
6051 }
6052# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006053
6054 my $barriers = qr{
6055 mb|
6056 rmb|
Will Deaconad83ec62019-11-07 14:49:00 +00006057 wmb
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006058 }x;
6059 my $barrier_stems = qr{
6060 mb__before_atomic|
6061 mb__after_atomic|
6062 store_release|
6063 load_acquire|
6064 store_mb|
6065 (?:$barriers)
6066 }x;
6067 my $all_barriers = qr{
6068 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02006069 smp_(?:$barrier_stems)|
6070 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006071 }x;
6072
6073 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006074 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08006075 WARN("MEMORY_BARRIER",
6076 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006077 }
6078 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006079
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02006080 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6081
6082 if ($realfile !~ m@^include/asm-generic/@ &&
6083 $realfile !~ m@/barrier\.h$@ &&
6084 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6085 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6086 WARN("MEMORY_BARRIER",
6087 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6088 }
6089
Joe Perchescb426e92015-06-25 15:02:46 -07006090# check for waitqueue_active without a comment.
6091 if ($line =~ /\bwaitqueue_active\s*\(/) {
6092 if (!ctx_has_comment($first_line, $linenr)) {
6093 WARN("WAITQUEUE_ACTIVE",
6094 "waitqueue_active without comment\n" . $herecurr);
6095 }
6096 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006097
Marco Elver5099a722020-04-01 12:17:14 +02006098# check for data_race without a comment.
6099 if ($line =~ /\bdata_race\s*\(/) {
6100 if (!ctx_has_comment($first_line, $linenr)) {
6101 WARN("DATA_RACE",
6102 "data_race without comment\n" . $herecurr);
6103 }
6104 }
6105
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006106# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006107 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006108 CHK("ARCH_DEFINES",
6109 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006110 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006111
Joe Perches596ed452017-07-12 14:37:02 -07006112# check that the storage class is not after a type
6113 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006114 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07006115 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6116 }
6117# Check that the storage class is at the beginning of a declaration
6118 if ($line =~ /\b$Storage\b/ &&
6119 $line !~ /^.\s*$Storage/ &&
6120 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6121 $1 !~ /[\,\)]\s*$/) {
6122 WARN("STORAGE_CLASS",
6123 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07006124 }
6125
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006126# check the location of the inline attribute, that it is between
6127# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006128 if ($line =~ /\b$Type\s+$Inline\b/ ||
6129 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006130 ERROR("INLINE_LOCATION",
6131 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006132 }
6133
Andy Whitcroft8905a672007-11-28 16:21:06 -08006134# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08006135 if ($realfile !~ m@\binclude/uapi/@ &&
6136 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006137 if (WARN("INLINE",
6138 "plain inline is preferred over $1\n" . $herecurr) &&
6139 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006140 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006141
6142 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006143 }
6144
Joe Perches3d130fd2011-01-12 17:00:00 -08006145# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08006146 if ($realfile !~ m@\binclude/uapi/@ &&
6147 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006148 WARN("PREFER_PACKED",
6149 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08006150 }
6151
Joe Perches39b7e282011-07-25 17:13:24 -07006152# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08006153 if ($realfile !~ m@\binclude/uapi/@ &&
6154 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006155 WARN("PREFER_ALIGNED",
6156 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07006157 }
6158
Joe Perches462811d2019-09-25 16:46:44 -07006159# Check for __attribute__ section, prefer __section
6160 if ($realfile !~ m@\binclude/uapi/@ &&
6161 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
6162 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6163 my $new = substr($old, 1, -1);
6164 if (WARN("PREFER_SECTION",
6165 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6166 $fix) {
6167 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6168 }
6169 }
6170
Joe Perches5f14d3b2012-01-10 15:09:52 -08006171# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08006172 if ($realfile !~ m@\binclude/uapi/@ &&
6173 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006174 if (WARN("PREFER_PRINTF",
6175 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6176 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006177 $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 -07006178
6179 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08006180 }
6181
Joe Perches6061d942012-03-23 15:02:16 -07006182# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08006183 if ($realfile !~ m@\binclude/uapi/@ &&
6184 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006185 if (WARN("PREFER_SCANF",
6186 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6187 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006188 $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 -07006189 }
Joe Perches6061d942012-03-23 15:02:16 -07006190 }
6191
Joe Perches619a9082014-12-10 15:51:35 -08006192# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006193 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006194 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6195 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6196 $line =~ /\b__weak\b/)) {
6197 ERROR("WEAK_DECLARATION",
6198 "Using weak declarations can have unintended link defects\n" . $herecurr);
6199 }
6200
Tomas Winklerfd39f902016-12-12 16:46:34 -08006201# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006202 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006203 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006204 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6205 my $type = $1;
6206 if ($type =~ /\b($typeC99Typedefs)\b/) {
6207 $type = $1;
6208 my $kernel_type = 'u';
6209 $kernel_type = 's' if ($type =~ /^_*[si]/);
6210 $type =~ /(\d+)/;
6211 $kernel_type .= $1;
6212 if (CHK("PREFER_KERNEL_TYPES",
6213 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6214 $fix) {
6215 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6216 }
6217 }
6218 }
6219
Joe Perches938224b2016-01-20 14:59:15 -08006220# check for cast of C90 native int or longer types constants
6221 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6222 my $cast = $1;
6223 my $const = $2;
6224 if (WARN("TYPECAST_INT_CONSTANT",
6225 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6226 $fix) {
6227 my $suffix = "";
6228 my $newconst = $const;
6229 $newconst =~ s/${Int_type}$//;
6230 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6231 if ($cast =~ /\blong\s+long\b/) {
6232 $suffix .= 'LL';
6233 } elsif ($cast =~ /\blong\b/) {
6234 $suffix .= 'L';
6235 }
6236 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6237 }
6238 }
6239
Joe Perches8f53a9b2010-03-05 13:43:48 -08006240# check for sizeof(&)
6241 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006242 WARN("SIZEOF_ADDRESS",
6243 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006244 }
6245
Joe Perches66c80b62012-07-30 14:41:22 -07006246# check for sizeof without parenthesis
6247 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006248 if (WARN("SIZEOF_PARENTHESIS",
6249 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6250 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006251 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006252 }
Joe Perches66c80b62012-07-30 14:41:22 -07006253 }
6254
Joe Perches88982fe2012-12-17 16:02:00 -08006255# check for struct spinlock declarations
6256 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6257 WARN("USE_SPINLOCK_T",
6258 "struct spinlock should be spinlock_t\n" . $herecurr);
6259 }
6260
Joe Perchesa6962d72013-04-29 16:18:13 -07006261# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006262 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006263 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006264 $fmt =~ s/%%//g;
6265 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006266 if (WARN("PREFER_SEQ_PUTS",
6267 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6268 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006269 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006270 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006271 }
6272 }
6273
Joe Perches478b1792018-04-10 16:33:34 -07006274# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006275 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006276 defined $stat &&
6277 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6278 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006279 my $stat_real;
6280
Joe Perches0b523762017-05-08 15:55:36 -07006281 my $lc = $stat =~ tr@\n@@;
6282 $lc = $lc + $linenr;
6283 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006284 my $specifier;
6285 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006286 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006287 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006288 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6289 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006290
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006291 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006292 $specifier = $1;
6293 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006294 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006295 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006296 ($extension eq "f" &&
6297 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006298 $bad_specifier = $specifier;
6299 last;
6300 }
6301 if ($extension eq "x" && !defined($stat_real)) {
6302 if (!defined($stat_real)) {
6303 $stat_real = get_stat_real($linenr, $lc);
6304 }
6305 WARN("VSPRINTF_SPECIFIER_PX",
6306 "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");
6307 }
6308 }
6309 if ($bad_specifier ne "") {
6310 my $stat_real = get_stat_real($linenr, $lc);
6311 my $ext_type = "Invalid";
6312 my $use = "";
6313 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006314 $use = " - use %pS instead";
6315 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6316 }
6317
6318 WARN("VSPRINTF_POINTER_EXTENSION",
6319 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6320 }
Joe Perches0b523762017-05-08 15:55:36 -07006321 }
6322 }
6323
Andy Whitcroft554e1652012-01-10 15:09:57 -08006324# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006325 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006326 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006327 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006328
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006329 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006330 my $ms_val = $7;
6331 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006332
Andy Whitcroft554e1652012-01-10 15:09:57 -08006333 if ($ms_size =~ /^(0x|)0$/i) {
6334 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006335 "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 -08006336 } elsif ($ms_size =~ /^(0x|)1$/i) {
6337 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006338 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6339 }
6340 }
6341
Joe Perches98a9bba2014-01-23 15:54:52 -08006342# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006343# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006344# defined $stat &&
6345# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6346# if (WARN("PREFER_ETHER_ADDR_COPY",
6347# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6348# $fix) {
6349# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6350# }
6351# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006352
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006353# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006354# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006355# defined $stat &&
6356# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6357# WARN("PREFER_ETHER_ADDR_EQUAL",
6358# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6359# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006360
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006361# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6362# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006363# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006364# defined $stat &&
6365# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6366#
6367# my $ms_val = $7;
6368#
6369# if ($ms_val =~ /^(?:0x|)0+$/i) {
6370# if (WARN("PREFER_ETH_ZERO_ADDR",
6371# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6372# $fix) {
6373# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6374# }
6375# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6376# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6377# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6378# $fix) {
6379# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6380# }
6381# }
6382# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006383
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006384# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006385 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006386 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006387 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006388 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006389 my $call = $1;
6390 my $cast1 = deparenthesize($2);
6391 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006392 my $cast2 = deparenthesize($7);
6393 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006394 my $cast;
6395
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006396 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006397 $cast = "$cast1 or $cast2";
6398 } elsif ($cast1 ne "") {
6399 $cast = $cast1;
6400 } else {
6401 $cast = $cast2;
6402 }
6403 WARN("MINMAX",
6404 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006405 }
6406 }
6407
Joe Perches4a273192012-07-30 14:41:20 -07006408# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006409 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006410 defined $stat &&
6411 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6412 my $min = $1;
6413 my $max = $7;
6414 if ($min eq $max) {
6415 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006416 "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 -07006417 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6418 $min > $max) {
6419 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006420 "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 -07006421 }
6422 }
6423
Joe Perches823b7942013-11-12 15:10:15 -08006424# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006425 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006426 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006427 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006428 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6429 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6430 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6431 my $lc = $stat =~ tr@\n@@;
6432 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006433 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006434 WARN("NAKED_SSCANF",
6435 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6436 }
6437
Joe Perchesafc819a2014-06-04 16:12:08 -07006438# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006439 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006440 defined $stat &&
6441 $line =~ /\bsscanf\b/) {
6442 my $lc = $stat =~ tr@\n@@;
6443 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006444 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006445 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6446 my $format = $6;
6447 my $count = $format =~ tr@%@%@;
6448 if ($count == 1 &&
6449 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6450 WARN("SSCANF_TO_KSTRTO",
6451 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6452 }
6453 }
6454 }
6455
Joe Perches70dc8a42013-09-11 14:23:58 -07006456# check for new externs in .h files.
6457 if ($realfile =~ /\.h$/ &&
6458 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006459 if (CHK("AVOID_EXTERNS",
6460 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006461 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006462 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006463 }
6464 }
6465
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006466# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006467 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006468 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006469 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006470 my $function_name = $1;
6471 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006472
6473 my $s = $stat;
6474 if (defined $cond) {
6475 substr($s, 0, length($cond), '');
6476 }
Kees Cookd8b44b52020-06-03 14:18:09 -07006477 if ($s =~ /^\s*;/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006478 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006479 WARN("AVOID_EXTERNS",
6480 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006481 }
6482
6483 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006484 WARN("FUNCTION_ARGUMENTS",
6485 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006486 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006487
6488 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6489 $stat =~ /^.\s*extern\s+/)
6490 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006491 WARN("AVOID_EXTERNS",
6492 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006493 }
6494
Joe Perchesa0ad7592017-07-10 15:52:19 -07006495# check for function declarations that have arguments without identifier names
6496 if (defined $stat &&
Kees Cookd8b44b52020-06-03 14:18:09 -07006497 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6498 $1 ne "void") {
6499 my $args = trim($1);
Joe Perchesca0d8922016-10-11 13:52:16 -07006500 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6501 my $arg = trim($1);
Kees Cookd8b44b52020-06-03 14:18:09 -07006502 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006503 WARN("FUNCTION_ARGUMENTS",
6504 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6505 }
6506 }
6507 }
6508
Joe Perchesa0ad7592017-07-10 15:52:19 -07006509# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006510 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006511 defined $stat &&
6512 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6513 $context_function = $1;
6514
6515# check for multiline function definition with misplaced open brace
6516 my $ok = 0;
6517 my $cnt = statement_rawlines($stat);
6518 my $herectx = $here . "\n";
6519 for (my $n = 0; $n < $cnt; $n++) {
6520 my $rl = raw_line($linenr, $n);
6521 $herectx .= $rl . "\n";
6522 $ok = 1 if ($rl =~ /^[ \+]\{/);
6523 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6524 last if $rl =~ /^[ \+].*\{/;
6525 }
6526 if (!$ok) {
6527 ERROR("OPEN_BRACE",
6528 "open brace '{' following function definitions go on the next line\n" . $herectx);
6529 }
6530 }
6531
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006532# checks for new __setup's
6533 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6534 my $name = $1;
6535
6536 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006537 CHK("UNDOCUMENTED_SETUP",
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07006538 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006539 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006540 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006541
Joe Perchese29a70f2019-03-07 16:28:35 -08006542# check for pointless casting of alloc functions
6543 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006544 WARN("UNNECESSARY_CASTS",
6545 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006546 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006547
Joe Perchesa640d252013-07-03 15:05:21 -07006548# alloc style
6549# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006550 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006551 $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 -07006552 CHK("ALLOC_SIZEOF_STRUCT",
6553 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6554 }
6555
Joe Perches60a55362014-06-04 16:12:07 -07006556# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006557 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006558 defined $stat &&
6559 $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 -07006560 my $oldfunc = $3;
6561 my $a1 = $4;
6562 my $a2 = $10;
6563 my $newfunc = "kmalloc_array";
6564 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006565 my $r1 = $a1;
6566 my $r2 = $a2;
6567 if ($a1 =~ /^sizeof\s*\S/) {
6568 $r1 = $a2;
6569 $r2 = $a1;
6570 }
6571 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6572 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006573 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006574 my $herectx = get_stat_here($linenr, $cnt, $here);
6575
Joe Perches60a55362014-06-04 16:12:07 -07006576 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006577 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6578 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006579 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006580 $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 -07006581 }
6582 }
6583 }
6584
Joe Perches972fdea2013-04-29 16:18:12 -07006585# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006586 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006587 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6588 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006589 WARN("KREALLOC_ARG_REUSE",
6590 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6591 }
6592
Joe Perches5ce59ae2013-02-21 16:44:18 -08006593# check for alloc argument mismatch
6594 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6595 WARN("ALLOC_ARRAY_ARGS",
6596 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6597 }
6598
Joe Perchescaf2a542011-01-12 16:59:56 -08006599# check for multiple semicolons
6600 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006601 if (WARN("ONE_SEMICOLON",
6602 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6603 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006604 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006605 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006606 }
6607
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006608# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6609 if ($realfile !~ m@^include/uapi/@ &&
6610 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006611 my $ull = "";
6612 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6613 if (CHK("BIT_MACRO",
6614 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6615 $fix) {
6616 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6617 }
6618 }
6619
Joe Perches50161262020-08-11 18:35:07 -07006620# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006621 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
Joe Perches50161262020-08-11 18:35:07 -07006622 WARN("IS_ENABLED_CONFIG",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006623 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
Joe Perches50161262020-08-11 18:35:07 -07006624 }
6625
Joe Perches2d632742016-05-20 17:04:00 -07006626# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006627 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
Joe Perches2d632742016-05-20 17:04:00 -07006628 my $config = $1;
6629 if (WARN("PREFER_IS_ENABLED",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006630 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
Joe Perches2d632742016-05-20 17:04:00 -07006631 $fix) {
6632 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6633 }
6634 }
6635
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006636# check for /* fallthrough */ like comment, prefer fallthrough;
6637 my @fallthroughs = (
6638 'fallthrough',
6639 '@fallthrough@',
6640 'lint -fallthrough[ \t]*',
6641 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6642 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6643 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6644 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6645 );
6646 if ($raw_comment ne '') {
6647 foreach my $ft (@fallthroughs) {
6648 if ($raw_comment =~ /$ft/) {
6649 my $msg_level = \&WARN;
6650 $msg_level = \&CHK if ($file);
6651 &{$msg_level}("PREFER_FALLTHROUGH",
6652 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6653 last;
6654 }
6655 }
6656 }
6657
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006658# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006659 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006660 defined $stat &&
6661 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006662 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006663 my $herectx = get_stat_here($linenr, $cnt, $here);
6664
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006665 WARN("DEFAULT_NO_BREAK",
6666 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006667 }
6668
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006669# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006670 if ($line =~ /\b__FUNCTION__\b/) {
6671 if (WARN("USE_FUNC",
6672 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6673 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006674 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006675 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006676 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006677
Joe Perches62ec8182015-02-13 14:38:18 -08006678# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6679 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6680 ERROR("DATE_TIME",
6681 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6682 }
6683
Joe Perches2c924882012-03-23 15:02:20 -07006684# check for use of yield()
6685 if ($line =~ /\byield\s*\(\s*\)/) {
6686 WARN("YIELD",
6687 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6688 }
6689
Joe Perches179f8f42013-07-03 15:05:30 -07006690# check for comparisons against true and false
6691 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6692 my $lead = $1;
6693 my $arg = $2;
6694 my $test = $3;
6695 my $otype = $4;
6696 my $trail = $5;
6697 my $op = "!";
6698
6699 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6700
6701 my $type = lc($otype);
6702 if ($type =~ /^(?:true|false)$/) {
6703 if (("$test" eq "==" && "$type" eq "true") ||
6704 ("$test" eq "!=" && "$type" eq "false")) {
6705 $op = "";
6706 }
6707
6708 CHK("BOOL_COMPARISON",
6709 "Using comparison to $otype is error prone\n" . $herecurr);
6710
6711## maybe suggesting a correct construct would better
6712## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6713
6714 }
6715 }
6716
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006717# check for semaphores initialized locked
6718 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006719 WARN("CONSIDER_COMPLETION",
6720 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006721 }
Joe Perches6712d852012-03-23 15:02:20 -07006722
Joe Perches67d0a072011-10-31 17:13:10 -07006723# recommend kstrto* over simple_strto* and strict_strto*
6724 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006725 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006726 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006727 }
Joe Perches6712d852012-03-23 15:02:20 -07006728
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006729# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006730 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006731 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006732 "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 -07006733 }
Joe Perches6712d852012-03-23 15:02:20 -07006734
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006735# check for spin_is_locked(), suggest lockdep instead
6736 if ($line =~ /\bspin_is_locked\(/) {
6737 WARN("USE_LOCKDEP",
6738 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6739 }
6740
Joe Perches9189c7e2018-09-07 15:26:18 -07006741# check for deprecated apis
6742 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6743 my $deprecated_api = $1;
6744 my $new_api = $deprecated_apis{$deprecated_api};
6745 WARN("DEPRECATED_API",
6746 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6747 }
6748
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006749# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006750# and avoid what seem like struct definitions 'struct foo {'
Quentin Monnetced69da12020-08-11 18:35:13 -07006751 if (defined($const_structs) &&
6752 $line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006753 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006754 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006755 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006756 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006757
6758# use of NR_CPUS is usually wrong
6759# ignore definitions of NR_CPUS and usage to define arrays as likely right
6760 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006761 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6762 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006763 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6764 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6765 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006766 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006767 WARN("NR_CPUS",
6768 "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 -07006769 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006770
Joe Perches52ea8502013-11-12 15:10:09 -08006771# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6772 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6773 ERROR("DEFINE_ARCH_HAS",
6774 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6775 }
6776
Joe Perchesacd93622015-02-13 14:38:38 -08006777# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006778 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006779 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6780 WARN("LIKELY_MISUSE",
6781 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6782 }
6783
Denis Efremovde3f1862019-09-25 16:49:25 -07006784# nested likely/unlikely calls
6785 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6786 WARN("LIKELY_MISUSE",
6787 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6788 }
6789
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006790# whine mightly about in_atomic
6791 if ($line =~ /\bin_atomic\s*\(/) {
6792 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006793 ERROR("IN_ATOMIC",
6794 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006795 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006796 WARN("IN_ATOMIC",
6797 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006798 }
6799 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006800
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006801# check for mutex_trylock_recursive usage
6802 if ($line =~ /mutex_trylock_recursive/) {
6803 ERROR("LOCKING",
6804 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6805 }
6806
Peter Zijlstra1704f472010-03-19 01:37:42 +01006807# check for lockdep_set_novalidate_class
6808 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6809 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6810 if ($realfile !~ m@^kernel/lockdep@ &&
6811 $realfile !~ m@^include/linux/lockdep@ &&
6812 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006813 ERROR("LOCKDEP",
6814 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006815 }
6816 }
Dave Jones88f88312011-01-12 16:59:59 -08006817
Joe Perchesb392c642015-04-16 12:44:16 -07006818 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6819 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006820 WARN("EXPORTED_WORLD_WRITABLE",
6821 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006822 }
Joe Perches24358802014-04-03 14:49:13 -07006823
Joe Perches00180462018-02-06 15:38:55 -08006824# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6825# and whether or not function naming is typical and if
6826# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006827 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006828 defined $stat &&
6829 $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*\)/) {
6830 my $var = $1;
6831 my $perms = $2;
6832 my $show = $3;
6833 my $store = $4;
6834 my $octal_perms = perms_to_octal($perms);
6835 if ($show =~ /^${var}_show$/ &&
6836 $store =~ /^${var}_store$/ &&
6837 $octal_perms eq "0644") {
6838 if (WARN("DEVICE_ATTR_RW",
6839 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6840 $fix) {
6841 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6842 }
6843 } elsif ($show =~ /^${var}_show$/ &&
6844 $store =~ /^NULL$/ &&
6845 $octal_perms eq "0444") {
6846 if (WARN("DEVICE_ATTR_RO",
6847 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6848 $fix) {
6849 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6850 }
6851 } elsif ($show =~ /^NULL$/ &&
6852 $store =~ /^${var}_store$/ &&
6853 $octal_perms eq "0200") {
6854 if (WARN("DEVICE_ATTR_WO",
6855 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6856 $fix) {
6857 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6858 }
6859 } elsif ($octal_perms eq "0644" ||
6860 $octal_perms eq "0444" ||
6861 $octal_perms eq "0200") {
6862 my $newshow = "$show";
6863 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6864 my $newstore = $store;
6865 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6866 my $rename = "";
6867 if ($show ne $newshow) {
6868 $rename .= " '$show' to '$newshow'";
6869 }
6870 if ($store ne $newstore) {
6871 $rename .= " '$store' to '$newstore'";
6872 }
6873 WARN("DEVICE_ATTR_FUNCTIONS",
6874 "Consider renaming function(s)$rename\n" . $herecurr);
6875 } else {
6876 WARN("DEVICE_ATTR_PERMS",
6877 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6878 }
6879 }
6880
Joe Perches515a2352014-04-03 14:49:24 -07006881# Mode permission misuses where it seems decimal should be octal
6882# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006883# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6884# specific definition of not visible in sysfs.
6885# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6886# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006887 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006888 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006889 $line =~ /$mode_perms_search/) {
6890 foreach my $entry (@mode_permission_funcs) {
6891 my $func = $entry->[0];
6892 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006893
Joe Perches459cf0a2016-10-11 13:52:19 -07006894 my $lc = $stat =~ tr@\n@@;
6895 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006896 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006897
Joe Perches515a2352014-04-03 14:49:24 -07006898 my $skip_args = "";
6899 if ($arg_pos > 1) {
6900 $arg_pos--;
6901 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6902 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006903 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006904 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006905 my $val = $1;
6906 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006907 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6908 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6909 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006910 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006911 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006912 }
6913 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006914 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006915 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006916 }
Joe Perches24358802014-04-03 14:49:13 -07006917 }
6918 }
6919 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006920
Joe Perches459cf0a2016-10-11 13:52:19 -07006921# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006922 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006923 my $oval = $1;
6924 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006925 if (WARN("SYMBOLIC_PERMS",
6926 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6927 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006928 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006929 }
6930 }
6931
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006932# validate content of MODULE_LICENSE against list from include/linux/module.h
6933 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6934 my $extracted_string = get_quoted_string($line, $rawline);
6935 my $valid_licenses = qr{
6936 GPL|
6937 GPL\ v2|
6938 GPL\ and\ additional\ rights|
6939 Dual\ BSD/GPL|
6940 Dual\ MIT/GPL|
6941 Dual\ MPL/GPL|
6942 Proprietary
6943 }x;
6944 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6945 WARN("MODULE_LICENSE",
6946 "unknown module license " . $extracted_string . "\n" . $herecurr);
6947 }
6948 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07006949
6950# check for sysctl duplicate constants
6951 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6952 WARN("DUPLICATED_SYSCTL_CONST",
6953 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6954 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006955 }
6956
6957 # If we have no input at all, then there is nothing to report on
6958 # so just keep quiet.
6959 if ($#rawlines == -1) {
6960 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006961 }
6962
Andy Whitcroft8905a672007-11-28 16:21:06 -08006963 # In mailback mode only produce a report in the negative, for
6964 # things that appear to be patches.
6965 if ($mailback && ($clean == 1 || !$is_patch)) {
6966 exit(0);
6967 }
6968
6969 # This is not a patch, and we are are in 'no-patch' mode so
6970 # just keep quiet.
6971 if (!$chk_patch && !$is_patch) {
6972 exit(0);
6973 }
6974
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006975 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006976 ERROR("NOT_UNIFIED_DIFF",
6977 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006978 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006979 if ($is_patch && $has_commit_log && $chk_signoff) {
6980 if ($signoff == 0) {
6981 ERROR("MISSING_SIGN_OFF",
6982 "Missing Signed-off-by: line(s)\n");
Dwaipayan Ray48ca2d82020-10-15 20:12:28 -07006983 } elsif ($authorsignoff != 1) {
6984 # authorsignoff values:
6985 # 0 -> missing sign off
6986 # 1 -> sign off identical
6987 # 2 -> names and addresses match, comments mismatch
6988 # 3 -> addresses match, names different
6989 # 4 -> names match, addresses different
6990 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
6991
6992 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
6993
6994 if ($authorsignoff == 0) {
6995 ERROR("NO_AUTHOR_SIGN_OFF",
6996 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6997 } elsif ($authorsignoff == 2) {
6998 CHK("FROM_SIGN_OFF_MISMATCH",
6999 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7000 } elsif ($authorsignoff == 3) {
7001 WARN("FROM_SIGN_OFF_MISMATCH",
7002 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7003 } elsif ($authorsignoff == 4) {
7004 WARN("FROM_SIGN_OFF_MISMATCH",
7005 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7006 } elsif ($authorsignoff == 5) {
7007 WARN("FROM_SIGN_OFF_MISMATCH",
7008 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7009 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07007010 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007011 }
7012
Andy Whitcroft8905a672007-11-28 16:21:06 -08007013 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08007014 if ($summary && !($clean == 1 && $quiet == 1)) {
7015 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08007016 print "total: $cnt_error errors, $cnt_warn warnings, " .
7017 (($check)? "$cnt_chk checks, " : "") .
7018 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07007019 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08007020
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007021 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07007022 # If there were any defects found and not already fixing them
7023 if (!$clean and !$fix) {
7024 print << "EOM"
7025
7026NOTE: For some of the reported defects, checkpatch may be able to
7027 mechanically convert to the typical style using --fix or --fix-inplace.
7028EOM
7029 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007030 # If there were whitespace errors which cleanpatch can fix
7031 # then suggest that.
7032 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07007033 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07007034 print << "EOM"
7035
7036NOTE: Whitespace errors detected.
7037 You may wish to use scripts/cleanpatch or scripts/cleanfile
7038EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07007039 }
7040 }
7041
Joe Perchesd752fcc2014-08-06 16:11:05 -07007042 if ($clean == 0 && $fix &&
7043 ("@rawlines" ne "@fixed" ||
7044 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08007045 my $newfile = $filename;
7046 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07007047 my $linecount = 0;
7048 my $f;
7049
Joe Perchesd752fcc2014-08-06 16:11:05 -07007050 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7051
Joe Perches3705ce52013-07-03 15:05:31 -07007052 open($f, '>', $newfile)
7053 or die "$P: Can't open $newfile for write\n";
7054 foreach my $fixed_line (@fixed) {
7055 $linecount++;
7056 if ($file) {
7057 if ($linecount > 3) {
7058 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07007059 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07007060 }
7061 } else {
7062 print $f $fixed_line . "\n";
7063 }
7064 }
7065 close($f);
7066
7067 if (!$quiet) {
7068 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07007069
Joe Perches3705ce52013-07-03 15:05:31 -07007070Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
7071
7072Do _NOT_ trust the results written to this file.
7073Do _NOT_ submit these changes without inspecting them for correctness.
7074
7075This EXPERIMENTAL file is simply a convenience to help rewrite patches.
7076No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07007077EOM
7078 }
7079 }
7080
Joe Perchesd8469f12015-06-25 15:03:00 -07007081 if ($quiet == 0) {
7082 print "\n";
7083 if ($clean == 1) {
7084 print "$vname has no obvious style problems and is ready for submission.\n";
7085 } else {
7086 print "$vname has style problems, please review.\n";
7087 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007088 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007089 return $clean;
7090}