blob: 5424134e201d226ff279da30bb78a9c954096a09 [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
Joe Perches3705ce52013-07-03 15:05:31 -07001184 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001185 $name =~ s/^\"|\"$//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001186 $name =~ s/(\s*\([^\)]+\))\s*//;
1187 if (defined($1)) {
1188 $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 {
1202 my ($name, $address) = @_;
1203
1204 my $formatted_email;
1205
Joe Perches3705ce52013-07-03 15:05:31 -07001206 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001207 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001208 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001209
1210 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1211 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1212 $name = "\"$name\"";
1213 }
1214
1215 if ("$name" eq "") {
1216 $formatted_email = "$address";
1217 } else {
1218 $formatted_email = "$name <$address>";
1219 }
1220
1221 return $formatted_email;
1222}
1223
Joe Perchesdfa05c22020-04-06 20:10:48 -07001224sub reformat_email {
1225 my ($email) = @_;
1226
1227 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1228 return format_email($email_name, $email_address);
1229}
1230
1231sub same_email_addresses {
1232 my ($email1, $email2) = @_;
1233
1234 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1235 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1236
1237 return $email1_name eq $email2_name &&
1238 $email1_address eq $email2_address;
1239}
1240
Joe Perchesd311cd42014-08-06 16:10:57 -07001241sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001242 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001243
Joe Perchesbd474ca2014-08-06 16:11:10 -07001244 foreach my $path (split(/:/, $ENV{PATH})) {
1245 if (-e "$path/$bin") {
1246 return "$path/$bin";
1247 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001248 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001249
Joe Perchesbd474ca2014-08-06 16:11:10 -07001250 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001251}
1252
Joe Perches000d1cc12011-07-25 17:13:25 -07001253sub which_conf {
1254 my ($conf) = @_;
1255
1256 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1257 if (-e "$path/$conf") {
1258 return "$path/$conf";
1259 }
1260 }
1261
1262 return "";
1263}
1264
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001265sub expand_tabs {
1266 my ($str) = @_;
1267
1268 my $res = '';
1269 my $n = 0;
1270 for my $c (split(//, $str)) {
1271 if ($c eq "\t") {
1272 $res .= ' ';
1273 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001274 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001275 $res .= ' ';
1276 }
1277 next;
1278 }
1279 $res .= $c;
1280 $n++;
1281 }
1282
1283 return $res;
1284}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001285sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001286 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001287 return $res;
1288}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001289
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001290sub line_stats {
1291 my ($line) = @_;
1292
1293 # Drop the diff line leader and expand tabs
1294 $line =~ s/^.//;
1295 $line = expand_tabs($line);
1296
1297 # Pick the indent from the front of the line.
1298 my ($white) = ($line =~ /^(\s*)/);
1299
1300 return (length($line), length($white));
1301}
1302
Andy Whitcroft773647a2008-03-28 14:15:58 -07001303my $sanitise_quote = '';
1304
1305sub sanitise_line_reset {
1306 my ($in_comment) = @_;
1307
1308 if ($in_comment) {
1309 $sanitise_quote = '*/';
1310 } else {
1311 $sanitise_quote = '';
1312 }
1313}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001314sub sanitise_line {
1315 my ($line) = @_;
1316
1317 my $res = '';
1318 my $l = '';
1319
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001320 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001321 my $off = 0;
1322 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001323
Andy Whitcroft773647a2008-03-28 14:15:58 -07001324 # Always copy over the diff marker.
1325 $res = substr($line, 0, 1);
1326
1327 for ($off = 1; $off < length($line); $off++) {
1328 $c = substr($line, $off, 1);
1329
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001330 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001331 # and end, all to $;.
1332 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1333 $sanitise_quote = '*/';
1334
1335 substr($res, $off, 2, "$;$;");
1336 $off++;
1337 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001338 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001339 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001340 $sanitise_quote = '';
1341 substr($res, $off, 2, "$;$;");
1342 $off++;
1343 next;
1344 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001345 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1346 $sanitise_quote = '//';
1347
1348 substr($res, $off, 2, $sanitise_quote);
1349 $off++;
1350 next;
1351 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001352
1353 # A \ in a string means ignore the next character.
1354 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1355 $c eq "\\") {
1356 substr($res, $off, 2, 'XX');
1357 $off++;
1358 next;
1359 }
1360 # Regular quotes.
1361 if ($c eq "'" || $c eq '"') {
1362 if ($sanitise_quote eq '') {
1363 $sanitise_quote = $c;
1364
1365 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001366 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001367 } elsif ($sanitise_quote eq $c) {
1368 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001369 }
1370 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001371
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001372 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001373 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1374 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001375 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1376 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001377 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1378 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001379 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001380 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001381 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001382 }
1383
Daniel Walker113f04a2009-09-21 17:04:35 -07001384 if ($sanitise_quote eq '//') {
1385 $sanitise_quote = '';
1386 }
1387
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001388 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001389 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001390 my $clean = 'X' x length($1);
1391 $res =~ s@\<.*\>@<$clean>@;
1392
1393 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001394 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001395 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001396 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001397 }
1398
Joe Perchesdadf6802016-08-02 14:04:33 -07001399 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1400 my $match = $1;
1401 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1402 }
1403
Andy Whitcroft00df3442007-06-08 13:47:06 -07001404 return $res;
1405}
1406
Joe Perchesa6962d72013-04-29 16:18:13 -07001407sub get_quoted_string {
1408 my ($line, $rawline) = @_;
1409
Joe Perches478b1792018-04-10 16:33:34 -07001410 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001411 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001412 return substr($rawline, $-[0], $+[0] - $-[0]);
1413}
1414
Andy Whitcroft8905a672007-11-28 16:21:06 -08001415sub ctx_statement_block {
1416 my ($linenr, $remain, $off) = @_;
1417 my $line = $linenr - 1;
1418 my $blk = '';
1419 my $soff = $off;
1420 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001421 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001422
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001423 my $loff = 0;
1424
Andy Whitcroft8905a672007-11-28 16:21:06 -08001425 my $type = '';
1426 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001427 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001428 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001429 my $c;
1430 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001431
1432 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001433 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001434 @stack = (['', 0]) if ($#stack == -1);
1435
Andy Whitcroft773647a2008-03-28 14:15:58 -07001436 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001437 # If we are about to drop off the end, pull in more
1438 # context.
1439 if ($off >= $len) {
1440 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001441 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001442 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001443 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001444 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001445 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001446 $len = length($blk);
1447 $line++;
1448 last;
1449 }
1450 # Bail if there is no further context.
1451 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001452 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001453 last;
1454 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001455 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1456 $level++;
1457 $type = '#';
1458 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001459 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001460 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001461 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001462 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001463
Andy Whitcroft773647a2008-03-28 14:15:58 -07001464 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001465
1466 # Handle nested #if/#else.
1467 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1468 push(@stack, [ $type, $level ]);
1469 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1470 ($type, $level) = @{$stack[$#stack - 1]};
1471 } elsif ($remainder =~ /^#\s*endif\b/) {
1472 ($type, $level) = @{pop(@stack)};
1473 }
1474
Andy Whitcroft8905a672007-11-28 16:21:06 -08001475 # Statement ends at the ';' or a close '}' at the
1476 # outermost level.
1477 if ($level == 0 && $c eq ';') {
1478 last;
1479 }
1480
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001481 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001482 if ($level == 0 && $coff_set == 0 &&
1483 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1484 $remainder =~ /^(else)(?:\s|{)/ &&
1485 $remainder !~ /^else\s+if\b/) {
1486 $coff = $off + length($1) - 1;
1487 $coff_set = 1;
1488 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1489 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001490 }
1491
Andy Whitcroft8905a672007-11-28 16:21:06 -08001492 if (($type eq '' || $type eq '(') && $c eq '(') {
1493 $level++;
1494 $type = '(';
1495 }
1496 if ($type eq '(' && $c eq ')') {
1497 $level--;
1498 $type = ($level != 0)? '(' : '';
1499
1500 if ($level == 0 && $coff < $soff) {
1501 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001502 $coff_set = 1;
1503 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001504 }
1505 }
1506 if (($type eq '' || $type eq '{') && $c eq '{') {
1507 $level++;
1508 $type = '{';
1509 }
1510 if ($type eq '{' && $c eq '}') {
1511 $level--;
1512 $type = ($level != 0)? '{' : '';
1513
1514 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001515 if (substr($blk, $off + 1, 1) eq ';') {
1516 $off++;
1517 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001518 last;
1519 }
1520 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001521 # Preprocessor commands end at the newline unless escaped.
1522 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1523 $level--;
1524 $type = '';
1525 $off++;
1526 last;
1527 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001528 $off++;
1529 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001530 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001531 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001532 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001533 $line++;
1534 $remain--;
1535 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001536
1537 my $statement = substr($blk, $soff, $off - $soff + 1);
1538 my $condition = substr($blk, $soff, $coff - $soff + 1);
1539
1540 #warn "STATEMENT<$statement>\n";
1541 #warn "CONDITION<$condition>\n";
1542
Andy Whitcroft773647a2008-03-28 14:15:58 -07001543 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001544
1545 return ($statement, $condition,
1546 $line, $remain + 1, $off - $loff + 1, $level);
1547}
1548
Andy Whitcroftcf655042008-03-04 14:28:20 -08001549sub statement_lines {
1550 my ($stmt) = @_;
1551
1552 # Strip the diff line prefixes and rip blank lines at start and end.
1553 $stmt =~ s/(^|\n)./$1/g;
1554 $stmt =~ s/^\s*//;
1555 $stmt =~ s/\s*$//;
1556
1557 my @stmt_lines = ($stmt =~ /\n/g);
1558
1559 return $#stmt_lines + 2;
1560}
1561
1562sub statement_rawlines {
1563 my ($stmt) = @_;
1564
1565 my @stmt_lines = ($stmt =~ /\n/g);
1566
1567 return $#stmt_lines + 2;
1568}
1569
1570sub statement_block_size {
1571 my ($stmt) = @_;
1572
1573 $stmt =~ s/(^|\n)./$1/g;
1574 $stmt =~ s/^\s*{//;
1575 $stmt =~ s/}\s*$//;
1576 $stmt =~ s/^\s*//;
1577 $stmt =~ s/\s*$//;
1578
1579 my @stmt_lines = ($stmt =~ /\n/g);
1580 my @stmt_statements = ($stmt =~ /;/g);
1581
1582 my $stmt_lines = $#stmt_lines + 2;
1583 my $stmt_statements = $#stmt_statements + 1;
1584
1585 if ($stmt_lines > $stmt_statements) {
1586 return $stmt_lines;
1587 } else {
1588 return $stmt_statements;
1589 }
1590}
1591
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001592sub ctx_statement_full {
1593 my ($linenr, $remain, $off) = @_;
1594 my ($statement, $condition, $level);
1595
1596 my (@chunks);
1597
Andy Whitcroftcf655042008-03-04 14:28:20 -08001598 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001599 ($statement, $condition, $linenr, $remain, $off, $level) =
1600 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001601 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001602 push(@chunks, [ $condition, $statement ]);
1603 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1604 return ($level, $linenr, @chunks);
1605 }
1606
1607 # Pull in the following conditional/block pairs and see if they
1608 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001609 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001610 ($statement, $condition, $linenr, $remain, $off, $level) =
1611 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001612 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001613 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001614 #print "C: push\n";
1615 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001616 }
1617
1618 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001619}
1620
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001621sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001622 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001623 my $line;
1624 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001625 my $blk = '';
1626 my @o;
1627 my @c;
1628 my @res = ();
1629
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001630 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001631 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001632 for ($line = $start; $remain > 0; $line++) {
1633 next if ($rawlines[$line] =~ /^-/);
1634 $remain--;
1635
1636 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001637
1638 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001639 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001640 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001641 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001642 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001643 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001644 $level = pop(@stack);
1645 }
1646
Andy Whitcroft01464f32010-10-26 14:23:19 -07001647 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001648 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1649 if ($off > 0) {
1650 $off--;
1651 next;
1652 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001653
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001654 if ($c eq $close && $level > 0) {
1655 $level--;
1656 last if ($level == 0);
1657 } elsif ($c eq $open) {
1658 $level++;
1659 }
1660 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001661
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001662 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001663 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001664 }
1665
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001666 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001667 }
1668
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001669 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001670}
1671sub ctx_block_outer {
1672 my ($linenr, $remain) = @_;
1673
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001674 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1675 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001676}
1677sub ctx_block {
1678 my ($linenr, $remain) = @_;
1679
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001680 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1681 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001682}
1683sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001684 my ($linenr, $remain, $off) = @_;
1685
1686 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1687 return @r;
1688}
1689sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001690 my ($linenr, $remain) = @_;
1691
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001692 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001693}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001694sub ctx_statement_level {
1695 my ($linenr, $remain, $off) = @_;
1696
1697 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1698}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001699
1700sub ctx_locate_comment {
1701 my ($first_line, $end_line) = @_;
1702
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001703 # If c99 comment on the current line, or the line before or after
1704 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1705 return $current_comment if (defined $current_comment);
1706 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1707 return $current_comment if (defined $current_comment);
1708 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1709 return $current_comment if (defined $current_comment);
1710
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001711 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001712 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001713 return $current_comment if (defined $current_comment);
1714
1715 # Look through the context and try and figure out if there is a
1716 # comment.
1717 my $in_comment = 0;
1718 $current_comment = '';
1719 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001720 my $line = $rawlines[$linenr - 1];
1721 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001722 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1723 $in_comment = 1;
1724 }
1725 if ($line =~ m@/\*@) {
1726 $in_comment = 1;
1727 }
1728 if (!$in_comment && $current_comment ne '') {
1729 $current_comment = '';
1730 }
1731 $current_comment .= $line . "\n" if ($in_comment);
1732 if ($line =~ m@\*/@) {
1733 $in_comment = 0;
1734 }
1735 }
1736
1737 chomp($current_comment);
1738 return($current_comment);
1739}
1740sub ctx_has_comment {
1741 my ($first_line, $end_line) = @_;
1742 my $cmt = ctx_locate_comment($first_line, $end_line);
1743
Andy Whitcroft00df3442007-06-08 13:47:06 -07001744 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001745 ##print "CMMT: $cmt\n";
1746
1747 return ($cmt ne '');
1748}
1749
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001750sub raw_line {
1751 my ($linenr, $cnt) = @_;
1752
1753 my $offset = $linenr - 1;
1754 $cnt++;
1755
1756 my $line;
1757 while ($cnt) {
1758 $line = $rawlines[$offset++];
1759 next if (defined($line) && $line =~ /^-/);
1760 $cnt--;
1761 }
1762
1763 return $line;
1764}
1765
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001766sub get_stat_real {
1767 my ($linenr, $lc) = @_;
1768
1769 my $stat_real = raw_line($linenr, 0);
1770 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1771 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1772 }
1773
1774 return $stat_real;
1775}
1776
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001777sub get_stat_here {
1778 my ($linenr, $cnt, $here) = @_;
1779
1780 my $herectx = $here . "\n";
1781 for (my $n = 0; $n < $cnt; $n++) {
1782 $herectx .= raw_line($linenr, $n) . "\n";
1783 }
1784
1785 return $herectx;
1786}
1787
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001788sub cat_vet {
1789 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001790 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001791
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001792 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001793 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1794 $res .= $1;
1795 if ($2 ne '') {
1796 $coded = sprintf("^%c", unpack('C', $2) + 64);
1797 $res .= $coded;
1798 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001799 }
1800 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001801
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001802 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001803}
1804
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001805my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001806my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001807my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001808my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001809
1810sub annotate_reset {
1811 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001812 $av_pending = '_';
1813 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001814 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001815}
1816
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001817sub annotate_values {
1818 my ($stream, $type) = @_;
1819
1820 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001821 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001822 my $cur = $stream;
1823
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001824 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001825
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001826 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001827 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001828 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001829 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001830 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001831 print "WS($1)\n" if ($dbg_values > 1);
1832 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001833 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001834 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001835 }
1836
Florian Micklerc023e4732011-01-12 16:59:58 -08001837 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001838 print "CAST($1)\n" if ($dbg_values > 1);
1839 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001840 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001841
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001842 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001843 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001844 $type = 'T';
1845
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001846 } elsif ($cur =~ /^($Modifier)\s*/) {
1847 print "MODIFIER($1)\n" if ($dbg_values > 1);
1848 $type = 'T';
1849
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001850 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001851 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001852 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001853 push(@av_paren_type, $type);
1854 if ($2 ne '') {
1855 $av_pending = 'N';
1856 }
1857 $type = 'E';
1858
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001859 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001860 print "UNDEF($1)\n" if ($dbg_values > 1);
1861 $av_preprocessor = 1;
1862 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001863
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001864 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001865 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001866 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001867
1868 push(@av_paren_type, $type);
1869 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001870 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001871
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001872 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001873 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1874 $av_preprocessor = 1;
1875
1876 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1877
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*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001881 print "PRE_END($1)\n" if ($dbg_values > 1);
1882
1883 $av_preprocessor = 1;
1884
1885 # Assume all arms of the conditional end as this
1886 # one does, and continue as if the #endif was not here.
1887 pop(@av_paren_type);
1888 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001889 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001890
1891 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001892 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001893
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001894 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1895 print "ATTR($1)\n" if ($dbg_values > 1);
1896 $av_pending = $type;
1897 $type = 'N';
1898
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001899 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001900 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001901 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001902 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001903 }
1904 $type = 'N';
1905
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001906 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001907 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001908 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001909 $type = 'N';
1910
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001911 } elsif ($cur =~/^(case)/o) {
1912 print "CASE($1)\n" if ($dbg_values > 1);
1913 $av_pend_colon = 'C';
1914 $type = 'N';
1915
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001916 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001917 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001918 $type = 'N';
1919
1920 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001921 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001922 push(@av_paren_type, $av_pending);
1923 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001924 $type = 'N';
1925
1926 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001927 my $new_type = pop(@av_paren_type);
1928 if ($new_type ne '_') {
1929 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001930 print "PAREN('$1') -> $type\n"
1931 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001932 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001933 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001934 }
1935
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001936 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001937 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001938 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001939 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001940
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001941 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1942 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001943 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001944 } elsif ($type eq 'E') {
1945 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001946 }
1947 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1948 $type = 'V';
1949
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001950 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001951 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001952 $type = 'V';
1953
1954 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001955 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001956 $type = 'N';
1957
Andy Whitcroftcf655042008-03-04 14:28:20 -08001958 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001959 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001960 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001961 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001962
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001963 } elsif ($cur =~/^(,)/) {
1964 print "COMMA($1)\n" if ($dbg_values > 1);
1965 $type = 'C';
1966
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001967 } elsif ($cur =~ /^(\?)/o) {
1968 print "QUESTION($1)\n" if ($dbg_values > 1);
1969 $type = 'N';
1970
1971 } elsif ($cur =~ /^(:)/o) {
1972 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1973
1974 substr($var, length($res), 1, $av_pend_colon);
1975 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1976 $type = 'E';
1977 } else {
1978 $type = 'N';
1979 }
1980 $av_pend_colon = 'O';
1981
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001982 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001983 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001984 $type = 'N';
1985
Andy Whitcroft0d413862008-10-15 22:02:16 -07001986 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001987 my $variant;
1988
1989 print "OPV($1)\n" if ($dbg_values > 1);
1990 if ($type eq 'V') {
1991 $variant = 'B';
1992 } else {
1993 $variant = 'U';
1994 }
1995
1996 substr($var, length($res), 1, $variant);
1997 $type = 'N';
1998
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001999 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002000 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002001 if ($1 ne '++' && $1 ne '--') {
2002 $type = 'N';
2003 }
2004
2005 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002006 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002007 }
2008 if (defined $1) {
2009 $cur = substr($cur, length($1));
2010 $res .= $type x length($1);
2011 }
2012 }
2013
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002014 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002015}
2016
Andy Whitcroft8905a672007-11-28 16:21:06 -08002017sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002018 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002019 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002020 ^(?:
2021 $Modifier|
2022 $Storage|
2023 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002024 DEFINE_\S+
2025 )$|
2026 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002027 goto|
2028 return|
2029 case|
2030 else|
2031 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002032 do|
2033 \#|
2034 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002035 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002036 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002037 )}x;
2038 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2039 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002040 # Check for modifiers.
2041 $possible =~ s/\s*$Storage\s*//g;
2042 $possible =~ s/\s*$Sparse\s*//g;
2043 if ($possible =~ /^\s*$/) {
2044
2045 } elsif ($possible =~ /\s/) {
2046 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002047 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002048 if ($modifier !~ $notPermitted) {
2049 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002050 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002051 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002052 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002053
2054 } else {
2055 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002056 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002057 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002058 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002059 } else {
2060 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002061 }
2062}
2063
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002064my $prefix = '';
2065
Joe Perches000d1cc12011-07-25 17:13:25 -07002066sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002067 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002068
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002069 $type =~ tr/[a-z]/[A-Z]/;
2070
Joe Perchescbec18a2014-04-03 14:49:19 -07002071 return defined $use_type{$type} if (scalar keys %use_type > 0);
2072
2073 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002074}
2075
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002076sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002077 my ($level, $type, $msg) = @_;
2078
2079 if (!show_type($type) ||
2080 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002081 return 0;
2082 }
Joe Perches57230292015-06-25 15:03:03 -07002083 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002084 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002085 if ($level eq 'ERROR') {
2086 $output .= RED;
2087 } elsif ($level eq 'WARNING') {
2088 $output .= YELLOW;
2089 } else {
2090 $output .= GREEN;
2091 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002092 }
Joe Perches57230292015-06-25 15:03:03 -07002093 $output .= $prefix . $level . ':';
2094 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002095 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002096 $output .= "$type:";
2097 }
John Brooks737c0762017-07-10 15:52:24 -07002098 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002099 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002100
2101 if ($showfile) {
2102 my @lines = split("\n", $output, -1);
2103 splice(@lines, 1, 1);
2104 $output = join("\n", @lines);
2105 }
Joe Perches57230292015-06-25 15:03:03 -07002106 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002107
Joe Perches57230292015-06-25 15:03:03 -07002108 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002109
2110 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002111}
Joe Perchescbec18a2014-04-03 14:49:19 -07002112
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002113sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002114 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002115}
Joe Perches000d1cc12011-07-25 17:13:25 -07002116
Joe Perchesd752fcc2014-08-06 16:11:05 -07002117sub fixup_current_range {
2118 my ($lineRef, $offset, $length) = @_;
2119
2120 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2121 my $o = $1;
2122 my $l = $2;
2123 my $no = $o + $offset;
2124 my $nl = $l + $length;
2125 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2126 }
2127}
2128
2129sub fix_inserted_deleted_lines {
2130 my ($linesRef, $insertedRef, $deletedRef) = @_;
2131
2132 my $range_last_linenr = 0;
2133 my $delta_offset = 0;
2134
2135 my $old_linenr = 0;
2136 my $new_linenr = 0;
2137
2138 my $next_insert = 0;
2139 my $next_delete = 0;
2140
2141 my @lines = ();
2142
2143 my $inserted = @{$insertedRef}[$next_insert++];
2144 my $deleted = @{$deletedRef}[$next_delete++];
2145
2146 foreach my $old_line (@{$linesRef}) {
2147 my $save_line = 1;
2148 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002149 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002150 $delta_offset = 0;
2151 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2152 $range_last_linenr = $new_linenr;
2153 fixup_current_range(\$line, $delta_offset, 0);
2154 }
2155
2156 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2157 $deleted = @{$deletedRef}[$next_delete++];
2158 $save_line = 0;
2159 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2160 }
2161
2162 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2163 push(@lines, ${$inserted}{'LINE'});
2164 $inserted = @{$insertedRef}[$next_insert++];
2165 $new_linenr++;
2166 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2167 }
2168
2169 if ($save_line) {
2170 push(@lines, $line);
2171 $new_linenr++;
2172 }
2173
2174 $old_linenr++;
2175 }
2176
2177 return @lines;
2178}
2179
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002180sub fix_insert_line {
2181 my ($linenr, $line) = @_;
2182
2183 my $inserted = {
2184 LINENR => $linenr,
2185 LINE => $line,
2186 };
2187 push(@fixed_inserted, $inserted);
2188}
2189
2190sub fix_delete_line {
2191 my ($linenr, $line) = @_;
2192
2193 my $deleted = {
2194 LINENR => $linenr,
2195 LINE => $line,
2196 };
2197
2198 push(@fixed_deleted, $deleted);
2199}
2200
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002201sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002202 my ($type, $msg) = @_;
2203
2204 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002205 our $clean = 0;
2206 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002207 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002208 }
Joe Perches3705ce52013-07-03 15:05:31 -07002209 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002210}
2211sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002212 my ($type, $msg) = @_;
2213
2214 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002215 our $clean = 0;
2216 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002217 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002218 }
Joe Perches3705ce52013-07-03 15:05:31 -07002219 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002220}
2221sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002222 my ($type, $msg) = @_;
2223
2224 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002225 our $clean = 0;
2226 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002227 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002228 }
Joe Perches3705ce52013-07-03 15:05:31 -07002229 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002230}
2231
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002232sub check_absolute_file {
2233 my ($absolute, $herecurr) = @_;
2234 my $file = $absolute;
2235
2236 ##print "absolute<$absolute>\n";
2237
2238 # See if any suffix of this path is a path within the tree.
2239 while ($file =~ s@^[^/]*/@@) {
2240 if (-f "$root/$file") {
2241 ##print "file<$file>\n";
2242 last;
2243 }
2244 }
2245 if (! -f _) {
2246 return 0;
2247 }
2248
2249 # It is, so see if the prefix is acceptable.
2250 my $prefix = $absolute;
2251 substr($prefix, -length($file)) = '';
2252
2253 ##print "prefix<$prefix>\n";
2254 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002255 WARN("USE_RELATIVE_PATH",
2256 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002257 }
2258}
2259
Joe Perches3705ce52013-07-03 15:05:31 -07002260sub trim {
2261 my ($string) = @_;
2262
Joe Perchesb34c6482013-09-11 14:24:01 -07002263 $string =~ s/^\s+|\s+$//g;
2264
2265 return $string;
2266}
2267
2268sub ltrim {
2269 my ($string) = @_;
2270
2271 $string =~ s/^\s+//;
2272
2273 return $string;
2274}
2275
2276sub rtrim {
2277 my ($string) = @_;
2278
2279 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002280
2281 return $string;
2282}
2283
Joe Perches52ea8502013-11-12 15:10:09 -08002284sub string_find_replace {
2285 my ($string, $find, $replace) = @_;
2286
2287 $string =~ s/$find/$replace/g;
2288
2289 return $string;
2290}
2291
Joe Perches3705ce52013-07-03 15:05:31 -07002292sub tabify {
2293 my ($leading) = @_;
2294
Antonio Borneo713a09d2020-04-06 20:11:07 -07002295 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002296 my $max_spaces_before_tab = $source_indent - 1;
2297 my $spaces_to_tab = " " x $source_indent;
2298
2299 #convert leading spaces to tabs
2300 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2301 #Remove spaces before a tab
2302 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2303
2304 return "$leading";
2305}
2306
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002307sub pos_last_openparen {
2308 my ($line) = @_;
2309
2310 my $pos = 0;
2311
2312 my $opens = $line =~ tr/\(/\(/;
2313 my $closes = $line =~ tr/\)/\)/;
2314
2315 my $last_openparen = 0;
2316
2317 if (($opens == 0) || ($closes >= $opens)) {
2318 return -1;
2319 }
2320
2321 my $len = length($line);
2322
2323 for ($pos = 0; $pos < $len; $pos++) {
2324 my $string = substr($line, $pos);
2325 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2326 $pos += length($1) - 1;
2327 } elsif (substr($line, $pos, 1) eq '(') {
2328 $last_openparen = $pos;
2329 } elsif (index($string, '(') == -1) {
2330 last;
2331 }
2332 }
2333
Joe Perches91cb5192014-04-03 14:49:32 -07002334 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002335}
2336
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002337sub get_raw_comment {
2338 my ($line, $rawline) = @_;
2339 my $comment = '';
2340
2341 for my $i (0 .. (length($line) - 1)) {
2342 if (substr($line, $i, 1) eq "$;") {
2343 $comment .= substr($rawline, $i, 1);
2344 }
2345 }
2346
2347 return $comment;
2348}
2349
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002350sub process {
2351 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002352
2353 my $linenr=0;
2354 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002355 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002356 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002357 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002358
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002359 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002360 my $indent;
2361 my $previndent=0;
2362 my $stashindent=0;
2363
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002364 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002365 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002366 my $author = '';
2367 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002368 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002369 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002370 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002371 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002372 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002373 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002374 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002375 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002376 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002377 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002378 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002379 my $non_utf8_charset = 0;
2380
Joe Perches365dd4e2014-08-06 16:10:42 -07002381 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002382 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002383
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002384 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002385 our $cnt_lines = 0;
2386 our $cnt_error = 0;
2387 our $cnt_warn = 0;
2388 our $cnt_chk = 0;
2389
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002390 # Trace the real file/line as we go.
2391 my $realfile = '';
2392 my $realline = 0;
2393 my $realcnt = 0;
2394 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002395 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002396 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002397 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002398 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002399 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002400
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002401 my $prev_values = 'E';
2402
2403 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002404 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002405 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002406 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002407 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002408
Joe Perches7e51f192013-09-11 14:23:57 -07002409 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002410
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002411 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002412 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002413 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002414 my @setup_docs = ();
2415 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002416
Joe Perchesd8b07712013-11-12 15:10:06 -08002417 my $camelcase_file_seeded = 0;
2418
Rob Herring9f3a8992018-04-10 16:33:13 -07002419 my $checklicenseline = 1;
2420
Andy Whitcroft773647a2008-03-28 14:15:58 -07002421 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002422 my $line;
2423 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002424 $linenr++;
2425 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002426
Joe Perches3705ce52013-07-03 15:05:31 -07002427 push(@fixed, $rawline) if ($fix);
2428
Andy Whitcroft773647a2008-03-28 14:15:58 -07002429 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002430 $setup_docs = 0;
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07002431 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002432 $setup_docs = 1;
2433 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002434 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002435 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002436 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002437 $realline=$1-1;
2438 if (defined $2) {
2439 $realcnt=$3+1;
2440 } else {
2441 $realcnt=1+1;
2442 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002443 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002444
2445 # Guestimate if this is a continuing comment. Run
2446 # the context looking for a comment "edge". If this
2447 # edge is a close comment then we must be in a comment
2448 # at context start.
2449 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002450 my $cnt = $realcnt;
2451 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2452 next if (defined $rawlines[$ln - 1] &&
2453 $rawlines[$ln - 1] =~ /^-/);
2454 $cnt--;
2455 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002456 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002457 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2458 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2459 ($edge) = $1;
2460 last;
2461 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002462 }
2463 if (defined $edge && $edge eq '*/') {
2464 $in_comment = 1;
2465 }
2466
2467 # Guestimate if this is a continuing comment. If this
2468 # is the start of a diff block and this line starts
2469 # ' *' then it is very likely a comment.
2470 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002471 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002472 {
2473 $in_comment = 1;
2474 }
2475
2476 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2477 sanitise_line_reset($in_comment);
2478
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002479 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002480 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002481 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002482 $line = sanitise_line($rawline);
2483 }
2484 push(@lines, $line);
2485
2486 if ($realcnt > 1) {
2487 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2488 } else {
2489 $realcnt = 0;
2490 }
2491
2492 #print "==>$rawline\n";
2493 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002494
2495 if ($setup_docs && $line =~ /^\+/) {
2496 push(@setup_docs, $line);
2497 }
2498 }
2499
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002500 $prefix = '';
2501
Andy Whitcroft773647a2008-03-28 14:15:58 -07002502 $realcnt = 0;
2503 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002504 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002505 foreach my $line (@lines) {
2506 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002507 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002508 my $sline = $line; #copy of $line
2509 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002510
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002511 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002512 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002513
Joe Perches12c253a2018-06-07 17:10:58 -07002514# check if it's a mode change, rename or start of a patch
2515 if (!$in_commit_log &&
2516 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2517 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2518 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2519 $is_patch = 1;
2520 }
2521
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002522#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002523 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002524 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2525 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002526 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002527 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002528 $realline=$1-1;
2529 if (defined $2) {
2530 $realcnt=$3+1;
2531 } else {
2532 $realcnt=1+1;
2533 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002534 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002535 $prev_values = 'E';
2536
Andy Whitcroft773647a2008-03-28 14:15:58 -07002537 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002538 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002539 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002540 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002541 if ($context =~ /\b(\w+)\s*\(/) {
2542 $context_function = $1;
2543 } else {
2544 undef $context_function;
2545 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002546 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002547
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002548# track the line number as we move through the hunk, note that
2549# new versions of GNU diff omit the leading space on completely
2550# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002551 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002552 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002553 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002554
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002555 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002556 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002557
2558 # Track the previous line.
2559 ($prevline, $stashline) = ($stashline, $line);
2560 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002561 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2562
Andy Whitcroft773647a2008-03-28 14:15:58 -07002563 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002564
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002565 } elsif ($realcnt == 1) {
2566 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002567 }
2568
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002569 my $hunk_line = ($realcnt != 0);
2570
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002571 $here = "#$linenr: " if (!$file);
2572 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002573
Joe Perches2ac73b42014-06-04 16:12:05 -07002574 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002575 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002576 if ($line =~ /^diff --git.*?(\S+)$/) {
2577 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002578 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002579 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002580 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002581 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002582 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002583 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002584 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002585
2586 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002587 if (!$file && $tree && $p1_prefix ne '' &&
2588 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002589 WARN("PATCH_PREFIX",
2590 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002591 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002592
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002593 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002594 ERROR("MODIFIED_INCLUDE_ASM",
2595 "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 -07002596 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002597 $found_file = 1;
2598 }
2599
Joe Perches34d88152015-06-25 15:03:05 -07002600#make up the handle for any error we report on this line
2601 if ($showfile) {
2602 $prefix = "$realfile:$realline: "
2603 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002604 if ($file) {
2605 $prefix = "$filename:$realline: ";
2606 } else {
2607 $prefix = "$filename:$linenr: ";
2608 }
Joe Perches34d88152015-06-25 15:03:05 -07002609 }
2610
Joe Perches2ac73b42014-06-04 16:12:05 -07002611 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002612 if (is_maintained_obsolete($realfile)) {
2613 WARN("OBSOLETE",
2614 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2615 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002616 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002617 $check = 1;
2618 } else {
2619 $check = $check_orig;
2620 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002621 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002622
2623 if ($realfile !~ /^MAINTAINERS/) {
2624 my $last_binding_patch = $is_binding_patch;
2625
2626 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2627
2628 if (($last_binding_patch != -1) &&
2629 ($last_binding_patch ^ $is_binding_patch)) {
2630 WARN("DT_SPLIT_BINDING_PATCH",
Mauro Carvalho Chehab858e6842020-04-15 16:45:25 +02002631 "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 -07002632 }
2633 }
2634
Andy Whitcroft773647a2008-03-28 14:15:58 -07002635 next;
2636 }
2637
Randy Dunlap389834b2007-06-08 13:47:03 -07002638 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002639
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002640 my $hereline = "$here\n$rawline\n";
2641 my $herecurr = "$here\n$rawline\n";
2642 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002643
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002644 $cnt_lines++ if ($realcnt != 0);
2645
Joe Perches490b2922018-08-21 21:58:01 -07002646# Verify the existence of a commit log if appropriate
2647# 2 is used because a $signature is counted in $commit_log_lines
2648 if ($in_commit_log) {
2649 if ($line !~ /^\s*$/) {
2650 $commit_log_lines++; #could be a $signature
2651 }
2652 } elsif ($has_commit_log && $commit_log_lines < 2) {
2653 WARN("COMMIT_MESSAGE",
2654 "Missing commit description - Add an appropriate one\n");
2655 $commit_log_lines = 2; #warn only once
2656 }
2657
Joe Perchese518e9a2015-06-25 15:03:27 -07002658# Check if the commit log has what seems like a diff which can confuse patch
2659 if ($in_commit_log && !$commit_log_has_diff &&
Mrinal Pandey13e45412020-09-04 16:35:52 -07002660 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2661 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
Joe Perchese518e9a2015-06-25 15:03:27 -07002662 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2663 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2664 ERROR("DIFF_IN_COMMIT_MSG",
2665 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2666 $commit_log_has_diff = 1;
2667 }
2668
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002669# Check for incorrect file permissions
2670 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2671 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002672 if ($realfile !~ m@scripts/@ &&
2673 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002674 ERROR("EXECUTE_PERMISSIONS",
2675 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002676 }
2677 }
2678
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002679# Check the patch for a From:
2680 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2681 $author = $1;
Dwaipayan Raye7f929f2020-10-15 20:12:15 -07002682 my $curline = $linenr;
2683 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2684 $author .= $1;
2685 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002686 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2687 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002688 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002689 }
2690
Joe Perches20112472011-07-25 17:13:23 -07002691# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002692 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002693 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002694 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002695 if ($author ne '') {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002696 if (same_email_addresses($1, $author)) {
2697 $authorsignoff = 1;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002698 }
2699 }
Joe Perches20112472011-07-25 17:13:23 -07002700 }
2701
Joe Perches44d303e2020-04-06 20:11:10 -07002702# Check for patch separator
2703 if ($line =~ /^---$/) {
2704 $has_patch_separator = 1;
2705 $in_commit_log = 0;
2706 }
2707
Joe Perchese0d975b2014-12-10 15:51:49 -08002708# Check if MAINTAINERS is being updated. If so, there's probably no need to
2709# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2710 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2711 $reported_maintainer_file = 1;
2712 }
2713
Joe Perches20112472011-07-25 17:13:23 -07002714# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002715 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002716 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002717 my $space_before = $1;
2718 my $sign_off = $2;
2719 my $space_after = $3;
2720 my $email = $4;
2721 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2722
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002723 if ($sign_off !~ /$signature_tags/) {
2724 WARN("BAD_SIGN_OFF",
2725 "Non-standard signature: $sign_off\n" . $herecurr);
2726 }
Joe Perches20112472011-07-25 17:13:23 -07002727 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002728 if (WARN("BAD_SIGN_OFF",
2729 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2730 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002731 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002732 "$ucfirst_sign_off $email";
2733 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002734 }
Joe Perches20112472011-07-25 17:13:23 -07002735 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002736 if (WARN("BAD_SIGN_OFF",
2737 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2738 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002739 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002740 "$ucfirst_sign_off $email";
2741 }
2742
Joe Perches20112472011-07-25 17:13:23 -07002743 }
2744 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002745 if (WARN("BAD_SIGN_OFF",
2746 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2747 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002748 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002749 "$ucfirst_sign_off $email";
2750 }
Joe Perches20112472011-07-25 17:13:23 -07002751 }
2752
Joe Perchesdfa05c22020-04-06 20:10:48 -07002753 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Joe Perches20112472011-07-25 17:13:23 -07002754 my $suggested_email = format_email(($email_name, $email_address));
2755 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002756 ERROR("BAD_SIGN_OFF",
2757 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002758 } else {
2759 my $dequoted = $suggested_email;
2760 $dequoted =~ s/^"//;
2761 $dequoted =~ s/" </ </;
2762 # Don't force email to have quotes
2763 # Allow just an angle bracketed address
Joe Perchesdfa05c22020-04-06 20:10:48 -07002764 if (!same_email_addresses($email, $suggested_email)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002765 WARN("BAD_SIGN_OFF",
2766 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002767 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002768 }
Joe Perches7e51f192013-09-11 14:23:57 -07002769
2770# Check for duplicate signatures
2771 my $sig_nospace = $line;
2772 $sig_nospace =~ s/\s//g;
2773 $sig_nospace = lc($sig_nospace);
2774 if (defined $signatures{$sig_nospace}) {
2775 WARN("BAD_SIGN_OFF",
2776 "Duplicate signature\n" . $herecurr);
2777 } else {
2778 $signatures{$sig_nospace} = 1;
2779 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002780
2781# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2782 if ($sign_off =~ /^co-developed-by:$/i) {
2783 if ($email eq $author) {
2784 WARN("BAD_SIGN_OFF",
2785 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2786 }
2787 if (!defined $lines[$linenr]) {
2788 WARN("BAD_SIGN_OFF",
2789 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2790 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2791 WARN("BAD_SIGN_OFF",
2792 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2793 } elsif ($1 ne $email) {
2794 WARN("BAD_SIGN_OFF",
2795 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2796 }
2797 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002798 }
2799
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002800# Check email subject for common tools that don't need to be mentioned
2801 if ($in_header_lines &&
2802 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2803 WARN("EMAIL_SUBJECT",
2804 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2805 }
2806
Joe Perches44d303e2020-04-06 20:11:10 -07002807# Check for Gerrit Change-Ids not in any patch context
2808 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002809 ERROR("GERRIT_CHANGE_ID",
Joe Perches44d303e2020-04-06 20:11:10 -07002810 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002811 }
2812
Joe Perches369c8dd2015-11-06 16:31:34 -08002813# Check if the commit log is in a possible stack dump
2814 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2815 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2816 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2817 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07002818 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2819 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2820 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2821 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08002822 $commit_log_possible_stack_dump = 1;
2823 }
2824
Joe Perches2a076f42015-04-16 12:44:28 -07002825# Check for line lengths > 75 in commit log, warn once
2826 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002827 length($line) > 75 &&
2828 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2829 # file delta changes
2830 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2831 # filename then :
2832 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2833 # A Fixes: or Link: line
2834 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002835 WARN("COMMIT_LOG_LONG_LINE",
2836 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2837 $commit_log_long_line = 1;
2838 }
2839
Joe Perchesbf4daf12015-09-09 15:37:50 -07002840# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002841 if ($in_commit_log && $commit_log_possible_stack_dump &&
2842 $line =~ /^\s*$/) {
2843 $commit_log_possible_stack_dump = 0;
2844 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002845
Joe Perches0d7835f2015-02-13 14:38:35 -08002846# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002847 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07002848 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002849 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002850 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002851 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002852 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2853 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002854 my $init_char = "c";
2855 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002856 my $short = 1;
2857 my $long = 0;
2858 my $case = 1;
2859 my $space = 1;
2860 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002861 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002862 my $id = '0123456789ab';
2863 my $orig_desc = "commit description";
2864 my $description = "";
2865
Joe Perchesfe043ea2015-09-09 15:37:25 -07002866 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2867 $init_char = $1;
2868 $orig_commit = lc($2);
2869 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2870 $orig_commit = lc($1);
2871 }
2872
Joe Perches0d7835f2015-02-13 14:38:35 -08002873 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2874 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2875 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2876 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2877 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2878 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002879 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002880 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2881 defined $rawlines[$linenr] &&
2882 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2883 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002884 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002885 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2886 defined $rawlines[$linenr] &&
2887 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2888 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2889 $orig_desc = $1;
2890 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2891 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002892 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002893 }
2894
2895 ($id, $description) = git_commit_info($orig_commit,
2896 $id, $orig_desc);
2897
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002898 if (defined($id) &&
2899 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002900 ERROR("GIT_COMMIT_ID",
2901 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2902 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002903 }
2904
Joe Perches13f19372014-08-06 16:10:59 -07002905# Check for added, moved or deleted files
2906 if (!$reported_maintainer_file && !$in_commit_log &&
2907 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2908 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2909 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2910 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002911 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002912 $reported_maintainer_file = 1;
2913 WARN("FILE_PATH_CHANGES",
2914 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2915 }
2916
Rob Herringe400edb2019-09-12 15:18:20 +01002917# Check for adding new DT bindings not in schema format
2918 if (!$in_commit_log &&
2919 ($line =~ /^new file mode\s*\d+\s*$/) &&
2920 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2921 WARN("DT_SCHEMA_BINDING_PATCH",
2922 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2923 }
2924
Andy Whitcroft00df3442007-06-08 13:47:06 -07002925# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002926 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002927 ERROR("CORRUPTED_PATCH",
2928 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002929 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002930 }
2931
2932# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2933 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002934 $rawline !~ m/^$UTF8*$/) {
2935 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2936
2937 my $blank = copy_spacing($rawline);
2938 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2939 my $hereptr = "$hereline$ptr\n";
2940
Joe Perches34d99212011-07-25 17:13:26 -07002941 CHK("INVALID_UTF8",
2942 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002943 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002944
Joe Perches15662b32011-10-31 17:13:12 -07002945# Check if it's the start of a commit log
2946# (not a header line and we haven't seen the patch filename)
2947 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002948 !($rawline =~ /^\s+(?:\S|$)/ ||
2949 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002950 $in_header_lines = 0;
2951 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002952 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002953 }
2954
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002955# Check if there is UTF-8 in a commit log when a mail header has explicitly
2956# declined it, i.e defined some charset where it is missing.
2957 if ($in_header_lines &&
2958 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2959 $1 !~ /utf-8/i) {
2960 $non_utf8_charset = 1;
2961 }
2962
2963 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002964 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002965 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002966 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2967 }
2968
Joe Perchesd6430f72016-12-12 16:46:28 -08002969# Check for absolute kernel paths in commit message
2970 if ($tree && $in_commit_log) {
2971 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2972 my $file = $1;
2973
2974 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2975 check_absolute_file($1, $herecurr)) {
2976 #
2977 } else {
2978 check_absolute_file($file, $herecurr);
2979 }
2980 }
2981 }
2982
Kees Cook66b47b42014-10-13 15:51:57 -07002983# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002984 if (defined($misspellings) &&
2985 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002986 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002987 my $typo = $1;
2988 my $typo_fix = $spelling_fix{lc($typo)};
2989 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2990 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002991 my $msg_level = \&WARN;
2992 $msg_level = \&CHK if ($file);
2993 if (&{$msg_level}("TYPO_SPELLING",
2994 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002995 $fix) {
2996 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2997 }
2998 }
2999 }
3000
Matteo Crocea8dd86b2019-09-25 16:46:38 -07003001# check for invalid commit id
3002 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3003 my $id;
3004 my $description;
3005 ($id, $description) = git_commit_info($2, undef, undef);
3006 if (!defined($id)) {
3007 WARN("UNKNOWN_COMMIT_ID",
3008 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3009 }
3010 }
3011
Joe Perches310cd062020-10-15 20:11:52 -07003012# check for repeated words separated by a single space
3013 if ($rawline =~ /^\+/ || $in_commit_log) {
3014 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3015
3016 my $first = $1;
3017 my $second = $2;
3018
3019 if ($first =~ /(?:struct|union|enum)/) {
3020 pos($rawline) += length($first) + length($second) + 1;
3021 next;
3022 }
3023
3024 next if ($first ne $second);
3025 next if ($first eq 'long');
3026
3027 if (WARN("REPEATED_WORD",
3028 "Possible repeated word: '$first'\n" . $herecurr) &&
3029 $fix) {
3030 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3031 }
3032 }
3033
3034 # if it's a repeated word on consecutive lines in a comment block
3035 if ($prevline =~ /$;+\s*$/ &&
3036 $prevrawline =~ /($word_pattern)\s*$/) {
3037 my $last_word = $1;
3038 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3039 if (WARN("REPEATED_WORD",
3040 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3041 $fix) {
3042 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3043 }
3044 }
3045 }
3046 }
3047
Andy Whitcroft306708542008-10-15 22:02:28 -07003048# ignore non-hunk lines and lines being removed
3049 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003050
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003051#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003052 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003053 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07003054 if (ERROR("DOS_LINE_ENDINGS",
3055 "DOS line endings\n" . $herevet) &&
3056 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003057 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003058 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003059 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3060 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003061 if (ERROR("TRAILING_WHITESPACE",
3062 "trailing whitespace\n" . $herevet) &&
3063 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003064 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003065 }
3066
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003067 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003068 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07003069
Josh Triplett4783f892013-11-12 15:10:12 -08003070# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003071 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003072 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003073 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3074 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003075 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003076 my $msg_level = \&ERROR;
3077 $msg_level = \&CHK if ($file);
3078 &{$msg_level}("FSF_MAILING_ADDRESS",
3079 "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 -08003080 }
3081
Andi Kleen33549572010-05-24 14:33:29 -07003082# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003083# Only applies when adding the entry originally, after that we do not have
3084# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003085 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003086 # 'choice' is usually the last thing on the line (though
3087 # Kconfig supports named choices), so use a word boundary
3088 # (\b) rather than a whitespace character (\s)
3089 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003090 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003091 my $cnt = $realcnt;
3092 my $ln = $linenr + 1;
3093 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003094 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003095 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003096 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003097 $f = $lines[$ln - 1];
3098 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3099 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003100
3101 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003102 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003103
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003104 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003105 $is_start = 1;
Masahiro Yamada22a4ac02020-06-17 12:02:20 +09003106 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003107 $length = -1;
3108 }
3109
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003110 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003111 $f =~ s/#.*//;
3112 $f =~ s/^\s+//;
3113 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003114
3115 # This only checks context lines in the patch
3116 # and so hopefully shouldn't trigger false
3117 # positives, even though some of these are
3118 # common words in help texts
3119 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3120 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003121 $is_end = 1;
3122 last;
3123 }
Andi Kleen33549572010-05-24 14:33:29 -07003124 $length++;
3125 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003126 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3127 WARN("CONFIG_DESCRIPTION",
3128 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3129 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003130 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003131 }
3132
Joe Perches7ccf41a2020-06-04 16:50:33 -07003133# check MAINTAINERS entries
3134 if ($realfile =~ /^MAINTAINERS$/) {
3135# check MAINTAINERS entries for the right form
3136 if ($rawline =~ /^\+[A-Z]:/ &&
3137 $rawline !~ /^\+[A-Z]:\t\S/) {
3138 if (WARN("MAINTAINERS_STYLE",
3139 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3140 $fix) {
3141 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3142 }
3143 }
3144# check MAINTAINERS entries for the right ordering too
3145 my $preferred_order = 'MRLSWQBCPTFXNK';
3146 if ($rawline =~ /^\+[A-Z]:/ &&
3147 $prevrawline =~ /^[\+ ][A-Z]:/) {
3148 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3149 my $cur = $1;
3150 my $curval = $2;
3151 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3152 my $prev = $1;
3153 my $prevval = $2;
3154 my $curindex = index($preferred_order, $cur);
3155 my $previndex = index($preferred_order, $prev);
3156 if ($curindex < 0) {
3157 WARN("MAINTAINERS_STYLE",
3158 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3159 } else {
3160 if ($previndex >= 0 && $curindex < $previndex) {
3161 WARN("MAINTAINERS_STYLE",
3162 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3163 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3164 ($prev eq 'X' && $cur eq 'X')) &&
3165 ($prevval cmp $curval) > 0) {
3166 WARN("MAINTAINERS_STYLE",
3167 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3168 }
3169 }
Joe Perches628f91a2017-07-10 15:52:07 -07003170 }
3171 }
3172
Christoph Jaeger327953e2015-02-13 14:38:29 -08003173# discourage the use of boolean for type definition attributes of Kconfig options
3174 if ($realfile =~ /Kconfig/ &&
3175 $line =~ /^\+\s*\bboolean\b/) {
3176 WARN("CONFIG_TYPE_BOOLEAN",
3177 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3178 }
3179
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003180 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3181 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3182 my $flag = $1;
3183 my $replacement = {
3184 'EXTRA_AFLAGS' => 'asflags-y',
3185 'EXTRA_CFLAGS' => 'ccflags-y',
3186 'EXTRA_CPPFLAGS' => 'cppflags-y',
3187 'EXTRA_LDFLAGS' => 'ldflags-y',
3188 };
3189
3190 WARN("DEPRECATED_VARIABLE",
3191 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3192 }
3193
Rob Herringbff5da42014-01-23 15:54:51 -08003194# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003195 if (defined $root &&
3196 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3197 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3198
Rob Herringbff5da42014-01-23 15:54:51 -08003199 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3200
Florian Vaussardcc933192014-04-03 14:49:27 -07003201 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003202 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003203
Rob Herringbff5da42014-01-23 15:54:51 -08003204 foreach my $compat (@compats) {
3205 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003206 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3207 my $compat3 = $compat;
3208 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3209 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003210 if ( $? >> 8 ) {
3211 WARN("UNDOCUMENTED_DT_STRING",
3212 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3213 }
3214
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003215 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3216 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003217 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003218 if ( $? >> 8 ) {
3219 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003220 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003221 }
3222 }
3223 }
3224
Rob Herring9f3a8992018-04-10 16:33:13 -07003225# check for using SPDX license tag at beginning of files
3226 if ($realline == $checklicenseline) {
3227 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3228 $checklicenseline = 2;
3229 } elsif ($rawline =~ /^\+/) {
3230 my $comment = "";
3231 if ($realfile =~ /\.(h|s|S)$/) {
3232 $comment = '/*';
3233 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3234 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003235 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003236 $comment = '#';
3237 } elsif ($realfile =~ /\.rst$/) {
3238 $comment = '..';
3239 }
3240
Joe Perchesfdf13692019-03-07 16:28:32 -08003241# check SPDX comment style for .[chsS] files
3242 if ($realfile =~ /\.[chsS]$/ &&
3243 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003244 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003245 WARN("SPDX_LICENSE_TAG",
3246 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3247 }
3248
Rob Herring9f3a8992018-04-10 16:33:13 -07003249 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003250 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3251 WARN("SPDX_LICENSE_TAG",
3252 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003253 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003254 my $spdx_license = $1;
3255 if (!is_SPDX_License_valid($spdx_license)) {
3256 WARN("SPDX_LICENSE_TAG",
3257 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3258 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003259 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3260 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3261 my $msg_level = \&WARN;
3262 $msg_level = \&CHK if ($file);
3263 if (&{$msg_level}("SPDX_LICENSE_TAG",
3264
3265 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3266 $fix) {
3267 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3268 }
3269 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003270 }
3271 }
3272 }
3273
Joe Perchesa0154cd2020-10-15 20:12:19 -07003274# check for embedded filenames
3275 if ($rawline =~ /^\+.*\Q$realfile\E/) {
3276 WARN("EMBEDDED_FILENAME",
3277 "It's generally not useful to have the filename in the file\n" . $herecurr);
3278 }
3279
Andy Whitcroft5368df202008-10-15 22:02:27 -07003280# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003281 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003282
Joe Perchesa8da38a2019-03-07 16:28:42 -08003283# check for using SPDX-License-Identifier on the wrong line number
3284 if ($realline != $checklicenseline &&
3285 $rawline =~ /\bSPDX-License-Identifier:/ &&
3286 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3287 WARN("SPDX_LICENSE_TAG",
3288 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3289 }
3290
Joe Perches47e0c882015-06-25 15:02:57 -07003291# line length limit (with some exclusions)
3292#
3293# There are a few types of lines that may extend beyond $max_line_length:
3294# logging functions like pr_info that end in a string
3295# lines with a single string
3296# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003297# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003298#
3299# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003300# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003301# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3302# LONG_LINE all other lines longer than $max_line_length
3303#
3304# if LONG_LINE is ignored, the other 2 types are also ignored
3305#
3306
Joe Perchesb4749e92015-07-17 16:24:01 -07003307 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003308 my $msg_type = "LONG_LINE";
3309
3310 # Check the allowed long line types first
3311
3312 # logging functions that end in a string that starts
3313 # before $max_line_length
3314 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3315 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3316 $msg_type = "";
3317
3318 # lines with only strings (w/ possible termination)
3319 # #defines with only strings
3320 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3321 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3322 $msg_type = "";
3323
Joe Perchescc147502017-11-17 15:28:44 -08003324 # More special cases
3325 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3326 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003327 $msg_type = "";
3328
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003329 # URL ($rawline is used in case the URL is in a comment)
3330 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3331 $msg_type = "";
3332
Joe Perches47e0c882015-06-25 15:02:57 -07003333 # Otherwise set the alternate message types
3334
3335 # a comment starts before $max_line_length
3336 } elsif ($line =~ /($;[\s$;]*)$/ &&
3337 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3338 $msg_type = "LONG_LINE_COMMENT"
3339
3340 # a quoted string starts before $max_line_length
3341 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3342 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3343 $msg_type = "LONG_LINE_STRING"
3344 }
3345
3346 if ($msg_type ne "" &&
3347 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003348 my $msg_level = \&WARN;
3349 $msg_level = \&CHK if ($file);
3350 &{$msg_level}($msg_type,
3351 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003352 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003353 }
3354
Andy Whitcroft8905a672007-11-28 16:21:06 -08003355# check for adding lines without a newline.
3356 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003357 WARN("MISSING_EOF_NEWLINE",
3358 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003359 }
3360
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003361# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003362 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003363
3364# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003365# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003366 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3367 $rawline =~ /^\+\s* \s*/) {
3368 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003369 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003370 if (ERROR("CODE_INDENT",
3371 "code indent should use tabs where possible\n" . $herevet) &&
3372 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003373 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003374 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003375 }
3376
Alberto Panizzo08e44362010-03-05 13:43:54 -08003377# check for space before tabs.
3378 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3379 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003380 if (WARN("SPACE_BEFORE_TAB",
3381 "please, no space before tabs\n" . $herevet) &&
3382 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003383 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003384 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003385 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003386 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003387 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003388 }
3389
Joe Perches6a487212018-04-10 16:34:04 -07003390# check for assignments on the start of a line
3391 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3392 CHK("ASSIGNMENT_CONTINUATIONS",
3393 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3394 }
3395
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003396# check for && or || at the start of a line
3397 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3398 CHK("LOGICAL_CONTINUATIONS",
3399 "Logical continuations should be on the previous line\n" . $hereprev);
3400 }
3401
Joe Perchesa91e8992016-05-20 17:04:05 -07003402# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003403 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003404 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003405 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003406 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003407 if (WARN("TABSTOP",
3408 "Statements should start on a tabstop\n" . $herecurr) &&
3409 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003410 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003411 }
3412 }
3413 }
3414
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003415# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003416 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003417 $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 -07003418 $prevline =~ /^\+(\t*)(.*)$/;
3419 my $oldindent = $1;
3420 my $rest = $2;
3421
3422 my $pos = pos_last_openparen($rest);
3423 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003424 $line =~ /^(\+| )([ \t]*)/;
3425 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003426
3427 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003428 "\t" x ($pos / $tabsize) .
3429 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003430 my $goodspaceindent = $oldindent . " " x $pos;
3431
3432 if ($newindent ne $goodtabindent &&
3433 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003434
3435 if (CHK("PARENTHESIS_ALIGNMENT",
3436 "Alignment should match open parenthesis\n" . $hereprev) &&
3437 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003438 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003439 s/^\+[ \t]*/\+$goodtabindent/;
3440 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003441 }
3442 }
3443 }
3444
Joe Perches6ab3a972015-04-16 12:44:05 -07003445# check for space after cast like "(int) foo" or "(struct foo) bar"
3446# avoid checking a few false positives:
3447# "sizeof(<type>)" or "__alignof__(<type>)"
3448# function pointer declarations like "(*foo)(int) = bar;"
3449# structure definitions like "(struct foo) { 0 };"
3450# multiline macros that define functions
3451# known attributes or the __attribute__ keyword
3452 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3453 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003454 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003455 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003456 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003457 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003458 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003459 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003460 }
3461
Joe Perches86406b12015-09-09 15:37:41 -07003462# Block comment styles
3463# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003464 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003465 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003466 $rawline =~ /^\+[ \t]*\*/ &&
3467 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003468 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3469 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3470 }
3471
Joe Perches86406b12015-09-09 15:37:41 -07003472# Block comments use * on subsequent lines
3473 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3474 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003475 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003476 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003477 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003478 WARN("BLOCK_COMMENT_STYLE",
3479 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003480 }
3481
Joe Perches86406b12015-09-09 15:37:41 -07003482# Block comments use */ on trailing lines
3483 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003484 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3485 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3486 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003487 WARN("BLOCK_COMMENT_STYLE",
3488 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003489 }
3490
Joe Perches08eb9b82016-10-11 13:51:50 -07003491# Block comment * alignment
3492 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003493 $line =~ /^\+[ \t]*$;/ && #leading comment
3494 $rawline =~ /^\+[ \t]*\*/ && #leading *
3495 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003496 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003497 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3498 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003499 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003500 if (defined($1)) {
3501 $oldindent = expand_tabs($1);
3502 } else {
3503 $prevrawline =~ m@^\+(.*/?)\*@;
3504 $oldindent = expand_tabs($1);
3505 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003506 $rawline =~ m@^\+([ \t]*)\*@;
3507 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003508 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003509 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003510 WARN("BLOCK_COMMENT_STYLE",
3511 "Block comments should align the * on each line\n" . $hereprev);
3512 }
3513 }
3514
Joe Perches7f619192014-08-06 16:10:39 -07003515# check for missing blank lines after struct/union declarations
3516# with exceptions for various attributes and macros
3517 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3518 $line =~ /^\+/ &&
3519 !($line =~ /^\+\s*$/ ||
3520 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3521 $line =~ /^\+\s*MODULE_/i ||
3522 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3523 $line =~ /^\+[a-z_]*init/ ||
3524 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3525 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003526 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003527 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003528 if (CHK("LINE_SPACING",
3529 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3530 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003531 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003532 }
Joe Perches7f619192014-08-06 16:10:39 -07003533 }
3534
Joe Perches365dd4e2014-08-06 16:10:42 -07003535# check for multiple consecutive blank lines
3536 if ($prevline =~ /^[\+ ]\s*$/ &&
3537 $line =~ /^\+\s*$/ &&
3538 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003539 if (CHK("LINE_SPACING",
3540 "Please don't use multiple blank lines\n" . $hereprev) &&
3541 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003542 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003543 }
3544
Joe Perches365dd4e2014-08-06 16:10:42 -07003545 $last_blank_line = $linenr;
3546 }
3547
Joe Perches3b617e32014-04-03 14:49:28 -07003548# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003549 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3550 # actual declarations
3551 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003552 # function pointer declarations
3553 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003554 # foo bar; where foo is some local typedef or #define
3555 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3556 # known declaration macros
3557 $prevline =~ /^\+\s+$declaration_macros/) &&
3558 # for "else if" which can look like "$Ident $Ident"
3559 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3560 # other possible extensions of declaration lines
3561 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3562 # not starting a section or a macro "\" extended line
3563 $prevline =~ /(?:\{\s*|\\)$/) &&
3564 # looks like a declaration
3565 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003566 # function pointer declarations
3567 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003568 # foo bar; where foo is some local typedef or #define
3569 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3570 # known declaration macros
3571 $sline =~ /^\+\s+$declaration_macros/ ||
3572 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003573 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003574 # start or end of block or continuation of declaration
3575 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3576 # bitfield continuation
3577 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3578 # other possible extensions of declaration lines
3579 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3580 # indentation of previous and current line are the same
3581 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003582 if (WARN("LINE_SPACING",
3583 "Missing a blank line after declarations\n" . $hereprev) &&
3584 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003585 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003586 }
Joe Perches3b617e32014-04-03 14:49:28 -07003587 }
3588
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003589# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003590# Exceptions:
3591# 1) within comments
3592# 2) indented preprocessor commands
3593# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003594 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003595 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003596 if (WARN("LEADING_SPACE",
3597 "please, no spaces at the start of a line\n" . $herevet) &&
3598 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003599 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003600 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003601 }
3602
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003603# check we are in a valid C source file if not then ignore this hunk
3604 next if ($realfile !~ /\.(h|c)$/);
3605
Joe Perches5751a242017-11-17 15:28:52 -08003606# check for unusual line ending [ or (
3607 if ($line =~ /^\+.*([\[\(])\s*$/) {
3608 CHK("OPEN_ENDED_LINE",
3609 "Lines should not end with a '$1'\n" . $herecurr);
3610 }
3611
Joe Perches4dbed762017-05-08 15:55:39 -07003612# check if this appears to be the start function declaration, save the name
3613 if ($sline =~ /^\+\{\s*$/ &&
3614 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3615 $context_function = $1;
3616 }
3617
3618# check if this appears to be the end of function declaration
3619 if ($sline =~ /^\+\}\s*$/) {
3620 undef $context_function;
3621 }
3622
Joe Perches032a4c02014-08-06 16:10:29 -07003623# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003624# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003625# if the previous line is a break or return and is indented 1 tab more...
3626 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3627 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003628 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3629 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3630 defined $lines[$linenr] &&
3631 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003632 WARN("UNNECESSARY_ELSE",
3633 "else is not generally useful after a break or return\n" . $hereprev);
3634 }
3635 }
3636
Joe Perchesc00df192014-08-06 16:11:01 -07003637# check indentation of a line with a break;
3638# if the previous line is a goto or return and is indented the same # of tabs
3639 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3640 my $tabs = $1;
3641 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3642 WARN("UNNECESSARY_BREAK",
3643 "break is not useful after a goto or return\n" . $hereprev);
3644 }
3645 }
3646
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003647# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003648 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003649 WARN("CVS_KEYWORD",
3650 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003651 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003652
Joe Perches56e77d72013-02-21 16:44:14 -08003653# check for old HOTPLUG __dev<foo> section markings
3654 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3655 WARN("HOTPLUG_SECTION",
3656 "Using $1 is unnecessary\n" . $herecurr);
3657 }
3658
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003659# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003660 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3661 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003662#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003663 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003664 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003665 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003666 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003667 $stat =~ s/\n./\n /g;
3668 $cond =~ s/\n./\n /g;
3669
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003670#print "linenr<$linenr> <$stat>\n";
3671 # If this statement has no statement boundaries within
3672 # it there is no point in retrying a statement scan
3673 # until we hit end of it.
3674 my $frag = $stat; $frag =~ s/;+\s*$//;
3675 if ($frag !~ /(?:{|;)/) {
3676#print "skip<$line_nr_next>\n";
3677 $suppress_statement = $line_nr_next;
3678 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003679
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003680 # Find the real next line.
3681 $realline_next = $line_nr_next;
3682 if (defined $realline_next &&
3683 (!defined $lines[$realline_next - 1] ||
3684 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3685 $realline_next++;
3686 }
3687
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003688 my $s = $stat;
3689 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003690
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003691 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003692 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003693
3694 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003695 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003696
Andy Whitcroft463f2862009-09-21 17:04:34 -07003697 } elsif ($s =~ /^.\s*else\b/s) {
3698
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003699 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003700 } 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 -07003701 my $type = $1;
3702 $type =~ s/\s+/ /g;
3703 possible($type, "A:" . $s);
3704
Andy Whitcroft8905a672007-11-28 16:21:06 -08003705 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003706 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003707 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003708 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003709
3710 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003711 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003712 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003713 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003714
3715 # Check for any sort of function declaration.
3716 # int foo(something bar, other baz);
3717 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003718 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 -08003719 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003720
Andy Whitcroftcf655042008-03-04 14:28:20 -08003721 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003722 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003723 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003724
Andy Whitcroft8905a672007-11-28 16:21:06 -08003725 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003726 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003727
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003728 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003729 }
3730 }
3731 }
3732
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003733 }
3734
Andy Whitcroft653d4872007-06-23 17:16:34 -07003735#
3736# Checks which may be anchored in the context.
3737#
3738
3739# Check for switch () and associated case and default
3740# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003741 if ($line=~/\bswitch\s*\(.*\)/) {
3742 my $err = '';
3743 my $sep = '';
3744 my @ctx = ctx_block_outer($linenr, $realcnt);
3745 shift(@ctx);
3746 for my $ctx (@ctx) {
3747 my ($clen, $cindent) = line_stats($ctx);
3748 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3749 $indent != $cindent) {
3750 $err .= "$sep$ctx\n";
3751 $sep = '';
3752 } else {
3753 $sep = "[...]\n";
3754 }
3755 }
3756 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003757 ERROR("SWITCH_CASE_INDENT_LEVEL",
3758 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003759 }
3760 }
3761
3762# if/while/etc brace do not go on next line, unless defining a do while loop,
3763# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003764 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 -07003765 my $pre_ctx = "$1$2";
3766
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003767 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003768
3769 if ($line =~ /^\+\t{6,}/) {
3770 WARN("DEEP_INDENTATION",
3771 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3772 }
3773
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003774 my $ctx_cnt = $realcnt - $#ctx - 1;
3775 my $ctx = join("\n", @ctx);
3776
Andy Whitcroft548596d2008-07-23 21:29:01 -07003777 my $ctx_ln = $linenr;
3778 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003779
Andy Whitcroft548596d2008-07-23 21:29:01 -07003780 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3781 defined $lines[$ctx_ln - 1] &&
3782 $lines[$ctx_ln - 1] =~ /^-/)) {
3783 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3784 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003785 $ctx_ln++;
3786 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003787
Andy Whitcroft53210162008-07-23 21:29:03 -07003788 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3789 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003790
Joe Perchesd752fcc2014-08-06 16:11:05 -07003791 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003792 ERROR("OPEN_BRACE",
3793 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003794 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003795 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003796 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3797 $ctx =~ /\)\s*\;\s*$/ &&
3798 defined $lines[$ctx_ln - 1])
3799 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003800 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3801 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003802 WARN("TRAILING_SEMICOLON",
3803 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003804 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003805 }
3806 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003807 }
3808
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003809# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003810 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 -08003811 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3812 ctx_statement_block($linenr, $realcnt, 0)
3813 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003814 my ($s, $c) = ($stat, $cond);
3815
3816 substr($s, 0, length($c), '');
3817
Joe Perches9f5af482015-09-09 15:37:30 -07003818 # remove inline comments
3819 $s =~ s/$;/ /g;
3820 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003821
3822 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003823 my @newlines = ($c =~ /\n/gs);
3824 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003825
Joe Perches9f5af482015-09-09 15:37:30 -07003826 # Make sure we remove the line prefixes as we have
3827 # none on the first line, and are going to readd them
3828 # where necessary.
3829 $s =~ s/\n./\n/gs;
3830 while ($s =~ /\n\s+\\\n/) {
3831 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3832 }
3833
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003834 # We want to check the first line inside the block
3835 # starting at the end of the conditional, so remove:
3836 # 1) any blank line termination
3837 # 2) any opening brace { on end of the line
3838 # 3) any do (...) {
3839 my $continuation = 0;
3840 my $check = 0;
3841 $s =~ s/^.*\bdo\b//;
3842 $s =~ s/^\s*{//;
3843 if ($s =~ s/^\s*\\//) {
3844 $continuation = 1;
3845 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003846 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003847 $check = 1;
3848 $cond_lines++;
3849 }
3850
3851 # Also ignore a loop construct at the end of a
3852 # preprocessor statement.
3853 if (($prevline =~ /^.\s*#\s*define\s/ ||
3854 $prevline =~ /\\\s*$/) && $continuation == 0) {
3855 $check = 0;
3856 }
3857
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003858 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003859 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003860 while ($cond_ptr != $cond_lines) {
3861 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003862
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003863 # If we see an #else/#elif then the code
3864 # is not linear.
3865 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3866 $check = 0;
3867 }
3868
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003869 # Ignore:
3870 # 1) blank lines, they should be at 0,
3871 # 2) preprocessor lines, and
3872 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003873 if ($continuation ||
3874 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003875 $s =~ /^\s*#\s*?/ ||
3876 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003877 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003878 if ($s =~ s/^.*?\n//) {
3879 $cond_lines++;
3880 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003881 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003882 }
3883
3884 my (undef, $sindent) = line_stats("+" . $s);
3885 my $stat_real = raw_line($linenr, $cond_lines);
3886
3887 # Check if either of these lines are modified, else
3888 # this is not this patch's fault.
3889 if (!defined($stat_real) ||
3890 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3891 $check = 0;
3892 }
3893 if (defined($stat_real) && $cond_lines > 1) {
3894 $stat_real = "[...]\n$stat_real";
3895 }
3896
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003897 #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 -07003898
Joe Perches9f5af482015-09-09 15:37:30 -07003899 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07003900 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07003901 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003902 ($sindent == $indent &&
3903 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07003904 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003905 WARN("SUSPECT_CODE_INDENT",
3906 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003907 }
3908 }
3909
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003910 # Track the 'values' across context and added lines.
3911 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003912 my ($curr_values, $curr_vars) =
3913 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003914 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003915 if ($dbg_values) {
3916 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003917 print "$linenr > .$outline\n";
3918 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003919 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003920 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003921 $prev_values = substr($curr_values, -1);
3922
Andy Whitcroft00df3442007-06-08 13:47:06 -07003923#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003924 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003925
Joe Perches99ca38c2020-10-15 20:12:09 -07003926# check for self assignments used to avoid compiler warnings
3927# e.g.: int foo = foo, *bar = NULL;
3928# struct foo bar = *(&(bar));
3929 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
3930 my $var = $1;
3931 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
3932 WARN("SELF_ASSIGNMENT",
3933 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
3934 }
3935 }
3936
Joe Perches11ca40a2016-12-12 16:46:31 -08003937# check for dereferences that span multiple lines
3938 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3939 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3940 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3941 my $ref = $1;
3942 $line =~ /^.\s*($Lval)/;
3943 $ref .= $1;
3944 $ref =~ s/\s//g;
3945 WARN("MULTILINE_DEREFERENCE",
3946 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3947 }
3948
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003949# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003950 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 -07003951 my $type = $1;
3952 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003953 $var = "" if (!defined $var);
3954 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003955 my $sign = $1;
3956 my $pointer = $2;
3957
3958 $pointer = "" if (!defined $pointer);
3959
3960 if (WARN("UNSPECIFIED_INT",
3961 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3962 $fix) {
3963 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003964 my $comp_pointer = $pointer;
3965 $comp_pointer =~ s/\s//g;
3966 $decl .= $comp_pointer;
3967 $decl = rtrim($decl) if ($var eq "");
3968 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003969 }
3970 }
3971 }
3972
Andy Whitcroft653d4872007-06-23 17:16:34 -07003973# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003974 if ($dbg_type) {
3975 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003976 ERROR("TEST_TYPE",
3977 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003978 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003979 ERROR("TEST_NOT_TYPE",
3980 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003981 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003982 next;
3983 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003984# TEST: allow direct testing of the attribute matcher.
3985 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003986 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003987 ERROR("TEST_ATTR",
3988 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003989 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003990 ERROR("TEST_NOT_ATTR",
3991 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003992 }
3993 next;
3994 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003995
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003996# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003997 if ($line =~ /^.\s*{/ &&
3998 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003999 if (ERROR("OPEN_BRACE",
4000 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004001 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4002 fix_delete_line($fixlinenr - 1, $prevrawline);
4003 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004004 my $fixedline = $prevrawline;
4005 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004006 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004007 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004008 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07004009 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07004010 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004011 }
4012
Andy Whitcroft653d4872007-06-23 17:16:34 -07004013#
4014# Checks which are anchored on the added line.
4015#
4016
4017# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004018 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07004019 my $path = $1;
4020 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004021 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08004022 "malformed #include filename\n" . $herecurr);
4023 }
4024 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4025 ERROR("UAPI_INCLUDE",
4026 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004027 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004028 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004029
4030# no C99 // comments
4031 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07004032 if (ERROR("C99_COMMENTS",
4033 "do not use C99 // comments\n" . $herecurr) &&
4034 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004035 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07004036 if ($line =~ /\/\/(.*)$/) {
4037 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07004038 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07004039 }
4040 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07004041 }
4042 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004043 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004044 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004045
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004046# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4047# the whole statement.
4048#print "APW <$lines[$realline_next - 1]>\n";
4049 if (defined $realline_next &&
4050 exists $lines[$realline_next - 1] &&
4051 !defined $suppress_export{$realline_next} &&
4052 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4053 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004054 # Handle definitions which produce identifiers with
4055 # a prefix:
4056 # XXX(foo);
4057 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07004058 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08004059 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07004060 $name =~ /^${Ident}_$2/) {
4061#print "FOO C name<$name>\n";
4062 $suppress_export{$realline_next} = 1;
4063
4064 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004065 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07004066 ^.DEFINE_$Ident\(\Q$name\E\)|
4067 ^.DECLARE_$Ident\(\Q$name\E\)|
4068 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004069 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4070 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07004071 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004072#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4073 $suppress_export{$realline_next} = 2;
4074 } else {
4075 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004076 }
4077 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004078 if (!defined $suppress_export{$linenr} &&
4079 $prevline =~ /^.\s*$/ &&
4080 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4081 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4082#print "FOO B <$lines[$linenr - 1]>\n";
4083 $suppress_export{$linenr} = 2;
4084 }
4085 if (defined $suppress_export{$linenr} &&
4086 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004087 WARN("EXPORT_SYMBOL",
4088 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004089 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004090
Joe Eloff5150bda2010-08-09 17:21:00 -07004091# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004092 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004093 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004094 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004095 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004096 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004097 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004098 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004099# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004100 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004101 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004102 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004103 $herecurr) &&
4104 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004105 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004106 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004107 }
4108
Joe Perches18130872014-08-06 16:11:22 -07004109# check for misordered declarations of char/short/int/long with signed/unsigned
4110 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4111 my $tmp = trim($1);
4112 WARN("MISORDERED_TYPE",
4113 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4114 }
4115
Joe Perches809e0822018-08-21 21:58:12 -07004116# check for unnecessary <signed> int declarations of short/long/long long
4117 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4118 my $type = trim($1);
4119 next if ($type !~ /\bint\b/);
4120 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4121 my $new_type = $type;
4122 $new_type =~ s/\b\s*int\s*\b/ /;
4123 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4124 $new_type =~ s/^const\s+//;
4125 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4126 $new_type = "const $new_type" if ($type =~ /^const\b/);
4127 $new_type =~ s/\s+/ /g;
4128 $new_type = trim($new_type);
4129 if (WARN("UNNECESSARY_INT",
4130 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4131 $fix) {
4132 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4133 }
4134 }
4135
Joe Perchescb710ec2010-10-26 14:23:20 -07004136# check for static const char * arrays.
4137 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004138 WARN("STATIC_CONST_CHAR_ARRAY",
4139 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004140 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004141 }
4142
4143# check for initialized const char arrays that should be static const
4144 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4145 if (WARN("STATIC_CONST_CHAR_ARRAY",
4146 "const array should probably be static const\n" . $herecurr) &&
4147 $fix) {
4148 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4149 }
4150 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004151
4152# check for static char foo[] = "bar" declarations.
4153 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004154 WARN("STATIC_CONST_CHAR_ARRAY",
4155 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004156 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004157 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004158
Joe Perchesab7e23f2015-04-16 12:44:22 -07004159# check for const <foo> const where <foo> is not a pointer or array type
4160 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4161 my $found = $1;
4162 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4163 WARN("CONST_CONST",
4164 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4165 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4166 WARN("CONST_CONST",
4167 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4168 }
4169 }
4170
Joe Perches9b0fa602014-04-03 14:49:18 -07004171# check for non-global char *foo[] = {"bar", ...} declarations.
4172 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4173 WARN("STATIC_CONST_CHAR_ARRAY",
4174 "char * array declaration might be better as static const\n" .
4175 $herecurr);
4176 }
4177
Joe Perchesb598b672015-04-16 12:44:36 -07004178# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4179 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4180 my $array = $1;
4181 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4182 my $array_div = $1;
4183 if (WARN("ARRAY_SIZE",
4184 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4185 $fix) {
4186 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4187 }
4188 }
4189 }
4190
Joe Perchesb36190c2014-01-27 17:07:18 -08004191# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004192 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004193 if (ERROR("FUNCTION_WITHOUT_ARGS",
4194 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4195 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004196 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004197 }
4198 }
4199
Andy Whitcroft653d4872007-06-23 17:16:34 -07004200# check for new typedefs, only function parameters and sparse annotations
4201# make sense.
4202 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004203 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004204 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004205 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004206 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004207 WARN("NEW_TYPEDEFS",
4208 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004209 }
4210
4211# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004212 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004213 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4214 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004215 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004216
Andy Whitcroft65863862009-01-06 14:41:21 -08004217 # Should start with a space.
4218 $to =~ s/^(\S)/ $1/;
4219 # Should not end with a space.
4220 $to =~ s/\s+$//;
4221 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004222 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004223 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004224
Joe Perches3705ce52013-07-03 15:05:31 -07004225## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004226 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004227 if (ERROR("POINTER_LOCATION",
4228 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4229 $fix) {
4230 my $sub_from = $ident;
4231 my $sub_to = $ident;
4232 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004233 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004234 s@\Q$sub_from\E@$sub_to@;
4235 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004236 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004237 }
4238 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4239 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004240 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004241
Andy Whitcroft65863862009-01-06 14:41:21 -08004242 # Should start with a space.
4243 $to =~ s/^(\S)/ $1/;
4244 # Should not end with a space.
4245 $to =~ s/\s+$//;
4246 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004247 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004248 }
4249 # Modifiers should have spaces.
4250 $to =~ s/(\b$Modifier$)/$1 /;
4251
Joe Perches3705ce52013-07-03 15:05:31 -07004252## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004253 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004254 if (ERROR("POINTER_LOCATION",
4255 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4256 $fix) {
4257
4258 my $sub_from = $match;
4259 my $sub_to = $match;
4260 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004261 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004262 s@\Q$sub_from\E@$sub_to@;
4263 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004264 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004265 }
4266
Joe Perches9d3e3c72015-09-09 15:37:27 -07004267# avoid BUG() or BUG_ON()
4268 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004269 my $msg_level = \&WARN;
4270 $msg_level = \&CHK if ($file);
4271 &{$msg_level}("AVOID_BUG",
4272 "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 -07004273 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004274
Joe Perches9d3e3c72015-09-09 15:37:27 -07004275# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004276 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004277 WARN("LINUX_VERSION_CODE",
4278 "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 -08004279 }
4280
Joe Perches17441222011-06-15 15:08:17 -07004281# check for uses of printk_ratelimit
4282 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004283 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004284 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004285 }
4286
Joe Percheseeef5732017-11-17 15:28:41 -08004287# printk should use KERN_* levels
4288 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4289 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4290 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004291 }
4292
Joe Perches243f3802012-05-31 16:26:09 -07004293 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4294 my $orig = $1;
4295 my $level = lc($orig);
4296 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004297 my $level2 = $level;
4298 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004299 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004300 "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 -07004301 }
4302
Joe Perchesdc139312013-02-21 16:44:13 -08004303 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4304 my $orig = $1;
4305 my $level = lc($orig);
4306 $level = "warn" if ($level eq "warning");
4307 $level = "dbg" if ($level eq "debug");
4308 WARN("PREFER_DEV_LEVEL",
4309 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4310 }
4311
Nicolas Boichat8020b252020-10-15 20:12:02 -07004312# trace_printk should not be used in production code.
4313 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4314 WARN("TRACE_PRINTK",
4315 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4316 }
4317
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004318# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4319# number of false positives, but assembly files are not checked, so at
4320# least the arch entry code will not trigger this warning.
4321 if ($line =~ /\bENOSYS\b/) {
4322 WARN("ENOSYS",
4323 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4324 }
4325
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004326# ENOTSUPP is not a standard error code and should be avoided in new patches.
4327# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4328# Similarly to ENOSYS warning a small number of false positives is expected.
4329 if (!$file && $line =~ /\bENOTSUPP\b/) {
4330 if (WARN("ENOTSUPP",
4331 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4332 $fix) {
4333 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4334 }
4335 }
4336
Andy Whitcroft653d4872007-06-23 17:16:34 -07004337# function brace can't be on same line, except for #defines of do while,
4338# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004339 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004340 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4341 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4342 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004343 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004344 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004345 $fix) {
4346 fix_delete_line($fixlinenr, $rawline);
4347 my $fixed_line = $rawline;
4348 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4349 my $line1 = $1;
4350 my $line2 = $2;
4351 fix_insert_line($fixlinenr, ltrim($line1));
4352 fix_insert_line($fixlinenr, "\+{");
4353 if ($line2 !~ /^\s*$/) {
4354 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4355 }
4356 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004357 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004358
Andy Whitcroft8905a672007-11-28 16:21:06 -08004359# open braces for enum, union and struct go on the same line.
4360 if ($line =~ /^.\s*{/ &&
4361 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004362 if (ERROR("OPEN_BRACE",
4363 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4364 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4365 fix_delete_line($fixlinenr - 1, $prevrawline);
4366 fix_delete_line($fixlinenr, $rawline);
4367 my $fixedline = rtrim($prevrawline) . " {";
4368 fix_insert_line($fixlinenr, $fixedline);
4369 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004370 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004371 if ($fixedline !~ /^\+\s*$/) {
4372 fix_insert_line($fixlinenr, $fixedline);
4373 }
4374 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004375 }
4376
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004377# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004378 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4379 if (WARN("SPACING",
4380 "missing space after $1 definition\n" . $herecurr) &&
4381 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004382 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004383 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4384 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004385 }
4386
Joe Perches31070b52014-01-23 15:54:49 -08004387# Function pointer declarations
4388# check spacing between type, funcptr, and args
4389# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004390 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004391 my $declare = $1;
4392 my $pre_pointer_space = $2;
4393 my $post_pointer_space = $3;
4394 my $funcname = $4;
4395 my $post_funcname_space = $5;
4396 my $pre_args_space = $6;
4397
Joe Perches91f72e92014-04-03 14:49:12 -07004398# the $Declare variable will capture all spaces after the type
4399# so check it for a missing trailing missing space but pointer return types
4400# don't need a space so don't warn for those.
4401 my $post_declare_space = "";
4402 if ($declare =~ /(\s+)$/) {
4403 $post_declare_space = $1;
4404 $declare = rtrim($declare);
4405 }
4406 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004407 WARN("SPACING",
4408 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004409 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004410 }
4411
4412# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004413# This test is not currently implemented because these declarations are
4414# equivalent to
4415# int foo(int bar, ...)
4416# and this is form shouldn't/doesn't generate a checkpatch warning.
4417#
4418# elsif ($declare =~ /\s{2,}$/) {
4419# WARN("SPACING",
4420# "Multiple spaces after return type\n" . $herecurr);
4421# }
Joe Perches31070b52014-01-23 15:54:49 -08004422
4423# unnecessary space "type ( *funcptr)(args...)"
4424 if (defined $pre_pointer_space &&
4425 $pre_pointer_space =~ /^\s/) {
4426 WARN("SPACING",
4427 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4428 }
4429
4430# unnecessary space "type (* funcptr)(args...)"
4431 if (defined $post_pointer_space &&
4432 $post_pointer_space =~ /^\s/) {
4433 WARN("SPACING",
4434 "Unnecessary space before function pointer name\n" . $herecurr);
4435 }
4436
4437# unnecessary space "type (*funcptr )(args...)"
4438 if (defined $post_funcname_space &&
4439 $post_funcname_space =~ /^\s/) {
4440 WARN("SPACING",
4441 "Unnecessary space after function pointer name\n" . $herecurr);
4442 }
4443
4444# unnecessary space "type (*funcptr) (args...)"
4445 if (defined $pre_args_space &&
4446 $pre_args_space =~ /^\s/) {
4447 WARN("SPACING",
4448 "Unnecessary space before function pointer arguments\n" . $herecurr);
4449 }
4450
4451 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004452 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004453 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004454 }
4455 }
4456
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004457# check for spacing round square brackets; allowed:
4458# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004459# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4460# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004461 while ($line =~ /(.*?\s)\[/g) {
4462 my ($where, $prefix) = ($-[1], $1);
4463 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004464 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004465 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004466 if (ERROR("BRACKET_SPACE",
4467 "space prohibited before open square bracket '['\n" . $herecurr) &&
4468 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004469 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004470 s/^(\+.*?)\s+\[/$1\[/;
4471 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004472 }
4473 }
4474
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004475# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004476 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004477 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004478 my $ctx_before = substr($line, 0, $-[1]);
4479 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004480
4481 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004482 if ($name =~ /^(?:
4483 if|for|while|switch|return|case|
4484 volatile|__volatile__|
4485 __attribute__|format|__extension__|
4486 asm|__asm__)$/x)
4487 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004488 # cpp #define statements have non-optional spaces, ie
4489 # if there is a space between the name and the open
4490 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004491 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004492
4493 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004494 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004495
4496 # If this whole things ends with a type its most
4497 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004498 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004499
4500 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004501 if (WARN("SPACING",
4502 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4503 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004504 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004505 s/\b$name\s+\(/$name\(/;
4506 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004507 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004508 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004509
Andy Whitcroft653d4872007-06-23 17:16:34 -07004510# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004511 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004512 my $fixed_line = "";
4513 my $line_fixed = 0;
4514
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004515 my $ops = qr{
4516 <<=|>>=|<=|>=|==|!=|
4517 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4518 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004519 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004520 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004521 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004522 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004523
4524## print("element count: <" . $#elements . ">\n");
4525## foreach my $el (@elements) {
4526## print("el: <$el>\n");
4527## }
4528
4529 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004530 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004531
Joe Perches3705ce52013-07-03 15:05:31 -07004532 foreach my $el (@elements) {
4533 push(@fix_elements, substr($rawline, $off, length($el)));
4534 $off += length($el);
4535 }
4536
4537 $off = 0;
4538
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004539 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004540 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004541
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004542 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004543
4544 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4545
4546## print("n: <$n> good: <$good>\n");
4547
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004548 $off += length($elements[$n]);
4549
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004550 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004551 my $ca = substr($opline, 0, $off);
4552 my $cc = '';
4553 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4554 $cc = substr($opline, $off + length($elements[$n + 1]));
4555 }
4556 my $cb = "$ca$;$cc";
4557
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004558 my $a = '';
4559 $a = 'V' if ($elements[$n] ne '');
4560 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004561 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004562 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4563 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004564 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004565
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004566 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004567
4568 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004569 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004570 $c = 'V' if ($elements[$n + 2] ne '');
4571 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004572 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004573 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4574 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004575 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004576 } else {
4577 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004578 }
4579
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004580 my $ctx = "${a}x${c}";
4581
4582 my $at = "(ctx:$ctx)";
4583
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004584 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004585 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004586
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004587 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004588 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004589
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004590 # Get the full operator variant.
4591 my $opv = $op . substr($curr_vars, $off, 1);
4592
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004593 # Ignore operators passed as parameters.
4594 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004595 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004596
Andy Whitcroftcf655042008-03-04 14:28:20 -08004597# # Ignore comments
4598# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004599
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004600 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004601 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004602 if ($ctx !~ /.x[WEBC]/ &&
4603 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004604 if (ERROR("SPACING",
4605 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004606 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004607 $line_fixed = 1;
4608 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004609 }
4610
4611 # // is a comment
4612 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004613
Joe Perchesb00e4812014-04-03 14:49:33 -07004614 # : when part of a bitfield
4615 } elsif ($opv eq ':B') {
4616 # skip the bitfield test for now
4617
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004618 # No spaces for:
4619 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004620 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004621 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004622 if (ERROR("SPACING",
4623 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004624 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004625 if (defined $fix_elements[$n + 2]) {
4626 $fix_elements[$n + 2] =~ s/^\s+//;
4627 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004628 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004629 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004630 }
4631
Joe Perches23810972014-12-10 15:51:32 -08004632 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004633 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004634 my $rtrim_before = 0;
4635 my $space_after = 0;
4636 if ($ctx =~ /Wx./) {
4637 if (ERROR("SPACING",
4638 "space prohibited before that '$op' $at\n" . $hereptr)) {
4639 $line_fixed = 1;
4640 $rtrim_before = 1;
4641 }
4642 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004643 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004644 if (ERROR("SPACING",
4645 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004646 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004647 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004648 $space_after = 1;
4649 }
4650 }
4651 if ($rtrim_before || $space_after) {
4652 if ($rtrim_before) {
4653 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4654 } else {
4655 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4656 }
4657 if ($space_after) {
4658 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004659 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004660 }
4661
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004662 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004663 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004664 #warn "'*' is part of type\n";
4665
4666 # unary operators should have a space before and
4667 # none after. May be left adjacent to another
4668 # unary operator, or a cast
4669 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004670 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004671 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004672 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004673 if (ERROR("SPACING",
4674 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004675 if ($n != $last_after + 2) {
4676 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4677 $line_fixed = 1;
4678 }
Joe Perches3705ce52013-07-03 15:05:31 -07004679 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004680 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004681 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004682 # A unary '*' may be const
4683
4684 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004685 if (ERROR("SPACING",
4686 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004687 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004688 if (defined $fix_elements[$n + 2]) {
4689 $fix_elements[$n + 2] =~ s/^\s+//;
4690 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004691 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004692 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004693 }
4694
4695 # unary ++ and unary -- are allowed no space on one side.
4696 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004697 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004698 if (ERROR("SPACING",
4699 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004700 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004701 $line_fixed = 1;
4702 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004703 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004704 if ($ctx =~ /Wx[BE]/ ||
4705 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004706 if (ERROR("SPACING",
4707 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004708 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004709 $line_fixed = 1;
4710 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004711 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004712 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004713 if (ERROR("SPACING",
4714 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004715 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004716 if (defined $fix_elements[$n + 2]) {
4717 $fix_elements[$n + 2] =~ s/^\s+//;
4718 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004719 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004720 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004721 }
4722
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004723 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004724 } elsif ($op eq '<<' or $op eq '>>' or
4725 $op eq '&' or $op eq '^' or $op eq '|' or
4726 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004727 $op eq '*' or $op eq '/' or
4728 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004729 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004730 if ($check) {
4731 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4732 if (CHK("SPACING",
4733 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4734 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4735 $fix_elements[$n + 2] =~ s/^\s+//;
4736 $line_fixed = 1;
4737 }
4738 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4739 if (CHK("SPACING",
4740 "space preferred before that '$op' $at\n" . $hereptr)) {
4741 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4742 $line_fixed = 1;
4743 }
4744 }
4745 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004746 if (ERROR("SPACING",
4747 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004748 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4749 if (defined $fix_elements[$n + 2]) {
4750 $fix_elements[$n + 2] =~ s/^\s+//;
4751 }
Joe Perches3705ce52013-07-03 15:05:31 -07004752 $line_fixed = 1;
4753 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004754 }
4755
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004756 # A colon needs no spaces before when it is
4757 # terminating a case value or a label.
4758 } elsif ($opv eq ':C' || $opv eq ':L') {
4759 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004760 if (ERROR("SPACING",
4761 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004762 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004763 $line_fixed = 1;
4764 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004765 }
4766
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004767 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004768 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004769 my $ok = 0;
4770
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004771 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004772 if (($op eq '<' &&
4773 $cc =~ /^\S+\@\S+>/) ||
4774 ($op eq '>' &&
4775 $ca =~ /<\S+\@\S+$/))
4776 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07004777 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004778 }
4779
Joe Perchese0df7e12015-04-16 12:44:53 -07004780 # for asm volatile statements
4781 # ignore a colon with another
4782 # colon immediately before or after
4783 if (($op eq ':') &&
4784 ($ca =~ /:$/ || $cc =~ /^:/)) {
4785 $ok = 1;
4786 }
4787
Joe Perches84731622013-11-12 15:10:05 -08004788 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004789 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004790 my $msg_level = \&ERROR;
4791 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004792
Jean Delvare0675a8f2017-09-08 16:16:07 -07004793 if (&{$msg_level}("SPACING",
4794 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004795 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4796 if (defined $fix_elements[$n + 2]) {
4797 $fix_elements[$n + 2] =~ s/^\s+//;
4798 }
Joe Perches3705ce52013-07-03 15:05:31 -07004799 $line_fixed = 1;
4800 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004801 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004802 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004803 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004804
4805## print("n: <$n> GOOD: <$good>\n");
4806
4807 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004808 }
Joe Perches3705ce52013-07-03 15:05:31 -07004809
4810 if (($#elements % 2) == 0) {
4811 $fixed_line = $fixed_line . $fix_elements[$#elements];
4812 }
4813
Joe Perches194f66f2014-08-06 16:11:03 -07004814 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4815 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004816 }
4817
4818
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004819 }
4820
Joe Perches786b6322013-07-03 15:05:32 -07004821# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004822 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004823 if (WARN("SPACING",
4824 "space prohibited before semicolon\n" . $herecurr) &&
4825 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004826 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004827 s/^(\+.*\S)\s+;/$1;/;
4828 }
4829 }
4830
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004831# check for multiple assignments
4832 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004833 CHK("MULTIPLE_ASSIGNMENTS",
4834 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004835 }
4836
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004837## # check for multiple declarations, allowing for a function declaration
4838## # continuation.
4839## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4840## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4841##
4842## # Remove any bracketed sections to ensure we do not
4843## # falsly report the parameters of functions.
4844## my $ln = $line;
4845## while ($ln =~ s/\([^\(\)]*\)//g) {
4846## }
4847## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004848## WARN("MULTIPLE_DECLARATION",
4849## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004850## }
4851## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004852
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004853#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004854 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004855 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004856 if (ERROR("SPACING",
4857 "space required before the open brace '{'\n" . $herecurr) &&
4858 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004859 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004860 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004861 }
4862
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004863## # check for blank lines before declarations
4864## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4865## $prevrawline =~ /^.\s*$/) {
4866## WARN("SPACING",
4867## "No blank lines before declarations\n" . $hereprev);
4868## }
4869##
4870
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004871# closing brace should have a space following it when it has anything
4872# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07004873 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004874 if (ERROR("SPACING",
4875 "space required after that close brace '}'\n" . $herecurr) &&
4876 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004877 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004878 s/}((?!(?:,|;|\)))\S)/} $1/;
4879 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004880 }
4881
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004882# check spacing on square brackets
4883 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004884 if (ERROR("SPACING",
4885 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4886 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004887 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004888 s/\[\s+/\[/;
4889 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004890 }
4891 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004892 if (ERROR("SPACING",
4893 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4894 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004895 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004896 s/\s+\]/\]/;
4897 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004898 }
4899
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004900# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004901 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4902 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004903 if (ERROR("SPACING",
4904 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4905 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004906 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004907 s/\(\s+/\(/;
4908 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004909 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004910 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004911 $line !~ /for\s*\(.*;\s+\)/ &&
4912 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004913 if (ERROR("SPACING",
4914 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4915 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004916 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004917 s/\s+\)/\)/;
4918 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004919 }
4920
Joe Perchese2826fd2014-08-06 16:10:48 -07004921# check unnecessary parentheses around addressof/dereference single $Lvals
4922# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4923
4924 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004925 my $var = $1;
4926 if (CHK("UNNECESSARY_PARENTHESES",
4927 "Unnecessary parentheses around $var\n" . $herecurr) &&
4928 $fix) {
4929 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4930 }
4931 }
4932
4933# check for unnecessary parentheses around function pointer uses
4934# ie: (foo->bar)(); should be foo->bar();
4935# but not "if (foo->bar) (" to avoid some false positives
4936 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4937 my $var = $2;
4938 if (CHK("UNNECESSARY_PARENTHESES",
4939 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4940 $fix) {
4941 my $var2 = deparenthesize($var);
4942 $var2 =~ s/\s//g;
4943 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4944 }
4945 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004946
Joe Perches63b7c732017-09-08 16:16:01 -07004947# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004948# when !drivers/staging or command-line uses --strict
4949 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004950 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004951 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4952 my $if_stat = $1;
4953 my $test = substr($2, 1, -1);
4954 my $herectx;
4955 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4956 my $match = $1;
4957 # avoid parentheses around potential macro args
4958 next if ($match =~ /^\s*\w+\s*$/);
4959 if (!defined($herectx)) {
4960 $herectx = $here . "\n";
4961 my $cnt = statement_rawlines($if_stat);
4962 for (my $n = 0; $n < $cnt; $n++) {
4963 my $rl = raw_line($linenr, $n);
4964 $herectx .= $rl . "\n";
4965 last if $rl =~ /^[ \+].*\{/;
4966 }
4967 }
4968 CHK("UNNECESSARY_PARENTHESES",
4969 "Unnecessary parentheses around '$match'\n" . $herectx);
4970 }
4971 }
4972
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004973#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004974 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004975 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004976 if (WARN("INDENTED_LABEL",
4977 "labels should not be indented\n" . $herecurr) &&
4978 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004979 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004980 s/^(.)\s+/$1/;
4981 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004982 }
4983
Joe Perches40873ab2020-10-15 20:11:56 -07004984# check if a statement with a comma should be two statements like:
4985# foo = bar(), /* comma should be semicolon */
4986# bar = baz();
4987 if (defined($stat) &&
4988 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
4989 my $cnt = statement_rawlines($stat);
4990 my $herectx = get_stat_here($linenr, $cnt, $here);
4991 WARN("SUSPECT_COMMA_SEMICOLON",
4992 "Possible comma where semicolon could be used\n" . $herectx);
4993 }
4994
Joe Perches5b9553a2014-04-03 14:49:21 -07004995# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004996 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004997 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004998 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004999 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5000 my $value = $1;
5001 $value = deparenthesize($value);
5002 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5003 ERROR("RETURN_PARENTHESES",
5004 "return is not a function, parentheses are not required\n" . $herecurr);
5005 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005006 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005007 ERROR("SPACING",
5008 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005009 }
5010 }
Joe Perches507e5142013-11-12 15:10:13 -08005011
Joe Perchesb43ae212014-06-23 13:22:07 -07005012# unnecessary return in a void function
5013# at end-of-function, with the previous line a single leading tab, then return;
5014# and the line before that not a goto label target like "out:"
5015 if ($sline =~ /^[ \+]}\s*$/ &&
5016 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5017 $linenr >= 3 &&
5018 $lines[$linenr - 3] =~ /^[ +]/ &&
5019 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07005020 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07005021 "void function return statements are not generally useful\n" . $hereprev);
5022 }
Joe Perches9819cf22014-06-04 16:12:09 -07005023
Joe Perches189248d2014-01-23 15:54:47 -08005024# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07005025 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08005026 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5027 my $openparens = $1;
5028 my $count = $openparens =~ tr@\(@\(@;
5029 my $msg = "";
5030 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5031 my $comp = $4; #Not $1 because of $LvalOrFunc
5032 $msg = " - maybe == should be = ?" if ($comp eq "==");
5033 WARN("UNNECESSARY_PARENTHESES",
5034 "Unnecessary parentheses$msg\n" . $herecurr);
5035 }
5036 }
5037
Joe Perchesc5595fa2015-09-09 15:37:58 -07005038# comparisons with a constant or upper case identifier on the left
5039# avoid cases like "foo + BAR < baz"
5040# only fix matches surrounded by parentheses to avoid incorrect
5041# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07005042 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005043 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5044 my $lead = $1;
5045 my $const = $2;
5046 my $comp = $3;
5047 my $to = $4;
5048 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07005049 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07005050 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5051 WARN("CONSTANT_COMPARISON",
5052 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5053 $fix) {
5054 if ($comp eq "<") {
5055 $newcomp = ">";
5056 } elsif ($comp eq "<=") {
5057 $newcomp = ">=";
5058 } elsif ($comp eq ">") {
5059 $newcomp = "<";
5060 } elsif ($comp eq ">=") {
5061 $newcomp = "<=";
5062 }
5063 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5064 }
5065 }
5066
Joe Perchesf34e4a42015-04-16 12:44:19 -07005067# Return of what appears to be an errno should normally be negative
5068 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005069 my $name = $1;
5070 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07005071 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07005072 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07005073 }
5074 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005075
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005076# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07005077 if ($line =~ /\b(if|while|for|switch)\(/) {
5078 if (ERROR("SPACING",
5079 "space required before the open parenthesis '('\n" . $herecurr) &&
5080 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005081 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005082 s/\b(if|while|for|switch)\(/$1 \(/;
5083 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005084 }
5085
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07005086# Check for illegal assignment in if conditional -- and check for trailing
5087# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005088 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08005089 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5090 ctx_statement_block($linenr, $realcnt, 0)
5091 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005092 my ($stat_next) = ctx_statement_block($line_nr_next,
5093 $remain_next, $off_next);
5094 $stat_next =~ s/\n./\n /g;
5095 ##print "stat<$stat> stat_next<$stat_next>\n";
5096
5097 if ($stat_next =~ /^\s*while\b/) {
5098 # If the statement carries leading newlines,
5099 # then count those as offsets.
5100 my ($whitespace) =
5101 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5102 my $offset =
5103 statement_rawlines($whitespace) - 1;
5104
5105 $suppress_whiletrailers{$line_nr_next +
5106 $offset} = 1;
5107 }
5108 }
5109 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005110 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005111 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005112 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005113
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005114 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches65b64b32020-08-11 18:35:10 -07005115 if (ERROR("ASSIGN_IN_IF",
5116 "do not use assignment in if condition\n" . $herecurr) &&
5117 $fix && $perl_version_ok) {
5118 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5119 my $space = $1;
5120 my $not = $2;
5121 my $statement = $3;
5122 my $assigned = $4;
5123 my $test = $8;
5124 my $against = $9;
5125 my $brace = $15;
5126 fix_delete_line($fixlinenr, $rawline);
5127 fix_insert_line($fixlinenr, "$space$statement;");
5128 my $newline = "${space}if (";
5129 $newline .= '!' if defined($not);
5130 $newline .= '(' if (defined $not && defined($test) && defined($against));
5131 $newline .= "$assigned";
5132 $newline .= " $test $against" if (defined($test) && defined($against));
5133 $newline .= ')' if (defined $not && defined($test) && defined($against));
5134 $newline .= ')';
5135 $newline .= " {" if (defined($brace));
5136 fix_insert_line($fixlinenr + 1, $newline);
5137 }
5138 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005139 }
5140
5141 # Find out what is on the end of the line after the
5142 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005143 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005144 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005145 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005146 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5147 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005148 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005149 # Find out how long the conditional actually is.
5150 my @newlines = ($c =~ /\n/gs);
5151 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005152 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005153
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005154 $stat_real = raw_line($linenr, $cond_lines)
5155 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005156 if (defined($stat_real) && $cond_lines > 1) {
5157 $stat_real = "[...]\n$stat_real";
5158 }
5159
Joe Perches000d1cc12011-07-25 17:13:25 -07005160 ERROR("TRAILING_STATEMENTS",
5161 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005162 }
5163 }
5164
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005165# Check for bitwise tests written as boolean
5166 if ($line =~ /
5167 (?:
5168 (?:\[|\(|\&\&|\|\|)
5169 \s*0[xX][0-9]+\s*
5170 (?:\&\&|\|\|)
5171 |
5172 (?:\&\&|\|\|)
5173 \s*0[xX][0-9]+\s*
5174 (?:\&\&|\|\||\)|\])
5175 )/x)
5176 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005177 WARN("HEXADECIMAL_BOOLEAN_TEST",
5178 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005179 }
5180
Andy Whitcroft8905a672007-11-28 16:21:06 -08005181# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005182 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5183 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005184 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005185 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005186 ERROR("TRAILING_STATEMENTS",
5187 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005188 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005189 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005190# if should not continue a brace
5191 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005192 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005193 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005194 $herecurr);
5195 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005196# case and default should not have general statements after them
5197 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5198 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005199 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005200 \s*return\s+
5201 )/xg)
5202 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005203 ERROR("TRAILING_STATEMENTS",
5204 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005205 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005206
5207 # Check for }<nl>else {, these must be at the same
5208 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005209 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5210 $previndent == $indent) {
5211 if (ERROR("ELSE_AFTER_BRACE",
5212 "else should follow close brace '}'\n" . $hereprev) &&
5213 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5214 fix_delete_line($fixlinenr - 1, $prevrawline);
5215 fix_delete_line($fixlinenr, $rawline);
5216 my $fixedline = $prevrawline;
5217 $fixedline =~ s/}\s*$//;
5218 if ($fixedline !~ /^\+\s*$/) {
5219 fix_insert_line($fixlinenr, $fixedline);
5220 }
5221 $fixedline = $rawline;
5222 $fixedline =~ s/^(.\s*)else/$1} else/;
5223 fix_insert_line($fixlinenr, $fixedline);
5224 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005225 }
5226
Joe Perches8b8856f2014-08-06 16:11:14 -07005227 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5228 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005229 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5230
5231 # Find out what is on the end of the line after the
5232 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005233 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005234 $s =~ s/\n.*//g;
5235
5236 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005237 if (ERROR("WHILE_AFTER_BRACE",
5238 "while should follow close brace '}'\n" . $hereprev) &&
5239 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5240 fix_delete_line($fixlinenr - 1, $prevrawline);
5241 fix_delete_line($fixlinenr, $rawline);
5242 my $fixedline = $prevrawline;
5243 my $trailing = $rawline;
5244 $trailing =~ s/^\+//;
5245 $trailing = trim($trailing);
5246 $fixedline =~ s/}\s*$/} $trailing/;
5247 fix_insert_line($fixlinenr, $fixedline);
5248 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005249 }
5250 }
5251
Joe Perches95e2c602013-07-03 15:05:20 -07005252#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005253 while ($line =~ m{($Constant|$Lval)}g) {
5254 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005255
Joe Perches95e2c602013-07-03 15:05:20 -07005256#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005257 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005258 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005259#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005260 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005261#Ignore SI style variants like nS, mV and dB
5262#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5263 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005264#Ignore some three character SI units explicitly, like MiB and KHz
5265 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005266 while ($var =~ m{($Ident)}g) {
5267 my $word = $1;
5268 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005269 if ($check) {
5270 seed_camelcase_includes();
5271 if (!$file && !$camelcase_file_seeded) {
5272 seed_camelcase_file($realfile);
5273 $camelcase_file_seeded = 1;
5274 }
5275 }
Joe Perches7e781f62013-09-11 14:23:55 -07005276 if (!defined $camelcase{$word}) {
5277 $camelcase{$word} = 1;
5278 CHK("CAMELCASE",
5279 "Avoid CamelCase: <$word>\n" . $herecurr);
5280 }
Joe Perches34456862013-07-03 15:05:34 -07005281 }
Joe Perches323c1262012-12-17 16:02:07 -08005282 }
5283 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005284
5285#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005286 if ($line =~ /\#\s*define.*\\\s+$/) {
5287 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5288 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5289 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005290 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005291 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005292 }
5293
Fabian Frederick0e212e02015-04-16 12:44:25 -07005294# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5295# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005296 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005297 my $file = "$1.h";
5298 my $checkfile = "include/linux/$file";
5299 if (-f "$root/$checkfile" &&
5300 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005301 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005302 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005303 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5304 if ($asminclude > 0) {
5305 if ($realfile =~ m{^arch/}) {
5306 CHK("ARCH_INCLUDE_LINUX",
5307 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5308 } else {
5309 WARN("INCLUDE_LINUX",
5310 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5311 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005312 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005313 }
5314 }
5315
Andy Whitcroft653d4872007-06-23 17:16:34 -07005316# multi-statement macros should be enclosed in a do while loop, grab the
5317# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005318# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005319 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5320 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005321 my $ln = $linenr;
5322 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005323 my ($off, $dstat, $dcond, $rest);
5324 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005325 my $has_flow_statement = 0;
5326 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005327 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005328 ctx_statement_block($linenr, $realcnt, 0);
5329 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005330 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005331 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005332
Joe Perches08a28432014-10-13 15:51:55 -07005333 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005334 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005335
Joe Perchesf59b64b2016-10-11 13:52:08 -07005336 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5337 my $define_args = $1;
5338 my $define_stmt = $dstat;
5339 my @def_args = ();
5340
5341 if (defined $define_args && $define_args ne "") {
5342 $define_args = substr($define_args, 1, length($define_args) - 2);
5343 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005344 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005345 @def_args = split(",", $define_args);
5346 }
5347
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005348 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005349 $dstat =~ s/\\\n.//g;
5350 $dstat =~ s/^\s*//s;
5351 $dstat =~ s/\s*$//s;
5352
5353 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005354 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5355 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005356 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005357 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005358 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005359
Antonio Borneo342d3d22020-04-06 20:11:01 -07005360 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005361 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5362 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005363 {
5364 }
5365
Joe Perches42e15292016-03-15 14:58:01 -07005366 # Make asm volatile uses seem like a generic function
5367 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5368
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005369 my $exceptions = qr{
5370 $Declare|
5371 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005372 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005373 DECLARE_PER_CPU|
5374 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005375 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005376 union|
5377 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005378 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005379 ^\"|\"$|
5380 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005381 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005382 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005383
5384 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005385 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005386 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005387
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005388 if ($dstat ne '' &&
5389 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5390 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005391 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005392 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005393 $dstat !~ /$exceptions/ &&
5394 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005395 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005396 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005397 $dstat !~ /^for\s*$Constant$/ && # for (...)
5398 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5399 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005400 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005401 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005402 {
Joe Perchese7955562017-05-08 15:55:48 -07005403 if ($dstat =~ /^\s*if\b/) {
5404 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5405 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5406 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005407 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5408 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5409 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005410 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005411 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005412 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005413
5414 }
Joe Perches52076492016-10-11 13:52:14 -07005415
5416 # Make $define_stmt single line, comment-free, etc
5417 my @stmt_array = split('\n', $define_stmt);
5418 my $first = 1;
5419 $define_stmt = "";
5420 foreach my $l (@stmt_array) {
5421 $l =~ s/\\$//;
5422 if ($first) {
5423 $define_stmt = $l;
5424 $first = 0;
5425 } elsif ($l =~ /^[\+ ]/) {
5426 $define_stmt .= substr($l, 1);
5427 }
5428 }
5429 $define_stmt =~ s/$;//g;
5430 $define_stmt =~ s/\s+/ /g;
5431 $define_stmt = trim($define_stmt);
5432
Joe Perchesf59b64b2016-10-11 13:52:08 -07005433# check if any macro arguments are reused (ignore '...' and 'type')
5434 foreach my $arg (@def_args) {
5435 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005436 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005437 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005438 $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 -07005439 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5440 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005441 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005442 if ($use_cnt > 1) {
5443 CHK("MACRO_ARG_REUSE",
5444 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005445 }
5446# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005447 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005448 ((defined($1) && $1 ne ',') ||
5449 (defined($2) && $2 ne ','))) {
5450 CHK("MACRO_ARG_PRECEDENCE",
5451 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005452 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005453 }
Joe Perches5023d342012-12-17 16:01:47 -08005454
Joe Perches08a28432014-10-13 15:51:55 -07005455# check for macros with flow control, but without ## concatenation
5456# ## concatenation is commonly a macro that defines a function so ignore those
5457 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005458 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005459 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005460
Joe Perches08a28432014-10-13 15:51:55 -07005461 WARN("MACRO_WITH_FLOW_CONTROL",
5462 "Macros with flow control statements should be avoided\n" . "$herectx");
5463 }
5464
Joe Perches481eb482012-12-17 16:01:56 -08005465# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005466
5467 } else {
5468 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005469 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5470 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005471 $line =~ /^\+.*\\$/) {
5472 WARN("LINE_CONTINUATIONS",
5473 "Avoid unnecessary line continuations\n" . $herecurr);
5474 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005475 }
5476
Joe Perchesb13edf72012-07-30 14:41:24 -07005477# do {} while (0) macro tests:
5478# single-statement macros do not need to be enclosed in do while (0) loop,
5479# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005480 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005481 $realfile !~ m@/vmlinux.lds.h$@ &&
5482 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5483 my $ln = $linenr;
5484 my $cnt = $realcnt;
5485 my ($off, $dstat, $dcond, $rest);
5486 my $ctx = '';
5487 ($dstat, $dcond, $ln, $cnt, $off) =
5488 ctx_statement_block($linenr, $realcnt, 0);
5489 $ctx = $dstat;
5490
5491 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005492 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005493
5494 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5495 my $stmts = $2;
5496 my $semis = $3;
5497
5498 $ctx =~ s/\n*$//;
5499 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005500 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005501
Joe Perchesac8e97f2012-08-21 16:15:53 -07005502 if (($stmts =~ tr/;/;/) == 1 &&
5503 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005504 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5505 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5506 }
5507 if (defined $semis && $semis ne "") {
5508 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5509 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5510 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005511 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5512 $ctx =~ s/\n*$//;
5513 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005514 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005515
5516 WARN("TRAILING_SEMICOLON",
5517 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005518 }
5519 }
5520
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005521# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005522 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5523 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005524 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005525 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005526 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5527 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005528 my @allowed = ();
5529 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005530 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005531 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005532 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005533 for my $chunk (@chunks) {
5534 my ($cond, $block) = @{$chunk};
5535
Andy Whitcroft773647a2008-03-28 14:15:58 -07005536 # If the condition carries leading newlines, then count those as offsets.
5537 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5538 my $offset = statement_rawlines($whitespace) - 1;
5539
Joe Perchesaad4f612012-03-23 15:02:19 -07005540 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005541 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5542
5543 # We have looked at and allowed this specific line.
5544 $suppress_ifbraces{$ln + $offset} = 1;
5545
5546 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005547 $ln += statement_rawlines($block) - 1;
5548
Andy Whitcroft773647a2008-03-28 14:15:58 -07005549 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005550
5551 $seen++ if ($block =~ /^\s*{/);
5552
Joe Perchesaad4f612012-03-23 15:02:19 -07005553 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005554 if (statement_lines($cond) > 1) {
5555 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005556 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005557 }
5558 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005559 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005560 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005561 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005562 if (statement_block_size($block) > 1) {
5563 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005564 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005565 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005566 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005567 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005568 if ($seen) {
5569 my $sum_allowed = 0;
5570 foreach (@allowed) {
5571 $sum_allowed += $_;
5572 }
5573 if ($sum_allowed == 0) {
5574 WARN("BRACES",
5575 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5576 } elsif ($sum_allowed != $allow &&
5577 $seen != $allow) {
5578 CHK("BRACES",
5579 "braces {} should be used on all arms of this statement\n" . $herectx);
5580 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005581 }
5582 }
5583 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005584 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005585 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005586 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005587
Andy Whitcroftcf655042008-03-04 14:28:20 -08005588 # Check the pre-context.
5589 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5590 #print "APW: ALLOWED: pre<$1>\n";
5591 $allowed = 1;
5592 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005593
5594 my ($level, $endln, @chunks) =
5595 ctx_statement_full($linenr, $realcnt, $-[0]);
5596
Andy Whitcroftcf655042008-03-04 14:28:20 -08005597 # Check the condition.
5598 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005599 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005600 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005601 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005602 }
5603 if (statement_lines($cond) > 1) {
5604 #print "APW: ALLOWED: cond<$cond>\n";
5605 $allowed = 1;
5606 }
5607 if ($block =~/\b(?:if|for|while)\b/) {
5608 #print "APW: ALLOWED: block<$block>\n";
5609 $allowed = 1;
5610 }
5611 if (statement_block_size($block) > 1) {
5612 #print "APW: ALLOWED: lines block<$block>\n";
5613 $allowed = 1;
5614 }
5615 # Check the post-context.
5616 if (defined $chunks[1]) {
5617 my ($cond, $block) = @{$chunks[1]};
5618 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005619 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005620 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005621 if ($block =~ /^\s*\{/) {
5622 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5623 $allowed = 1;
5624 }
5625 }
5626 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005627 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005628 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005629
Joe Perches000d1cc12011-07-25 17:13:25 -07005630 WARN("BRACES",
5631 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005632 }
5633 }
5634
Joe Perchese4c5bab2017-02-24 15:01:41 -08005635# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005636 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5637 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005638 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5639 }
5640
Joe Perches0979ae62012-12-17 16:01:59 -08005641# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005642 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005643 if (CHK("BRACES",
5644 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5645 $fix && $prevrawline =~ /^\+/) {
5646 fix_delete_line($fixlinenr - 1, $prevrawline);
5647 }
Joe Perches0979ae62012-12-17 16:01:59 -08005648 }
Joe Perches77b9a532013-07-03 15:05:29 -07005649 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005650 if (CHK("BRACES",
5651 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5652 $fix) {
5653 fix_delete_line($fixlinenr, $rawline);
5654 }
Joe Perches0979ae62012-12-17 16:01:59 -08005655 }
5656
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005657# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005658 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5659 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005660 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005661 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005662 }
5663
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005664# Check for user-visible strings broken across lines, which breaks the ability
5665# to grep for the string. Make exceptions when the previous string ends in a
5666# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5667# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005668 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005669 $prevline =~ /"\s*$/ &&
5670 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5671 if (WARN("SPLIT_STRING",
5672 "quoted string split across lines\n" . $hereprev) &&
5673 $fix &&
5674 $prevrawline =~ /^\+.*"\s*$/ &&
5675 $last_coalesced_string_linenr != $linenr - 1) {
5676 my $extracted_string = get_quoted_string($line, $rawline);
5677 my $comma_close = "";
5678 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5679 $comma_close = $1;
5680 }
5681
5682 fix_delete_line($fixlinenr - 1, $prevrawline);
5683 fix_delete_line($fixlinenr, $rawline);
5684 my $fixedline = $prevrawline;
5685 $fixedline =~ s/"\s*$//;
5686 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5687 fix_insert_line($fixlinenr - 1, $fixedline);
5688 $fixedline = $rawline;
5689 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5690 if ($fixedline !~ /\+\s*$/) {
5691 fix_insert_line($fixlinenr, $fixedline);
5692 }
5693 $last_coalesced_string_linenr = $linenr;
5694 }
5695 }
5696
5697# check for missing a space in a string concatenation
5698 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5699 WARN('MISSING_SPACE',
5700 "break quoted strings at a space character\n" . $hereprev);
5701 }
5702
Joe Perchese4b7d302017-05-08 15:55:51 -07005703# check for an embedded function name in a string when the function is known
5704# This does not work very well for -f --file checking as it depends on patch
5705# context providing the function name or a single line form for in-file
5706# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005707 if ($line =~ /^\+.*$String/ &&
5708 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005709 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5710 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005711 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005712 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005713 }
5714
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005715# check for spaces before a quoted newline
5716 if ($rawline =~ /^.*\".*\s\\n/) {
5717 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5718 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5719 $fix) {
5720 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5721 }
5722
5723 }
5724
Joe Perchesf17dba42014-10-13 15:51:51 -07005725# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005726 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5727 if (CHK("CONCATENATED_STRING",
5728 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5729 $fix) {
5730 while ($line =~ /($String)/g) {
5731 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5732 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5733 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5734 }
5735 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005736 }
5737
Joe Perches90ad30e2014-12-10 15:51:59 -08005738# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005739 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005740 if (WARN("STRING_FRAGMENTS",
5741 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5742 $fix) {
5743 while ($line =~ /($String)(?=\s*")/g) {
5744 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5745 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5746 }
5747 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005748 }
5749
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005750# check for non-standard and hex prefixed decimal printf formats
5751 my $show_L = 1; #don't show the same defect twice
5752 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005753 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005754 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005755 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005756 # check for %L
5757 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005758 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005759 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5760 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005761 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005762 # check for %Z
5763 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5764 WARN("PRINTF_Z",
5765 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5766 $show_Z = 0;
5767 }
5768 # check for 0x<decimal>
5769 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5770 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005771 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5772 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005773 }
5774
5775# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005776 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005777 WARN("LINE_CONTINUATIONS",
5778 "Avoid line continuations in quoted strings\n" . $herecurr);
5779 }
5780
Andy Whitcroft00df3442007-06-08 13:47:06 -07005781# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005782 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005783 WARN("IF_0",
5784 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5785 }
5786
5787# warn about #if 1
5788 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5789 WARN("IF_1",
5790 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005791 }
5792
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005793# check for needless "if (<foo>) fn(<foo>)" uses
5794 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005795 my $tested = quotemeta($1);
5796 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5797 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5798 my $func = $1;
5799 if (WARN('NEEDLESS_IF',
5800 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5801 $fix) {
5802 my $do_fix = 1;
5803 my $leading_tabs = "";
5804 my $new_leading_tabs = "";
5805 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5806 $leading_tabs = $1;
5807 } else {
5808 $do_fix = 0;
5809 }
5810 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5811 $new_leading_tabs = $1;
5812 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5813 $do_fix = 0;
5814 }
5815 } else {
5816 $do_fix = 0;
5817 }
5818 if ($do_fix) {
5819 fix_delete_line($fixlinenr - 1, $prevrawline);
5820 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5821 }
5822 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005823 }
5824 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005825
Joe Perchesebfdc402014-08-06 16:10:27 -07005826# check for unnecessary "Out of Memory" messages
5827 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5828 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5829 (defined $1 || defined $3) &&
5830 $linenr > 3) {
5831 my $testval = $2;
5832 my $testline = $lines[$linenr - 3];
5833
5834 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5835# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5836
Joe Perchese29a70f2019-03-07 16:28:35 -08005837 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5838 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005839 WARN("OOM_MESSAGE",
5840 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5841 }
5842 }
5843
Joe Perchesf78d98f2014-10-13 15:52:01 -07005844# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005845 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005846 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5847 my $level = $1;
5848 if (WARN("UNNECESSARY_KERN_LEVEL",
5849 "Possible unnecessary $level\n" . $herecurr) &&
5850 $fix) {
5851 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5852 }
5853 }
5854
Joe Perches45c55e92017-02-24 15:01:31 -08005855# check for logging continuations
5856 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5857 WARN("LOGGING_CONTINUATION",
5858 "Avoid logging continuation uses where feasible\n" . $herecurr);
5859 }
5860
Joe Perchesabb08a52014-12-10 15:51:46 -08005861# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005862 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005863 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5864 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5865 WARN("MASK_THEN_SHIFT",
5866 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5867 }
5868
Joe Perchesb75ac612014-12-10 15:52:02 -08005869# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005870 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005871 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5872 my $val = $1;
5873 my $equal = "!";
5874 $equal = "" if ($4 eq "!=");
5875 if (CHK("COMPARISON_TO_NULL",
5876 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5877 $fix) {
5878 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5879 }
5880 }
5881 }
5882
Joe Perches8716de32013-09-11 14:24:05 -07005883# check for bad placement of section $InitAttribute (e.g.: __initdata)
5884 if ($line =~ /(\b$InitAttribute\b)/) {
5885 my $attr = $1;
5886 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5887 my $ptr = $1;
5888 my $var = $2;
5889 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5890 ERROR("MISPLACED_INIT",
5891 "$attr should be placed after $var\n" . $herecurr)) ||
5892 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5893 WARN("MISPLACED_INIT",
5894 "$attr should be placed after $var\n" . $herecurr))) &&
5895 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005896 $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 -07005897 }
5898 }
5899 }
5900
Joe Perchese970b8842013-11-12 15:10:10 -08005901# check for $InitAttributeData (ie: __initdata) with const
5902 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5903 my $attr = $1;
5904 $attr =~ /($InitAttributePrefix)(.*)/;
5905 my $attr_prefix = $1;
5906 my $attr_type = $2;
5907 if (ERROR("INIT_ATTRIBUTE",
5908 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5909 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005910 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005911 s/$InitAttributeData/${attr_prefix}initconst/;
5912 }
5913 }
5914
5915# check for $InitAttributeConst (ie: __initconst) without const
5916 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5917 my $attr = $1;
5918 if (ERROR("INIT_ATTRIBUTE",
5919 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5920 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005921 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005922 /(^\+\s*(?:static\s+))/;
5923 $lead = rtrim($1);
5924 $lead = "$lead " if ($lead !~ /^\+$/);
5925 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005926 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005927 }
5928 }
5929
Joe Perchesc17893c2015-04-16 12:44:42 -07005930# check for __read_mostly with const non-pointer (should just be const)
5931 if ($line =~ /\b__read_mostly\b/ &&
5932 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5933 if (ERROR("CONST_READ_MOSTLY",
5934 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5935 $fix) {
5936 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5937 }
5938 }
5939
Joe Perchesfbdb8132014-04-03 14:49:14 -07005940# don't use __constant_<foo> functions outside of include/uapi/
5941 if ($realfile !~ m@^include/uapi/@ &&
5942 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5943 my $constant_func = $1;
5944 my $func = $constant_func;
5945 $func =~ s/^__constant_//;
5946 if (WARN("CONSTANT_CONVERSION",
5947 "$constant_func should be $func\n" . $herecurr) &&
5948 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005949 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005950 }
5951 }
5952
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005953# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005954 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005955 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005956 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005957 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005958 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005959 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07005960 }
5961 if ($delay > 2000) {
5962 WARN("LONG_UDELAY",
5963 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005964 }
5965 }
5966
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005967# warn about unexpectedly long msleep's
5968 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5969 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005970 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005971 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005972 }
5973 }
5974
Joe Perches36ec1932013-07-03 15:05:25 -07005975# check for comparisons of jiffies
5976 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5977 WARN("JIFFIES_COMPARISON",
5978 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5979 }
5980
Joe Perches9d7a34a2013-07-03 15:05:26 -07005981# check for comparisons of get_jiffies_64()
5982 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5983 WARN("JIFFIES_COMPARISON",
5984 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5985 }
5986
Andy Whitcroft00df3442007-06-08 13:47:06 -07005987# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005988# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005989# print "#ifdef in C files should be avoided\n";
5990# print "$herecurr";
5991# $clean = 0;
5992# }
5993
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005994# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005995 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005996 if (ERROR("SPACING",
5997 "exactly one space required after that #$1\n" . $herecurr) &&
5998 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005999 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07006000 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6001 }
6002
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07006003 }
6004
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006005# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006006 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6007 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006008 my $which = $1;
6009 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006010 CHK("UNCOMMENTED_DEFINITION",
6011 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006012 }
6013 }
6014# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006015
6016 my $barriers = qr{
6017 mb|
6018 rmb|
Will Deaconad83ec62019-11-07 14:49:00 +00006019 wmb
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006020 }x;
6021 my $barrier_stems = qr{
6022 mb__before_atomic|
6023 mb__after_atomic|
6024 store_release|
6025 load_acquire|
6026 store_mb|
6027 (?:$barriers)
6028 }x;
6029 my $all_barriers = qr{
6030 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02006031 smp_(?:$barrier_stems)|
6032 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02006033 }x;
6034
6035 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006036 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08006037 WARN("MEMORY_BARRIER",
6038 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006039 }
6040 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006041
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02006042 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6043
6044 if ($realfile !~ m@^include/asm-generic/@ &&
6045 $realfile !~ m@/barrier\.h$@ &&
6046 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6047 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6048 WARN("MEMORY_BARRIER",
6049 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6050 }
6051
Joe Perchescb426e92015-06-25 15:02:46 -07006052# check for waitqueue_active without a comment.
6053 if ($line =~ /\bwaitqueue_active\s*\(/) {
6054 if (!ctx_has_comment($first_line, $linenr)) {
6055 WARN("WAITQUEUE_ACTIVE",
6056 "waitqueue_active without comment\n" . $herecurr);
6057 }
6058 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07006059
Marco Elver5099a722020-04-01 12:17:14 +02006060# check for data_race without a comment.
6061 if ($line =~ /\bdata_race\s*\(/) {
6062 if (!ctx_has_comment($first_line, $linenr)) {
6063 WARN("DATA_RACE",
6064 "data_race without comment\n" . $herecurr);
6065 }
6066 }
6067
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07006068# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006069 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006070 CHK("ARCH_DEFINES",
6071 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006072 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006073
Joe Perches596ed452017-07-12 14:37:02 -07006074# check that the storage class is not after a type
6075 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006076 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07006077 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6078 }
6079# Check that the storage class is at the beginning of a declaration
6080 if ($line =~ /\b$Storage\b/ &&
6081 $line !~ /^.\s*$Storage/ &&
6082 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6083 $1 !~ /[\,\)]\s*$/) {
6084 WARN("STORAGE_CLASS",
6085 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07006086 }
6087
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006088# check the location of the inline attribute, that it is between
6089# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006090 if ($line =~ /\b$Type\s+$Inline\b/ ||
6091 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006092 ERROR("INLINE_LOCATION",
6093 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006094 }
6095
Andy Whitcroft8905a672007-11-28 16:21:06 -08006096# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08006097 if ($realfile !~ m@\binclude/uapi/@ &&
6098 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006099 if (WARN("INLINE",
6100 "plain inline is preferred over $1\n" . $herecurr) &&
6101 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006102 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006103
6104 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006105 }
6106
Joe Perches3d130fd2011-01-12 17:00:00 -08006107# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08006108 if ($realfile !~ m@\binclude/uapi/@ &&
6109 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006110 WARN("PREFER_PACKED",
6111 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08006112 }
6113
Joe Perches39b7e282011-07-25 17:13:24 -07006114# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08006115 if ($realfile !~ m@\binclude/uapi/@ &&
6116 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006117 WARN("PREFER_ALIGNED",
6118 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07006119 }
6120
Joe Perches462811d2019-09-25 16:46:44 -07006121# Check for __attribute__ section, prefer __section
6122 if ($realfile !~ m@\binclude/uapi/@ &&
6123 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
6124 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6125 my $new = substr($old, 1, -1);
6126 if (WARN("PREFER_SECTION",
6127 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6128 $fix) {
6129 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6130 }
6131 }
6132
Joe Perches5f14d3b2012-01-10 15:09:52 -08006133# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08006134 if ($realfile !~ m@\binclude/uapi/@ &&
6135 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006136 if (WARN("PREFER_PRINTF",
6137 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6138 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006139 $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 -07006140
6141 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08006142 }
6143
Joe Perches6061d942012-03-23 15:02:16 -07006144# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08006145 if ($realfile !~ m@\binclude/uapi/@ &&
6146 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006147 if (WARN("PREFER_SCANF",
6148 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6149 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006150 $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 -07006151 }
Joe Perches6061d942012-03-23 15:02:16 -07006152 }
6153
Joe Perches619a9082014-12-10 15:51:35 -08006154# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006155 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006156 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6157 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6158 $line =~ /\b__weak\b/)) {
6159 ERROR("WEAK_DECLARATION",
6160 "Using weak declarations can have unintended link defects\n" . $herecurr);
6161 }
6162
Tomas Winklerfd39f902016-12-12 16:46:34 -08006163# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006164 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006165 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006166 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6167 my $type = $1;
6168 if ($type =~ /\b($typeC99Typedefs)\b/) {
6169 $type = $1;
6170 my $kernel_type = 'u';
6171 $kernel_type = 's' if ($type =~ /^_*[si]/);
6172 $type =~ /(\d+)/;
6173 $kernel_type .= $1;
6174 if (CHK("PREFER_KERNEL_TYPES",
6175 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6176 $fix) {
6177 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6178 }
6179 }
6180 }
6181
Joe Perches938224b2016-01-20 14:59:15 -08006182# check for cast of C90 native int or longer types constants
6183 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6184 my $cast = $1;
6185 my $const = $2;
6186 if (WARN("TYPECAST_INT_CONSTANT",
6187 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6188 $fix) {
6189 my $suffix = "";
6190 my $newconst = $const;
6191 $newconst =~ s/${Int_type}$//;
6192 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6193 if ($cast =~ /\blong\s+long\b/) {
6194 $suffix .= 'LL';
6195 } elsif ($cast =~ /\blong\b/) {
6196 $suffix .= 'L';
6197 }
6198 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6199 }
6200 }
6201
Joe Perches8f53a9b2010-03-05 13:43:48 -08006202# check for sizeof(&)
6203 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006204 WARN("SIZEOF_ADDRESS",
6205 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006206 }
6207
Joe Perches66c80b62012-07-30 14:41:22 -07006208# check for sizeof without parenthesis
6209 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006210 if (WARN("SIZEOF_PARENTHESIS",
6211 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6212 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006213 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006214 }
Joe Perches66c80b62012-07-30 14:41:22 -07006215 }
6216
Joe Perches88982fe2012-12-17 16:02:00 -08006217# check for struct spinlock declarations
6218 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6219 WARN("USE_SPINLOCK_T",
6220 "struct spinlock should be spinlock_t\n" . $herecurr);
6221 }
6222
Joe Perchesa6962d72013-04-29 16:18:13 -07006223# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006224 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006225 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006226 $fmt =~ s/%%//g;
6227 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006228 if (WARN("PREFER_SEQ_PUTS",
6229 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6230 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006231 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006232 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006233 }
6234 }
6235
Joe Perches478b1792018-04-10 16:33:34 -07006236# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006237 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006238 defined $stat &&
6239 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6240 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006241 my $stat_real;
6242
Joe Perches0b523762017-05-08 15:55:36 -07006243 my $lc = $stat =~ tr@\n@@;
6244 $lc = $lc + $linenr;
6245 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006246 my $specifier;
6247 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006248 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006249 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006250 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6251 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006252
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006253 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006254 $specifier = $1;
6255 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006256 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006257 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006258 ($extension eq "f" &&
6259 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006260 $bad_specifier = $specifier;
6261 last;
6262 }
6263 if ($extension eq "x" && !defined($stat_real)) {
6264 if (!defined($stat_real)) {
6265 $stat_real = get_stat_real($linenr, $lc);
6266 }
6267 WARN("VSPRINTF_SPECIFIER_PX",
6268 "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");
6269 }
6270 }
6271 if ($bad_specifier ne "") {
6272 my $stat_real = get_stat_real($linenr, $lc);
6273 my $ext_type = "Invalid";
6274 my $use = "";
6275 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006276 $use = " - use %pS instead";
6277 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6278 }
6279
6280 WARN("VSPRINTF_POINTER_EXTENSION",
6281 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6282 }
Joe Perches0b523762017-05-08 15:55:36 -07006283 }
6284 }
6285
Andy Whitcroft554e1652012-01-10 15:09:57 -08006286# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006287 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006288 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006289 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006290
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006291 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006292 my $ms_val = $7;
6293 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006294
Andy Whitcroft554e1652012-01-10 15:09:57 -08006295 if ($ms_size =~ /^(0x|)0$/i) {
6296 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006297 "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 -08006298 } elsif ($ms_size =~ /^(0x|)1$/i) {
6299 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006300 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6301 }
6302 }
6303
Joe Perches98a9bba2014-01-23 15:54:52 -08006304# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006305# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006306# defined $stat &&
6307# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6308# if (WARN("PREFER_ETHER_ADDR_COPY",
6309# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6310# $fix) {
6311# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6312# }
6313# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006314
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006315# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006316# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006317# defined $stat &&
6318# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6319# WARN("PREFER_ETHER_ADDR_EQUAL",
6320# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6321# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006322
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006323# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6324# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006325# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006326# defined $stat &&
6327# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6328#
6329# my $ms_val = $7;
6330#
6331# if ($ms_val =~ /^(?:0x|)0+$/i) {
6332# if (WARN("PREFER_ETH_ZERO_ADDR",
6333# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6334# $fix) {
6335# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6336# }
6337# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6338# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6339# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6340# $fix) {
6341# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6342# }
6343# }
6344# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006345
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006346# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006347 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006348 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006349 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006350 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006351 my $call = $1;
6352 my $cast1 = deparenthesize($2);
6353 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006354 my $cast2 = deparenthesize($7);
6355 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006356 my $cast;
6357
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006358 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006359 $cast = "$cast1 or $cast2";
6360 } elsif ($cast1 ne "") {
6361 $cast = $cast1;
6362 } else {
6363 $cast = $cast2;
6364 }
6365 WARN("MINMAX",
6366 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006367 }
6368 }
6369
Joe Perches4a273192012-07-30 14:41:20 -07006370# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006371 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006372 defined $stat &&
6373 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6374 my $min = $1;
6375 my $max = $7;
6376 if ($min eq $max) {
6377 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006378 "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 -07006379 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6380 $min > $max) {
6381 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006382 "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 -07006383 }
6384 }
6385
Joe Perches823b7942013-11-12 15:10:15 -08006386# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006387 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006388 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006389 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006390 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6391 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6392 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6393 my $lc = $stat =~ tr@\n@@;
6394 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006395 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006396 WARN("NAKED_SSCANF",
6397 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6398 }
6399
Joe Perchesafc819a2014-06-04 16:12:08 -07006400# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006401 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006402 defined $stat &&
6403 $line =~ /\bsscanf\b/) {
6404 my $lc = $stat =~ tr@\n@@;
6405 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006406 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006407 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6408 my $format = $6;
6409 my $count = $format =~ tr@%@%@;
6410 if ($count == 1 &&
6411 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6412 WARN("SSCANF_TO_KSTRTO",
6413 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6414 }
6415 }
6416 }
6417
Joe Perches70dc8a42013-09-11 14:23:58 -07006418# check for new externs in .h files.
6419 if ($realfile =~ /\.h$/ &&
6420 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006421 if (CHK("AVOID_EXTERNS",
6422 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006423 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006424 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006425 }
6426 }
6427
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006428# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006429 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006430 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006431 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006432 my $function_name = $1;
6433 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006434
6435 my $s = $stat;
6436 if (defined $cond) {
6437 substr($s, 0, length($cond), '');
6438 }
Kees Cookd8b44b52020-06-03 14:18:09 -07006439 if ($s =~ /^\s*;/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006440 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006441 WARN("AVOID_EXTERNS",
6442 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006443 }
6444
6445 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006446 WARN("FUNCTION_ARGUMENTS",
6447 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006448 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006449
6450 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6451 $stat =~ /^.\s*extern\s+/)
6452 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006453 WARN("AVOID_EXTERNS",
6454 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006455 }
6456
Joe Perchesa0ad7592017-07-10 15:52:19 -07006457# check for function declarations that have arguments without identifier names
6458 if (defined $stat &&
Kees Cookd8b44b52020-06-03 14:18:09 -07006459 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6460 $1 ne "void") {
6461 my $args = trim($1);
Joe Perchesca0d8922016-10-11 13:52:16 -07006462 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6463 my $arg = trim($1);
Kees Cookd8b44b52020-06-03 14:18:09 -07006464 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006465 WARN("FUNCTION_ARGUMENTS",
6466 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6467 }
6468 }
6469 }
6470
Joe Perchesa0ad7592017-07-10 15:52:19 -07006471# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006472 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006473 defined $stat &&
6474 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6475 $context_function = $1;
6476
6477# check for multiline function definition with misplaced open brace
6478 my $ok = 0;
6479 my $cnt = statement_rawlines($stat);
6480 my $herectx = $here . "\n";
6481 for (my $n = 0; $n < $cnt; $n++) {
6482 my $rl = raw_line($linenr, $n);
6483 $herectx .= $rl . "\n";
6484 $ok = 1 if ($rl =~ /^[ \+]\{/);
6485 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6486 last if $rl =~ /^[ \+].*\{/;
6487 }
6488 if (!$ok) {
6489 ERROR("OPEN_BRACE",
6490 "open brace '{' following function definitions go on the next line\n" . $herectx);
6491 }
6492 }
6493
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006494# checks for new __setup's
6495 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6496 my $name = $1;
6497
6498 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006499 CHK("UNDOCUMENTED_SETUP",
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07006500 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006501 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006502 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006503
Joe Perchese29a70f2019-03-07 16:28:35 -08006504# check for pointless casting of alloc functions
6505 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006506 WARN("UNNECESSARY_CASTS",
6507 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006508 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006509
Joe Perchesa640d252013-07-03 15:05:21 -07006510# alloc style
6511# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006512 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006513 $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 -07006514 CHK("ALLOC_SIZEOF_STRUCT",
6515 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6516 }
6517
Joe Perches60a55362014-06-04 16:12:07 -07006518# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006519 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006520 defined $stat &&
6521 $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 -07006522 my $oldfunc = $3;
6523 my $a1 = $4;
6524 my $a2 = $10;
6525 my $newfunc = "kmalloc_array";
6526 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006527 my $r1 = $a1;
6528 my $r2 = $a2;
6529 if ($a1 =~ /^sizeof\s*\S/) {
6530 $r1 = $a2;
6531 $r2 = $a1;
6532 }
6533 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6534 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006535 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006536 my $herectx = get_stat_here($linenr, $cnt, $here);
6537
Joe Perches60a55362014-06-04 16:12:07 -07006538 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006539 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6540 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006541 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006542 $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 -07006543 }
6544 }
6545 }
6546
Joe Perches972fdea2013-04-29 16:18:12 -07006547# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006548 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006549 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6550 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006551 WARN("KREALLOC_ARG_REUSE",
6552 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6553 }
6554
Joe Perches5ce59ae2013-02-21 16:44:18 -08006555# check for alloc argument mismatch
6556 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6557 WARN("ALLOC_ARRAY_ARGS",
6558 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6559 }
6560
Joe Perchescaf2a542011-01-12 16:59:56 -08006561# check for multiple semicolons
6562 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006563 if (WARN("ONE_SEMICOLON",
6564 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6565 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006566 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006567 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006568 }
6569
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006570# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6571 if ($realfile !~ m@^include/uapi/@ &&
6572 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006573 my $ull = "";
6574 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6575 if (CHK("BIT_MACRO",
6576 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6577 $fix) {
6578 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6579 }
6580 }
6581
Joe Perches50161262020-08-11 18:35:07 -07006582# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006583 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
Joe Perches50161262020-08-11 18:35:07 -07006584 WARN("IS_ENABLED_CONFIG",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006585 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
Joe Perches50161262020-08-11 18:35:07 -07006586 }
6587
Joe Perches2d632742016-05-20 17:04:00 -07006588# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006589 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 -07006590 my $config = $1;
6591 if (WARN("PREFER_IS_ENABLED",
Jerome Forissier3e89ad82020-10-15 20:11:49 -07006592 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
Joe Perches2d632742016-05-20 17:04:00 -07006593 $fix) {
6594 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6595 }
6596 }
6597
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006598# check for /* fallthrough */ like comment, prefer fallthrough;
6599 my @fallthroughs = (
6600 'fallthrough',
6601 '@fallthrough@',
6602 'lint -fallthrough[ \t]*',
6603 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6604 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6605 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6606 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6607 );
6608 if ($raw_comment ne '') {
6609 foreach my $ft (@fallthroughs) {
6610 if ($raw_comment =~ /$ft/) {
6611 my $msg_level = \&WARN;
6612 $msg_level = \&CHK if ($file);
6613 &{$msg_level}("PREFER_FALLTHROUGH",
6614 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6615 last;
6616 }
6617 }
6618 }
6619
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006620# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006621 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006622 defined $stat &&
6623 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006624 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006625 my $herectx = get_stat_here($linenr, $cnt, $here);
6626
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006627 WARN("DEFAULT_NO_BREAK",
6628 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006629 }
6630
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006631# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006632 if ($line =~ /\b__FUNCTION__\b/) {
6633 if (WARN("USE_FUNC",
6634 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6635 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006636 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006637 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006638 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006639
Joe Perches62ec8182015-02-13 14:38:18 -08006640# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6641 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6642 ERROR("DATE_TIME",
6643 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6644 }
6645
Joe Perches2c924882012-03-23 15:02:20 -07006646# check for use of yield()
6647 if ($line =~ /\byield\s*\(\s*\)/) {
6648 WARN("YIELD",
6649 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6650 }
6651
Joe Perches179f8f42013-07-03 15:05:30 -07006652# check for comparisons against true and false
6653 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6654 my $lead = $1;
6655 my $arg = $2;
6656 my $test = $3;
6657 my $otype = $4;
6658 my $trail = $5;
6659 my $op = "!";
6660
6661 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6662
6663 my $type = lc($otype);
6664 if ($type =~ /^(?:true|false)$/) {
6665 if (("$test" eq "==" && "$type" eq "true") ||
6666 ("$test" eq "!=" && "$type" eq "false")) {
6667 $op = "";
6668 }
6669
6670 CHK("BOOL_COMPARISON",
6671 "Using comparison to $otype is error prone\n" . $herecurr);
6672
6673## maybe suggesting a correct construct would better
6674## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6675
6676 }
6677 }
6678
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006679# check for semaphores initialized locked
6680 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006681 WARN("CONSIDER_COMPLETION",
6682 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006683 }
Joe Perches6712d852012-03-23 15:02:20 -07006684
Joe Perches67d0a072011-10-31 17:13:10 -07006685# recommend kstrto* over simple_strto* and strict_strto*
6686 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006687 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006688 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006689 }
Joe Perches6712d852012-03-23 15:02:20 -07006690
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006691# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006692 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006693 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006694 "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 -07006695 }
Joe Perches6712d852012-03-23 15:02:20 -07006696
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006697# check for spin_is_locked(), suggest lockdep instead
6698 if ($line =~ /\bspin_is_locked\(/) {
6699 WARN("USE_LOCKDEP",
6700 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6701 }
6702
Joe Perches9189c7e2018-09-07 15:26:18 -07006703# check for deprecated apis
6704 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6705 my $deprecated_api = $1;
6706 my $new_api = $deprecated_apis{$deprecated_api};
6707 WARN("DEPRECATED_API",
6708 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6709 }
6710
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006711# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006712# and avoid what seem like struct definitions 'struct foo {'
Quentin Monnetced69da12020-08-11 18:35:13 -07006713 if (defined($const_structs) &&
6714 $line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006715 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006716 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006717 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006718 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006719
6720# use of NR_CPUS is usually wrong
6721# ignore definitions of NR_CPUS and usage to define arrays as likely right
6722 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006723 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6724 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006725 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6726 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6727 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006728 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006729 WARN("NR_CPUS",
6730 "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 -07006731 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006732
Joe Perches52ea8502013-11-12 15:10:09 -08006733# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6734 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6735 ERROR("DEFINE_ARCH_HAS",
6736 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6737 }
6738
Joe Perchesacd93622015-02-13 14:38:38 -08006739# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006740 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006741 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6742 WARN("LIKELY_MISUSE",
6743 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6744 }
6745
Denis Efremovde3f1862019-09-25 16:49:25 -07006746# nested likely/unlikely calls
6747 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6748 WARN("LIKELY_MISUSE",
6749 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6750 }
6751
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006752# whine mightly about in_atomic
6753 if ($line =~ /\bin_atomic\s*\(/) {
6754 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006755 ERROR("IN_ATOMIC",
6756 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006757 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006758 WARN("IN_ATOMIC",
6759 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006760 }
6761 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006762
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006763# check for mutex_trylock_recursive usage
6764 if ($line =~ /mutex_trylock_recursive/) {
6765 ERROR("LOCKING",
6766 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6767 }
6768
Peter Zijlstra1704f472010-03-19 01:37:42 +01006769# check for lockdep_set_novalidate_class
6770 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6771 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6772 if ($realfile !~ m@^kernel/lockdep@ &&
6773 $realfile !~ m@^include/linux/lockdep@ &&
6774 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006775 ERROR("LOCKDEP",
6776 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006777 }
6778 }
Dave Jones88f88312011-01-12 16:59:59 -08006779
Joe Perchesb392c642015-04-16 12:44:16 -07006780 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6781 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006782 WARN("EXPORTED_WORLD_WRITABLE",
6783 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006784 }
Joe Perches24358802014-04-03 14:49:13 -07006785
Joe Perches00180462018-02-06 15:38:55 -08006786# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6787# and whether or not function naming is typical and if
6788# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006789 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006790 defined $stat &&
6791 $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*\)/) {
6792 my $var = $1;
6793 my $perms = $2;
6794 my $show = $3;
6795 my $store = $4;
6796 my $octal_perms = perms_to_octal($perms);
6797 if ($show =~ /^${var}_show$/ &&
6798 $store =~ /^${var}_store$/ &&
6799 $octal_perms eq "0644") {
6800 if (WARN("DEVICE_ATTR_RW",
6801 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6802 $fix) {
6803 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6804 }
6805 } elsif ($show =~ /^${var}_show$/ &&
6806 $store =~ /^NULL$/ &&
6807 $octal_perms eq "0444") {
6808 if (WARN("DEVICE_ATTR_RO",
6809 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6810 $fix) {
6811 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6812 }
6813 } elsif ($show =~ /^NULL$/ &&
6814 $store =~ /^${var}_store$/ &&
6815 $octal_perms eq "0200") {
6816 if (WARN("DEVICE_ATTR_WO",
6817 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6818 $fix) {
6819 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6820 }
6821 } elsif ($octal_perms eq "0644" ||
6822 $octal_perms eq "0444" ||
6823 $octal_perms eq "0200") {
6824 my $newshow = "$show";
6825 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6826 my $newstore = $store;
6827 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6828 my $rename = "";
6829 if ($show ne $newshow) {
6830 $rename .= " '$show' to '$newshow'";
6831 }
6832 if ($store ne $newstore) {
6833 $rename .= " '$store' to '$newstore'";
6834 }
6835 WARN("DEVICE_ATTR_FUNCTIONS",
6836 "Consider renaming function(s)$rename\n" . $herecurr);
6837 } else {
6838 WARN("DEVICE_ATTR_PERMS",
6839 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6840 }
6841 }
6842
Joe Perches515a2352014-04-03 14:49:24 -07006843# Mode permission misuses where it seems decimal should be octal
6844# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006845# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6846# specific definition of not visible in sysfs.
6847# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6848# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006849 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006850 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006851 $line =~ /$mode_perms_search/) {
6852 foreach my $entry (@mode_permission_funcs) {
6853 my $func = $entry->[0];
6854 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006855
Joe Perches459cf0a2016-10-11 13:52:19 -07006856 my $lc = $stat =~ tr@\n@@;
6857 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006858 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006859
Joe Perches515a2352014-04-03 14:49:24 -07006860 my $skip_args = "";
6861 if ($arg_pos > 1) {
6862 $arg_pos--;
6863 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6864 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006865 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006866 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006867 my $val = $1;
6868 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006869 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6870 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6871 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006872 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006873 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006874 }
6875 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006876 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006877 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006878 }
Joe Perches24358802014-04-03 14:49:13 -07006879 }
6880 }
6881 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006882
Joe Perches459cf0a2016-10-11 13:52:19 -07006883# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006884 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006885 my $oval = $1;
6886 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006887 if (WARN("SYMBOLIC_PERMS",
6888 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6889 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006890 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006891 }
6892 }
6893
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006894# validate content of MODULE_LICENSE against list from include/linux/module.h
6895 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6896 my $extracted_string = get_quoted_string($line, $rawline);
6897 my $valid_licenses = qr{
6898 GPL|
6899 GPL\ v2|
6900 GPL\ and\ additional\ rights|
6901 Dual\ BSD/GPL|
6902 Dual\ MIT/GPL|
6903 Dual\ MPL/GPL|
6904 Proprietary
6905 }x;
6906 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6907 WARN("MODULE_LICENSE",
6908 "unknown module license " . $extracted_string . "\n" . $herecurr);
6909 }
6910 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07006911
6912# check for sysctl duplicate constants
6913 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6914 WARN("DUPLICATED_SYSCTL_CONST",
6915 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6916 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006917 }
6918
6919 # If we have no input at all, then there is nothing to report on
6920 # so just keep quiet.
6921 if ($#rawlines == -1) {
6922 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006923 }
6924
Andy Whitcroft8905a672007-11-28 16:21:06 -08006925 # In mailback mode only produce a report in the negative, for
6926 # things that appear to be patches.
6927 if ($mailback && ($clean == 1 || !$is_patch)) {
6928 exit(0);
6929 }
6930
6931 # This is not a patch, and we are are in 'no-patch' mode so
6932 # just keep quiet.
6933 if (!$chk_patch && !$is_patch) {
6934 exit(0);
6935 }
6936
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006937 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006938 ERROR("NOT_UNIFIED_DIFF",
6939 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006940 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006941 if ($is_patch && $has_commit_log && $chk_signoff) {
6942 if ($signoff == 0) {
6943 ERROR("MISSING_SIGN_OFF",
6944 "Missing Signed-off-by: line(s)\n");
6945 } elsif (!$authorsignoff) {
6946 WARN("NO_AUTHOR_SIGN_OFF",
6947 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6948 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006949 }
6950
Andy Whitcroft8905a672007-11-28 16:21:06 -08006951 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006952 if ($summary && !($clean == 1 && $quiet == 1)) {
6953 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006954 print "total: $cnt_error errors, $cnt_warn warnings, " .
6955 (($check)? "$cnt_chk checks, " : "") .
6956 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006957 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006958
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006959 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006960 # If there were any defects found and not already fixing them
6961 if (!$clean and !$fix) {
6962 print << "EOM"
6963
6964NOTE: For some of the reported defects, checkpatch may be able to
6965 mechanically convert to the typical style using --fix or --fix-inplace.
6966EOM
6967 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006968 # If there were whitespace errors which cleanpatch can fix
6969 # then suggest that.
6970 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006971 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006972 print << "EOM"
6973
6974NOTE: Whitespace errors detected.
6975 You may wish to use scripts/cleanpatch or scripts/cleanfile
6976EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006977 }
6978 }
6979
Joe Perchesd752fcc2014-08-06 16:11:05 -07006980 if ($clean == 0 && $fix &&
6981 ("@rawlines" ne "@fixed" ||
6982 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006983 my $newfile = $filename;
6984 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006985 my $linecount = 0;
6986 my $f;
6987
Joe Perchesd752fcc2014-08-06 16:11:05 -07006988 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6989
Joe Perches3705ce52013-07-03 15:05:31 -07006990 open($f, '>', $newfile)
6991 or die "$P: Can't open $newfile for write\n";
6992 foreach my $fixed_line (@fixed) {
6993 $linecount++;
6994 if ($file) {
6995 if ($linecount > 3) {
6996 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006997 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006998 }
6999 } else {
7000 print $f $fixed_line . "\n";
7001 }
7002 }
7003 close($f);
7004
7005 if (!$quiet) {
7006 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07007007
Joe Perches3705ce52013-07-03 15:05:31 -07007008Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
7009
7010Do _NOT_ trust the results written to this file.
7011Do _NOT_ submit these changes without inspecting them for correctness.
7012
7013This EXPERIMENTAL file is simply a convenience to help rewrite patches.
7014No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07007015EOM
7016 }
7017 }
7018
Joe Perchesd8469f12015-06-25 15:03:00 -07007019 if ($quiet == 0) {
7020 print "\n";
7021 if ($clean == 1) {
7022 print "$vname has no obvious style problems and is ready for submission.\n";
7023 } else {
7024 print "$vname has style problems, please review.\n";
7025 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007026 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07007027 return $clean;
7028}