blob: 599b8c4933a783534d005689431342329309fe6f [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Joe Perches882ea1d2018-06-07 17:04:33 -07002# SPDX-License-Identifier: GPL-2.0
3#
Dave Jonesdbf004d2010-01-12 16:59:52 -05004# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07005# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08006# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07007# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Joe Perches882ea1d2018-06-07 17:04:33 -07008# (c) 2010-2018 Joe Perches <joe@perches.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07009
10use strict;
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020011use warnings;
Joe Perchesc707a812013-07-08 16:00:43 -070012use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080013use File::Basename;
14use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070015use Term::ANSIColor qw(:constants);
Geert Uytterhoevencd261492018-08-21 21:57:40 -070016use Encode qw(decode encode);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
18my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080019my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070020
Joe Perches000d1cc12011-07-25 17:13:25 -070021my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070022
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
26my $tree = 1;
27my $chk_signoff = 1;
28my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070029my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080031my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070032my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070034my $git = 0;
Joe Perches0dea9f1e2016-05-20 17:04:19 -070035my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070037my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080038my $summary = 1;
39my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080040my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070041my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070042my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070043my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080044my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070045my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080046my %debug;
Joe Perches34456862013-07-03 15:05:34 -070047my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070048my %use_type = ();
49my @use = ();
50my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070051my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070052my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070053my $configuration_file = ".checkpatch.conf";
Joe Perchesbdc48fa2020-05-29 16:12:21 -070054my $max_line_length = 100;
Dave Hansend62a2012013-09-11 14:23:56 -070055my $ignore_perl_version = 0;
56my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070057my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070058my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070059my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070060my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070061my $conststructsfile = "$D/const_structs.checkpatch";
Jerome Forissier75ad8c52017-05-08 15:56:00 -070062my $typedefsfile = "";
John Brooks737c0762017-07-10 15:52:24 -070063my $color = "auto";
Vadim Bendebury98005e82019-03-07 16:28:38 -080064my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
Joe Perchesdbbf8692019-09-25 16:46:52 -070065# git output parsing needs US English output, so first set backtick child process LANGUAGE
66my $git_command ='export LANGUAGE=en_US.UTF-8; git';
Antonio Borneo713a09d2020-04-06 20:11:07 -070067my $tabsize = 8;
Hannes Eder77f5b102009-09-21 17:04:37 -070068
69sub help {
70 my ($exitcode) = @_;
71
72 print << "EOM";
73Usage: $P [OPTION]... [FILE]...
74Version: $V
75
76Options:
77 -q, --quiet quiet
78 --no-tree run without a kernel tree
79 --no-signoff do not check for 'Signed-off-by' line
80 --patch treat FILE as patchfile (default)
81 --emacs emacs compile window format
82 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070083 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070084 -g, --git treat FILE as a single commit or git revision range
85 single git commit with:
86 <rev>
87 <rev>^
88 <rev>~n
89 multiple git commits with:
90 <rev1>..<rev2>
91 <rev1>...<rev2>
92 <rev>-<count>
93 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070094 -f, --file treat FILE as regular source file
95 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070096 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070097 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070098 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070099 --show-types show the specific message type in the output
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700100 --max-line-length=n set the maximum line length, (default $max_line_length)
101 if exceeded, warn on patches
102 requires --strict for use with --file
Vadim Bendebury56193272014-10-13 15:51:48 -0700103 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perchesbdc48fa2020-05-29 16:12:21 -0700104 --tab-size=n set the number of spaces for tab (default $tabsize)
Hannes Eder77f5b102009-09-21 17:04:37 -0700105 --root=PATH PATH to the kernel tree root
106 --no-summary suppress the per-file summary
107 --mailback only produce a report in case of warnings/errors
108 --summary-file include the filename in summary
109 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
110 'values', 'possible', 'type', and 'attr' (default
111 is all off)
112 --test-only=WORD report only warnings/errors containing WORD
113 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700114 --fix EXPERIMENTAL - may create horrible results
115 If correctable single-line errors exist, create
116 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
117 with potential errors corrected to the preferred
118 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800119 --fix-inplace EXPERIMENTAL - may create horrible results
120 Is the same as --fix, but overwrites the input
121 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700122 --ignore-perl-version override checking of perl version. expect
123 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700124 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700125 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700126 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700127 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700128 --color[=WHEN] Use colors 'always', 'never', or only when output
129 is a terminal ('auto'). Default is 'auto'.
Hannes Eder77f5b102009-09-21 17:04:37 -0700130 -h, --help, --version display this help and exit
131
132When FILE is - read standard input.
133EOM
134
135 exit($exitcode);
136}
137
Joe Perches3beb42e2016-05-20 17:04:14 -0700138sub uniq {
139 my %seen;
140 return grep { !$seen{$_}++ } @_;
141}
142
143sub list_types {
144 my ($exitcode) = @_;
145
146 my $count = 0;
147
148 local $/ = undef;
149
150 open(my $script, '<', abs_path($P)) or
151 die "$P: Can't read '$P' $!\n";
152
153 my $text = <$script>;
154 close($script);
155
156 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700157 # Also catch when type or level is passed through a variable
158 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700159 push (@types, $_);
160 }
161 @types = sort(uniq(@types));
162 print("#\tMessage type\n\n");
163 foreach my $type (@types) {
164 print(++$count . "\t" . $type . "\n");
165 }
166
167 exit($exitcode);
168}
169
Joe Perches000d1cc12011-07-25 17:13:25 -0700170my $conf = which_conf($configuration_file);
171if (-f $conf) {
172 my @conf_args;
173 open(my $conffile, '<', "$conf")
174 or warn "$P: Can't find a readable $configuration_file file $!\n";
175
176 while (<$conffile>) {
177 my $line = $_;
178
179 $line =~ s/\s*\n?$//g;
180 $line =~ s/^\s*//g;
181 $line =~ s/\s+/ /g;
182
183 next if ($line =~ m/^\s*#/);
184 next if ($line =~ m/^\s*$/);
185
186 my @words = split(" ", $line);
187 foreach my $word (@words) {
188 last if ($word =~ m/^#/);
189 push (@conf_args, $word);
190 }
191 }
192 close($conffile);
193 unshift(@ARGV, @conf_args) if @conf_args;
194}
195
John Brooks737c0762017-07-10 15:52:24 -0700196# Perl's Getopt::Long allows options to take optional arguments after a space.
197# Prevent --color by itself from consuming other arguments
198foreach (@ARGV) {
199 if ($_ eq "--color" || $_ eq "-color") {
200 $_ = "--color=$color";
201 }
202}
203
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700204GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700205 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700206 'tree!' => \$tree,
207 'signoff!' => \$chk_signoff,
208 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700209 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800210 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700211 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700212 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700213 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700214 'subjective!' => \$check,
215 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700216 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700217 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700218 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700219 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800220 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700221 'min-conf-desc-length=i' => \$min_conf_desc_length,
Antonio Borneo713a09d2020-04-06 20:11:07 -0700222 'tab-size=i' => \$tabsize,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700223 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800224 'summary!' => \$summary,
225 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800226 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700227 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800228 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700229 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800230 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700231 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700232 'codespell!' => \$codespell,
233 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700234 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700235 'color=s' => \$color,
236 'no-color' => \$color, #keep old behaviors of -nocolor
237 'nocolor' => \$color, #keep old behaviors of -nocolor
Hannes Eder77f5b102009-09-21 17:04:37 -0700238 'h|help' => \$help,
239 'version' => \$help
240) or help(1);
241
242help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700243
Joe Perches3beb42e2016-05-20 17:04:14 -0700244list_types(0) if ($list_types);
245
Joe Perches9624b8d2014-01-23 15:54:44 -0800246$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700247$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800248
Joe Perches32f30ca2020-06-04 16:50:40 -0700249die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
250
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700251my $exit = 0;
252
Joe Perches5b579802018-08-21 21:57:33 -0700253my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700254if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700255 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700256 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700257 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700258}
259
Allen Hubbe45107ff2016-08-02 14:04:47 -0700260#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700261if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700262 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700263}
264
John Brooks737c0762017-07-10 15:52:24 -0700265if ($color =~ /^[01]$/) {
266 $color = !$color;
267} elsif ($color =~ /^always$/i) {
268 $color = 1;
269} elsif ($color =~ /^never$/i) {
270 $color = 0;
271} elsif ($color =~ /^auto$/i) {
272 $color = (-t STDOUT);
273} else {
Joe Perches32f30ca2020-06-04 16:50:40 -0700274 die "$P: Invalid color mode: $color\n";
John Brooks737c0762017-07-10 15:52:24 -0700275}
276
Antonio Borneo713a09d2020-04-06 20:11:07 -0700277# skip TAB size 1 to avoid additional checks on $tabsize - 1
Joe Perches32f30ca2020-06-04 16:50:40 -0700278die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
Antonio Borneo713a09d2020-04-06 20:11:07 -0700279
Joe Perches91bfe482013-09-11 14:23:59 -0700280sub hash_save_array_words {
281 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700282
Joe Perches91bfe482013-09-11 14:23:59 -0700283 my @array = split(/,/, join(',', @$arrayRef));
284 foreach my $word (@array) {
285 $word =~ s/\s*\n?$//g;
286 $word =~ s/^\s*//g;
287 $word =~ s/\s+/ /g;
288 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700289
Joe Perches91bfe482013-09-11 14:23:59 -0700290 next if ($word =~ m/^\s*#/);
291 next if ($word =~ m/^\s*$/);
292
293 $hashRef->{$word}++;
294 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700295}
296
Joe Perches91bfe482013-09-11 14:23:59 -0700297sub hash_show_words {
298 my ($hashRef, $prefix) = @_;
299
Joe Perches3c816e42015-06-25 15:03:29 -0700300 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700301 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700302 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700303 print " $word";
304 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700305 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700306 }
307}
308
309hash_save_array_words(\%ignore_type, \@ignore);
310hash_save_array_words(\%use_type, \@use);
311
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800312my $dbg_values = 0;
313my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700314my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700315my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800316for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800317 ## no critic
318 eval "\${dbg_$key} = '$debug{$key}';";
319 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800320}
321
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700322my $rpt_cleaners = 0;
323
Andy Whitcroft8905a672007-11-28 16:21:06 -0800324if ($terse) {
325 $emacs = 1;
326 $quiet++;
327}
328
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700329if ($tree) {
330 if (defined $root) {
331 if (!top_of_kernel_tree($root)) {
332 die "$P: $root: --root does not point at a valid tree\n";
333 }
334 } else {
335 if (top_of_kernel_tree('.')) {
336 $root = '.';
337 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
338 top_of_kernel_tree($1)) {
339 $root = $1;
340 }
341 }
342
343 if (!defined $root) {
344 print "Must be run from the top-level dir. of a kernel tree\n";
345 exit(2);
346 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700347}
348
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700349my $emitted_corrupt = 0;
350
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700351our $Ident = qr{
352 [A-Za-z_][A-Za-z\d_]*
353 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
354 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700355our $Storage = qr{extern|static|asmlinkage};
356our $Sparse = qr{
357 __user|
358 __kernel|
359 __force|
360 __iomem|
361 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800362 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700363 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700364 __refconst|
365 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800366 __rcu|
367 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700368 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800369our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
370our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
371our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
372our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
373our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700374
Wolfram Sang52131292010-03-05 13:43:51 -0800375# Notes to $Attribute:
376# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700377our $Attribute = qr{
378 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700379 __percpu|
380 __nocast|
381 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200382 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700383 __packed__|
384 __packed2__|
385 __naked|
386 __maybe_unused|
387 __always_unused|
388 __noreturn|
389 __used|
390 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800391 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700392 __noclone|
393 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700394 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700395 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700396 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700397 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700398 ____cacheline_aligned|
399 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800400 ____cacheline_internodealigned_in_smp|
401 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700402 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700403our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700404our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700405our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
406our $Lval = qr{$Ident(?:$Member)*};
407
Joe Perches95e2c602013-07-03 15:05:20 -0700408our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
409our $Binary = qr{(?i)0b[01]+$Int_type?};
410our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
411our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700412our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800413our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800414our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
415our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
416our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800417our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700418our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800419our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700420our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700421our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700422our $Operators = qr{
423 <=|>=|==|!=|
424 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700425 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700426 }x;
427
Joe Perches91cb5192014-04-03 14:49:32 -0700428our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
429
Joe Perchesab7e23f2015-04-16 12:44:22 -0700430our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800431our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700432our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700433our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800434our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700435our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800436our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700437our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800438
Joe Perches15662b32011-10-31 17:13:12 -0700439our $NON_ASCII_UTF8 = qr{
440 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700441 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
442 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
443 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
444 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
445 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
446 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
447}x;
448
Joe Perches15662b32011-10-31 17:13:12 -0700449our $UTF8 = qr{
450 [\x09\x0A\x0D\x20-\x7E] # ASCII
451 | $NON_ASCII_UTF8
452}x;
453
Joe Perchese6176fa2015-06-25 15:02:49 -0700454our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800455our $typeOtherOSTypedefs = qr{(?x:
456 u_(?:char|short|int|long) | # bsd
457 u(?:nchar|short|int|long) # sysv
458)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700459our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700460 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700461 atomic_t
462)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700463our $typeTypedefs = qr{(?x:
464 $typeC99Typedefs\b|
465 $typeOtherOSTypedefs\b|
466 $typeKernelTypedefs\b
467)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700468
Joe Perches6d32f7a2015-11-06 16:31:37 -0800469our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
470
Joe Perches691e6692010-03-05 13:43:51 -0800471our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800472 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700473 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches87bd4992017-11-17 15:28:48 -0800474 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700475 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700476 panic|
Joe Perches06668722013-11-12 15:10:07 -0800477 MODULE_[A-Z_]+|
478 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800479)};
480
Joe Perchese29a70f2019-03-07 16:28:35 -0800481our $allocFunctions = qr{(?x:
482 (?:(?:devm_)?
483 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
484 kstrdup(?:_const)? |
485 kmemdup(?:_nul)?) |
Christophe JAILLET461e1562020-04-20 18:13:58 -0700486 (?:\w+)?alloc_skb(?:_ip_align)? |
Joe Perchese29a70f2019-03-07 16:28:35 -0800487 # dev_alloc_skb/netdev_alloc_skb, et al
488 dma_alloc_coherent
489)};
490
Joe Perches20112472011-07-25 17:13:23 -0700491our $signature_tags = qr{(?xi:
492 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800493 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700494 Acked-by:|
495 Tested-by:|
496 Reviewed-by:|
497 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700498 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700499 To:|
500 Cc:
501)};
502
Joe Perches18130872014-08-06 16:11:22 -0700503our @typeListMisordered = (
504 qr{char\s+(?:un)?signed},
505 qr{int\s+(?:(?:un)?signed\s+)?short\s},
506 qr{int\s+short(?:\s+(?:un)?signed)},
507 qr{short\s+int(?:\s+(?:un)?signed)},
508 qr{(?:un)?signed\s+int\s+short},
509 qr{short\s+(?:un)?signed},
510 qr{long\s+int\s+(?:un)?signed},
511 qr{int\s+long\s+(?:un)?signed},
512 qr{long\s+(?:un)?signed\s+int},
513 qr{int\s+(?:un)?signed\s+long},
514 qr{int\s+(?:un)?signed},
515 qr{int\s+long\s+long\s+(?:un)?signed},
516 qr{long\s+long\s+int\s+(?:un)?signed},
517 qr{long\s+long\s+(?:un)?signed\s+int},
518 qr{long\s+long\s+(?:un)?signed},
519 qr{long\s+(?:un)?signed},
520);
521
Andy Whitcroft8905a672007-11-28 16:21:06 -0800522our @typeList = (
523 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700524 qr{(?:(?:un)?signed\s+)?char},
525 qr{(?:(?:un)?signed\s+)?short\s+int},
526 qr{(?:(?:un)?signed\s+)?short},
527 qr{(?:(?:un)?signed\s+)?int},
528 qr{(?:(?:un)?signed\s+)?long\s+int},
529 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
530 qr{(?:(?:un)?signed\s+)?long\s+long},
531 qr{(?:(?:un)?signed\s+)?long},
532 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800533 qr{float},
534 qr{double},
535 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800536 qr{struct\s+$Ident},
537 qr{union\s+$Ident},
538 qr{enum\s+$Ident},
539 qr{${Ident}_t},
540 qr{${Ident}_handler},
541 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700542 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800543);
Joe Perches938224b2016-01-20 14:59:15 -0800544
545our $C90_int_types = qr{(?x:
546 long\s+long\s+int\s+(?:un)?signed|
547 long\s+long\s+(?:un)?signed\s+int|
548 long\s+long\s+(?:un)?signed|
549 (?:(?:un)?signed\s+)?long\s+long\s+int|
550 (?:(?:un)?signed\s+)?long\s+long|
551 int\s+long\s+long\s+(?:un)?signed|
552 int\s+(?:(?:un)?signed\s+)?long\s+long|
553
554 long\s+int\s+(?:un)?signed|
555 long\s+(?:un)?signed\s+int|
556 long\s+(?:un)?signed|
557 (?:(?:un)?signed\s+)?long\s+int|
558 (?:(?:un)?signed\s+)?long|
559 int\s+long\s+(?:un)?signed|
560 int\s+(?:(?:un)?signed\s+)?long|
561
562 int\s+(?:un)?signed|
563 (?:(?:un)?signed\s+)?int
564)};
565
Alex Dowad485ff232015-06-25 15:02:52 -0700566our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700567our @typeListWithAttr = (
568 @typeList,
569 qr{struct\s+$InitAttribute\s+$Ident},
570 qr{union\s+$InitAttribute\s+$Ident},
571);
572
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700573our @modifierList = (
574 qr{fastcall},
575);
Alex Dowad485ff232015-06-25 15:02:52 -0700576our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800577
Joe Perches24358802014-04-03 14:49:13 -0700578our @mode_permission_funcs = (
579 ["module_param", 3],
580 ["module_param_(?:array|named|string)", 4],
581 ["module_param_array_named", 5],
582 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
583 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700584 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
585 ["IIO_DEV_ATTR_[A-Z_]+", 1],
586 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
587 ["SENSOR_TEMPLATE(?:_2|)", 3],
588 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700589);
590
Joe Perches515a2352014-04-03 14:49:24 -0700591#Create a search pattern for all these functions to speed up a loop below
592our $mode_perms_search = "";
593foreach my $entry (@mode_permission_funcs) {
594 $mode_perms_search .= '|' if ($mode_perms_search ne "");
595 $mode_perms_search .= $entry->[0];
596}
Joe Perches00180462018-02-06 15:38:55 -0800597$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700598
Joe Perches9189c7e2018-09-07 15:26:18 -0700599our %deprecated_apis = (
600 "synchronize_rcu_bh" => "synchronize_rcu",
601 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
602 "call_rcu_bh" => "call_rcu",
603 "rcu_barrier_bh" => "rcu_barrier",
604 "synchronize_sched" => "synchronize_rcu",
605 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
606 "call_rcu_sched" => "call_rcu",
607 "rcu_barrier_sched" => "rcu_barrier",
608 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
609 "cond_synchronize_sched" => "cond_synchronize_rcu",
610);
611
612#Create a search pattern for all these strings to speed up a loop below
613our $deprecated_apis_search = "";
614foreach my $entry (keys %deprecated_apis) {
615 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
616 $deprecated_apis_search .= $entry;
617}
618$deprecated_apis_search = "(?:${deprecated_apis_search})";
619
Joe Perchesb392c642015-04-16 12:44:16 -0700620our $mode_perms_world_writable = qr{
621 S_IWUGO |
622 S_IWOTH |
623 S_IRWXUGO |
624 S_IALLUGO |
625 0[0-7][0-7][2367]
626}x;
627
Joe Perchesf90774e2016-10-11 13:51:47 -0700628our %mode_permission_string_types = (
629 "S_IRWXU" => 0700,
630 "S_IRUSR" => 0400,
631 "S_IWUSR" => 0200,
632 "S_IXUSR" => 0100,
633 "S_IRWXG" => 0070,
634 "S_IRGRP" => 0040,
635 "S_IWGRP" => 0020,
636 "S_IXGRP" => 0010,
637 "S_IRWXO" => 0007,
638 "S_IROTH" => 0004,
639 "S_IWOTH" => 0002,
640 "S_IXOTH" => 0001,
641 "S_IRWXUGO" => 0777,
642 "S_IRUGO" => 0444,
643 "S_IWUGO" => 0222,
644 "S_IXUGO" => 0111,
645);
646
647#Create a search pattern for all these strings to speed up a loop below
648our $mode_perms_string_search = "";
649foreach my $entry (keys %mode_permission_string_types) {
650 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
651 $mode_perms_string_search .= $entry;
652}
Joe Perches00180462018-02-06 15:38:55 -0800653our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
654our $multi_mode_perms_string_search = qr{
655 ${single_mode_perms_string_search}
656 (?:\s*\|\s*${single_mode_perms_string_search})*
657}x;
658
659sub perms_to_octal {
660 my ($string) = @_;
661
662 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
663
664 my $val = "";
665 my $oval = "";
666 my $to = 0;
667 my $curpos = 0;
668 my $lastpos = 0;
669 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
670 $curpos = pos($string);
671 my $match = $2;
672 my $omatch = $1;
673 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
674 $lastpos = $curpos;
675 $to |= $mode_permission_string_types{$match};
676 $val .= '\s*\|\s*' if ($val ne "");
677 $val .= $match;
678 $oval .= $omatch;
679 }
680 $oval =~ s/^\s*\|\s*//;
681 $oval =~ s/\s*\|\s*$//;
682 return sprintf("%04o", $to);
683}
Joe Perchesf90774e2016-10-11 13:51:47 -0700684
Wolfram Sang7840a942010-08-09 17:20:57 -0700685our $allowed_asm_includes = qr{(?x:
686 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700687 memory|
688 time|
689 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700690)};
691# memory.h: ARM has a custom one
692
Kees Cook66b47b42014-10-13 15:51:57 -0700693# Load common spelling mistakes and build regular expression list.
694my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700695my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700696
Joe Perches36061e32014-12-10 15:51:43 -0800697if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800698 while (<$spelling>) {
699 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700700
Joe Perches36061e32014-12-10 15:51:43 -0800701 $line =~ s/\s*\n?$//g;
702 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700703
Joe Perches36061e32014-12-10 15:51:43 -0800704 next if ($line =~ m/^\s*#/);
705 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700706
Joe Perches36061e32014-12-10 15:51:43 -0800707 my ($suspect, $fix) = split(/\|\|/, $line);
708
Joe Perches36061e32014-12-10 15:51:43 -0800709 $spelling_fix{$suspect} = $fix;
710 }
711 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800712} else {
713 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700714}
Kees Cook66b47b42014-10-13 15:51:57 -0700715
Joe Perchesebfd7d62015-04-16 12:44:14 -0700716if ($codespell) {
717 if (open(my $spelling, '<', $codespellfile)) {
718 while (<$spelling>) {
719 my $line = $_;
720
721 $line =~ s/\s*\n?$//g;
722 $line =~ s/^\s*//g;
723
724 next if ($line =~ m/^\s*#/);
725 next if ($line =~ m/^\s*$/);
726 next if ($line =~ m/, disabled/i);
727
728 $line =~ s/,.*$//;
729
730 my ($suspect, $fix) = split(/->/, $line);
731
732 $spelling_fix{$suspect} = $fix;
733 }
734 close($spelling);
735 } else {
736 warn "No codespell typos will be found - file '$codespellfile': $!\n";
737 }
738}
739
740$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
741
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700742sub read_words {
743 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700744
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700745 if (open(my $words, '<', $file)) {
746 while (<$words>) {
747 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700748
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700749 $line =~ s/\s*\n?$//g;
750 $line =~ s/^\s*//g;
751
752 next if ($line =~ m/^\s*#/);
753 next if ($line =~ m/^\s*$/);
754 if ($line =~ /\s/) {
755 print("$file: '$line' invalid - ignored\n");
756 next;
757 }
758
759 $$wordsRef .= '|' if ($$wordsRef ne "");
760 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700761 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700762 close($file);
763 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700764 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700765
766 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700767}
768
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700769my $const_structs = "";
770read_words(\$const_structs, $conststructsfile)
771 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
772
773my $typeOtherTypedefs = "";
774if (length($typedefsfile)) {
775 read_words(\$typeOtherTypedefs, $typedefsfile)
776 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
777}
778$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
779
Andy Whitcroft8905a672007-11-28 16:21:06 -0800780sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700781 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
782 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700783 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700784 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700785 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700786 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700787 (?:$typeTypedefs\b)|
788 (?:${all}\b)
789 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800790 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700791 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800792 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800793 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700794 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700795 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800796 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700797 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800798 }x;
Joe Perches18130872014-08-06 16:11:22 -0700799 $NonptrTypeMisordered = qr{
800 (?:$Modifier\s+|const\s+)*
801 (?:
802 (?:${Misordered}\b)
803 )
804 (?:\s+$Modifier|\s+const)*
805 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700806 $NonptrTypeWithAttr = qr{
807 (?:$Modifier\s+|const\s+)*
808 (?:
809 (?:typeof|__typeof__)\s*\([^\)]*\)|
810 (?:$typeTypedefs\b)|
811 (?:${allWithAttr}\b)
812 )
813 (?:\s+$Modifier|\s+const)*
814 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800815 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700816 $NonptrType
Antonio Borneo7b184962020-04-06 20:11:04 -0700817 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700818 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800819 }x;
Joe Perches18130872014-08-06 16:11:22 -0700820 $TypeMisordered = qr{
821 $NonptrTypeMisordered
Antonio Borneo7b184962020-04-06 20:11:04 -0700822 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Perches18130872014-08-06 16:11:22 -0700823 (?:\s+$Inline|\s+$Modifier)*
824 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700825 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700826 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800827}
828build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700829
Joe Perches7d2367a2011-07-25 17:13:22 -0700830our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700831
832# Using $balanced_parens, $LvalOrFunc, or $FuncArg
833# requires at least perl version v5.10.0
834# Any use must be runtime checked with $^V
835
836our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700837our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800838our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700839
Joe Perchesf8422302014-08-06 16:11:31 -0700840our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700841 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700842 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700843 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700844)};
845
Joe Perches7d2367a2011-07-25 17:13:22 -0700846sub deparenthesize {
847 my ($string) = @_;
848 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700849
850 while ($string =~ /^\s*\(.*\)\s*$/) {
851 $string =~ s@^\s*\(\s*@@;
852 $string =~ s@\s*\)\s*$@@;
853 }
854
Joe Perches7d2367a2011-07-25 17:13:22 -0700855 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700856
Joe Perches7d2367a2011-07-25 17:13:22 -0700857 return $string;
858}
859
Joe Perches34456862013-07-03 15:05:34 -0700860sub seed_camelcase_file {
861 my ($file) = @_;
862
863 return if (!(-f $file));
864
865 local $/;
866
867 open(my $include_file, '<', "$file")
868 or warn "$P: Can't read '$file' $!\n";
869 my $text = <$include_file>;
870 close($include_file);
871
872 my @lines = split('\n', $text);
873
874 foreach my $line (@lines) {
875 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
876 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
877 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800878 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
879 $camelcase{$1} = 1;
880 } 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 -0700881 $camelcase{$1} = 1;
882 }
883 }
884}
885
Joe Perchescd28b112019-12-04 16:52:09 -0800886our %maintained_status = ();
887
Joe Perches85b0ee12016-10-11 13:51:44 -0700888sub is_maintained_obsolete {
889 my ($filename) = @_;
890
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800891 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700892
Joe Perchescd28b112019-12-04 16:52:09 -0800893 if (!exists($maintained_status{$filename})) {
894 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
895 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700896
Joe Perchescd28b112019-12-04 16:52:09 -0800897 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700898}
899
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700900sub is_SPDX_License_valid {
901 my ($license) = @_;
902
Joe Perches56294112018-08-21 21:58:04 -0700903 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700904
Joe Perches56294112018-08-21 21:58:04 -0700905 my $root_path = abs_path($root);
906 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700907 return 0 if ($status ne "");
908 return 1;
909}
910
Joe Perches34456862013-07-03 15:05:34 -0700911my $camelcase_seeded = 0;
912sub seed_camelcase_includes {
913 return if ($camelcase_seeded);
914
915 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700916 my $camelcase_cache = "";
917 my @include_files = ();
918
919 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700920
Richard Genoud3645e322014-02-10 14:25:32 -0800921 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700922 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -0700923 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700924 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700925 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700926 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700927 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700928 @include_files = split('\n', $files);
929 foreach my $file (@include_files) {
930 my $date = POSIX::strftime("%Y%m%d%H%M",
931 localtime((stat $file)[9]));
932 $last_mod_date = $date if ($last_mod_date < $date);
933 }
934 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700935 }
Joe Perchesc707a812013-07-08 16:00:43 -0700936
937 if ($camelcase_cache ne "" && -f $camelcase_cache) {
938 open(my $camelcase_file, '<', "$camelcase_cache")
939 or warn "$P: Can't read '$camelcase_cache' $!\n";
940 while (<$camelcase_file>) {
941 chomp;
942 $camelcase{$_} = 1;
943 }
944 close($camelcase_file);
945
946 return;
947 }
948
Richard Genoud3645e322014-02-10 14:25:32 -0800949 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700950 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700951 @include_files = split('\n', $files);
952 }
953
Joe Perches34456862013-07-03 15:05:34 -0700954 foreach my $file (@include_files) {
955 seed_camelcase_file($file);
956 }
Joe Perches351b2a12013-07-03 15:05:36 -0700957
Joe Perchesc707a812013-07-08 16:00:43 -0700958 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700959 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700960 open(my $camelcase_file, '>', "$camelcase_cache")
961 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700962 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
963 print $camelcase_file ("$_\n");
964 }
965 close($camelcase_file);
966 }
Joe Perches34456862013-07-03 15:05:34 -0700967}
968
Joe Perchesd311cd42014-08-06 16:10:57 -0700969sub git_commit_info {
970 my ($commit, $id, $desc) = @_;
971
972 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
973
Joe Perchesdbbf8692019-09-25 16:46:52 -0700974 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -0700975 $output =~ s/^\s*//gm;
976 my @lines = split("\n", $output);
977
Joe Perches0d7835f2015-02-13 14:38:35 -0800978 return ($id, $desc) if ($#lines < 0);
979
Sean Christopherson5a7f4452019-09-25 16:46:49 -0700980 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -0700981# Maybe one day convert this block of bash into something that returns
982# all matching commit ids, but it's very slow...
983#
984# echo "checking commits $1..."
985# git rev-list --remotes | grep -i "^$1" |
986# while read line ; do
987# git log --format='%H %s' -1 $line |
988# echo "commit $(cut -c 1-12,41-)"
989# done
990 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700991 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700992 } else {
993 $id = substr($lines[0], 0, 12);
994 $desc = substr($lines[0], 41);
995 }
996
997 return ($id, $desc);
998}
999
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001000$chk_signoff = 0 if ($file);
1001
Andy Whitcroft00df3442007-06-08 13:47:06 -07001002my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001003my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001004my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001005my @fixed_inserted = ();
1006my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001007my $fixlinenr = -1;
1008
Du, Changbin4a593c32016-05-20 17:04:16 -07001009# If input is git commits, extract all commits from the commit expressions.
1010# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1011die "$P: No git repository found\n" if ($git && !-e ".git");
1012
1013if ($git) {
1014 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001015 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001016 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001017 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1018 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001019 } elsif ($commit_expr =~ m/\.\./) {
1020 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001021 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001022 $git_range = "-1 $commit_expr";
1023 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001024 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001025 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001026 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1027 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001028 my $sha1 = $1;
1029 my $subject = $2;
1030 unshift(@commits, $sha1);
1031 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001032 }
1033 }
1034 die "$P: no git commits after extraction!\n" if (@commits == 0);
1035 @ARGV = @commits;
1036}
1037
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001038my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001039$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001040for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001041 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -07001042 if ($git) {
1043 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1044 die "$P: $filename: git format-patch failed - $!\n";
1045 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001046 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001047 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001048 } elsif ($filename eq '-') {
1049 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001050 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001051 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001052 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001053 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001054 if ($filename eq '-') {
1055 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001056 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001057 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001058 } else {
1059 $vname = $filename;
1060 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001061 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001062 chomp;
1063 push(@rawlines, $_);
Geert Uytterhoevenc7f574d2020-06-04 16:50:43 -07001064 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001065 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001066 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001067
1068 if ($#ARGV > 0 && $quiet == 0) {
1069 print '-' x length($vname) . "\n";
1070 print "$vname\n";
1071 print '-' x length($vname) . "\n";
1072 }
1073
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001074 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001075 $exit = 1;
1076 }
1077 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001078 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001079 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001080 @fixed_inserted = ();
1081 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001082 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001083 @modifierListFile = ();
1084 @typeListFile = ();
1085 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001086}
1087
Joe Perchesd8469f12015-06-25 15:03:00 -07001088if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001089 hash_show_words(\%use_type, "Used");
1090 hash_show_words(\%ignore_type, "Ignored");
1091
Joe Perches5b579802018-08-21 21:57:33 -07001092 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001093 print << "EOM"
1094
1095NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001096 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001097EOM
1098 }
1099 if ($exit) {
1100 print << "EOM"
1101
1102NOTE: If any of the errors are false positives, please report
1103 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1104EOM
1105 }
1106}
1107
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001108exit($exit);
1109
1110sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001111 my ($root) = @_;
1112
1113 my @tree_check = (
1114 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1115 "README", "Documentation", "arch", "include", "drivers",
1116 "fs", "init", "ipc", "kernel", "lib", "scripts",
1117 );
1118
1119 foreach my $check (@tree_check) {
1120 if (! -e $root . '/' . $check) {
1121 return 0;
1122 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001123 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001124 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001125}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001126
Joe Perches20112472011-07-25 17:13:23 -07001127sub parse_email {
1128 my ($formatted_email) = @_;
1129
1130 my $name = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001131 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001132 my $address = "";
1133 my $comment = "";
1134
1135 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1136 $name = $1;
1137 $address = $2;
1138 $comment = $3 if defined $3;
1139 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1140 $address = $1;
1141 $comment = $2 if defined $2;
1142 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1143 $address = $1;
1144 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001145 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001146 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001147 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001148 $name =~ s/^\"|\"$//g;
1149 # If there's a name left after stripping spaces and
1150 # leading quotes, and the address doesn't have both
1151 # leading and trailing angle brackets, the address
1152 # is invalid. ie:
1153 # "joe smith joe@smith.com" bad
1154 # "joe smith <joe@smith.com" bad
1155 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1156 $name = "";
1157 $address = "";
1158 $comment = "";
1159 }
1160 }
1161
Joe Perches3705ce52013-07-03 15:05:31 -07001162 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001163 $name =~ s/^\"|\"$//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001164 $name =~ s/(\s*\([^\)]+\))\s*//;
1165 if (defined($1)) {
1166 $name_comment = trim($1);
1167 }
Joe Perches3705ce52013-07-03 15:05:31 -07001168 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001169 $address =~ s/^\<|\>$//g;
1170
1171 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1172 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1173 $name = "\"$name\"";
1174 }
1175
Joe Perchesdfa05c22020-04-06 20:10:48 -07001176 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001177}
1178
1179sub format_email {
1180 my ($name, $address) = @_;
1181
1182 my $formatted_email;
1183
Joe Perches3705ce52013-07-03 15:05:31 -07001184 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001185 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001186 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001187
1188 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1189 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1190 $name = "\"$name\"";
1191 }
1192
1193 if ("$name" eq "") {
1194 $formatted_email = "$address";
1195 } else {
1196 $formatted_email = "$name <$address>";
1197 }
1198
1199 return $formatted_email;
1200}
1201
Joe Perchesdfa05c22020-04-06 20:10:48 -07001202sub reformat_email {
1203 my ($email) = @_;
1204
1205 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1206 return format_email($email_name, $email_address);
1207}
1208
1209sub same_email_addresses {
1210 my ($email1, $email2) = @_;
1211
1212 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1213 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1214
1215 return $email1_name eq $email2_name &&
1216 $email1_address eq $email2_address;
1217}
1218
Joe Perchesd311cd42014-08-06 16:10:57 -07001219sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001220 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001221
Joe Perchesbd474ca2014-08-06 16:11:10 -07001222 foreach my $path (split(/:/, $ENV{PATH})) {
1223 if (-e "$path/$bin") {
1224 return "$path/$bin";
1225 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001226 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001227
Joe Perchesbd474ca2014-08-06 16:11:10 -07001228 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001229}
1230
Joe Perches000d1cc12011-07-25 17:13:25 -07001231sub which_conf {
1232 my ($conf) = @_;
1233
1234 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1235 if (-e "$path/$conf") {
1236 return "$path/$conf";
1237 }
1238 }
1239
1240 return "";
1241}
1242
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001243sub expand_tabs {
1244 my ($str) = @_;
1245
1246 my $res = '';
1247 my $n = 0;
1248 for my $c (split(//, $str)) {
1249 if ($c eq "\t") {
1250 $res .= ' ';
1251 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001252 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001253 $res .= ' ';
1254 }
1255 next;
1256 }
1257 $res .= $c;
1258 $n++;
1259 }
1260
1261 return $res;
1262}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001263sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001264 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001265 return $res;
1266}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001267
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001268sub line_stats {
1269 my ($line) = @_;
1270
1271 # Drop the diff line leader and expand tabs
1272 $line =~ s/^.//;
1273 $line = expand_tabs($line);
1274
1275 # Pick the indent from the front of the line.
1276 my ($white) = ($line =~ /^(\s*)/);
1277
1278 return (length($line), length($white));
1279}
1280
Andy Whitcroft773647a2008-03-28 14:15:58 -07001281my $sanitise_quote = '';
1282
1283sub sanitise_line_reset {
1284 my ($in_comment) = @_;
1285
1286 if ($in_comment) {
1287 $sanitise_quote = '*/';
1288 } else {
1289 $sanitise_quote = '';
1290 }
1291}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001292sub sanitise_line {
1293 my ($line) = @_;
1294
1295 my $res = '';
1296 my $l = '';
1297
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001298 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001299 my $off = 0;
1300 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001301
Andy Whitcroft773647a2008-03-28 14:15:58 -07001302 # Always copy over the diff marker.
1303 $res = substr($line, 0, 1);
1304
1305 for ($off = 1; $off < length($line); $off++) {
1306 $c = substr($line, $off, 1);
1307
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001308 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001309 # and end, all to $;.
1310 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1311 $sanitise_quote = '*/';
1312
1313 substr($res, $off, 2, "$;$;");
1314 $off++;
1315 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001316 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001317 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001318 $sanitise_quote = '';
1319 substr($res, $off, 2, "$;$;");
1320 $off++;
1321 next;
1322 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001323 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1324 $sanitise_quote = '//';
1325
1326 substr($res, $off, 2, $sanitise_quote);
1327 $off++;
1328 next;
1329 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001330
1331 # A \ in a string means ignore the next character.
1332 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1333 $c eq "\\") {
1334 substr($res, $off, 2, 'XX');
1335 $off++;
1336 next;
1337 }
1338 # Regular quotes.
1339 if ($c eq "'" || $c eq '"') {
1340 if ($sanitise_quote eq '') {
1341 $sanitise_quote = $c;
1342
1343 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001344 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001345 } elsif ($sanitise_quote eq $c) {
1346 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001347 }
1348 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001349
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001350 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001351 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1352 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001353 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1354 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001355 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1356 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001357 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001358 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001359 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001360 }
1361
Daniel Walker113f04a2009-09-21 17:04:35 -07001362 if ($sanitise_quote eq '//') {
1363 $sanitise_quote = '';
1364 }
1365
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001366 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001367 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001368 my $clean = 'X' x length($1);
1369 $res =~ s@\<.*\>@<$clean>@;
1370
1371 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001372 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001373 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001374 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001375 }
1376
Joe Perchesdadf6802016-08-02 14:04:33 -07001377 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1378 my $match = $1;
1379 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1380 }
1381
Andy Whitcroft00df3442007-06-08 13:47:06 -07001382 return $res;
1383}
1384
Joe Perchesa6962d72013-04-29 16:18:13 -07001385sub get_quoted_string {
1386 my ($line, $rawline) = @_;
1387
Joe Perches478b1792018-04-10 16:33:34 -07001388 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001389 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001390 return substr($rawline, $-[0], $+[0] - $-[0]);
1391}
1392
Andy Whitcroft8905a672007-11-28 16:21:06 -08001393sub ctx_statement_block {
1394 my ($linenr, $remain, $off) = @_;
1395 my $line = $linenr - 1;
1396 my $blk = '';
1397 my $soff = $off;
1398 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001399 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001400
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001401 my $loff = 0;
1402
Andy Whitcroft8905a672007-11-28 16:21:06 -08001403 my $type = '';
1404 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001405 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001406 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001407 my $c;
1408 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001409
1410 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001411 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001412 @stack = (['', 0]) if ($#stack == -1);
1413
Andy Whitcroft773647a2008-03-28 14:15:58 -07001414 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001415 # If we are about to drop off the end, pull in more
1416 # context.
1417 if ($off >= $len) {
1418 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001419 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001420 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001421 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001422 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001423 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001424 $len = length($blk);
1425 $line++;
1426 last;
1427 }
1428 # Bail if there is no further context.
1429 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001430 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001431 last;
1432 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001433 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1434 $level++;
1435 $type = '#';
1436 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001437 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001438 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001439 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001440 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001441
Andy Whitcroft773647a2008-03-28 14:15:58 -07001442 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001443
1444 # Handle nested #if/#else.
1445 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1446 push(@stack, [ $type, $level ]);
1447 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1448 ($type, $level) = @{$stack[$#stack - 1]};
1449 } elsif ($remainder =~ /^#\s*endif\b/) {
1450 ($type, $level) = @{pop(@stack)};
1451 }
1452
Andy Whitcroft8905a672007-11-28 16:21:06 -08001453 # Statement ends at the ';' or a close '}' at the
1454 # outermost level.
1455 if ($level == 0 && $c eq ';') {
1456 last;
1457 }
1458
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001459 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001460 if ($level == 0 && $coff_set == 0 &&
1461 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1462 $remainder =~ /^(else)(?:\s|{)/ &&
1463 $remainder !~ /^else\s+if\b/) {
1464 $coff = $off + length($1) - 1;
1465 $coff_set = 1;
1466 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1467 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001468 }
1469
Andy Whitcroft8905a672007-11-28 16:21:06 -08001470 if (($type eq '' || $type eq '(') && $c eq '(') {
1471 $level++;
1472 $type = '(';
1473 }
1474 if ($type eq '(' && $c eq ')') {
1475 $level--;
1476 $type = ($level != 0)? '(' : '';
1477
1478 if ($level == 0 && $coff < $soff) {
1479 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001480 $coff_set = 1;
1481 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001482 }
1483 }
1484 if (($type eq '' || $type eq '{') && $c eq '{') {
1485 $level++;
1486 $type = '{';
1487 }
1488 if ($type eq '{' && $c eq '}') {
1489 $level--;
1490 $type = ($level != 0)? '{' : '';
1491
1492 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001493 if (substr($blk, $off + 1, 1) eq ';') {
1494 $off++;
1495 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001496 last;
1497 }
1498 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001499 # Preprocessor commands end at the newline unless escaped.
1500 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1501 $level--;
1502 $type = '';
1503 $off++;
1504 last;
1505 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001506 $off++;
1507 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001508 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001509 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001510 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001511 $line++;
1512 $remain--;
1513 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001514
1515 my $statement = substr($blk, $soff, $off - $soff + 1);
1516 my $condition = substr($blk, $soff, $coff - $soff + 1);
1517
1518 #warn "STATEMENT<$statement>\n";
1519 #warn "CONDITION<$condition>\n";
1520
Andy Whitcroft773647a2008-03-28 14:15:58 -07001521 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001522
1523 return ($statement, $condition,
1524 $line, $remain + 1, $off - $loff + 1, $level);
1525}
1526
Andy Whitcroftcf655042008-03-04 14:28:20 -08001527sub statement_lines {
1528 my ($stmt) = @_;
1529
1530 # Strip the diff line prefixes and rip blank lines at start and end.
1531 $stmt =~ s/(^|\n)./$1/g;
1532 $stmt =~ s/^\s*//;
1533 $stmt =~ s/\s*$//;
1534
1535 my @stmt_lines = ($stmt =~ /\n/g);
1536
1537 return $#stmt_lines + 2;
1538}
1539
1540sub statement_rawlines {
1541 my ($stmt) = @_;
1542
1543 my @stmt_lines = ($stmt =~ /\n/g);
1544
1545 return $#stmt_lines + 2;
1546}
1547
1548sub statement_block_size {
1549 my ($stmt) = @_;
1550
1551 $stmt =~ s/(^|\n)./$1/g;
1552 $stmt =~ s/^\s*{//;
1553 $stmt =~ s/}\s*$//;
1554 $stmt =~ s/^\s*//;
1555 $stmt =~ s/\s*$//;
1556
1557 my @stmt_lines = ($stmt =~ /\n/g);
1558 my @stmt_statements = ($stmt =~ /;/g);
1559
1560 my $stmt_lines = $#stmt_lines + 2;
1561 my $stmt_statements = $#stmt_statements + 1;
1562
1563 if ($stmt_lines > $stmt_statements) {
1564 return $stmt_lines;
1565 } else {
1566 return $stmt_statements;
1567 }
1568}
1569
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001570sub ctx_statement_full {
1571 my ($linenr, $remain, $off) = @_;
1572 my ($statement, $condition, $level);
1573
1574 my (@chunks);
1575
Andy Whitcroftcf655042008-03-04 14:28:20 -08001576 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001577 ($statement, $condition, $linenr, $remain, $off, $level) =
1578 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001579 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001580 push(@chunks, [ $condition, $statement ]);
1581 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1582 return ($level, $linenr, @chunks);
1583 }
1584
1585 # Pull in the following conditional/block pairs and see if they
1586 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001587 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001588 ($statement, $condition, $linenr, $remain, $off, $level) =
1589 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001590 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001591 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001592 #print "C: push\n";
1593 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001594 }
1595
1596 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001597}
1598
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001599sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001600 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001601 my $line;
1602 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001603 my $blk = '';
1604 my @o;
1605 my @c;
1606 my @res = ();
1607
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001608 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001609 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001610 for ($line = $start; $remain > 0; $line++) {
1611 next if ($rawlines[$line] =~ /^-/);
1612 $remain--;
1613
1614 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001615
1616 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001617 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001618 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001619 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001620 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001621 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001622 $level = pop(@stack);
1623 }
1624
Andy Whitcroft01464f32010-10-26 14:23:19 -07001625 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001626 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1627 if ($off > 0) {
1628 $off--;
1629 next;
1630 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001631
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001632 if ($c eq $close && $level > 0) {
1633 $level--;
1634 last if ($level == 0);
1635 } elsif ($c eq $open) {
1636 $level++;
1637 }
1638 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001639
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001640 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001641 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001642 }
1643
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001644 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001645 }
1646
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001647 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001648}
1649sub ctx_block_outer {
1650 my ($linenr, $remain) = @_;
1651
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001652 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1653 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001654}
1655sub ctx_block {
1656 my ($linenr, $remain) = @_;
1657
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001658 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1659 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001660}
1661sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001662 my ($linenr, $remain, $off) = @_;
1663
1664 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1665 return @r;
1666}
1667sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001668 my ($linenr, $remain) = @_;
1669
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001670 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001671}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001672sub ctx_statement_level {
1673 my ($linenr, $remain, $off) = @_;
1674
1675 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1676}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001677
1678sub ctx_locate_comment {
1679 my ($first_line, $end_line) = @_;
1680
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001681 # If c99 comment on the current line, or the line before or after
1682 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1683 return $current_comment if (defined $current_comment);
1684 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1685 return $current_comment if (defined $current_comment);
1686 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1687 return $current_comment if (defined $current_comment);
1688
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001689 # Catch a comment on the end of the line itself.
Joe Perchesa55ee0c2020-06-04 16:50:36 -07001690 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001691 return $current_comment if (defined $current_comment);
1692
1693 # Look through the context and try and figure out if there is a
1694 # comment.
1695 my $in_comment = 0;
1696 $current_comment = '';
1697 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001698 my $line = $rawlines[$linenr - 1];
1699 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001700 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1701 $in_comment = 1;
1702 }
1703 if ($line =~ m@/\*@) {
1704 $in_comment = 1;
1705 }
1706 if (!$in_comment && $current_comment ne '') {
1707 $current_comment = '';
1708 }
1709 $current_comment .= $line . "\n" if ($in_comment);
1710 if ($line =~ m@\*/@) {
1711 $in_comment = 0;
1712 }
1713 }
1714
1715 chomp($current_comment);
1716 return($current_comment);
1717}
1718sub ctx_has_comment {
1719 my ($first_line, $end_line) = @_;
1720 my $cmt = ctx_locate_comment($first_line, $end_line);
1721
Andy Whitcroft00df3442007-06-08 13:47:06 -07001722 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001723 ##print "CMMT: $cmt\n";
1724
1725 return ($cmt ne '');
1726}
1727
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001728sub raw_line {
1729 my ($linenr, $cnt) = @_;
1730
1731 my $offset = $linenr - 1;
1732 $cnt++;
1733
1734 my $line;
1735 while ($cnt) {
1736 $line = $rawlines[$offset++];
1737 next if (defined($line) && $line =~ /^-/);
1738 $cnt--;
1739 }
1740
1741 return $line;
1742}
1743
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001744sub get_stat_real {
1745 my ($linenr, $lc) = @_;
1746
1747 my $stat_real = raw_line($linenr, 0);
1748 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1749 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1750 }
1751
1752 return $stat_real;
1753}
1754
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001755sub get_stat_here {
1756 my ($linenr, $cnt, $here) = @_;
1757
1758 my $herectx = $here . "\n";
1759 for (my $n = 0; $n < $cnt; $n++) {
1760 $herectx .= raw_line($linenr, $n) . "\n";
1761 }
1762
1763 return $herectx;
1764}
1765
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001766sub cat_vet {
1767 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001768 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001769
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001770 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001771 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1772 $res .= $1;
1773 if ($2 ne '') {
1774 $coded = sprintf("^%c", unpack('C', $2) + 64);
1775 $res .= $coded;
1776 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001777 }
1778 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001779
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001780 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001781}
1782
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001783my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001784my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001785my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001786my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001787
1788sub annotate_reset {
1789 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001790 $av_pending = '_';
1791 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001792 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001793}
1794
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001795sub annotate_values {
1796 my ($stream, $type) = @_;
1797
1798 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001799 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001800 my $cur = $stream;
1801
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001802 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001803
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001804 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001805 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001806 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001807 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001808 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001809 print "WS($1)\n" if ($dbg_values > 1);
1810 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001811 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001812 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001813 }
1814
Florian Micklerc023e4732011-01-12 16:59:58 -08001815 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001816 print "CAST($1)\n" if ($dbg_values > 1);
1817 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001818 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001819
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001820 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001821 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001822 $type = 'T';
1823
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001824 } elsif ($cur =~ /^($Modifier)\s*/) {
1825 print "MODIFIER($1)\n" if ($dbg_values > 1);
1826 $type = 'T';
1827
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001828 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001829 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001830 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001831 push(@av_paren_type, $type);
1832 if ($2 ne '') {
1833 $av_pending = 'N';
1834 }
1835 $type = 'E';
1836
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001837 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001838 print "UNDEF($1)\n" if ($dbg_values > 1);
1839 $av_preprocessor = 1;
1840 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001841
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001842 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001843 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001844 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001845
1846 push(@av_paren_type, $type);
1847 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001848 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001849
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001850 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001851 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1852 $av_preprocessor = 1;
1853
1854 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1855
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001856 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001857
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001858 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001859 print "PRE_END($1)\n" if ($dbg_values > 1);
1860
1861 $av_preprocessor = 1;
1862
1863 # Assume all arms of the conditional end as this
1864 # one does, and continue as if the #endif was not here.
1865 pop(@av_paren_type);
1866 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001867 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001868
1869 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001870 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001871
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001872 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1873 print "ATTR($1)\n" if ($dbg_values > 1);
1874 $av_pending = $type;
1875 $type = 'N';
1876
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001877 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001878 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001879 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001880 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001881 }
1882 $type = 'N';
1883
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001884 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001885 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001886 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001887 $type = 'N';
1888
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001889 } elsif ($cur =~/^(case)/o) {
1890 print "CASE($1)\n" if ($dbg_values > 1);
1891 $av_pend_colon = 'C';
1892 $type = 'N';
1893
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001894 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001895 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001896 $type = 'N';
1897
1898 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001899 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001900 push(@av_paren_type, $av_pending);
1901 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001902 $type = 'N';
1903
1904 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001905 my $new_type = pop(@av_paren_type);
1906 if ($new_type ne '_') {
1907 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001908 print "PAREN('$1') -> $type\n"
1909 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001910 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001911 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001912 }
1913
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001914 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001915 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001916 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001917 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001918
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001919 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1920 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001921 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001922 } elsif ($type eq 'E') {
1923 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001924 }
1925 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1926 $type = 'V';
1927
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001928 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001929 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001930 $type = 'V';
1931
1932 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001933 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001934 $type = 'N';
1935
Andy Whitcroftcf655042008-03-04 14:28:20 -08001936 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001937 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001938 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001939 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001940
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001941 } elsif ($cur =~/^(,)/) {
1942 print "COMMA($1)\n" if ($dbg_values > 1);
1943 $type = 'C';
1944
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001945 } elsif ($cur =~ /^(\?)/o) {
1946 print "QUESTION($1)\n" if ($dbg_values > 1);
1947 $type = 'N';
1948
1949 } elsif ($cur =~ /^(:)/o) {
1950 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1951
1952 substr($var, length($res), 1, $av_pend_colon);
1953 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1954 $type = 'E';
1955 } else {
1956 $type = 'N';
1957 }
1958 $av_pend_colon = 'O';
1959
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001960 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001961 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001962 $type = 'N';
1963
Andy Whitcroft0d413862008-10-15 22:02:16 -07001964 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001965 my $variant;
1966
1967 print "OPV($1)\n" if ($dbg_values > 1);
1968 if ($type eq 'V') {
1969 $variant = 'B';
1970 } else {
1971 $variant = 'U';
1972 }
1973
1974 substr($var, length($res), 1, $variant);
1975 $type = 'N';
1976
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001977 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001978 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001979 if ($1 ne '++' && $1 ne '--') {
1980 $type = 'N';
1981 }
1982
1983 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001984 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001985 }
1986 if (defined $1) {
1987 $cur = substr($cur, length($1));
1988 $res .= $type x length($1);
1989 }
1990 }
1991
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001992 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001993}
1994
Andy Whitcroft8905a672007-11-28 16:21:06 -08001995sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001996 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001997 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001998 ^(?:
1999 $Modifier|
2000 $Storage|
2001 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002002 DEFINE_\S+
2003 )$|
2004 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07002005 goto|
2006 return|
2007 case|
2008 else|
2009 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08002010 do|
2011 \#|
2012 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002013 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002014 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002015 )}x;
2016 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2017 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002018 # Check for modifiers.
2019 $possible =~ s/\s*$Storage\s*//g;
2020 $possible =~ s/\s*$Sparse\s*//g;
2021 if ($possible =~ /^\s*$/) {
2022
2023 } elsif ($possible =~ /\s/) {
2024 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002025 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002026 if ($modifier !~ $notPermitted) {
2027 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002028 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002029 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002030 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002031
2032 } else {
2033 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002034 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002035 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002036 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002037 } else {
2038 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002039 }
2040}
2041
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002042my $prefix = '';
2043
Joe Perches000d1cc12011-07-25 17:13:25 -07002044sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002045 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002046
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002047 $type =~ tr/[a-z]/[A-Z]/;
2048
Joe Perchescbec18a2014-04-03 14:49:19 -07002049 return defined $use_type{$type} if (scalar keys %use_type > 0);
2050
2051 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002052}
2053
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002054sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002055 my ($level, $type, $msg) = @_;
2056
2057 if (!show_type($type) ||
2058 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002059 return 0;
2060 }
Joe Perches57230292015-06-25 15:03:03 -07002061 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002062 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002063 if ($level eq 'ERROR') {
2064 $output .= RED;
2065 } elsif ($level eq 'WARNING') {
2066 $output .= YELLOW;
2067 } else {
2068 $output .= GREEN;
2069 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002070 }
Joe Perches57230292015-06-25 15:03:03 -07002071 $output .= $prefix . $level . ':';
2072 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002073 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002074 $output .= "$type:";
2075 }
John Brooks737c0762017-07-10 15:52:24 -07002076 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002077 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002078
2079 if ($showfile) {
2080 my @lines = split("\n", $output, -1);
2081 splice(@lines, 1, 1);
2082 $output = join("\n", @lines);
2083 }
Joe Perches57230292015-06-25 15:03:03 -07002084 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002085
Joe Perches57230292015-06-25 15:03:03 -07002086 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002087
2088 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002089}
Joe Perchescbec18a2014-04-03 14:49:19 -07002090
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002091sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002092 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002093}
Joe Perches000d1cc12011-07-25 17:13:25 -07002094
Joe Perchesd752fcc2014-08-06 16:11:05 -07002095sub fixup_current_range {
2096 my ($lineRef, $offset, $length) = @_;
2097
2098 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2099 my $o = $1;
2100 my $l = $2;
2101 my $no = $o + $offset;
2102 my $nl = $l + $length;
2103 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2104 }
2105}
2106
2107sub fix_inserted_deleted_lines {
2108 my ($linesRef, $insertedRef, $deletedRef) = @_;
2109
2110 my $range_last_linenr = 0;
2111 my $delta_offset = 0;
2112
2113 my $old_linenr = 0;
2114 my $new_linenr = 0;
2115
2116 my $next_insert = 0;
2117 my $next_delete = 0;
2118
2119 my @lines = ();
2120
2121 my $inserted = @{$insertedRef}[$next_insert++];
2122 my $deleted = @{$deletedRef}[$next_delete++];
2123
2124 foreach my $old_line (@{$linesRef}) {
2125 my $save_line = 1;
2126 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002127 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002128 $delta_offset = 0;
2129 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2130 $range_last_linenr = $new_linenr;
2131 fixup_current_range(\$line, $delta_offset, 0);
2132 }
2133
2134 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2135 $deleted = @{$deletedRef}[$next_delete++];
2136 $save_line = 0;
2137 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2138 }
2139
2140 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2141 push(@lines, ${$inserted}{'LINE'});
2142 $inserted = @{$insertedRef}[$next_insert++];
2143 $new_linenr++;
2144 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2145 }
2146
2147 if ($save_line) {
2148 push(@lines, $line);
2149 $new_linenr++;
2150 }
2151
2152 $old_linenr++;
2153 }
2154
2155 return @lines;
2156}
2157
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002158sub fix_insert_line {
2159 my ($linenr, $line) = @_;
2160
2161 my $inserted = {
2162 LINENR => $linenr,
2163 LINE => $line,
2164 };
2165 push(@fixed_inserted, $inserted);
2166}
2167
2168sub fix_delete_line {
2169 my ($linenr, $line) = @_;
2170
2171 my $deleted = {
2172 LINENR => $linenr,
2173 LINE => $line,
2174 };
2175
2176 push(@fixed_deleted, $deleted);
2177}
2178
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002179sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002180 my ($type, $msg) = @_;
2181
2182 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002183 our $clean = 0;
2184 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002185 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002186 }
Joe Perches3705ce52013-07-03 15:05:31 -07002187 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002188}
2189sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002190 my ($type, $msg) = @_;
2191
2192 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002193 our $clean = 0;
2194 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002195 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002196 }
Joe Perches3705ce52013-07-03 15:05:31 -07002197 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002198}
2199sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002200 my ($type, $msg) = @_;
2201
2202 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002203 our $clean = 0;
2204 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002205 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002206 }
Joe Perches3705ce52013-07-03 15:05:31 -07002207 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002208}
2209
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002210sub check_absolute_file {
2211 my ($absolute, $herecurr) = @_;
2212 my $file = $absolute;
2213
2214 ##print "absolute<$absolute>\n";
2215
2216 # See if any suffix of this path is a path within the tree.
2217 while ($file =~ s@^[^/]*/@@) {
2218 if (-f "$root/$file") {
2219 ##print "file<$file>\n";
2220 last;
2221 }
2222 }
2223 if (! -f _) {
2224 return 0;
2225 }
2226
2227 # It is, so see if the prefix is acceptable.
2228 my $prefix = $absolute;
2229 substr($prefix, -length($file)) = '';
2230
2231 ##print "prefix<$prefix>\n";
2232 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002233 WARN("USE_RELATIVE_PATH",
2234 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002235 }
2236}
2237
Joe Perches3705ce52013-07-03 15:05:31 -07002238sub trim {
2239 my ($string) = @_;
2240
Joe Perchesb34c6482013-09-11 14:24:01 -07002241 $string =~ s/^\s+|\s+$//g;
2242
2243 return $string;
2244}
2245
2246sub ltrim {
2247 my ($string) = @_;
2248
2249 $string =~ s/^\s+//;
2250
2251 return $string;
2252}
2253
2254sub rtrim {
2255 my ($string) = @_;
2256
2257 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002258
2259 return $string;
2260}
2261
Joe Perches52ea8502013-11-12 15:10:09 -08002262sub string_find_replace {
2263 my ($string, $find, $replace) = @_;
2264
2265 $string =~ s/$find/$replace/g;
2266
2267 return $string;
2268}
2269
Joe Perches3705ce52013-07-03 15:05:31 -07002270sub tabify {
2271 my ($leading) = @_;
2272
Antonio Borneo713a09d2020-04-06 20:11:07 -07002273 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002274 my $max_spaces_before_tab = $source_indent - 1;
2275 my $spaces_to_tab = " " x $source_indent;
2276
2277 #convert leading spaces to tabs
2278 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2279 #Remove spaces before a tab
2280 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2281
2282 return "$leading";
2283}
2284
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002285sub pos_last_openparen {
2286 my ($line) = @_;
2287
2288 my $pos = 0;
2289
2290 my $opens = $line =~ tr/\(/\(/;
2291 my $closes = $line =~ tr/\)/\)/;
2292
2293 my $last_openparen = 0;
2294
2295 if (($opens == 0) || ($closes >= $opens)) {
2296 return -1;
2297 }
2298
2299 my $len = length($line);
2300
2301 for ($pos = 0; $pos < $len; $pos++) {
2302 my $string = substr($line, $pos);
2303 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2304 $pos += length($1) - 1;
2305 } elsif (substr($line, $pos, 1) eq '(') {
2306 $last_openparen = $pos;
2307 } elsif (index($string, '(') == -1) {
2308 last;
2309 }
2310 }
2311
Joe Perches91cb5192014-04-03 14:49:32 -07002312 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002313}
2314
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002315sub get_raw_comment {
2316 my ($line, $rawline) = @_;
2317 my $comment = '';
2318
2319 for my $i (0 .. (length($line) - 1)) {
2320 if (substr($line, $i, 1) eq "$;") {
2321 $comment .= substr($rawline, $i, 1);
2322 }
2323 }
2324
2325 return $comment;
2326}
2327
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002328sub process {
2329 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002330
2331 my $linenr=0;
2332 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002333 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002334 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002335 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002336
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002337 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002338 my $indent;
2339 my $previndent=0;
2340 my $stashindent=0;
2341
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002342 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002343 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002344 my $author = '';
2345 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002346 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002347 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002348 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002349 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches44d303e2020-04-06 20:11:10 -07002350 my $has_patch_separator = 0; #Found a --- line
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002351 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002352 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002353 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002354 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002355 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002356 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002357 my $non_utf8_charset = 0;
2358
Joe Perches365dd4e2014-08-06 16:10:42 -07002359 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002360 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002361
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002362 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002363 our $cnt_lines = 0;
2364 our $cnt_error = 0;
2365 our $cnt_warn = 0;
2366 our $cnt_chk = 0;
2367
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002368 # Trace the real file/line as we go.
2369 my $realfile = '';
2370 my $realline = 0;
2371 my $realcnt = 0;
2372 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002373 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002374 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002375 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002376 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002377 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002378
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002379 my $prev_values = 'E';
2380
2381 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002382 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002383 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002384 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002385 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002386
Joe Perches7e51f192013-09-11 14:23:57 -07002387 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002388
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002389 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002390 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002391 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002392 my @setup_docs = ();
2393 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002394
Joe Perchesd8b07712013-11-12 15:10:06 -08002395 my $camelcase_file_seeded = 0;
2396
Rob Herring9f3a8992018-04-10 16:33:13 -07002397 my $checklicenseline = 1;
2398
Andy Whitcroft773647a2008-03-28 14:15:58 -07002399 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002400 my $line;
2401 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002402 $linenr++;
2403 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002404
Joe Perches3705ce52013-07-03 15:05:31 -07002405 push(@fixed, $rawline) if ($fix);
2406
Andy Whitcroft773647a2008-03-28 14:15:58 -07002407 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002408 $setup_docs = 0;
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07002409 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002410 $setup_docs = 1;
2411 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002412 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002413 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002414 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002415 $realline=$1-1;
2416 if (defined $2) {
2417 $realcnt=$3+1;
2418 } else {
2419 $realcnt=1+1;
2420 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002421 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002422
2423 # Guestimate if this is a continuing comment. Run
2424 # the context looking for a comment "edge". If this
2425 # edge is a close comment then we must be in a comment
2426 # at context start.
2427 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002428 my $cnt = $realcnt;
2429 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2430 next if (defined $rawlines[$ln - 1] &&
2431 $rawlines[$ln - 1] =~ /^-/);
2432 $cnt--;
2433 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002434 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002435 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2436 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2437 ($edge) = $1;
2438 last;
2439 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002440 }
2441 if (defined $edge && $edge eq '*/') {
2442 $in_comment = 1;
2443 }
2444
2445 # Guestimate if this is a continuing comment. If this
2446 # is the start of a diff block and this line starts
2447 # ' *' then it is very likely a comment.
2448 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002449 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002450 {
2451 $in_comment = 1;
2452 }
2453
2454 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2455 sanitise_line_reset($in_comment);
2456
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002457 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002458 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002459 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002460 $line = sanitise_line($rawline);
2461 }
2462 push(@lines, $line);
2463
2464 if ($realcnt > 1) {
2465 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2466 } else {
2467 $realcnt = 0;
2468 }
2469
2470 #print "==>$rawline\n";
2471 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002472
2473 if ($setup_docs && $line =~ /^\+/) {
2474 push(@setup_docs, $line);
2475 }
2476 }
2477
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002478 $prefix = '';
2479
Andy Whitcroft773647a2008-03-28 14:15:58 -07002480 $realcnt = 0;
2481 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002482 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002483 foreach my $line (@lines) {
2484 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002485 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002486 my $sline = $line; #copy of $line
2487 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002488
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002489 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002490 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002491
Joe Perches12c253a2018-06-07 17:10:58 -07002492# check if it's a mode change, rename or start of a patch
2493 if (!$in_commit_log &&
2494 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2495 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2496 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2497 $is_patch = 1;
2498 }
2499
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002500#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002501 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002502 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2503 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002504 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002505 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002506 $realline=$1-1;
2507 if (defined $2) {
2508 $realcnt=$3+1;
2509 } else {
2510 $realcnt=1+1;
2511 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002512 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002513 $prev_values = 'E';
2514
Andy Whitcroft773647a2008-03-28 14:15:58 -07002515 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002516 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002517 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002518 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002519 if ($context =~ /\b(\w+)\s*\(/) {
2520 $context_function = $1;
2521 } else {
2522 undef $context_function;
2523 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002524 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002525
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002526# track the line number as we move through the hunk, note that
2527# new versions of GNU diff omit the leading space on completely
2528# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002529 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002530 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002531 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002532
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002533 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002534 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002535
2536 # Track the previous line.
2537 ($prevline, $stashline) = ($stashline, $line);
2538 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002539 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2540
Andy Whitcroft773647a2008-03-28 14:15:58 -07002541 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002542
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002543 } elsif ($realcnt == 1) {
2544 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002545 }
2546
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002547 my $hunk_line = ($realcnt != 0);
2548
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002549 $here = "#$linenr: " if (!$file);
2550 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002551
Joe Perches2ac73b42014-06-04 16:12:05 -07002552 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002553 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002554 if ($line =~ /^diff --git.*?(\S+)$/) {
2555 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002556 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002557 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002558 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002559 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002560 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002561 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002562 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002563
2564 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002565 if (!$file && $tree && $p1_prefix ne '' &&
2566 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002567 WARN("PATCH_PREFIX",
2568 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002569 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002570
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002571 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002572 ERROR("MODIFIED_INCLUDE_ASM",
2573 "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 -07002574 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002575 $found_file = 1;
2576 }
2577
Joe Perches34d88152015-06-25 15:03:05 -07002578#make up the handle for any error we report on this line
2579 if ($showfile) {
2580 $prefix = "$realfile:$realline: "
2581 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002582 if ($file) {
2583 $prefix = "$filename:$realline: ";
2584 } else {
2585 $prefix = "$filename:$linenr: ";
2586 }
Joe Perches34d88152015-06-25 15:03:05 -07002587 }
2588
Joe Perches2ac73b42014-06-04 16:12:05 -07002589 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002590 if (is_maintained_obsolete($realfile)) {
2591 WARN("OBSOLETE",
2592 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2593 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002594 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002595 $check = 1;
2596 } else {
2597 $check = $check_orig;
2598 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002599 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002600
2601 if ($realfile !~ /^MAINTAINERS/) {
2602 my $last_binding_patch = $is_binding_patch;
2603
2604 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2605
2606 if (($last_binding_patch != -1) &&
2607 ($last_binding_patch ^ $is_binding_patch)) {
2608 WARN("DT_SPLIT_BINDING_PATCH",
Mauro Carvalho Chehab858e6842020-04-15 16:45:25 +02002609 "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 -07002610 }
2611 }
2612
Andy Whitcroft773647a2008-03-28 14:15:58 -07002613 next;
2614 }
2615
Randy Dunlap389834b2007-06-08 13:47:03 -07002616 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002617
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002618 my $hereline = "$here\n$rawline\n";
2619 my $herecurr = "$here\n$rawline\n";
2620 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002621
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002622 $cnt_lines++ if ($realcnt != 0);
2623
Joe Perches490b2922018-08-21 21:58:01 -07002624# Verify the existence of a commit log if appropriate
2625# 2 is used because a $signature is counted in $commit_log_lines
2626 if ($in_commit_log) {
2627 if ($line !~ /^\s*$/) {
2628 $commit_log_lines++; #could be a $signature
2629 }
2630 } elsif ($has_commit_log && $commit_log_lines < 2) {
2631 WARN("COMMIT_MESSAGE",
2632 "Missing commit description - Add an appropriate one\n");
2633 $commit_log_lines = 2; #warn only once
2634 }
2635
Joe Perchese518e9a2015-06-25 15:03:27 -07002636# Check if the commit log has what seems like a diff which can confuse patch
2637 if ($in_commit_log && !$commit_log_has_diff &&
2638 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2639 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2640 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2641 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2642 ERROR("DIFF_IN_COMMIT_MSG",
2643 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2644 $commit_log_has_diff = 1;
2645 }
2646
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002647# Check for incorrect file permissions
2648 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2649 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002650 if ($realfile !~ m@scripts/@ &&
2651 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002652 ERROR("EXECUTE_PERMISSIONS",
2653 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002654 }
2655 }
2656
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002657# Check the patch for a From:
2658 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2659 $author = $1;
2660 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2661 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002662 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002663 }
2664
Joe Perches20112472011-07-25 17:13:23 -07002665# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002666 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002667 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002668 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002669 if ($author ne '') {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002670 if (same_email_addresses($1, $author)) {
2671 $authorsignoff = 1;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002672 }
2673 }
Joe Perches20112472011-07-25 17:13:23 -07002674 }
2675
Joe Perches44d303e2020-04-06 20:11:10 -07002676# Check for patch separator
2677 if ($line =~ /^---$/) {
2678 $has_patch_separator = 1;
2679 $in_commit_log = 0;
2680 }
2681
Joe Perchese0d975b2014-12-10 15:51:49 -08002682# Check if MAINTAINERS is being updated. If so, there's probably no need to
2683# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2684 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2685 $reported_maintainer_file = 1;
2686 }
2687
Joe Perches20112472011-07-25 17:13:23 -07002688# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002689 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002690 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002691 my $space_before = $1;
2692 my $sign_off = $2;
2693 my $space_after = $3;
2694 my $email = $4;
2695 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2696
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002697 if ($sign_off !~ /$signature_tags/) {
2698 WARN("BAD_SIGN_OFF",
2699 "Non-standard signature: $sign_off\n" . $herecurr);
2700 }
Joe Perches20112472011-07-25 17:13:23 -07002701 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002702 if (WARN("BAD_SIGN_OFF",
2703 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2704 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002705 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002706 "$ucfirst_sign_off $email";
2707 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002708 }
Joe Perches20112472011-07-25 17:13:23 -07002709 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002710 if (WARN("BAD_SIGN_OFF",
2711 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2712 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002713 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002714 "$ucfirst_sign_off $email";
2715 }
2716
Joe Perches20112472011-07-25 17:13:23 -07002717 }
2718 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002719 if (WARN("BAD_SIGN_OFF",
2720 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2721 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002722 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002723 "$ucfirst_sign_off $email";
2724 }
Joe Perches20112472011-07-25 17:13:23 -07002725 }
2726
Joe Perchesdfa05c22020-04-06 20:10:48 -07002727 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Joe Perches20112472011-07-25 17:13:23 -07002728 my $suggested_email = format_email(($email_name, $email_address));
2729 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002730 ERROR("BAD_SIGN_OFF",
2731 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002732 } else {
2733 my $dequoted = $suggested_email;
2734 $dequoted =~ s/^"//;
2735 $dequoted =~ s/" </ </;
2736 # Don't force email to have quotes
2737 # Allow just an angle bracketed address
Joe Perchesdfa05c22020-04-06 20:10:48 -07002738 if (!same_email_addresses($email, $suggested_email)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002739 WARN("BAD_SIGN_OFF",
2740 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002741 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002742 }
Joe Perches7e51f192013-09-11 14:23:57 -07002743
2744# Check for duplicate signatures
2745 my $sig_nospace = $line;
2746 $sig_nospace =~ s/\s//g;
2747 $sig_nospace = lc($sig_nospace);
2748 if (defined $signatures{$sig_nospace}) {
2749 WARN("BAD_SIGN_OFF",
2750 "Duplicate signature\n" . $herecurr);
2751 } else {
2752 $signatures{$sig_nospace} = 1;
2753 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002754
2755# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2756 if ($sign_off =~ /^co-developed-by:$/i) {
2757 if ($email eq $author) {
2758 WARN("BAD_SIGN_OFF",
2759 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2760 }
2761 if (!defined $lines[$linenr]) {
2762 WARN("BAD_SIGN_OFF",
2763 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2764 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2765 WARN("BAD_SIGN_OFF",
2766 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2767 } elsif ($1 ne $email) {
2768 WARN("BAD_SIGN_OFF",
2769 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2770 }
2771 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002772 }
2773
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002774# Check email subject for common tools that don't need to be mentioned
2775 if ($in_header_lines &&
2776 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2777 WARN("EMAIL_SUBJECT",
2778 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2779 }
2780
Joe Perches44d303e2020-04-06 20:11:10 -07002781# Check for Gerrit Change-Ids not in any patch context
2782 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002783 ERROR("GERRIT_CHANGE_ID",
Joe Perches44d303e2020-04-06 20:11:10 -07002784 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002785 }
2786
Joe Perches369c8dd2015-11-06 16:31:34 -08002787# Check if the commit log is in a possible stack dump
2788 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2789 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2790 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2791 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07002792 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2793 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2794 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2795 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08002796 $commit_log_possible_stack_dump = 1;
2797 }
2798
Joe Perches2a076f42015-04-16 12:44:28 -07002799# Check for line lengths > 75 in commit log, warn once
2800 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002801 length($line) > 75 &&
2802 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2803 # file delta changes
2804 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2805 # filename then :
2806 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2807 # A Fixes: or Link: line
2808 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002809 WARN("COMMIT_LOG_LONG_LINE",
2810 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2811 $commit_log_long_line = 1;
2812 }
2813
Joe Perchesbf4daf12015-09-09 15:37:50 -07002814# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002815 if ($in_commit_log && $commit_log_possible_stack_dump &&
2816 $line =~ /^\s*$/) {
2817 $commit_log_possible_stack_dump = 0;
2818 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002819
Joe Perches0d7835f2015-02-13 14:38:35 -08002820# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002821 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07002822 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002823 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002824 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002825 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002826 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2827 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002828 my $init_char = "c";
2829 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002830 my $short = 1;
2831 my $long = 0;
2832 my $case = 1;
2833 my $space = 1;
2834 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002835 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002836 my $id = '0123456789ab';
2837 my $orig_desc = "commit description";
2838 my $description = "";
2839
Joe Perchesfe043ea2015-09-09 15:37:25 -07002840 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2841 $init_char = $1;
2842 $orig_commit = lc($2);
2843 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2844 $orig_commit = lc($1);
2845 }
2846
Joe Perches0d7835f2015-02-13 14:38:35 -08002847 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2848 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2849 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2850 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2851 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2852 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002853 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002854 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2855 defined $rawlines[$linenr] &&
2856 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2857 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002858 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002859 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2860 defined $rawlines[$linenr] &&
2861 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2862 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2863 $orig_desc = $1;
2864 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2865 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002866 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002867 }
2868
2869 ($id, $description) = git_commit_info($orig_commit,
2870 $id, $orig_desc);
2871
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002872 if (defined($id) &&
2873 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002874 ERROR("GIT_COMMIT_ID",
2875 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2876 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002877 }
2878
Joe Perches13f19372014-08-06 16:10:59 -07002879# Check for added, moved or deleted files
2880 if (!$reported_maintainer_file && !$in_commit_log &&
2881 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2882 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2883 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2884 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002885 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002886 $reported_maintainer_file = 1;
2887 WARN("FILE_PATH_CHANGES",
2888 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2889 }
2890
Rob Herringe400edb2019-09-12 15:18:20 +01002891# Check for adding new DT bindings not in schema format
2892 if (!$in_commit_log &&
2893 ($line =~ /^new file mode\s*\d+\s*$/) &&
2894 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2895 WARN("DT_SCHEMA_BINDING_PATCH",
2896 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2897 }
2898
Andy Whitcroft00df3442007-06-08 13:47:06 -07002899# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002900 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002901 ERROR("CORRUPTED_PATCH",
2902 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002903 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002904 }
2905
2906# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2907 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002908 $rawline !~ m/^$UTF8*$/) {
2909 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2910
2911 my $blank = copy_spacing($rawline);
2912 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2913 my $hereptr = "$hereline$ptr\n";
2914
Joe Perches34d99212011-07-25 17:13:26 -07002915 CHK("INVALID_UTF8",
2916 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002917 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002918
Joe Perches15662b32011-10-31 17:13:12 -07002919# Check if it's the start of a commit log
2920# (not a header line and we haven't seen the patch filename)
2921 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002922 !($rawline =~ /^\s+(?:\S|$)/ ||
2923 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002924 $in_header_lines = 0;
2925 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002926 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002927 }
2928
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002929# Check if there is UTF-8 in a commit log when a mail header has explicitly
2930# declined it, i.e defined some charset where it is missing.
2931 if ($in_header_lines &&
2932 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2933 $1 !~ /utf-8/i) {
2934 $non_utf8_charset = 1;
2935 }
2936
2937 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002938 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002939 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002940 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2941 }
2942
Joe Perchesd6430f72016-12-12 16:46:28 -08002943# Check for absolute kernel paths in commit message
2944 if ($tree && $in_commit_log) {
2945 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2946 my $file = $1;
2947
2948 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2949 check_absolute_file($1, $herecurr)) {
2950 #
2951 } else {
2952 check_absolute_file($file, $herecurr);
2953 }
2954 }
2955 }
2956
Kees Cook66b47b42014-10-13 15:51:57 -07002957# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002958 if (defined($misspellings) &&
2959 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002960 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002961 my $typo = $1;
2962 my $typo_fix = $spelling_fix{lc($typo)};
2963 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2964 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002965 my $msg_level = \&WARN;
2966 $msg_level = \&CHK if ($file);
2967 if (&{$msg_level}("TYPO_SPELLING",
2968 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002969 $fix) {
2970 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2971 }
2972 }
2973 }
2974
Matteo Crocea8dd86b2019-09-25 16:46:38 -07002975# check for invalid commit id
2976 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2977 my $id;
2978 my $description;
2979 ($id, $description) = git_commit_info($2, undef, undef);
2980 if (!defined($id)) {
2981 WARN("UNKNOWN_COMMIT_ID",
2982 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2983 }
2984 }
2985
Andy Whitcroft306708542008-10-15 22:02:28 -07002986# ignore non-hunk lines and lines being removed
2987 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002988
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002989#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002990 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002991 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002992 if (ERROR("DOS_LINE_ENDINGS",
2993 "DOS line endings\n" . $herevet) &&
2994 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002995 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002996 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002997 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2998 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002999 if (ERROR("TRAILING_WHITESPACE",
3000 "trailing whitespace\n" . $herevet) &&
3001 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003002 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003003 }
3004
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003005 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003006 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07003007
Josh Triplett4783f892013-11-12 15:10:12 -08003008# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003009 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003010 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003011 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3012 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003013 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003014 my $msg_level = \&ERROR;
3015 $msg_level = \&CHK if ($file);
3016 &{$msg_level}("FSF_MAILING_ADDRESS",
3017 "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 -08003018 }
3019
Andi Kleen33549572010-05-24 14:33:29 -07003020# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003021# Only applies when adding the entry originally, after that we do not have
3022# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003023 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003024 # 'choice' is usually the last thing on the line (though
3025 # Kconfig supports named choices), so use a word boundary
3026 # (\b) rather than a whitespace character (\s)
3027 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003028 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003029 my $cnt = $realcnt;
3030 my $ln = $linenr + 1;
3031 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003032 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003033 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003034 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003035 $f = $lines[$ln - 1];
3036 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3037 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003038
3039 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003040 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003041
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003042 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003043 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01003044 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
3045 if ($lines[$ln - 1] =~ "---help---") {
3046 WARN("CONFIG_DESCRIPTION",
3047 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
3048 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003049 $length = -1;
3050 }
3051
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003052 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003053 $f =~ s/#.*//;
3054 $f =~ s/^\s+//;
3055 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003056
3057 # This only checks context lines in the patch
3058 # and so hopefully shouldn't trigger false
3059 # positives, even though some of these are
3060 # common words in help texts
3061 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3062 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003063 $is_end = 1;
3064 last;
3065 }
Andi Kleen33549572010-05-24 14:33:29 -07003066 $length++;
3067 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003068 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3069 WARN("CONFIG_DESCRIPTION",
3070 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3071 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003072 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003073 }
3074
Joe Perches7ccf41a2020-06-04 16:50:33 -07003075# check MAINTAINERS entries
3076 if ($realfile =~ /^MAINTAINERS$/) {
3077# check MAINTAINERS entries for the right form
3078 if ($rawline =~ /^\+[A-Z]:/ &&
3079 $rawline !~ /^\+[A-Z]:\t\S/) {
3080 if (WARN("MAINTAINERS_STYLE",
3081 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3082 $fix) {
3083 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3084 }
3085 }
3086# check MAINTAINERS entries for the right ordering too
3087 my $preferred_order = 'MRLSWQBCPTFXNK';
3088 if ($rawline =~ /^\+[A-Z]:/ &&
3089 $prevrawline =~ /^[\+ ][A-Z]:/) {
3090 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3091 my $cur = $1;
3092 my $curval = $2;
3093 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3094 my $prev = $1;
3095 my $prevval = $2;
3096 my $curindex = index($preferred_order, $cur);
3097 my $previndex = index($preferred_order, $prev);
3098 if ($curindex < 0) {
3099 WARN("MAINTAINERS_STYLE",
3100 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3101 } else {
3102 if ($previndex >= 0 && $curindex < $previndex) {
3103 WARN("MAINTAINERS_STYLE",
3104 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3105 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3106 ($prev eq 'X' && $cur eq 'X')) &&
3107 ($prevval cmp $curval) > 0) {
3108 WARN("MAINTAINERS_STYLE",
3109 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3110 }
3111 }
Joe Perches628f91a2017-07-10 15:52:07 -07003112 }
3113 }
3114
Christoph Jaeger327953e2015-02-13 14:38:29 -08003115# discourage the use of boolean for type definition attributes of Kconfig options
3116 if ($realfile =~ /Kconfig/ &&
3117 $line =~ /^\+\s*\bboolean\b/) {
3118 WARN("CONFIG_TYPE_BOOLEAN",
3119 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3120 }
3121
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003122 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3123 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3124 my $flag = $1;
3125 my $replacement = {
3126 'EXTRA_AFLAGS' => 'asflags-y',
3127 'EXTRA_CFLAGS' => 'ccflags-y',
3128 'EXTRA_CPPFLAGS' => 'cppflags-y',
3129 'EXTRA_LDFLAGS' => 'ldflags-y',
3130 };
3131
3132 WARN("DEPRECATED_VARIABLE",
3133 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3134 }
3135
Rob Herringbff5da42014-01-23 15:54:51 -08003136# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003137 if (defined $root &&
3138 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3139 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3140
Rob Herringbff5da42014-01-23 15:54:51 -08003141 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3142
Florian Vaussardcc933192014-04-03 14:49:27 -07003143 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003144 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003145
Rob Herringbff5da42014-01-23 15:54:51 -08003146 foreach my $compat (@compats) {
3147 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003148 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3149 my $compat3 = $compat;
3150 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3151 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003152 if ( $? >> 8 ) {
3153 WARN("UNDOCUMENTED_DT_STRING",
3154 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3155 }
3156
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003157 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3158 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003159 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003160 if ( $? >> 8 ) {
3161 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003162 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003163 }
3164 }
3165 }
3166
Rob Herring9f3a8992018-04-10 16:33:13 -07003167# check for using SPDX license tag at beginning of files
3168 if ($realline == $checklicenseline) {
3169 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3170 $checklicenseline = 2;
3171 } elsif ($rawline =~ /^\+/) {
3172 my $comment = "";
3173 if ($realfile =~ /\.(h|s|S)$/) {
3174 $comment = '/*';
3175 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3176 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003177 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003178 $comment = '#';
3179 } elsif ($realfile =~ /\.rst$/) {
3180 $comment = '..';
3181 }
3182
Joe Perchesfdf13692019-03-07 16:28:32 -08003183# check SPDX comment style for .[chsS] files
3184 if ($realfile =~ /\.[chsS]$/ &&
3185 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003186 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003187 WARN("SPDX_LICENSE_TAG",
3188 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3189 }
3190
Rob Herring9f3a8992018-04-10 16:33:13 -07003191 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003192 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3193 WARN("SPDX_LICENSE_TAG",
3194 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003195 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003196 my $spdx_license = $1;
3197 if (!is_SPDX_License_valid($spdx_license)) {
3198 WARN("SPDX_LICENSE_TAG",
3199 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3200 }
Lubomir Rintel50c92902020-04-06 20:11:13 -07003201 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3202 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3203 my $msg_level = \&WARN;
3204 $msg_level = \&CHK if ($file);
3205 if (&{$msg_level}("SPDX_LICENSE_TAG",
3206
3207 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3208 $fix) {
3209 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3210 }
3211 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003212 }
3213 }
3214 }
3215
Andy Whitcroft5368df22008-10-15 22:02:27 -07003216# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003217 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07003218
Joe Perchesa8da38a2019-03-07 16:28:42 -08003219# check for using SPDX-License-Identifier on the wrong line number
3220 if ($realline != $checklicenseline &&
3221 $rawline =~ /\bSPDX-License-Identifier:/ &&
3222 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3223 WARN("SPDX_LICENSE_TAG",
3224 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3225 }
3226
Joe Perches47e0c882015-06-25 15:02:57 -07003227# line length limit (with some exclusions)
3228#
3229# There are a few types of lines that may extend beyond $max_line_length:
3230# logging functions like pr_info that end in a string
3231# lines with a single string
3232# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003233# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003234#
3235# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003236# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003237# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3238# LONG_LINE all other lines longer than $max_line_length
3239#
3240# if LONG_LINE is ignored, the other 2 types are also ignored
3241#
3242
Joe Perchesb4749e92015-07-17 16:24:01 -07003243 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003244 my $msg_type = "LONG_LINE";
3245
3246 # Check the allowed long line types first
3247
3248 # logging functions that end in a string that starts
3249 # before $max_line_length
3250 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3251 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3252 $msg_type = "";
3253
3254 # lines with only strings (w/ possible termination)
3255 # #defines with only strings
3256 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3257 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3258 $msg_type = "";
3259
Joe Perchescc147502017-11-17 15:28:44 -08003260 # More special cases
3261 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3262 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003263 $msg_type = "";
3264
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003265 # URL ($rawline is used in case the URL is in a comment)
3266 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3267 $msg_type = "";
3268
Joe Perches47e0c882015-06-25 15:02:57 -07003269 # Otherwise set the alternate message types
3270
3271 # a comment starts before $max_line_length
3272 } elsif ($line =~ /($;[\s$;]*)$/ &&
3273 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3274 $msg_type = "LONG_LINE_COMMENT"
3275
3276 # a quoted string starts before $max_line_length
3277 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3278 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3279 $msg_type = "LONG_LINE_STRING"
3280 }
3281
3282 if ($msg_type ne "" &&
3283 (show_type("LONG_LINE") || show_type($msg_type))) {
Joe Perchesbdc48fa2020-05-29 16:12:21 -07003284 my $msg_level = \&WARN;
3285 $msg_level = \&CHK if ($file);
3286 &{$msg_level}($msg_type,
3287 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Joe Perches47e0c882015-06-25 15:02:57 -07003288 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003289 }
3290
Andy Whitcroft8905a672007-11-28 16:21:06 -08003291# check for adding lines without a newline.
3292 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003293 WARN("MISSING_EOF_NEWLINE",
3294 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003295 }
3296
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003297# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003298 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003299
3300# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003301# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003302 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3303 $rawline =~ /^\+\s* \s*/) {
3304 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003305 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003306 if (ERROR("CODE_INDENT",
3307 "code indent should use tabs where possible\n" . $herevet) &&
3308 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003309 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003310 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003311 }
3312
Alberto Panizzo08e44362010-03-05 13:43:54 -08003313# check for space before tabs.
3314 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3315 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003316 if (WARN("SPACE_BEFORE_TAB",
3317 "please, no space before tabs\n" . $herevet) &&
3318 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003319 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003320 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003321 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003322 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003323 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003324 }
3325
Joe Perches6a487212018-04-10 16:34:04 -07003326# check for assignments on the start of a line
3327 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3328 CHK("ASSIGNMENT_CONTINUATIONS",
3329 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3330 }
3331
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003332# check for && or || at the start of a line
3333 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3334 CHK("LOGICAL_CONTINUATIONS",
3335 "Logical continuations should be on the previous line\n" . $hereprev);
3336 }
3337
Joe Perchesa91e8992016-05-20 17:04:05 -07003338# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003339 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003340 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003341 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003342 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003343 if (WARN("TABSTOP",
3344 "Statements should start on a tabstop\n" . $herecurr) &&
3345 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003346 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003347 }
3348 }
3349 }
3350
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003351# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003352 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003353 $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 -07003354 $prevline =~ /^\+(\t*)(.*)$/;
3355 my $oldindent = $1;
3356 my $rest = $2;
3357
3358 my $pos = pos_last_openparen($rest);
3359 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003360 $line =~ /^(\+| )([ \t]*)/;
3361 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003362
3363 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003364 "\t" x ($pos / $tabsize) .
3365 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003366 my $goodspaceindent = $oldindent . " " x $pos;
3367
3368 if ($newindent ne $goodtabindent &&
3369 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003370
3371 if (CHK("PARENTHESIS_ALIGNMENT",
3372 "Alignment should match open parenthesis\n" . $hereprev) &&
3373 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003374 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003375 s/^\+[ \t]*/\+$goodtabindent/;
3376 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003377 }
3378 }
3379 }
3380
Joe Perches6ab3a972015-04-16 12:44:05 -07003381# check for space after cast like "(int) foo" or "(struct foo) bar"
3382# avoid checking a few false positives:
3383# "sizeof(<type>)" or "__alignof__(<type>)"
3384# function pointer declarations like "(*foo)(int) = bar;"
3385# structure definitions like "(struct foo) { 0 };"
3386# multiline macros that define functions
3387# known attributes or the __attribute__ keyword
3388 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3389 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003390 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003391 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003392 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003393 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003394 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003395 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003396 }
3397
Joe Perches86406b12015-09-09 15:37:41 -07003398# Block comment styles
3399# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003400 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003401 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003402 $rawline =~ /^\+[ \t]*\*/ &&
3403 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003404 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3405 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3406 }
3407
Joe Perches86406b12015-09-09 15:37:41 -07003408# Block comments use * on subsequent lines
3409 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3410 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003411 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003412 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003413 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003414 WARN("BLOCK_COMMENT_STYLE",
3415 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003416 }
3417
Joe Perches86406b12015-09-09 15:37:41 -07003418# Block comments use */ on trailing lines
3419 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003420 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3421 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3422 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003423 WARN("BLOCK_COMMENT_STYLE",
3424 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003425 }
3426
Joe Perches08eb9b82016-10-11 13:51:50 -07003427# Block comment * alignment
3428 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003429 $line =~ /^\+[ \t]*$;/ && #leading comment
3430 $rawline =~ /^\+[ \t]*\*/ && #leading *
3431 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003432 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003433 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3434 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003435 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003436 if (defined($1)) {
3437 $oldindent = expand_tabs($1);
3438 } else {
3439 $prevrawline =~ m@^\+(.*/?)\*@;
3440 $oldindent = expand_tabs($1);
3441 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003442 $rawline =~ m@^\+([ \t]*)\*@;
3443 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003444 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003445 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003446 WARN("BLOCK_COMMENT_STYLE",
3447 "Block comments should align the * on each line\n" . $hereprev);
3448 }
3449 }
3450
Joe Perches7f619192014-08-06 16:10:39 -07003451# check for missing blank lines after struct/union declarations
3452# with exceptions for various attributes and macros
3453 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3454 $line =~ /^\+/ &&
3455 !($line =~ /^\+\s*$/ ||
3456 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3457 $line =~ /^\+\s*MODULE_/i ||
3458 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3459 $line =~ /^\+[a-z_]*init/ ||
3460 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3461 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003462 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003463 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003464 if (CHK("LINE_SPACING",
3465 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3466 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003467 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003468 }
Joe Perches7f619192014-08-06 16:10:39 -07003469 }
3470
Joe Perches365dd4e2014-08-06 16:10:42 -07003471# check for multiple consecutive blank lines
3472 if ($prevline =~ /^[\+ ]\s*$/ &&
3473 $line =~ /^\+\s*$/ &&
3474 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003475 if (CHK("LINE_SPACING",
3476 "Please don't use multiple blank lines\n" . $hereprev) &&
3477 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003478 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003479 }
3480
Joe Perches365dd4e2014-08-06 16:10:42 -07003481 $last_blank_line = $linenr;
3482 }
3483
Joe Perches3b617e32014-04-03 14:49:28 -07003484# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003485 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3486 # actual declarations
3487 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003488 # function pointer declarations
3489 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003490 # foo bar; where foo is some local typedef or #define
3491 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3492 # known declaration macros
3493 $prevline =~ /^\+\s+$declaration_macros/) &&
3494 # for "else if" which can look like "$Ident $Ident"
3495 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3496 # other possible extensions of declaration lines
3497 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3498 # not starting a section or a macro "\" extended line
3499 $prevline =~ /(?:\{\s*|\\)$/) &&
3500 # looks like a declaration
3501 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003502 # function pointer declarations
3503 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003504 # foo bar; where foo is some local typedef or #define
3505 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3506 # known declaration macros
3507 $sline =~ /^\+\s+$declaration_macros/ ||
3508 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003509 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003510 # start or end of block or continuation of declaration
3511 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3512 # bitfield continuation
3513 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3514 # other possible extensions of declaration lines
3515 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3516 # indentation of previous and current line are the same
3517 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003518 if (WARN("LINE_SPACING",
3519 "Missing a blank line after declarations\n" . $hereprev) &&
3520 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003521 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003522 }
Joe Perches3b617e32014-04-03 14:49:28 -07003523 }
3524
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003525# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003526# Exceptions:
3527# 1) within comments
3528# 2) indented preprocessor commands
3529# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003530 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003531 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003532 if (WARN("LEADING_SPACE",
3533 "please, no spaces at the start of a line\n" . $herevet) &&
3534 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003535 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003536 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003537 }
3538
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003539# check we are in a valid C source file if not then ignore this hunk
3540 next if ($realfile !~ /\.(h|c)$/);
3541
Joe Perches5751a242017-11-17 15:28:52 -08003542# check for unusual line ending [ or (
3543 if ($line =~ /^\+.*([\[\(])\s*$/) {
3544 CHK("OPEN_ENDED_LINE",
3545 "Lines should not end with a '$1'\n" . $herecurr);
3546 }
3547
Joe Perches4dbed762017-05-08 15:55:39 -07003548# check if this appears to be the start function declaration, save the name
3549 if ($sline =~ /^\+\{\s*$/ &&
3550 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3551 $context_function = $1;
3552 }
3553
3554# check if this appears to be the end of function declaration
3555 if ($sline =~ /^\+\}\s*$/) {
3556 undef $context_function;
3557 }
3558
Joe Perches032a4c02014-08-06 16:10:29 -07003559# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003560# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003561# if the previous line is a break or return and is indented 1 tab more...
3562 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3563 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003564 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3565 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3566 defined $lines[$linenr] &&
3567 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003568 WARN("UNNECESSARY_ELSE",
3569 "else is not generally useful after a break or return\n" . $hereprev);
3570 }
3571 }
3572
Joe Perchesc00df192014-08-06 16:11:01 -07003573# check indentation of a line with a break;
3574# if the previous line is a goto or return and is indented the same # of tabs
3575 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3576 my $tabs = $1;
3577 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3578 WARN("UNNECESSARY_BREAK",
3579 "break is not useful after a goto or return\n" . $hereprev);
3580 }
3581 }
3582
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003583# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003584 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003585 WARN("CVS_KEYWORD",
3586 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003587 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003588
Joe Perches56e77d72013-02-21 16:44:14 -08003589# check for old HOTPLUG __dev<foo> section markings
3590 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3591 WARN("HOTPLUG_SECTION",
3592 "Using $1 is unnecessary\n" . $herecurr);
3593 }
3594
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003595# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003596 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3597 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003598#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003599 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003600 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003601 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003602 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003603 $stat =~ s/\n./\n /g;
3604 $cond =~ s/\n./\n /g;
3605
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003606#print "linenr<$linenr> <$stat>\n";
3607 # If this statement has no statement boundaries within
3608 # it there is no point in retrying a statement scan
3609 # until we hit end of it.
3610 my $frag = $stat; $frag =~ s/;+\s*$//;
3611 if ($frag !~ /(?:{|;)/) {
3612#print "skip<$line_nr_next>\n";
3613 $suppress_statement = $line_nr_next;
3614 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003615
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003616 # Find the real next line.
3617 $realline_next = $line_nr_next;
3618 if (defined $realline_next &&
3619 (!defined $lines[$realline_next - 1] ||
3620 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3621 $realline_next++;
3622 }
3623
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003624 my $s = $stat;
3625 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003626
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003627 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003628 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003629
3630 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003631 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003632
Andy Whitcroft463f2862009-09-21 17:04:34 -07003633 } elsif ($s =~ /^.\s*else\b/s) {
3634
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003635 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003636 } 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 -07003637 my $type = $1;
3638 $type =~ s/\s+/ /g;
3639 possible($type, "A:" . $s);
3640
Andy Whitcroft8905a672007-11-28 16:21:06 -08003641 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003642 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003643 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003644 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003645
3646 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003647 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003648 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003649 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003650
3651 # Check for any sort of function declaration.
3652 # int foo(something bar, other baz);
3653 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003654 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 -08003655 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003656
Andy Whitcroftcf655042008-03-04 14:28:20 -08003657 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003658 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003659 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003660
Andy Whitcroft8905a672007-11-28 16:21:06 -08003661 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003662 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003663
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003664 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003665 }
3666 }
3667 }
3668
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003669 }
3670
Andy Whitcroft653d4872007-06-23 17:16:34 -07003671#
3672# Checks which may be anchored in the context.
3673#
3674
3675# Check for switch () and associated case and default
3676# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003677 if ($line=~/\bswitch\s*\(.*\)/) {
3678 my $err = '';
3679 my $sep = '';
3680 my @ctx = ctx_block_outer($linenr, $realcnt);
3681 shift(@ctx);
3682 for my $ctx (@ctx) {
3683 my ($clen, $cindent) = line_stats($ctx);
3684 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3685 $indent != $cindent) {
3686 $err .= "$sep$ctx\n";
3687 $sep = '';
3688 } else {
3689 $sep = "[...]\n";
3690 }
3691 }
3692 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003693 ERROR("SWITCH_CASE_INDENT_LEVEL",
3694 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003695 }
3696 }
3697
3698# if/while/etc brace do not go on next line, unless defining a do while loop,
3699# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003700 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 -07003701 my $pre_ctx = "$1$2";
3702
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003703 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003704
3705 if ($line =~ /^\+\t{6,}/) {
3706 WARN("DEEP_INDENTATION",
3707 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3708 }
3709
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003710 my $ctx_cnt = $realcnt - $#ctx - 1;
3711 my $ctx = join("\n", @ctx);
3712
Andy Whitcroft548596d2008-07-23 21:29:01 -07003713 my $ctx_ln = $linenr;
3714 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003715
Andy Whitcroft548596d2008-07-23 21:29:01 -07003716 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3717 defined $lines[$ctx_ln - 1] &&
3718 $lines[$ctx_ln - 1] =~ /^-/)) {
3719 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3720 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003721 $ctx_ln++;
3722 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003723
Andy Whitcroft53210162008-07-23 21:29:03 -07003724 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3725 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003726
Joe Perchesd752fcc2014-08-06 16:11:05 -07003727 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003728 ERROR("OPEN_BRACE",
3729 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003730 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003731 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003732 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3733 $ctx =~ /\)\s*\;\s*$/ &&
3734 defined $lines[$ctx_ln - 1])
3735 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003736 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3737 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003738 WARN("TRAILING_SEMICOLON",
3739 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003740 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003741 }
3742 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003743 }
3744
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003745# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003746 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 -08003747 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3748 ctx_statement_block($linenr, $realcnt, 0)
3749 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003750 my ($s, $c) = ($stat, $cond);
3751
3752 substr($s, 0, length($c), '');
3753
Joe Perches9f5af482015-09-09 15:37:30 -07003754 # remove inline comments
3755 $s =~ s/$;/ /g;
3756 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003757
3758 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003759 my @newlines = ($c =~ /\n/gs);
3760 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003761
Joe Perches9f5af482015-09-09 15:37:30 -07003762 # Make sure we remove the line prefixes as we have
3763 # none on the first line, and are going to readd them
3764 # where necessary.
3765 $s =~ s/\n./\n/gs;
3766 while ($s =~ /\n\s+\\\n/) {
3767 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3768 }
3769
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003770 # We want to check the first line inside the block
3771 # starting at the end of the conditional, so remove:
3772 # 1) any blank line termination
3773 # 2) any opening brace { on end of the line
3774 # 3) any do (...) {
3775 my $continuation = 0;
3776 my $check = 0;
3777 $s =~ s/^.*\bdo\b//;
3778 $s =~ s/^\s*{//;
3779 if ($s =~ s/^\s*\\//) {
3780 $continuation = 1;
3781 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003782 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003783 $check = 1;
3784 $cond_lines++;
3785 }
3786
3787 # Also ignore a loop construct at the end of a
3788 # preprocessor statement.
3789 if (($prevline =~ /^.\s*#\s*define\s/ ||
3790 $prevline =~ /\\\s*$/) && $continuation == 0) {
3791 $check = 0;
3792 }
3793
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003794 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003795 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003796 while ($cond_ptr != $cond_lines) {
3797 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003798
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003799 # If we see an #else/#elif then the code
3800 # is not linear.
3801 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3802 $check = 0;
3803 }
3804
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003805 # Ignore:
3806 # 1) blank lines, they should be at 0,
3807 # 2) preprocessor lines, and
3808 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003809 if ($continuation ||
3810 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003811 $s =~ /^\s*#\s*?/ ||
3812 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003813 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003814 if ($s =~ s/^.*?\n//) {
3815 $cond_lines++;
3816 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003817 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003818 }
3819
3820 my (undef, $sindent) = line_stats("+" . $s);
3821 my $stat_real = raw_line($linenr, $cond_lines);
3822
3823 # Check if either of these lines are modified, else
3824 # this is not this patch's fault.
3825 if (!defined($stat_real) ||
3826 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3827 $check = 0;
3828 }
3829 if (defined($stat_real) && $cond_lines > 1) {
3830 $stat_real = "[...]\n$stat_real";
3831 }
3832
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003833 #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 -07003834
Joe Perches9f5af482015-09-09 15:37:30 -07003835 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07003836 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07003837 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003838 ($sindent == $indent &&
3839 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07003840 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003841 WARN("SUSPECT_CODE_INDENT",
3842 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003843 }
3844 }
3845
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003846 # Track the 'values' across context and added lines.
3847 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003848 my ($curr_values, $curr_vars) =
3849 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003850 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003851 if ($dbg_values) {
3852 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003853 print "$linenr > .$outline\n";
3854 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003855 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003856 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003857 $prev_values = substr($curr_values, -1);
3858
Andy Whitcroft00df3442007-06-08 13:47:06 -07003859#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003860 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003861
Joe Perches11ca40a2016-12-12 16:46:31 -08003862# check for dereferences that span multiple lines
3863 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3864 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3865 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3866 my $ref = $1;
3867 $line =~ /^.\s*($Lval)/;
3868 $ref .= $1;
3869 $ref =~ s/\s//g;
3870 WARN("MULTILINE_DEREFERENCE",
3871 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3872 }
3873
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003874# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003875 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 -07003876 my $type = $1;
3877 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003878 $var = "" if (!defined $var);
3879 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003880 my $sign = $1;
3881 my $pointer = $2;
3882
3883 $pointer = "" if (!defined $pointer);
3884
3885 if (WARN("UNSPECIFIED_INT",
3886 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3887 $fix) {
3888 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003889 my $comp_pointer = $pointer;
3890 $comp_pointer =~ s/\s//g;
3891 $decl .= $comp_pointer;
3892 $decl = rtrim($decl) if ($var eq "");
3893 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003894 }
3895 }
3896 }
3897
Andy Whitcroft653d4872007-06-23 17:16:34 -07003898# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003899 if ($dbg_type) {
3900 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003901 ERROR("TEST_TYPE",
3902 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003903 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003904 ERROR("TEST_NOT_TYPE",
3905 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003906 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003907 next;
3908 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003909# TEST: allow direct testing of the attribute matcher.
3910 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003911 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003912 ERROR("TEST_ATTR",
3913 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003914 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003915 ERROR("TEST_NOT_ATTR",
3916 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003917 }
3918 next;
3919 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003920
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003921# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003922 if ($line =~ /^.\s*{/ &&
3923 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003924 if (ERROR("OPEN_BRACE",
3925 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003926 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3927 fix_delete_line($fixlinenr - 1, $prevrawline);
3928 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003929 my $fixedline = $prevrawline;
3930 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003931 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003932 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003933 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003934 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003935 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003936 }
3937
Andy Whitcroft653d4872007-06-23 17:16:34 -07003938#
3939# Checks which are anchored on the added line.
3940#
3941
3942# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003943 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003944 my $path = $1;
3945 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003946 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003947 "malformed #include filename\n" . $herecurr);
3948 }
3949 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3950 ERROR("UAPI_INCLUDE",
3951 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003952 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003953 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003954
3955# no C99 // comments
3956 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003957 if (ERROR("C99_COMMENTS",
3958 "do not use C99 // comments\n" . $herecurr) &&
3959 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003960 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003961 if ($line =~ /\/\/(.*)$/) {
3962 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003963 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003964 }
3965 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003966 }
3967 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003968 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003969 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003970
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003971# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3972# the whole statement.
3973#print "APW <$lines[$realline_next - 1]>\n";
3974 if (defined $realline_next &&
3975 exists $lines[$realline_next - 1] &&
3976 !defined $suppress_export{$realline_next} &&
3977 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3978 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003979 # Handle definitions which produce identifiers with
3980 # a prefix:
3981 # XXX(foo);
3982 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003983 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003984 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003985 $name =~ /^${Ident}_$2/) {
3986#print "FOO C name<$name>\n";
3987 $suppress_export{$realline_next} = 1;
3988
3989 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003990 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003991 ^.DEFINE_$Ident\(\Q$name\E\)|
3992 ^.DECLARE_$Ident\(\Q$name\E\)|
3993 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003994 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3995 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003996 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003997#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3998 $suppress_export{$realline_next} = 2;
3999 } else {
4000 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004001 }
4002 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004003 if (!defined $suppress_export{$linenr} &&
4004 $prevline =~ /^.\s*$/ &&
4005 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4006 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4007#print "FOO B <$lines[$linenr - 1]>\n";
4008 $suppress_export{$linenr} = 2;
4009 }
4010 if (defined $suppress_export{$linenr} &&
4011 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004012 WARN("EXPORT_SYMBOL",
4013 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07004014 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004015
Joe Eloff5150bda2010-08-09 17:21:00 -07004016# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004017 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004018 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004019 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07004020 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004021 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004022 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004023 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004024# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08004025 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004026 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08004027 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07004028 $herecurr) &&
4029 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08004030 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004031 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004032 }
4033
Joe Perches18130872014-08-06 16:11:22 -07004034# check for misordered declarations of char/short/int/long with signed/unsigned
4035 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4036 my $tmp = trim($1);
4037 WARN("MISORDERED_TYPE",
4038 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4039 }
4040
Joe Perches809e0822018-08-21 21:58:12 -07004041# check for unnecessary <signed> int declarations of short/long/long long
4042 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4043 my $type = trim($1);
4044 next if ($type !~ /\bint\b/);
4045 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4046 my $new_type = $type;
4047 $new_type =~ s/\b\s*int\s*\b/ /;
4048 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4049 $new_type =~ s/^const\s+//;
4050 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4051 $new_type = "const $new_type" if ($type =~ /^const\b/);
4052 $new_type =~ s/\s+/ /g;
4053 $new_type = trim($new_type);
4054 if (WARN("UNNECESSARY_INT",
4055 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4056 $fix) {
4057 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4058 }
4059 }
4060
Joe Perchescb710ec2010-10-26 14:23:20 -07004061# check for static const char * arrays.
4062 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004063 WARN("STATIC_CONST_CHAR_ARRAY",
4064 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004065 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004066 }
4067
4068# check for initialized const char arrays that should be static const
4069 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4070 if (WARN("STATIC_CONST_CHAR_ARRAY",
4071 "const array should probably be static const\n" . $herecurr) &&
4072 $fix) {
4073 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4074 }
4075 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004076
4077# check for static char foo[] = "bar" declarations.
4078 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004079 WARN("STATIC_CONST_CHAR_ARRAY",
4080 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004081 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004082 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004083
Joe Perchesab7e23f2015-04-16 12:44:22 -07004084# check for const <foo> const where <foo> is not a pointer or array type
4085 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4086 my $found = $1;
4087 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4088 WARN("CONST_CONST",
4089 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4090 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4091 WARN("CONST_CONST",
4092 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4093 }
4094 }
4095
Joe Perches9b0fa602014-04-03 14:49:18 -07004096# check for non-global char *foo[] = {"bar", ...} declarations.
4097 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4098 WARN("STATIC_CONST_CHAR_ARRAY",
4099 "char * array declaration might be better as static const\n" .
4100 $herecurr);
4101 }
4102
Joe Perchesb598b672015-04-16 12:44:36 -07004103# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4104 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4105 my $array = $1;
4106 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4107 my $array_div = $1;
4108 if (WARN("ARRAY_SIZE",
4109 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4110 $fix) {
4111 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4112 }
4113 }
4114 }
4115
Joe Perchesb36190c2014-01-27 17:07:18 -08004116# check for function declarations without arguments like "int foo()"
Joe Perches16b7f3c2020-04-06 20:11:17 -07004117 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Joe Perchesb36190c2014-01-27 17:07:18 -08004118 if (ERROR("FUNCTION_WITHOUT_ARGS",
4119 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4120 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004121 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004122 }
4123 }
4124
Andy Whitcroft653d4872007-06-23 17:16:34 -07004125# check for new typedefs, only function parameters and sparse annotations
4126# make sense.
4127 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004128 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004129 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004130 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004131 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004132 WARN("NEW_TYPEDEFS",
4133 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004134 }
4135
4136# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004137 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004138 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4139 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004140 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004141
Andy Whitcroft65863862009-01-06 14:41:21 -08004142 # Should start with a space.
4143 $to =~ s/^(\S)/ $1/;
4144 # Should not end with a space.
4145 $to =~ s/\s+$//;
4146 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004147 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004148 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004149
Joe Perches3705ce52013-07-03 15:05:31 -07004150## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004151 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004152 if (ERROR("POINTER_LOCATION",
4153 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4154 $fix) {
4155 my $sub_from = $ident;
4156 my $sub_to = $ident;
4157 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004158 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004159 s@\Q$sub_from\E@$sub_to@;
4160 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004161 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004162 }
4163 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4164 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004165 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004166
Andy Whitcroft65863862009-01-06 14:41:21 -08004167 # Should start with a space.
4168 $to =~ s/^(\S)/ $1/;
4169 # Should not end with a space.
4170 $to =~ s/\s+$//;
4171 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004172 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004173 }
4174 # Modifiers should have spaces.
4175 $to =~ s/(\b$Modifier$)/$1 /;
4176
Joe Perches3705ce52013-07-03 15:05:31 -07004177## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004178 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004179 if (ERROR("POINTER_LOCATION",
4180 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4181 $fix) {
4182
4183 my $sub_from = $match;
4184 my $sub_to = $match;
4185 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004186 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004187 s@\Q$sub_from\E@$sub_to@;
4188 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004189 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004190 }
4191
Joe Perches9d3e3c72015-09-09 15:37:27 -07004192# avoid BUG() or BUG_ON()
4193 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004194 my $msg_level = \&WARN;
4195 $msg_level = \&CHK if ($file);
4196 &{$msg_level}("AVOID_BUG",
4197 "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 -07004198 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004199
Joe Perches9d3e3c72015-09-09 15:37:27 -07004200# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004201 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004202 WARN("LINUX_VERSION_CODE",
4203 "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 -08004204 }
4205
Joe Perches17441222011-06-15 15:08:17 -07004206# check for uses of printk_ratelimit
4207 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004208 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004209 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004210 }
4211
Joe Percheseeef5732017-11-17 15:28:41 -08004212# printk should use KERN_* levels
4213 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4214 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4215 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004216 }
4217
Joe Perches243f3802012-05-31 16:26:09 -07004218 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4219 my $orig = $1;
4220 my $level = lc($orig);
4221 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004222 my $level2 = $level;
4223 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004224 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004225 "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 -07004226 }
4227
Joe Perchesdc139312013-02-21 16:44:13 -08004228 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4229 my $orig = $1;
4230 my $level = lc($orig);
4231 $level = "warn" if ($level eq "warning");
4232 $level = "dbg" if ($level eq "debug");
4233 WARN("PREFER_DEV_LEVEL",
4234 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4235 }
4236
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004237# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4238# number of false positives, but assembly files are not checked, so at
4239# least the arch entry code will not trigger this warning.
4240 if ($line =~ /\bENOSYS\b/) {
4241 WARN("ENOSYS",
4242 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4243 }
4244
Jakub Kicinski6b9ea5f2020-05-11 10:08:07 -07004245# ENOTSUPP is not a standard error code and should be avoided in new patches.
4246# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4247# Similarly to ENOSYS warning a small number of false positives is expected.
4248 if (!$file && $line =~ /\bENOTSUPP\b/) {
4249 if (WARN("ENOTSUPP",
4250 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4251 $fix) {
4252 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4253 }
4254 }
4255
Andy Whitcroft653d4872007-06-23 17:16:34 -07004256# function brace can't be on same line, except for #defines of do while,
4257# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004258 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004259 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4260 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4261 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004262 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004263 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004264 $fix) {
4265 fix_delete_line($fixlinenr, $rawline);
4266 my $fixed_line = $rawline;
4267 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4268 my $line1 = $1;
4269 my $line2 = $2;
4270 fix_insert_line($fixlinenr, ltrim($line1));
4271 fix_insert_line($fixlinenr, "\+{");
4272 if ($line2 !~ /^\s*$/) {
4273 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4274 }
4275 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004276 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004277
Andy Whitcroft8905a672007-11-28 16:21:06 -08004278# open braces for enum, union and struct go on the same line.
4279 if ($line =~ /^.\s*{/ &&
4280 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004281 if (ERROR("OPEN_BRACE",
4282 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4283 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4284 fix_delete_line($fixlinenr - 1, $prevrawline);
4285 fix_delete_line($fixlinenr, $rawline);
4286 my $fixedline = rtrim($prevrawline) . " {";
4287 fix_insert_line($fixlinenr, $fixedline);
4288 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004289 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004290 if ($fixedline !~ /^\+\s*$/) {
4291 fix_insert_line($fixlinenr, $fixedline);
4292 }
4293 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004294 }
4295
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004296# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004297 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4298 if (WARN("SPACING",
4299 "missing space after $1 definition\n" . $herecurr) &&
4300 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004301 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004302 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4303 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004304 }
4305
Joe Perches31070b52014-01-23 15:54:49 -08004306# Function pointer declarations
4307# check spacing between type, funcptr, and args
4308# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004309 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004310 my $declare = $1;
4311 my $pre_pointer_space = $2;
4312 my $post_pointer_space = $3;
4313 my $funcname = $4;
4314 my $post_funcname_space = $5;
4315 my $pre_args_space = $6;
4316
Joe Perches91f72e92014-04-03 14:49:12 -07004317# the $Declare variable will capture all spaces after the type
4318# so check it for a missing trailing missing space but pointer return types
4319# don't need a space so don't warn for those.
4320 my $post_declare_space = "";
4321 if ($declare =~ /(\s+)$/) {
4322 $post_declare_space = $1;
4323 $declare = rtrim($declare);
4324 }
4325 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004326 WARN("SPACING",
4327 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004328 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004329 }
4330
4331# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004332# This test is not currently implemented because these declarations are
4333# equivalent to
4334# int foo(int bar, ...)
4335# and this is form shouldn't/doesn't generate a checkpatch warning.
4336#
4337# elsif ($declare =~ /\s{2,}$/) {
4338# WARN("SPACING",
4339# "Multiple spaces after return type\n" . $herecurr);
4340# }
Joe Perches31070b52014-01-23 15:54:49 -08004341
4342# unnecessary space "type ( *funcptr)(args...)"
4343 if (defined $pre_pointer_space &&
4344 $pre_pointer_space =~ /^\s/) {
4345 WARN("SPACING",
4346 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4347 }
4348
4349# unnecessary space "type (* funcptr)(args...)"
4350 if (defined $post_pointer_space &&
4351 $post_pointer_space =~ /^\s/) {
4352 WARN("SPACING",
4353 "Unnecessary space before function pointer name\n" . $herecurr);
4354 }
4355
4356# unnecessary space "type (*funcptr )(args...)"
4357 if (defined $post_funcname_space &&
4358 $post_funcname_space =~ /^\s/) {
4359 WARN("SPACING",
4360 "Unnecessary space after function pointer name\n" . $herecurr);
4361 }
4362
4363# unnecessary space "type (*funcptr) (args...)"
4364 if (defined $pre_args_space &&
4365 $pre_args_space =~ /^\s/) {
4366 WARN("SPACING",
4367 "Unnecessary space before function pointer arguments\n" . $herecurr);
4368 }
4369
4370 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004371 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004372 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004373 }
4374 }
4375
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004376# check for spacing round square brackets; allowed:
4377# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004378# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4379# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004380 while ($line =~ /(.*?\s)\[/g) {
4381 my ($where, $prefix) = ($-[1], $1);
4382 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004383 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004384 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004385 if (ERROR("BRACKET_SPACE",
4386 "space prohibited before open square bracket '['\n" . $herecurr) &&
4387 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004388 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004389 s/^(\+.*?)\s+\[/$1\[/;
4390 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004391 }
4392 }
4393
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004394# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004395 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004396 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004397 my $ctx_before = substr($line, 0, $-[1]);
4398 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004399
4400 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004401 if ($name =~ /^(?:
4402 if|for|while|switch|return|case|
4403 volatile|__volatile__|
4404 __attribute__|format|__extension__|
4405 asm|__asm__)$/x)
4406 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004407 # cpp #define statements have non-optional spaces, ie
4408 # if there is a space between the name and the open
4409 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004410 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004411
4412 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004413 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004414
4415 # If this whole things ends with a type its most
4416 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004417 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004418
4419 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004420 if (WARN("SPACING",
4421 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4422 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004423 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004424 s/\b$name\s+\(/$name\(/;
4425 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004426 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004427 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004428
Andy Whitcroft653d4872007-06-23 17:16:34 -07004429# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004430 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004431 my $fixed_line = "";
4432 my $line_fixed = 0;
4433
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004434 my $ops = qr{
4435 <<=|>>=|<=|>=|==|!=|
4436 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4437 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004438 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004439 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004440 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004441 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004442
4443## print("element count: <" . $#elements . ">\n");
4444## foreach my $el (@elements) {
4445## print("el: <$el>\n");
4446## }
4447
4448 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004449 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004450
Joe Perches3705ce52013-07-03 15:05:31 -07004451 foreach my $el (@elements) {
4452 push(@fix_elements, substr($rawline, $off, length($el)));
4453 $off += length($el);
4454 }
4455
4456 $off = 0;
4457
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004458 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004459 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004460
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004461 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004462
4463 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4464
4465## print("n: <$n> good: <$good>\n");
4466
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004467 $off += length($elements[$n]);
4468
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004469 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004470 my $ca = substr($opline, 0, $off);
4471 my $cc = '';
4472 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4473 $cc = substr($opline, $off + length($elements[$n + 1]));
4474 }
4475 my $cb = "$ca$;$cc";
4476
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004477 my $a = '';
4478 $a = 'V' if ($elements[$n] ne '');
4479 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004480 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004481 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4482 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004483 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004484
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004485 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004486
4487 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004488 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004489 $c = 'V' if ($elements[$n + 2] ne '');
4490 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004491 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004492 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4493 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004494 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004495 } else {
4496 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004497 }
4498
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004499 my $ctx = "${a}x${c}";
4500
4501 my $at = "(ctx:$ctx)";
4502
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004503 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004504 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004505
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004506 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004507 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004508
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004509 # Get the full operator variant.
4510 my $opv = $op . substr($curr_vars, $off, 1);
4511
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004512 # Ignore operators passed as parameters.
4513 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004514 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004515
Andy Whitcroftcf655042008-03-04 14:28:20 -08004516# # Ignore comments
4517# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004518
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004519 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004520 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004521 if ($ctx !~ /.x[WEBC]/ &&
4522 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004523 if (ERROR("SPACING",
4524 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004525 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004526 $line_fixed = 1;
4527 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004528 }
4529
4530 # // is a comment
4531 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004532
Joe Perchesb00e4812014-04-03 14:49:33 -07004533 # : when part of a bitfield
4534 } elsif ($opv eq ':B') {
4535 # skip the bitfield test for now
4536
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004537 # No spaces for:
4538 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004539 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004540 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004541 if (ERROR("SPACING",
4542 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004543 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004544 if (defined $fix_elements[$n + 2]) {
4545 $fix_elements[$n + 2] =~ s/^\s+//;
4546 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004547 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004548 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004549 }
4550
Joe Perches23810972014-12-10 15:51:32 -08004551 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004552 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004553 my $rtrim_before = 0;
4554 my $space_after = 0;
4555 if ($ctx =~ /Wx./) {
4556 if (ERROR("SPACING",
4557 "space prohibited before that '$op' $at\n" . $hereptr)) {
4558 $line_fixed = 1;
4559 $rtrim_before = 1;
4560 }
4561 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004562 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004563 if (ERROR("SPACING",
4564 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004565 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004566 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004567 $space_after = 1;
4568 }
4569 }
4570 if ($rtrim_before || $space_after) {
4571 if ($rtrim_before) {
4572 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4573 } else {
4574 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4575 }
4576 if ($space_after) {
4577 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004578 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004579 }
4580
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004581 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004582 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004583 #warn "'*' is part of type\n";
4584
4585 # unary operators should have a space before and
4586 # none after. May be left adjacent to another
4587 # unary operator, or a cast
4588 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004589 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004590 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004591 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004592 if (ERROR("SPACING",
4593 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004594 if ($n != $last_after + 2) {
4595 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4596 $line_fixed = 1;
4597 }
Joe Perches3705ce52013-07-03 15:05:31 -07004598 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004599 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004600 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004601 # A unary '*' may be const
4602
4603 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004604 if (ERROR("SPACING",
4605 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004606 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004607 if (defined $fix_elements[$n + 2]) {
4608 $fix_elements[$n + 2] =~ s/^\s+//;
4609 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004610 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004611 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004612 }
4613
4614 # unary ++ and unary -- are allowed no space on one side.
4615 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004616 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004617 if (ERROR("SPACING",
4618 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004619 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004620 $line_fixed = 1;
4621 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004622 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004623 if ($ctx =~ /Wx[BE]/ ||
4624 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004625 if (ERROR("SPACING",
4626 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004627 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004628 $line_fixed = 1;
4629 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004630 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004631 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004632 if (ERROR("SPACING",
4633 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004634 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004635 if (defined $fix_elements[$n + 2]) {
4636 $fix_elements[$n + 2] =~ s/^\s+//;
4637 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004638 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004639 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004640 }
4641
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004642 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004643 } elsif ($op eq '<<' or $op eq '>>' or
4644 $op eq '&' or $op eq '^' or $op eq '|' or
4645 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004646 $op eq '*' or $op eq '/' or
4647 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004648 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004649 if ($check) {
4650 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4651 if (CHK("SPACING",
4652 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4653 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4654 $fix_elements[$n + 2] =~ s/^\s+//;
4655 $line_fixed = 1;
4656 }
4657 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4658 if (CHK("SPACING",
4659 "space preferred before that '$op' $at\n" . $hereptr)) {
4660 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4661 $line_fixed = 1;
4662 }
4663 }
4664 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004665 if (ERROR("SPACING",
4666 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004667 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4668 if (defined $fix_elements[$n + 2]) {
4669 $fix_elements[$n + 2] =~ s/^\s+//;
4670 }
Joe Perches3705ce52013-07-03 15:05:31 -07004671 $line_fixed = 1;
4672 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004673 }
4674
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004675 # A colon needs no spaces before when it is
4676 # terminating a case value or a label.
4677 } elsif ($opv eq ':C' || $opv eq ':L') {
4678 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004679 if (ERROR("SPACING",
4680 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004681 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004682 $line_fixed = 1;
4683 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004684 }
4685
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004686 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004687 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004688 my $ok = 0;
4689
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004690 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004691 if (($op eq '<' &&
4692 $cc =~ /^\S+\@\S+>/) ||
4693 ($op eq '>' &&
4694 $ca =~ /<\S+\@\S+$/))
4695 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07004696 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004697 }
4698
Joe Perchese0df7e12015-04-16 12:44:53 -07004699 # for asm volatile statements
4700 # ignore a colon with another
4701 # colon immediately before or after
4702 if (($op eq ':') &&
4703 ($ca =~ /:$/ || $cc =~ /^:/)) {
4704 $ok = 1;
4705 }
4706
Joe Perches84731622013-11-12 15:10:05 -08004707 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004708 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004709 my $msg_level = \&ERROR;
4710 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004711
Jean Delvare0675a8f2017-09-08 16:16:07 -07004712 if (&{$msg_level}("SPACING",
4713 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004714 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4715 if (defined $fix_elements[$n + 2]) {
4716 $fix_elements[$n + 2] =~ s/^\s+//;
4717 }
Joe Perches3705ce52013-07-03 15:05:31 -07004718 $line_fixed = 1;
4719 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004720 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004721 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004722 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004723
4724## print("n: <$n> GOOD: <$good>\n");
4725
4726 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004727 }
Joe Perches3705ce52013-07-03 15:05:31 -07004728
4729 if (($#elements % 2) == 0) {
4730 $fixed_line = $fixed_line . $fix_elements[$#elements];
4731 }
4732
Joe Perches194f66f2014-08-06 16:11:03 -07004733 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4734 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004735 }
4736
4737
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004738 }
4739
Joe Perches786b6322013-07-03 15:05:32 -07004740# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004741 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004742 if (WARN("SPACING",
4743 "space prohibited before semicolon\n" . $herecurr) &&
4744 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004745 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004746 s/^(\+.*\S)\s+;/$1;/;
4747 }
4748 }
4749
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004750# check for multiple assignments
4751 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004752 CHK("MULTIPLE_ASSIGNMENTS",
4753 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004754 }
4755
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004756## # check for multiple declarations, allowing for a function declaration
4757## # continuation.
4758## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4759## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4760##
4761## # Remove any bracketed sections to ensure we do not
4762## # falsly report the parameters of functions.
4763## my $ln = $line;
4764## while ($ln =~ s/\([^\(\)]*\)//g) {
4765## }
4766## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004767## WARN("MULTIPLE_DECLARATION",
4768## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004769## }
4770## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004771
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004772#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004773 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004774 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004775 if (ERROR("SPACING",
4776 "space required before the open brace '{'\n" . $herecurr) &&
4777 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004778 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004779 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004780 }
4781
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004782## # check for blank lines before declarations
4783## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4784## $prevrawline =~ /^.\s*$/) {
4785## WARN("SPACING",
4786## "No blank lines before declarations\n" . $hereprev);
4787## }
4788##
4789
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004790# closing brace should have a space following it when it has anything
4791# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07004792 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004793 if (ERROR("SPACING",
4794 "space required after that close brace '}'\n" . $herecurr) &&
4795 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004796 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004797 s/}((?!(?:,|;|\)))\S)/} $1/;
4798 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004799 }
4800
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004801# check spacing on square brackets
4802 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004803 if (ERROR("SPACING",
4804 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4805 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004806 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004807 s/\[\s+/\[/;
4808 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004809 }
4810 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004811 if (ERROR("SPACING",
4812 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4813 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004814 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004815 s/\s+\]/\]/;
4816 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004817 }
4818
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004819# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004820 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4821 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004822 if (ERROR("SPACING",
4823 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4824 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004825 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004826 s/\(\s+/\(/;
4827 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004828 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004829 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004830 $line !~ /for\s*\(.*;\s+\)/ &&
4831 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004832 if (ERROR("SPACING",
4833 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4834 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004835 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004836 s/\s+\)/\)/;
4837 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004838 }
4839
Joe Perchese2826fd2014-08-06 16:10:48 -07004840# check unnecessary parentheses around addressof/dereference single $Lvals
4841# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4842
4843 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004844 my $var = $1;
4845 if (CHK("UNNECESSARY_PARENTHESES",
4846 "Unnecessary parentheses around $var\n" . $herecurr) &&
4847 $fix) {
4848 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4849 }
4850 }
4851
4852# check for unnecessary parentheses around function pointer uses
4853# ie: (foo->bar)(); should be foo->bar();
4854# but not "if (foo->bar) (" to avoid some false positives
4855 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4856 my $var = $2;
4857 if (CHK("UNNECESSARY_PARENTHESES",
4858 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4859 $fix) {
4860 my $var2 = deparenthesize($var);
4861 $var2 =~ s/\s//g;
4862 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4863 }
4864 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004865
Joe Perches63b7c732017-09-08 16:16:01 -07004866# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004867# when !drivers/staging or command-line uses --strict
4868 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004869 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004870 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4871 my $if_stat = $1;
4872 my $test = substr($2, 1, -1);
4873 my $herectx;
4874 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4875 my $match = $1;
4876 # avoid parentheses around potential macro args
4877 next if ($match =~ /^\s*\w+\s*$/);
4878 if (!defined($herectx)) {
4879 $herectx = $here . "\n";
4880 my $cnt = statement_rawlines($if_stat);
4881 for (my $n = 0; $n < $cnt; $n++) {
4882 my $rl = raw_line($linenr, $n);
4883 $herectx .= $rl . "\n";
4884 last if $rl =~ /^[ \+].*\{/;
4885 }
4886 }
4887 CHK("UNNECESSARY_PARENTHESES",
4888 "Unnecessary parentheses around '$match'\n" . $herectx);
4889 }
4890 }
4891
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004892#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004893 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004894 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004895 if (WARN("INDENTED_LABEL",
4896 "labels should not be indented\n" . $herecurr) &&
4897 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004898 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004899 s/^(.)\s+/$1/;
4900 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004901 }
4902
Joe Perches5b9553a2014-04-03 14:49:21 -07004903# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004904 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004905 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004906 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004907 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4908 my $value = $1;
4909 $value = deparenthesize($value);
4910 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4911 ERROR("RETURN_PARENTHESES",
4912 "return is not a function, parentheses are not required\n" . $herecurr);
4913 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004914 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004915 ERROR("SPACING",
4916 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004917 }
4918 }
Joe Perches507e5142013-11-12 15:10:13 -08004919
Joe Perchesb43ae212014-06-23 13:22:07 -07004920# unnecessary return in a void function
4921# at end-of-function, with the previous line a single leading tab, then return;
4922# and the line before that not a goto label target like "out:"
4923 if ($sline =~ /^[ \+]}\s*$/ &&
4924 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4925 $linenr >= 3 &&
4926 $lines[$linenr - 3] =~ /^[ +]/ &&
4927 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004928 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004929 "void function return statements are not generally useful\n" . $hereprev);
4930 }
Joe Perches9819cf22014-06-04 16:12:09 -07004931
Joe Perches189248d2014-01-23 15:54:47 -08004932# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004933 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004934 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4935 my $openparens = $1;
4936 my $count = $openparens =~ tr@\(@\(@;
4937 my $msg = "";
4938 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4939 my $comp = $4; #Not $1 because of $LvalOrFunc
4940 $msg = " - maybe == should be = ?" if ($comp eq "==");
4941 WARN("UNNECESSARY_PARENTHESES",
4942 "Unnecessary parentheses$msg\n" . $herecurr);
4943 }
4944 }
4945
Joe Perchesc5595fa2015-09-09 15:37:58 -07004946# comparisons with a constant or upper case identifier on the left
4947# avoid cases like "foo + BAR < baz"
4948# only fix matches surrounded by parentheses to avoid incorrect
4949# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004950 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004951 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4952 my $lead = $1;
4953 my $const = $2;
4954 my $comp = $3;
4955 my $to = $4;
4956 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004957 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004958 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4959 WARN("CONSTANT_COMPARISON",
4960 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4961 $fix) {
4962 if ($comp eq "<") {
4963 $newcomp = ">";
4964 } elsif ($comp eq "<=") {
4965 $newcomp = ">=";
4966 } elsif ($comp eq ">") {
4967 $newcomp = "<";
4968 } elsif ($comp eq ">=") {
4969 $newcomp = "<=";
4970 }
4971 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4972 }
4973 }
4974
Joe Perchesf34e4a42015-04-16 12:44:19 -07004975# Return of what appears to be an errno should normally be negative
4976 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004977 my $name = $1;
4978 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004979 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004980 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004981 }
4982 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004983
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004984# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004985 if ($line =~ /\b(if|while|for|switch)\(/) {
4986 if (ERROR("SPACING",
4987 "space required before the open parenthesis '('\n" . $herecurr) &&
4988 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004989 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004990 s/\b(if|while|for|switch)\(/$1 \(/;
4991 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004992 }
4993
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004994# Check for illegal assignment in if conditional -- and check for trailing
4995# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004996 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004997 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4998 ctx_statement_block($linenr, $realcnt, 0)
4999 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005000 my ($stat_next) = ctx_statement_block($line_nr_next,
5001 $remain_next, $off_next);
5002 $stat_next =~ s/\n./\n /g;
5003 ##print "stat<$stat> stat_next<$stat_next>\n";
5004
5005 if ($stat_next =~ /^\s*while\b/) {
5006 # If the statement carries leading newlines,
5007 # then count those as offsets.
5008 my ($whitespace) =
5009 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5010 my $offset =
5011 statement_rawlines($whitespace) - 1;
5012
5013 $suppress_whiletrailers{$line_nr_next +
5014 $offset} = 1;
5015 }
5016 }
5017 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08005018 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07005019 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005020 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005021
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08005022 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005023 ERROR("ASSIGN_IN_IF",
5024 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005025 }
5026
5027 # Find out what is on the end of the line after the
5028 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005029 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08005030 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005031 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07005032 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5033 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005034 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005035 # Find out how long the conditional actually is.
5036 my @newlines = ($c =~ /\n/gs);
5037 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005038 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005039
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08005040 $stat_real = raw_line($linenr, $cond_lines)
5041 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07005042 if (defined($stat_real) && $cond_lines > 1) {
5043 $stat_real = "[...]\n$stat_real";
5044 }
5045
Joe Perches000d1cc12011-07-25 17:13:25 -07005046 ERROR("TRAILING_STATEMENTS",
5047 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005048 }
5049 }
5050
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005051# Check for bitwise tests written as boolean
5052 if ($line =~ /
5053 (?:
5054 (?:\[|\(|\&\&|\|\|)
5055 \s*0[xX][0-9]+\s*
5056 (?:\&\&|\|\|)
5057 |
5058 (?:\&\&|\|\|)
5059 \s*0[xX][0-9]+\s*
5060 (?:\&\&|\|\||\)|\])
5061 )/x)
5062 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005063 WARN("HEXADECIMAL_BOOLEAN_TEST",
5064 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005065 }
5066
Andy Whitcroft8905a672007-11-28 16:21:06 -08005067# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005068 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5069 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07005070 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005071 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005072 ERROR("TRAILING_STATEMENTS",
5073 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005074 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005075 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005076# if should not continue a brace
5077 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005078 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005079 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005080 $herecurr);
5081 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005082# case and default should not have general statements after them
5083 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5084 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005085 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005086 \s*return\s+
5087 )/xg)
5088 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005089 ERROR("TRAILING_STATEMENTS",
5090 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005091 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005092
5093 # Check for }<nl>else {, these must be at the same
5094 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005095 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5096 $previndent == $indent) {
5097 if (ERROR("ELSE_AFTER_BRACE",
5098 "else should follow close brace '}'\n" . $hereprev) &&
5099 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5100 fix_delete_line($fixlinenr - 1, $prevrawline);
5101 fix_delete_line($fixlinenr, $rawline);
5102 my $fixedline = $prevrawline;
5103 $fixedline =~ s/}\s*$//;
5104 if ($fixedline !~ /^\+\s*$/) {
5105 fix_insert_line($fixlinenr, $fixedline);
5106 }
5107 $fixedline = $rawline;
5108 $fixedline =~ s/^(.\s*)else/$1} else/;
5109 fix_insert_line($fixlinenr, $fixedline);
5110 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005111 }
5112
Joe Perches8b8856f2014-08-06 16:11:14 -07005113 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5114 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005115 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5116
5117 # Find out what is on the end of the line after the
5118 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005119 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005120 $s =~ s/\n.*//g;
5121
5122 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005123 if (ERROR("WHILE_AFTER_BRACE",
5124 "while should follow close brace '}'\n" . $hereprev) &&
5125 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5126 fix_delete_line($fixlinenr - 1, $prevrawline);
5127 fix_delete_line($fixlinenr, $rawline);
5128 my $fixedline = $prevrawline;
5129 my $trailing = $rawline;
5130 $trailing =~ s/^\+//;
5131 $trailing = trim($trailing);
5132 $fixedline =~ s/}\s*$/} $trailing/;
5133 fix_insert_line($fixlinenr, $fixedline);
5134 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005135 }
5136 }
5137
Joe Perches95e2c602013-07-03 15:05:20 -07005138#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005139 while ($line =~ m{($Constant|$Lval)}g) {
5140 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005141
Joe Perches95e2c602013-07-03 15:05:20 -07005142#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005143 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005144 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005145#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005146 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005147#Ignore SI style variants like nS, mV and dB
5148#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5149 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005150#Ignore some three character SI units explicitly, like MiB and KHz
5151 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005152 while ($var =~ m{($Ident)}g) {
5153 my $word = $1;
5154 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005155 if ($check) {
5156 seed_camelcase_includes();
5157 if (!$file && !$camelcase_file_seeded) {
5158 seed_camelcase_file($realfile);
5159 $camelcase_file_seeded = 1;
5160 }
5161 }
Joe Perches7e781f62013-09-11 14:23:55 -07005162 if (!defined $camelcase{$word}) {
5163 $camelcase{$word} = 1;
5164 CHK("CAMELCASE",
5165 "Avoid CamelCase: <$word>\n" . $herecurr);
5166 }
Joe Perches34456862013-07-03 15:05:34 -07005167 }
Joe Perches323c1262012-12-17 16:02:07 -08005168 }
5169 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005170
5171#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005172 if ($line =~ /\#\s*define.*\\\s+$/) {
5173 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5174 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5175 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005176 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005177 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005178 }
5179
Fabian Frederick0e212e02015-04-16 12:44:25 -07005180# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5181# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005182 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005183 my $file = "$1.h";
5184 my $checkfile = "include/linux/$file";
5185 if (-f "$root/$checkfile" &&
5186 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005187 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005188 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005189 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5190 if ($asminclude > 0) {
5191 if ($realfile =~ m{^arch/}) {
5192 CHK("ARCH_INCLUDE_LINUX",
5193 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5194 } else {
5195 WARN("INCLUDE_LINUX",
5196 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5197 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005198 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005199 }
5200 }
5201
Andy Whitcroft653d4872007-06-23 17:16:34 -07005202# multi-statement macros should be enclosed in a do while loop, grab the
5203# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005204# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005205 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5206 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005207 my $ln = $linenr;
5208 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005209 my ($off, $dstat, $dcond, $rest);
5210 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005211 my $has_flow_statement = 0;
5212 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005213 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005214 ctx_statement_block($linenr, $realcnt, 0);
5215 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005216 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005217 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005218
Joe Perches08a28432014-10-13 15:51:55 -07005219 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005220 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005221
Joe Perchesf59b64b2016-10-11 13:52:08 -07005222 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5223 my $define_args = $1;
5224 my $define_stmt = $dstat;
5225 my @def_args = ();
5226
5227 if (defined $define_args && $define_args ne "") {
5228 $define_args = substr($define_args, 1, length($define_args) - 2);
5229 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005230 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005231 @def_args = split(",", $define_args);
5232 }
5233
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005234 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005235 $dstat =~ s/\\\n.//g;
5236 $dstat =~ s/^\s*//s;
5237 $dstat =~ s/\s*$//s;
5238
5239 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005240 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5241 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005242 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005243 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005244 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005245
Antonio Borneo342d3d22020-04-06 20:11:01 -07005246 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005247 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5248 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005249 {
5250 }
5251
Joe Perches42e15292016-03-15 14:58:01 -07005252 # Make asm volatile uses seem like a generic function
5253 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5254
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005255 my $exceptions = qr{
5256 $Declare|
5257 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005258 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005259 DECLARE_PER_CPU|
5260 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005261 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005262 union|
5263 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005264 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005265 ^\"|\"$|
5266 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005267 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005268 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005269
5270 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005271 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005272 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005273
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005274 if ($dstat ne '' &&
5275 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5276 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005277 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005278 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005279 $dstat !~ /$exceptions/ &&
5280 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005281 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005282 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005283 $dstat !~ /^for\s*$Constant$/ && # for (...)
5284 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5285 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005286 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005287 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005288 {
Joe Perchese7955562017-05-08 15:55:48 -07005289 if ($dstat =~ /^\s*if\b/) {
5290 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5291 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5292 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005293 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5294 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5295 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005296 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005297 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005298 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005299
5300 }
Joe Perches52076492016-10-11 13:52:14 -07005301
5302 # Make $define_stmt single line, comment-free, etc
5303 my @stmt_array = split('\n', $define_stmt);
5304 my $first = 1;
5305 $define_stmt = "";
5306 foreach my $l (@stmt_array) {
5307 $l =~ s/\\$//;
5308 if ($first) {
5309 $define_stmt = $l;
5310 $first = 0;
5311 } elsif ($l =~ /^[\+ ]/) {
5312 $define_stmt .= substr($l, 1);
5313 }
5314 }
5315 $define_stmt =~ s/$;//g;
5316 $define_stmt =~ s/\s+/ /g;
5317 $define_stmt = trim($define_stmt);
5318
Joe Perchesf59b64b2016-10-11 13:52:08 -07005319# check if any macro arguments are reused (ignore '...' and 'type')
5320 foreach my $arg (@def_args) {
5321 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005322 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005323 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005324 $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 -07005325 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5326 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005327 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005328 if ($use_cnt > 1) {
5329 CHK("MACRO_ARG_REUSE",
5330 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005331 }
5332# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005333 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005334 ((defined($1) && $1 ne ',') ||
5335 (defined($2) && $2 ne ','))) {
5336 CHK("MACRO_ARG_PRECEDENCE",
5337 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005338 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005339 }
Joe Perches5023d342012-12-17 16:01:47 -08005340
Joe Perches08a28432014-10-13 15:51:55 -07005341# check for macros with flow control, but without ## concatenation
5342# ## concatenation is commonly a macro that defines a function so ignore those
5343 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005344 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005345 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005346
Joe Perches08a28432014-10-13 15:51:55 -07005347 WARN("MACRO_WITH_FLOW_CONTROL",
5348 "Macros with flow control statements should be avoided\n" . "$herectx");
5349 }
5350
Joe Perches481eb482012-12-17 16:01:56 -08005351# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005352
5353 } else {
5354 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005355 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5356 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005357 $line =~ /^\+.*\\$/) {
5358 WARN("LINE_CONTINUATIONS",
5359 "Avoid unnecessary line continuations\n" . $herecurr);
5360 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005361 }
5362
Joe Perchesb13edf72012-07-30 14:41:24 -07005363# do {} while (0) macro tests:
5364# single-statement macros do not need to be enclosed in do while (0) loop,
5365# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005366 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005367 $realfile !~ m@/vmlinux.lds.h$@ &&
5368 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5369 my $ln = $linenr;
5370 my $cnt = $realcnt;
5371 my ($off, $dstat, $dcond, $rest);
5372 my $ctx = '';
5373 ($dstat, $dcond, $ln, $cnt, $off) =
5374 ctx_statement_block($linenr, $realcnt, 0);
5375 $ctx = $dstat;
5376
5377 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005378 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005379
5380 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5381 my $stmts = $2;
5382 my $semis = $3;
5383
5384 $ctx =~ s/\n*$//;
5385 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005386 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005387
Joe Perchesac8e97f2012-08-21 16:15:53 -07005388 if (($stmts =~ tr/;/;/) == 1 &&
5389 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005390 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5391 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5392 }
5393 if (defined $semis && $semis ne "") {
5394 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5395 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5396 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005397 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5398 $ctx =~ s/\n*$//;
5399 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005400 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005401
5402 WARN("TRAILING_SEMICOLON",
5403 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005404 }
5405 }
5406
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005407# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005408 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5409 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005410 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005411 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005412 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5413 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005414 my @allowed = ();
5415 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005416 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005417 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005418 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005419 for my $chunk (@chunks) {
5420 my ($cond, $block) = @{$chunk};
5421
Andy Whitcroft773647a2008-03-28 14:15:58 -07005422 # If the condition carries leading newlines, then count those as offsets.
5423 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5424 my $offset = statement_rawlines($whitespace) - 1;
5425
Joe Perchesaad4f612012-03-23 15:02:19 -07005426 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005427 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5428
5429 # We have looked at and allowed this specific line.
5430 $suppress_ifbraces{$ln + $offset} = 1;
5431
5432 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005433 $ln += statement_rawlines($block) - 1;
5434
Andy Whitcroft773647a2008-03-28 14:15:58 -07005435 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005436
5437 $seen++ if ($block =~ /^\s*{/);
5438
Joe Perchesaad4f612012-03-23 15:02:19 -07005439 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005440 if (statement_lines($cond) > 1) {
5441 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005442 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005443 }
5444 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005445 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005446 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005447 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005448 if (statement_block_size($block) > 1) {
5449 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005450 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005451 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005452 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005453 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005454 if ($seen) {
5455 my $sum_allowed = 0;
5456 foreach (@allowed) {
5457 $sum_allowed += $_;
5458 }
5459 if ($sum_allowed == 0) {
5460 WARN("BRACES",
5461 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5462 } elsif ($sum_allowed != $allow &&
5463 $seen != $allow) {
5464 CHK("BRACES",
5465 "braces {} should be used on all arms of this statement\n" . $herectx);
5466 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005467 }
5468 }
5469 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005470 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005471 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005472 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005473
Andy Whitcroftcf655042008-03-04 14:28:20 -08005474 # Check the pre-context.
5475 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5476 #print "APW: ALLOWED: pre<$1>\n";
5477 $allowed = 1;
5478 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005479
5480 my ($level, $endln, @chunks) =
5481 ctx_statement_full($linenr, $realcnt, $-[0]);
5482
Andy Whitcroftcf655042008-03-04 14:28:20 -08005483 # Check the condition.
5484 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005485 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005486 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005487 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005488 }
5489 if (statement_lines($cond) > 1) {
5490 #print "APW: ALLOWED: cond<$cond>\n";
5491 $allowed = 1;
5492 }
5493 if ($block =~/\b(?:if|for|while)\b/) {
5494 #print "APW: ALLOWED: block<$block>\n";
5495 $allowed = 1;
5496 }
5497 if (statement_block_size($block) > 1) {
5498 #print "APW: ALLOWED: lines block<$block>\n";
5499 $allowed = 1;
5500 }
5501 # Check the post-context.
5502 if (defined $chunks[1]) {
5503 my ($cond, $block) = @{$chunks[1]};
5504 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005505 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005506 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005507 if ($block =~ /^\s*\{/) {
5508 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5509 $allowed = 1;
5510 }
5511 }
5512 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005513 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005514 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005515
Joe Perches000d1cc12011-07-25 17:13:25 -07005516 WARN("BRACES",
5517 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005518 }
5519 }
5520
Joe Perchese4c5bab2017-02-24 15:01:41 -08005521# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005522 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5523 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005524 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5525 }
5526
Joe Perches0979ae62012-12-17 16:01:59 -08005527# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005528 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005529 if (CHK("BRACES",
5530 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5531 $fix && $prevrawline =~ /^\+/) {
5532 fix_delete_line($fixlinenr - 1, $prevrawline);
5533 }
Joe Perches0979ae62012-12-17 16:01:59 -08005534 }
Joe Perches77b9a532013-07-03 15:05:29 -07005535 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005536 if (CHK("BRACES",
5537 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5538 $fix) {
5539 fix_delete_line($fixlinenr, $rawline);
5540 }
Joe Perches0979ae62012-12-17 16:01:59 -08005541 }
5542
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005543# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005544 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5545 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005546 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005547 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005548 }
5549
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005550# Check for user-visible strings broken across lines, which breaks the ability
5551# to grep for the string. Make exceptions when the previous string ends in a
5552# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5553# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005554 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005555 $prevline =~ /"\s*$/ &&
5556 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5557 if (WARN("SPLIT_STRING",
5558 "quoted string split across lines\n" . $hereprev) &&
5559 $fix &&
5560 $prevrawline =~ /^\+.*"\s*$/ &&
5561 $last_coalesced_string_linenr != $linenr - 1) {
5562 my $extracted_string = get_quoted_string($line, $rawline);
5563 my $comma_close = "";
5564 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5565 $comma_close = $1;
5566 }
5567
5568 fix_delete_line($fixlinenr - 1, $prevrawline);
5569 fix_delete_line($fixlinenr, $rawline);
5570 my $fixedline = $prevrawline;
5571 $fixedline =~ s/"\s*$//;
5572 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5573 fix_insert_line($fixlinenr - 1, $fixedline);
5574 $fixedline = $rawline;
5575 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5576 if ($fixedline !~ /\+\s*$/) {
5577 fix_insert_line($fixlinenr, $fixedline);
5578 }
5579 $last_coalesced_string_linenr = $linenr;
5580 }
5581 }
5582
5583# check for missing a space in a string concatenation
5584 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5585 WARN('MISSING_SPACE',
5586 "break quoted strings at a space character\n" . $hereprev);
5587 }
5588
Joe Perchese4b7d302017-05-08 15:55:51 -07005589# check for an embedded function name in a string when the function is known
5590# This does not work very well for -f --file checking as it depends on patch
5591# context providing the function name or a single line form for in-file
5592# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005593 if ($line =~ /^\+.*$String/ &&
5594 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005595 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5596 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005597 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005598 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005599 }
5600
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005601# check for spaces before a quoted newline
5602 if ($rawline =~ /^.*\".*\s\\n/) {
5603 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5604 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5605 $fix) {
5606 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5607 }
5608
5609 }
5610
Joe Perchesf17dba42014-10-13 15:51:51 -07005611# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005612 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5613 if (CHK("CONCATENATED_STRING",
5614 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5615 $fix) {
5616 while ($line =~ /($String)/g) {
5617 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5618 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5619 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5620 }
5621 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005622 }
5623
Joe Perches90ad30e2014-12-10 15:51:59 -08005624# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005625 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005626 if (WARN("STRING_FRAGMENTS",
5627 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5628 $fix) {
5629 while ($line =~ /($String)(?=\s*")/g) {
5630 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5631 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5632 }
5633 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005634 }
5635
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005636# check for non-standard and hex prefixed decimal printf formats
5637 my $show_L = 1; #don't show the same defect twice
5638 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005639 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005640 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005641 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005642 # check for %L
5643 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005644 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005645 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5646 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005647 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005648 # check for %Z
5649 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5650 WARN("PRINTF_Z",
5651 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5652 $show_Z = 0;
5653 }
5654 # check for 0x<decimal>
5655 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5656 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005657 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5658 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005659 }
5660
5661# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005662 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005663 WARN("LINE_CONTINUATIONS",
5664 "Avoid line continuations in quoted strings\n" . $herecurr);
5665 }
5666
Andy Whitcroft00df3442007-06-08 13:47:06 -07005667# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005668 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005669 WARN("IF_0",
5670 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5671 }
5672
5673# warn about #if 1
5674 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5675 WARN("IF_1",
5676 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005677 }
5678
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005679# check for needless "if (<foo>) fn(<foo>)" uses
5680 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005681 my $tested = quotemeta($1);
5682 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5683 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5684 my $func = $1;
5685 if (WARN('NEEDLESS_IF',
5686 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5687 $fix) {
5688 my $do_fix = 1;
5689 my $leading_tabs = "";
5690 my $new_leading_tabs = "";
5691 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5692 $leading_tabs = $1;
5693 } else {
5694 $do_fix = 0;
5695 }
5696 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5697 $new_leading_tabs = $1;
5698 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5699 $do_fix = 0;
5700 }
5701 } else {
5702 $do_fix = 0;
5703 }
5704 if ($do_fix) {
5705 fix_delete_line($fixlinenr - 1, $prevrawline);
5706 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5707 }
5708 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005709 }
5710 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005711
Joe Perchesebfdc402014-08-06 16:10:27 -07005712# check for unnecessary "Out of Memory" messages
5713 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5714 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5715 (defined $1 || defined $3) &&
5716 $linenr > 3) {
5717 my $testval = $2;
5718 my $testline = $lines[$linenr - 3];
5719
5720 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5721# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5722
Joe Perchese29a70f2019-03-07 16:28:35 -08005723 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5724 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005725 WARN("OOM_MESSAGE",
5726 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5727 }
5728 }
5729
Joe Perchesf78d98f2014-10-13 15:52:01 -07005730# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005731 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005732 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5733 my $level = $1;
5734 if (WARN("UNNECESSARY_KERN_LEVEL",
5735 "Possible unnecessary $level\n" . $herecurr) &&
5736 $fix) {
5737 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5738 }
5739 }
5740
Joe Perches45c55e92017-02-24 15:01:31 -08005741# check for logging continuations
5742 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5743 WARN("LOGGING_CONTINUATION",
5744 "Avoid logging continuation uses where feasible\n" . $herecurr);
5745 }
5746
Joe Perchesabb08a52014-12-10 15:51:46 -08005747# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005748 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005749 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5750 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5751 WARN("MASK_THEN_SHIFT",
5752 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5753 }
5754
Joe Perchesb75ac612014-12-10 15:52:02 -08005755# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005756 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005757 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5758 my $val = $1;
5759 my $equal = "!";
5760 $equal = "" if ($4 eq "!=");
5761 if (CHK("COMPARISON_TO_NULL",
5762 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5763 $fix) {
5764 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5765 }
5766 }
5767 }
5768
Joe Perches8716de32013-09-11 14:24:05 -07005769# check for bad placement of section $InitAttribute (e.g.: __initdata)
5770 if ($line =~ /(\b$InitAttribute\b)/) {
5771 my $attr = $1;
5772 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5773 my $ptr = $1;
5774 my $var = $2;
5775 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5776 ERROR("MISPLACED_INIT",
5777 "$attr should be placed after $var\n" . $herecurr)) ||
5778 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5779 WARN("MISPLACED_INIT",
5780 "$attr should be placed after $var\n" . $herecurr))) &&
5781 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005782 $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 -07005783 }
5784 }
5785 }
5786
Joe Perchese970b8842013-11-12 15:10:10 -08005787# check for $InitAttributeData (ie: __initdata) with const
5788 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5789 my $attr = $1;
5790 $attr =~ /($InitAttributePrefix)(.*)/;
5791 my $attr_prefix = $1;
5792 my $attr_type = $2;
5793 if (ERROR("INIT_ATTRIBUTE",
5794 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5795 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005796 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005797 s/$InitAttributeData/${attr_prefix}initconst/;
5798 }
5799 }
5800
5801# check for $InitAttributeConst (ie: __initconst) without const
5802 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5803 my $attr = $1;
5804 if (ERROR("INIT_ATTRIBUTE",
5805 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5806 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005807 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005808 /(^\+\s*(?:static\s+))/;
5809 $lead = rtrim($1);
5810 $lead = "$lead " if ($lead !~ /^\+$/);
5811 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005812 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005813 }
5814 }
5815
Joe Perchesc17893c2015-04-16 12:44:42 -07005816# check for __read_mostly with const non-pointer (should just be const)
5817 if ($line =~ /\b__read_mostly\b/ &&
5818 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5819 if (ERROR("CONST_READ_MOSTLY",
5820 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5821 $fix) {
5822 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5823 }
5824 }
5825
Joe Perchesfbdb8132014-04-03 14:49:14 -07005826# don't use __constant_<foo> functions outside of include/uapi/
5827 if ($realfile !~ m@^include/uapi/@ &&
5828 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5829 my $constant_func = $1;
5830 my $func = $constant_func;
5831 $func =~ s/^__constant_//;
5832 if (WARN("CONSTANT_CONVERSION",
5833 "$constant_func should be $func\n" . $herecurr) &&
5834 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005835 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005836 }
5837 }
5838
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005839# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005840 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005841 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005842 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005843 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005844 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005845 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07005846 }
5847 if ($delay > 2000) {
5848 WARN("LONG_UDELAY",
5849 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005850 }
5851 }
5852
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005853# warn about unexpectedly long msleep's
5854 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5855 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005856 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005857 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005858 }
5859 }
5860
Joe Perches36ec1932013-07-03 15:05:25 -07005861# check for comparisons of jiffies
5862 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5863 WARN("JIFFIES_COMPARISON",
5864 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5865 }
5866
Joe Perches9d7a34a2013-07-03 15:05:26 -07005867# check for comparisons of get_jiffies_64()
5868 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5869 WARN("JIFFIES_COMPARISON",
5870 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5871 }
5872
Andy Whitcroft00df3442007-06-08 13:47:06 -07005873# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005874# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005875# print "#ifdef in C files should be avoided\n";
5876# print "$herecurr";
5877# $clean = 0;
5878# }
5879
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005880# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005881 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005882 if (ERROR("SPACING",
5883 "exactly one space required after that #$1\n" . $herecurr) &&
5884 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005885 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005886 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5887 }
5888
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005889 }
5890
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005891# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005892 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5893 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005894 my $which = $1;
5895 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005896 CHK("UNCOMMENTED_DEFINITION",
5897 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005898 }
5899 }
5900# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005901
5902 my $barriers = qr{
5903 mb|
5904 rmb|
Will Deaconad83ec62019-11-07 14:49:00 +00005905 wmb
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005906 }x;
5907 my $barrier_stems = qr{
5908 mb__before_atomic|
5909 mb__after_atomic|
5910 store_release|
5911 load_acquire|
5912 store_mb|
5913 (?:$barriers)
5914 }x;
5915 my $all_barriers = qr{
5916 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005917 smp_(?:$barrier_stems)|
5918 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005919 }x;
5920
5921 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005922 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005923 WARN("MEMORY_BARRIER",
5924 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005925 }
5926 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005927
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005928 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5929
5930 if ($realfile !~ m@^include/asm-generic/@ &&
5931 $realfile !~ m@/barrier\.h$@ &&
5932 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5933 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5934 WARN("MEMORY_BARRIER",
5935 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5936 }
5937
Joe Perchescb426e92015-06-25 15:02:46 -07005938# check for waitqueue_active without a comment.
5939 if ($line =~ /\bwaitqueue_active\s*\(/) {
5940 if (!ctx_has_comment($first_line, $linenr)) {
5941 WARN("WAITQUEUE_ACTIVE",
5942 "waitqueue_active without comment\n" . $herecurr);
5943 }
5944 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005945
Marco Elver5099a722020-04-01 12:17:14 +02005946# check for data_race without a comment.
5947 if ($line =~ /\bdata_race\s*\(/) {
5948 if (!ctx_has_comment($first_line, $linenr)) {
5949 WARN("DATA_RACE",
5950 "data_race without comment\n" . $herecurr);
5951 }
5952 }
5953
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005954# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005955 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005956 CHK("ARCH_DEFINES",
5957 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005958 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005959
Joe Perches596ed452017-07-12 14:37:02 -07005960# check that the storage class is not after a type
5961 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005962 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005963 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5964 }
5965# Check that the storage class is at the beginning of a declaration
5966 if ($line =~ /\b$Storage\b/ &&
5967 $line !~ /^.\s*$Storage/ &&
5968 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5969 $1 !~ /[\,\)]\s*$/) {
5970 WARN("STORAGE_CLASS",
5971 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005972 }
5973
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005974# check the location of the inline attribute, that it is between
5975# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005976 if ($line =~ /\b$Type\s+$Inline\b/ ||
5977 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005978 ERROR("INLINE_LOCATION",
5979 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005980 }
5981
Andy Whitcroft8905a672007-11-28 16:21:06 -08005982# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005983 if ($realfile !~ m@\binclude/uapi/@ &&
5984 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005985 if (WARN("INLINE",
5986 "plain inline is preferred over $1\n" . $herecurr) &&
5987 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005988 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005989
5990 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005991 }
5992
Joe Perches3d130fd2011-01-12 17:00:00 -08005993# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005994 if ($realfile !~ m@\binclude/uapi/@ &&
5995 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005996 WARN("PREFER_PACKED",
5997 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005998 }
5999
Joe Perches39b7e282011-07-25 17:13:24 -07006000# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08006001 if ($realfile !~ m@\binclude/uapi/@ &&
6002 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006003 WARN("PREFER_ALIGNED",
6004 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07006005 }
6006
Joe Perches462811d2019-09-25 16:46:44 -07006007# Check for __attribute__ section, prefer __section
6008 if ($realfile !~ m@\binclude/uapi/@ &&
6009 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
6010 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6011 my $new = substr($old, 1, -1);
6012 if (WARN("PREFER_SECTION",
6013 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6014 $fix) {
6015 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6016 }
6017 }
6018
Joe Perches5f14d3b2012-01-10 15:09:52 -08006019# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08006020 if ($realfile !~ m@\binclude/uapi/@ &&
6021 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006022 if (WARN("PREFER_PRINTF",
6023 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6024 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006025 $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 -07006026
6027 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08006028 }
6029
Joe Perches6061d942012-03-23 15:02:16 -07006030# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08006031 if ($realfile !~ m@\binclude/uapi/@ &&
6032 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006033 if (WARN("PREFER_SCANF",
6034 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6035 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006036 $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 -07006037 }
Joe Perches6061d942012-03-23 15:02:16 -07006038 }
6039
Joe Perches619a9082014-12-10 15:51:35 -08006040# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006041 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006042 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6043 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6044 $line =~ /\b__weak\b/)) {
6045 ERROR("WEAK_DECLARATION",
6046 "Using weak declarations can have unintended link defects\n" . $herecurr);
6047 }
6048
Tomas Winklerfd39f902016-12-12 16:46:34 -08006049# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006050 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006051 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006052 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6053 my $type = $1;
6054 if ($type =~ /\b($typeC99Typedefs)\b/) {
6055 $type = $1;
6056 my $kernel_type = 'u';
6057 $kernel_type = 's' if ($type =~ /^_*[si]/);
6058 $type =~ /(\d+)/;
6059 $kernel_type .= $1;
6060 if (CHK("PREFER_KERNEL_TYPES",
6061 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6062 $fix) {
6063 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6064 }
6065 }
6066 }
6067
Joe Perches938224b2016-01-20 14:59:15 -08006068# check for cast of C90 native int or longer types constants
6069 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6070 my $cast = $1;
6071 my $const = $2;
6072 if (WARN("TYPECAST_INT_CONSTANT",
6073 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6074 $fix) {
6075 my $suffix = "";
6076 my $newconst = $const;
6077 $newconst =~ s/${Int_type}$//;
6078 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6079 if ($cast =~ /\blong\s+long\b/) {
6080 $suffix .= 'LL';
6081 } elsif ($cast =~ /\blong\b/) {
6082 $suffix .= 'L';
6083 }
6084 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6085 }
6086 }
6087
Joe Perches8f53a9b2010-03-05 13:43:48 -08006088# check for sizeof(&)
6089 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006090 WARN("SIZEOF_ADDRESS",
6091 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006092 }
6093
Joe Perches66c80b62012-07-30 14:41:22 -07006094# check for sizeof without parenthesis
6095 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006096 if (WARN("SIZEOF_PARENTHESIS",
6097 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6098 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006099 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006100 }
Joe Perches66c80b62012-07-30 14:41:22 -07006101 }
6102
Joe Perches88982fe2012-12-17 16:02:00 -08006103# check for struct spinlock declarations
6104 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6105 WARN("USE_SPINLOCK_T",
6106 "struct spinlock should be spinlock_t\n" . $herecurr);
6107 }
6108
Joe Perchesa6962d72013-04-29 16:18:13 -07006109# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006110 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006111 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006112 $fmt =~ s/%%//g;
6113 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006114 if (WARN("PREFER_SEQ_PUTS",
6115 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6116 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006117 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006118 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006119 }
6120 }
6121
Joe Perches478b1792018-04-10 16:33:34 -07006122# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006123 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006124 defined $stat &&
6125 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6126 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006127 my $stat_real;
6128
Joe Perches0b523762017-05-08 15:55:36 -07006129 my $lc = $stat =~ tr@\n@@;
6130 $lc = $lc + $linenr;
6131 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006132 my $specifier;
6133 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006134 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006135 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006136 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6137 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006138
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006139 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006140 $specifier = $1;
6141 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006142 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006143 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006144 ($extension eq "f" &&
6145 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006146 $bad_specifier = $specifier;
6147 last;
6148 }
6149 if ($extension eq "x" && !defined($stat_real)) {
6150 if (!defined($stat_real)) {
6151 $stat_real = get_stat_real($linenr, $lc);
6152 }
6153 WARN("VSPRINTF_SPECIFIER_PX",
6154 "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");
6155 }
6156 }
6157 if ($bad_specifier ne "") {
6158 my $stat_real = get_stat_real($linenr, $lc);
6159 my $ext_type = "Invalid";
6160 my $use = "";
6161 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006162 $use = " - use %pS instead";
6163 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6164 }
6165
6166 WARN("VSPRINTF_POINTER_EXTENSION",
6167 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6168 }
Joe Perches0b523762017-05-08 15:55:36 -07006169 }
6170 }
6171
Andy Whitcroft554e1652012-01-10 15:09:57 -08006172# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006173 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006174 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006175 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006176
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006177 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006178 my $ms_val = $7;
6179 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006180
Andy Whitcroft554e1652012-01-10 15:09:57 -08006181 if ($ms_size =~ /^(0x|)0$/i) {
6182 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006183 "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 -08006184 } elsif ($ms_size =~ /^(0x|)1$/i) {
6185 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006186 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6187 }
6188 }
6189
Joe Perches98a9bba2014-01-23 15:54:52 -08006190# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006191# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006192# defined $stat &&
6193# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6194# if (WARN("PREFER_ETHER_ADDR_COPY",
6195# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6196# $fix) {
6197# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6198# }
6199# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006200
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006201# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006202# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006203# defined $stat &&
6204# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6205# WARN("PREFER_ETHER_ADDR_EQUAL",
6206# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6207# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006208
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006209# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6210# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006211# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006212# defined $stat &&
6213# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6214#
6215# my $ms_val = $7;
6216#
6217# if ($ms_val =~ /^(?:0x|)0+$/i) {
6218# if (WARN("PREFER_ETH_ZERO_ADDR",
6219# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6220# $fix) {
6221# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6222# }
6223# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6224# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6225# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6226# $fix) {
6227# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6228# }
6229# }
6230# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006231
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006232# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006233 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006234 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006235 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006236 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006237 my $call = $1;
6238 my $cast1 = deparenthesize($2);
6239 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006240 my $cast2 = deparenthesize($7);
6241 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006242 my $cast;
6243
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006244 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006245 $cast = "$cast1 or $cast2";
6246 } elsif ($cast1 ne "") {
6247 $cast = $cast1;
6248 } else {
6249 $cast = $cast2;
6250 }
6251 WARN("MINMAX",
6252 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006253 }
6254 }
6255
Joe Perches4a273192012-07-30 14:41:20 -07006256# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006257 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006258 defined $stat &&
6259 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6260 my $min = $1;
6261 my $max = $7;
6262 if ($min eq $max) {
6263 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006264 "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 -07006265 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6266 $min > $max) {
6267 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006268 "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 -07006269 }
6270 }
6271
Joe Perches823b7942013-11-12 15:10:15 -08006272# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006273 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006274 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006275 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006276 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6277 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6278 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6279 my $lc = $stat =~ tr@\n@@;
6280 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006281 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006282 WARN("NAKED_SSCANF",
6283 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6284 }
6285
Joe Perchesafc819a2014-06-04 16:12:08 -07006286# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006287 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006288 defined $stat &&
6289 $line =~ /\bsscanf\b/) {
6290 my $lc = $stat =~ tr@\n@@;
6291 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006292 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006293 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6294 my $format = $6;
6295 my $count = $format =~ tr@%@%@;
6296 if ($count == 1 &&
6297 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6298 WARN("SSCANF_TO_KSTRTO",
6299 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6300 }
6301 }
6302 }
6303
Joe Perches70dc8a42013-09-11 14:23:58 -07006304# check for new externs in .h files.
6305 if ($realfile =~ /\.h$/ &&
6306 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006307 if (CHK("AVOID_EXTERNS",
6308 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006309 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006310 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006311 }
6312 }
6313
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006314# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006315 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006316 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006317 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006318 my $function_name = $1;
6319 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006320
6321 my $s = $stat;
6322 if (defined $cond) {
6323 substr($s, 0, length($cond), '');
6324 }
Kees Cookd8b44b52020-06-03 14:18:09 -07006325 if ($s =~ /^\s*;/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006326 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006327 WARN("AVOID_EXTERNS",
6328 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006329 }
6330
6331 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006332 WARN("FUNCTION_ARGUMENTS",
6333 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006334 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006335
6336 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6337 $stat =~ /^.\s*extern\s+/)
6338 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006339 WARN("AVOID_EXTERNS",
6340 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006341 }
6342
Joe Perchesa0ad7592017-07-10 15:52:19 -07006343# check for function declarations that have arguments without identifier names
6344 if (defined $stat &&
Kees Cookd8b44b52020-06-03 14:18:09 -07006345 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6346 $1 ne "void") {
6347 my $args = trim($1);
Joe Perchesca0d8922016-10-11 13:52:16 -07006348 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6349 my $arg = trim($1);
Kees Cookd8b44b52020-06-03 14:18:09 -07006350 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
Joe Perchesca0d8922016-10-11 13:52:16 -07006351 WARN("FUNCTION_ARGUMENTS",
6352 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6353 }
6354 }
6355 }
6356
Joe Perchesa0ad7592017-07-10 15:52:19 -07006357# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006358 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006359 defined $stat &&
6360 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6361 $context_function = $1;
6362
6363# check for multiline function definition with misplaced open brace
6364 my $ok = 0;
6365 my $cnt = statement_rawlines($stat);
6366 my $herectx = $here . "\n";
6367 for (my $n = 0; $n < $cnt; $n++) {
6368 my $rl = raw_line($linenr, $n);
6369 $herectx .= $rl . "\n";
6370 $ok = 1 if ($rl =~ /^[ \+]\{/);
6371 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6372 last if $rl =~ /^[ \+].*\{/;
6373 }
6374 if (!$ok) {
6375 ERROR("OPEN_BRACE",
6376 "open brace '{' following function definitions go on the next line\n" . $herectx);
6377 }
6378 }
6379
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006380# checks for new __setup's
6381 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6382 my $name = $1;
6383
6384 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006385 CHK("UNDOCUMENTED_SETUP",
Tim Froidcoeur2581ac72020-06-10 18:41:38 -07006386 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006387 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006388 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006389
Joe Perchese29a70f2019-03-07 16:28:35 -08006390# check for pointless casting of alloc functions
6391 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006392 WARN("UNNECESSARY_CASTS",
6393 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006394 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006395
Joe Perchesa640d252013-07-03 15:05:21 -07006396# alloc style
6397# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006398 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006399 $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 -07006400 CHK("ALLOC_SIZEOF_STRUCT",
6401 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6402 }
6403
Joe Perches60a55362014-06-04 16:12:07 -07006404# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006405 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006406 defined $stat &&
6407 $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 -07006408 my $oldfunc = $3;
6409 my $a1 = $4;
6410 my $a2 = $10;
6411 my $newfunc = "kmalloc_array";
6412 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006413 my $r1 = $a1;
6414 my $r2 = $a2;
6415 if ($a1 =~ /^sizeof\s*\S/) {
6416 $r1 = $a2;
6417 $r2 = $a1;
6418 }
6419 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6420 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006421 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006422 my $herectx = get_stat_here($linenr, $cnt, $here);
6423
Joe Perches60a55362014-06-04 16:12:07 -07006424 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006425 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6426 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006427 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006428 $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 -07006429 }
6430 }
6431 }
6432
Joe Perches972fdea2013-04-29 16:18:12 -07006433# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006434 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006435 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6436 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006437 WARN("KREALLOC_ARG_REUSE",
6438 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6439 }
6440
Joe Perches5ce59ae2013-02-21 16:44:18 -08006441# check for alloc argument mismatch
6442 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6443 WARN("ALLOC_ARRAY_ARGS",
6444 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6445 }
6446
Joe Perchescaf2a542011-01-12 16:59:56 -08006447# check for multiple semicolons
6448 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006449 if (WARN("ONE_SEMICOLON",
6450 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6451 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006452 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006453 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006454 }
6455
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006456# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6457 if ($realfile !~ m@^include/uapi/@ &&
6458 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006459 my $ull = "";
6460 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6461 if (CHK("BIT_MACRO",
6462 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6463 $fix) {
6464 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6465 }
6466 }
6467
Joe Perches2d632742016-05-20 17:04:00 -07006468# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6469 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6470 my $config = $1;
6471 if (WARN("PREFER_IS_ENABLED",
6472 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6473 $fix) {
6474 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6475 }
6476 }
6477
Joe Perchese81f2392014-08-06 16:11:25 -07006478# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006479 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6480 my $has_break = 0;
6481 my $has_statement = 0;
6482 my $count = 0;
6483 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006484 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006485 $prevline--;
6486 my $rline = $rawlines[$prevline - 1];
6487 my $fline = $lines[$prevline - 1];
6488 last if ($fline =~ /^\@\@/);
6489 next if ($fline =~ /^\-/);
6490 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6491 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6492 next if ($fline =~ /^.[\s$;]*$/);
6493 $has_statement = 1;
6494 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006495 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006496 }
6497 if (!$has_break && $has_statement) {
6498 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006499 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006500 }
6501 }
6502
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006503# check for /* fallthrough */ like comment, prefer fallthrough;
6504 my @fallthroughs = (
6505 'fallthrough',
6506 '@fallthrough@',
6507 'lint -fallthrough[ \t]*',
6508 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6509 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6510 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6511 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6512 );
6513 if ($raw_comment ne '') {
6514 foreach my $ft (@fallthroughs) {
6515 if ($raw_comment =~ /$ft/) {
6516 my $msg_level = \&WARN;
6517 $msg_level = \&CHK if ($file);
6518 &{$msg_level}("PREFER_FALLTHROUGH",
6519 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6520 last;
6521 }
6522 }
6523 }
6524
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006525# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006526 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006527 defined $stat &&
6528 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006529 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006530 my $herectx = get_stat_here($linenr, $cnt, $here);
6531
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006532 WARN("DEFAULT_NO_BREAK",
6533 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006534 }
6535
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006536# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006537 if ($line =~ /\b__FUNCTION__\b/) {
6538 if (WARN("USE_FUNC",
6539 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6540 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006541 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006542 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006543 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006544
Joe Perches62ec8182015-02-13 14:38:18 -08006545# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6546 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6547 ERROR("DATE_TIME",
6548 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6549 }
6550
Joe Perches2c924882012-03-23 15:02:20 -07006551# check for use of yield()
6552 if ($line =~ /\byield\s*\(\s*\)/) {
6553 WARN("YIELD",
6554 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6555 }
6556
Joe Perches179f8f42013-07-03 15:05:30 -07006557# check for comparisons against true and false
6558 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6559 my $lead = $1;
6560 my $arg = $2;
6561 my $test = $3;
6562 my $otype = $4;
6563 my $trail = $5;
6564 my $op = "!";
6565
6566 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6567
6568 my $type = lc($otype);
6569 if ($type =~ /^(?:true|false)$/) {
6570 if (("$test" eq "==" && "$type" eq "true") ||
6571 ("$test" eq "!=" && "$type" eq "false")) {
6572 $op = "";
6573 }
6574
6575 CHK("BOOL_COMPARISON",
6576 "Using comparison to $otype is error prone\n" . $herecurr);
6577
6578## maybe suggesting a correct construct would better
6579## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6580
6581 }
6582 }
6583
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006584# check for semaphores initialized locked
6585 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006586 WARN("CONSIDER_COMPLETION",
6587 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006588 }
Joe Perches6712d852012-03-23 15:02:20 -07006589
Joe Perches67d0a072011-10-31 17:13:10 -07006590# recommend kstrto* over simple_strto* and strict_strto*
6591 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006592 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006593 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006594 }
Joe Perches6712d852012-03-23 15:02:20 -07006595
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006596# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006597 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006598 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006599 "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 -07006600 }
Joe Perches6712d852012-03-23 15:02:20 -07006601
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006602# check for spin_is_locked(), suggest lockdep instead
6603 if ($line =~ /\bspin_is_locked\(/) {
6604 WARN("USE_LOCKDEP",
6605 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6606 }
6607
Joe Perches9189c7e2018-09-07 15:26:18 -07006608# check for deprecated apis
6609 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6610 my $deprecated_api = $1;
6611 my $new_api = $deprecated_apis{$deprecated_api};
6612 WARN("DEPRECATED_API",
6613 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6614 }
6615
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006616# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006617# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006618 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006619 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006620 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006621 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006622 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006623
6624# use of NR_CPUS is usually wrong
6625# ignore definitions of NR_CPUS and usage to define arrays as likely right
6626 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006627 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6628 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006629 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6630 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6631 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006632 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006633 WARN("NR_CPUS",
6634 "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 -07006635 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006636
Joe Perches52ea8502013-11-12 15:10:09 -08006637# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6638 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6639 ERROR("DEFINE_ARCH_HAS",
6640 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6641 }
6642
Joe Perchesacd93622015-02-13 14:38:38 -08006643# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006644 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006645 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6646 WARN("LIKELY_MISUSE",
6647 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6648 }
6649
Denis Efremovde3f1862019-09-25 16:49:25 -07006650# nested likely/unlikely calls
6651 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6652 WARN("LIKELY_MISUSE",
6653 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6654 }
6655
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006656# whine mightly about in_atomic
6657 if ($line =~ /\bin_atomic\s*\(/) {
6658 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006659 ERROR("IN_ATOMIC",
6660 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006661 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006662 WARN("IN_ATOMIC",
6663 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006664 }
6665 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006666
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006667# check for mutex_trylock_recursive usage
6668 if ($line =~ /mutex_trylock_recursive/) {
6669 ERROR("LOCKING",
6670 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6671 }
6672
Peter Zijlstra1704f472010-03-19 01:37:42 +01006673# check for lockdep_set_novalidate_class
6674 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6675 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6676 if ($realfile !~ m@^kernel/lockdep@ &&
6677 $realfile !~ m@^include/linux/lockdep@ &&
6678 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006679 ERROR("LOCKDEP",
6680 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006681 }
6682 }
Dave Jones88f88312011-01-12 16:59:59 -08006683
Joe Perchesb392c642015-04-16 12:44:16 -07006684 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6685 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006686 WARN("EXPORTED_WORLD_WRITABLE",
6687 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006688 }
Joe Perches24358802014-04-03 14:49:13 -07006689
Joe Perches00180462018-02-06 15:38:55 -08006690# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6691# and whether or not function naming is typical and if
6692# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006693 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006694 defined $stat &&
6695 $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*\)/) {
6696 my $var = $1;
6697 my $perms = $2;
6698 my $show = $3;
6699 my $store = $4;
6700 my $octal_perms = perms_to_octal($perms);
6701 if ($show =~ /^${var}_show$/ &&
6702 $store =~ /^${var}_store$/ &&
6703 $octal_perms eq "0644") {
6704 if (WARN("DEVICE_ATTR_RW",
6705 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6706 $fix) {
6707 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6708 }
6709 } elsif ($show =~ /^${var}_show$/ &&
6710 $store =~ /^NULL$/ &&
6711 $octal_perms eq "0444") {
6712 if (WARN("DEVICE_ATTR_RO",
6713 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6714 $fix) {
6715 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6716 }
6717 } elsif ($show =~ /^NULL$/ &&
6718 $store =~ /^${var}_store$/ &&
6719 $octal_perms eq "0200") {
6720 if (WARN("DEVICE_ATTR_WO",
6721 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6722 $fix) {
6723 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6724 }
6725 } elsif ($octal_perms eq "0644" ||
6726 $octal_perms eq "0444" ||
6727 $octal_perms eq "0200") {
6728 my $newshow = "$show";
6729 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6730 my $newstore = $store;
6731 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6732 my $rename = "";
6733 if ($show ne $newshow) {
6734 $rename .= " '$show' to '$newshow'";
6735 }
6736 if ($store ne $newstore) {
6737 $rename .= " '$store' to '$newstore'";
6738 }
6739 WARN("DEVICE_ATTR_FUNCTIONS",
6740 "Consider renaming function(s)$rename\n" . $herecurr);
6741 } else {
6742 WARN("DEVICE_ATTR_PERMS",
6743 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6744 }
6745 }
6746
Joe Perches515a2352014-04-03 14:49:24 -07006747# Mode permission misuses where it seems decimal should be octal
6748# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006749# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6750# specific definition of not visible in sysfs.
6751# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6752# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006753 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006754 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006755 $line =~ /$mode_perms_search/) {
6756 foreach my $entry (@mode_permission_funcs) {
6757 my $func = $entry->[0];
6758 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006759
Joe Perches459cf0a2016-10-11 13:52:19 -07006760 my $lc = $stat =~ tr@\n@@;
6761 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006762 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006763
Joe Perches515a2352014-04-03 14:49:24 -07006764 my $skip_args = "";
6765 if ($arg_pos > 1) {
6766 $arg_pos--;
6767 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6768 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006769 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006770 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006771 my $val = $1;
6772 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006773 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6774 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6775 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006776 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006777 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006778 }
6779 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006780 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006781 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006782 }
Joe Perches24358802014-04-03 14:49:13 -07006783 }
6784 }
6785 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006786
Joe Perches459cf0a2016-10-11 13:52:19 -07006787# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006788 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006789 my $oval = $1;
6790 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006791 if (WARN("SYMBOLIC_PERMS",
6792 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6793 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006794 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006795 }
6796 }
6797
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006798# validate content of MODULE_LICENSE against list from include/linux/module.h
6799 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6800 my $extracted_string = get_quoted_string($line, $rawline);
6801 my $valid_licenses = qr{
6802 GPL|
6803 GPL\ v2|
6804 GPL\ and\ additional\ rights|
6805 Dual\ BSD/GPL|
6806 Dual\ MIT/GPL|
6807 Dual\ MPL/GPL|
6808 Proprietary
6809 }x;
6810 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6811 WARN("MODULE_LICENSE",
6812 "unknown module license " . $extracted_string . "\n" . $herecurr);
6813 }
6814 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07006815
6816# check for sysctl duplicate constants
6817 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6818 WARN("DUPLICATED_SYSCTL_CONST",
6819 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6820 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006821 }
6822
6823 # If we have no input at all, then there is nothing to report on
6824 # so just keep quiet.
6825 if ($#rawlines == -1) {
6826 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006827 }
6828
Andy Whitcroft8905a672007-11-28 16:21:06 -08006829 # In mailback mode only produce a report in the negative, for
6830 # things that appear to be patches.
6831 if ($mailback && ($clean == 1 || !$is_patch)) {
6832 exit(0);
6833 }
6834
6835 # This is not a patch, and we are are in 'no-patch' mode so
6836 # just keep quiet.
6837 if (!$chk_patch && !$is_patch) {
6838 exit(0);
6839 }
6840
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006841 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006842 ERROR("NOT_UNIFIED_DIFF",
6843 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006844 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006845 if ($is_patch && $has_commit_log && $chk_signoff) {
6846 if ($signoff == 0) {
6847 ERROR("MISSING_SIGN_OFF",
6848 "Missing Signed-off-by: line(s)\n");
6849 } elsif (!$authorsignoff) {
6850 WARN("NO_AUTHOR_SIGN_OFF",
6851 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6852 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006853 }
6854
Andy Whitcroft8905a672007-11-28 16:21:06 -08006855 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006856 if ($summary && !($clean == 1 && $quiet == 1)) {
6857 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006858 print "total: $cnt_error errors, $cnt_warn warnings, " .
6859 (($check)? "$cnt_chk checks, " : "") .
6860 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006861 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006862
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006863 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006864 # If there were any defects found and not already fixing them
6865 if (!$clean and !$fix) {
6866 print << "EOM"
6867
6868NOTE: For some of the reported defects, checkpatch may be able to
6869 mechanically convert to the typical style using --fix or --fix-inplace.
6870EOM
6871 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006872 # If there were whitespace errors which cleanpatch can fix
6873 # then suggest that.
6874 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006875 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006876 print << "EOM"
6877
6878NOTE: Whitespace errors detected.
6879 You may wish to use scripts/cleanpatch or scripts/cleanfile
6880EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006881 }
6882 }
6883
Joe Perchesd752fcc2014-08-06 16:11:05 -07006884 if ($clean == 0 && $fix &&
6885 ("@rawlines" ne "@fixed" ||
6886 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006887 my $newfile = $filename;
6888 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006889 my $linecount = 0;
6890 my $f;
6891
Joe Perchesd752fcc2014-08-06 16:11:05 -07006892 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6893
Joe Perches3705ce52013-07-03 15:05:31 -07006894 open($f, '>', $newfile)
6895 or die "$P: Can't open $newfile for write\n";
6896 foreach my $fixed_line (@fixed) {
6897 $linecount++;
6898 if ($file) {
6899 if ($linecount > 3) {
6900 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006901 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006902 }
6903 } else {
6904 print $f $fixed_line . "\n";
6905 }
6906 }
6907 close($f);
6908
6909 if (!$quiet) {
6910 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006911
Joe Perches3705ce52013-07-03 15:05:31 -07006912Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6913
6914Do _NOT_ trust the results written to this file.
6915Do _NOT_ submit these changes without inspecting them for correctness.
6916
6917This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6918No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006919EOM
6920 }
6921 }
6922
Joe Perchesd8469f12015-06-25 15:03:00 -07006923 if ($quiet == 0) {
6924 print "\n";
6925 if ($clean == 1) {
6926 print "$vname has no obvious style problems and is ready for submission.\n";
6927 } else {
6928 print "$vname has style problems, please review.\n";
6929 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006930 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006931 return $clean;
6932}