blob: fd16f2239a00e7af6cfe431f3c833aa66ef215bf [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 Perches6cd7f382012-12-17 16:01:54 -080054my $max_line_length = 80;
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 Perches6cd7f382012-12-17 16:01:54 -0800100 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -0700101 --min-conf-desc-length=n set the min description length, if shorter, warn
Antonio Borneo713a09d2020-04-06 20:11:07 -0700102 --tab-size=n set the number of spaces for tab (default 8)
Hannes Eder77f5b102009-09-21 17:04:37 -0700103 --root=PATH PATH to the kernel tree root
104 --no-summary suppress the per-file summary
105 --mailback only produce a report in case of warnings/errors
106 --summary-file include the filename in summary
107 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
108 'values', 'possible', 'type', and 'attr' (default
109 is all off)
110 --test-only=WORD report only warnings/errors containing WORD
111 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700112 --fix EXPERIMENTAL - may create horrible results
113 If correctable single-line errors exist, create
114 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
115 with potential errors corrected to the preferred
116 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800117 --fix-inplace EXPERIMENTAL - may create horrible results
118 Is the same as --fix, but overwrites the input
119 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700120 --ignore-perl-version override checking of perl version. expect
121 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700122 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700123 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700124 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700125 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700126 --color[=WHEN] Use colors 'always', 'never', or only when output
127 is a terminal ('auto'). Default is 'auto'.
Hannes Eder77f5b102009-09-21 17:04:37 -0700128 -h, --help, --version display this help and exit
129
130When FILE is - read standard input.
131EOM
132
133 exit($exitcode);
134}
135
Joe Perches3beb42e2016-05-20 17:04:14 -0700136sub uniq {
137 my %seen;
138 return grep { !$seen{$_}++ } @_;
139}
140
141sub list_types {
142 my ($exitcode) = @_;
143
144 my $count = 0;
145
146 local $/ = undef;
147
148 open(my $script, '<', abs_path($P)) or
149 die "$P: Can't read '$P' $!\n";
150
151 my $text = <$script>;
152 close($script);
153
154 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700155 # Also catch when type or level is passed through a variable
156 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700157 push (@types, $_);
158 }
159 @types = sort(uniq(@types));
160 print("#\tMessage type\n\n");
161 foreach my $type (@types) {
162 print(++$count . "\t" . $type . "\n");
163 }
164
165 exit($exitcode);
166}
167
Joe Perches000d1cc12011-07-25 17:13:25 -0700168my $conf = which_conf($configuration_file);
169if (-f $conf) {
170 my @conf_args;
171 open(my $conffile, '<', "$conf")
172 or warn "$P: Can't find a readable $configuration_file file $!\n";
173
174 while (<$conffile>) {
175 my $line = $_;
176
177 $line =~ s/\s*\n?$//g;
178 $line =~ s/^\s*//g;
179 $line =~ s/\s+/ /g;
180
181 next if ($line =~ m/^\s*#/);
182 next if ($line =~ m/^\s*$/);
183
184 my @words = split(" ", $line);
185 foreach my $word (@words) {
186 last if ($word =~ m/^#/);
187 push (@conf_args, $word);
188 }
189 }
190 close($conffile);
191 unshift(@ARGV, @conf_args) if @conf_args;
192}
193
John Brooks737c0762017-07-10 15:52:24 -0700194# Perl's Getopt::Long allows options to take optional arguments after a space.
195# Prevent --color by itself from consuming other arguments
196foreach (@ARGV) {
197 if ($_ eq "--color" || $_ eq "-color") {
198 $_ = "--color=$color";
199 }
200}
201
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700202GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700203 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700204 'tree!' => \$tree,
205 'signoff!' => \$chk_signoff,
206 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700207 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800208 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700209 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700210 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700211 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700212 'subjective!' => \$check,
213 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700214 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700215 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700216 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700217 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800218 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700219 'min-conf-desc-length=i' => \$min_conf_desc_length,
Antonio Borneo713a09d2020-04-06 20:11:07 -0700220 'tab-size=i' => \$tabsize,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700221 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800222 'summary!' => \$summary,
223 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800224 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700225 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800226 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700227 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800228 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700229 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700230 'codespell!' => \$codespell,
231 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700232 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700233 'color=s' => \$color,
234 'no-color' => \$color, #keep old behaviors of -nocolor
235 'nocolor' => \$color, #keep old behaviors of -nocolor
Hannes Eder77f5b102009-09-21 17:04:37 -0700236 'h|help' => \$help,
237 'version' => \$help
238) or help(1);
239
240help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700241
Joe Perches3beb42e2016-05-20 17:04:14 -0700242list_types(0) if ($list_types);
243
Joe Perches9624b8d2014-01-23 15:54:44 -0800244$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700245$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800246
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700247my $exit = 0;
248
Joe Perches5b579802018-08-21 21:57:33 -0700249my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700250if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700251 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700252 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700253 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700254}
255
Allen Hubbe45107ff2016-08-02 14:04:47 -0700256#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700257if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700258 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700259}
260
John Brooks737c0762017-07-10 15:52:24 -0700261if ($color =~ /^[01]$/) {
262 $color = !$color;
263} elsif ($color =~ /^always$/i) {
264 $color = 1;
265} elsif ($color =~ /^never$/i) {
266 $color = 0;
267} elsif ($color =~ /^auto$/i) {
268 $color = (-t STDOUT);
269} else {
270 die "Invalid color mode: $color\n";
271}
272
Antonio Borneo713a09d2020-04-06 20:11:07 -0700273# skip TAB size 1 to avoid additional checks on $tabsize - 1
274die "Invalid TAB size: $tabsize\n" if ($tabsize < 2);
275
Joe Perches91bfe482013-09-11 14:23:59 -0700276sub hash_save_array_words {
277 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700278
Joe Perches91bfe482013-09-11 14:23:59 -0700279 my @array = split(/,/, join(',', @$arrayRef));
280 foreach my $word (@array) {
281 $word =~ s/\s*\n?$//g;
282 $word =~ s/^\s*//g;
283 $word =~ s/\s+/ /g;
284 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700285
Joe Perches91bfe482013-09-11 14:23:59 -0700286 next if ($word =~ m/^\s*#/);
287 next if ($word =~ m/^\s*$/);
288
289 $hashRef->{$word}++;
290 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700291}
292
Joe Perches91bfe482013-09-11 14:23:59 -0700293sub hash_show_words {
294 my ($hashRef, $prefix) = @_;
295
Joe Perches3c816e42015-06-25 15:03:29 -0700296 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700297 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700298 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700299 print " $word";
300 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700301 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700302 }
303}
304
305hash_save_array_words(\%ignore_type, \@ignore);
306hash_save_array_words(\%use_type, \@use);
307
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800308my $dbg_values = 0;
309my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700310my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700311my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800312for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800313 ## no critic
314 eval "\${dbg_$key} = '$debug{$key}';";
315 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800316}
317
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700318my $rpt_cleaners = 0;
319
Andy Whitcroft8905a672007-11-28 16:21:06 -0800320if ($terse) {
321 $emacs = 1;
322 $quiet++;
323}
324
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700325if ($tree) {
326 if (defined $root) {
327 if (!top_of_kernel_tree($root)) {
328 die "$P: $root: --root does not point at a valid tree\n";
329 }
330 } else {
331 if (top_of_kernel_tree('.')) {
332 $root = '.';
333 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
334 top_of_kernel_tree($1)) {
335 $root = $1;
336 }
337 }
338
339 if (!defined $root) {
340 print "Must be run from the top-level dir. of a kernel tree\n";
341 exit(2);
342 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700343}
344
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700345my $emitted_corrupt = 0;
346
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700347our $Ident = qr{
348 [A-Za-z_][A-Za-z\d_]*
349 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
350 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700351our $Storage = qr{extern|static|asmlinkage};
352our $Sparse = qr{
353 __user|
354 __kernel|
355 __force|
356 __iomem|
357 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800358 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700359 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700360 __refconst|
361 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800362 __rcu|
363 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700364 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800365our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
366our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
367our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
368our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
369our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700370
Wolfram Sang52131292010-03-05 13:43:51 -0800371# Notes to $Attribute:
372# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700373our $Attribute = qr{
374 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700375 __percpu|
376 __nocast|
377 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200378 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700379 __packed__|
380 __packed2__|
381 __naked|
382 __maybe_unused|
383 __always_unused|
384 __noreturn|
385 __used|
386 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800387 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700388 __noclone|
389 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700390 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700391 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700392 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700393 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700394 ____cacheline_aligned|
395 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800396 ____cacheline_internodealigned_in_smp|
397 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700398 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700399our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700400our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700401our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
402our $Lval = qr{$Ident(?:$Member)*};
403
Joe Perches95e2c602013-07-03 15:05:20 -0700404our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
405our $Binary = qr{(?i)0b[01]+$Int_type?};
406our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
407our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700408our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800409our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800410our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
411our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
412our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800413our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700414our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800415our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700416our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700417our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700418our $Operators = qr{
419 <=|>=|==|!=|
420 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700421 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700422 }x;
423
Joe Perches91cb5192014-04-03 14:49:32 -0700424our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
425
Joe Perchesab7e23f2015-04-16 12:44:22 -0700426our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800427our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700428our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700429our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800430our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700431our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800432our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700433our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800434
Joe Perches15662b32011-10-31 17:13:12 -0700435our $NON_ASCII_UTF8 = qr{
436 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700437 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
438 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
439 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
440 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
441 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
442 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
443}x;
444
Joe Perches15662b32011-10-31 17:13:12 -0700445our $UTF8 = qr{
446 [\x09\x0A\x0D\x20-\x7E] # ASCII
447 | $NON_ASCII_UTF8
448}x;
449
Joe Perchese6176fa2015-06-25 15:02:49 -0700450our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800451our $typeOtherOSTypedefs = qr{(?x:
452 u_(?:char|short|int|long) | # bsd
453 u(?:nchar|short|int|long) # sysv
454)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700455our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700456 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700457 atomic_t
458)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700459our $typeTypedefs = qr{(?x:
460 $typeC99Typedefs\b|
461 $typeOtherOSTypedefs\b|
462 $typeKernelTypedefs\b
463)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700464
Joe Perches6d32f7a2015-11-06 16:31:37 -0800465our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
466
Joe Perches691e6692010-03-05 13:43:51 -0800467our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800468 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700469 (?:[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 -0800470 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700471 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700472 panic|
Joe Perches06668722013-11-12 15:10:07 -0800473 MODULE_[A-Z_]+|
474 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800475)};
476
Joe Perchese29a70f2019-03-07 16:28:35 -0800477our $allocFunctions = qr{(?x:
478 (?:(?:devm_)?
479 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
480 kstrdup(?:_const)? |
481 kmemdup(?:_nul)?) |
482 (?:\w+)?alloc_skb(?:ip_align)? |
483 # dev_alloc_skb/netdev_alloc_skb, et al
484 dma_alloc_coherent
485)};
486
Joe Perches20112472011-07-25 17:13:23 -0700487our $signature_tags = qr{(?xi:
488 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800489 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700490 Acked-by:|
491 Tested-by:|
492 Reviewed-by:|
493 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700494 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700495 To:|
496 Cc:
497)};
498
Joe Perches18130872014-08-06 16:11:22 -0700499our @typeListMisordered = (
500 qr{char\s+(?:un)?signed},
501 qr{int\s+(?:(?:un)?signed\s+)?short\s},
502 qr{int\s+short(?:\s+(?:un)?signed)},
503 qr{short\s+int(?:\s+(?:un)?signed)},
504 qr{(?:un)?signed\s+int\s+short},
505 qr{short\s+(?:un)?signed},
506 qr{long\s+int\s+(?:un)?signed},
507 qr{int\s+long\s+(?:un)?signed},
508 qr{long\s+(?:un)?signed\s+int},
509 qr{int\s+(?:un)?signed\s+long},
510 qr{int\s+(?:un)?signed},
511 qr{int\s+long\s+long\s+(?:un)?signed},
512 qr{long\s+long\s+int\s+(?:un)?signed},
513 qr{long\s+long\s+(?:un)?signed\s+int},
514 qr{long\s+long\s+(?:un)?signed},
515 qr{long\s+(?:un)?signed},
516);
517
Andy Whitcroft8905a672007-11-28 16:21:06 -0800518our @typeList = (
519 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700520 qr{(?:(?:un)?signed\s+)?char},
521 qr{(?:(?:un)?signed\s+)?short\s+int},
522 qr{(?:(?:un)?signed\s+)?short},
523 qr{(?:(?:un)?signed\s+)?int},
524 qr{(?:(?:un)?signed\s+)?long\s+int},
525 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
526 qr{(?:(?:un)?signed\s+)?long\s+long},
527 qr{(?:(?:un)?signed\s+)?long},
528 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800529 qr{float},
530 qr{double},
531 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800532 qr{struct\s+$Ident},
533 qr{union\s+$Ident},
534 qr{enum\s+$Ident},
535 qr{${Ident}_t},
536 qr{${Ident}_handler},
537 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700538 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800539);
Joe Perches938224b2016-01-20 14:59:15 -0800540
541our $C90_int_types = qr{(?x:
542 long\s+long\s+int\s+(?:un)?signed|
543 long\s+long\s+(?:un)?signed\s+int|
544 long\s+long\s+(?:un)?signed|
545 (?:(?:un)?signed\s+)?long\s+long\s+int|
546 (?:(?:un)?signed\s+)?long\s+long|
547 int\s+long\s+long\s+(?:un)?signed|
548 int\s+(?:(?:un)?signed\s+)?long\s+long|
549
550 long\s+int\s+(?:un)?signed|
551 long\s+(?:un)?signed\s+int|
552 long\s+(?:un)?signed|
553 (?:(?:un)?signed\s+)?long\s+int|
554 (?:(?:un)?signed\s+)?long|
555 int\s+long\s+(?:un)?signed|
556 int\s+(?:(?:un)?signed\s+)?long|
557
558 int\s+(?:un)?signed|
559 (?:(?:un)?signed\s+)?int
560)};
561
Alex Dowad485ff232015-06-25 15:02:52 -0700562our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700563our @typeListWithAttr = (
564 @typeList,
565 qr{struct\s+$InitAttribute\s+$Ident},
566 qr{union\s+$InitAttribute\s+$Ident},
567);
568
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700569our @modifierList = (
570 qr{fastcall},
571);
Alex Dowad485ff232015-06-25 15:02:52 -0700572our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800573
Joe Perches24358802014-04-03 14:49:13 -0700574our @mode_permission_funcs = (
575 ["module_param", 3],
576 ["module_param_(?:array|named|string)", 4],
577 ["module_param_array_named", 5],
578 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
579 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700580 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
581 ["IIO_DEV_ATTR_[A-Z_]+", 1],
582 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
583 ["SENSOR_TEMPLATE(?:_2|)", 3],
584 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700585);
586
Joe Perches515a2352014-04-03 14:49:24 -0700587#Create a search pattern for all these functions to speed up a loop below
588our $mode_perms_search = "";
589foreach my $entry (@mode_permission_funcs) {
590 $mode_perms_search .= '|' if ($mode_perms_search ne "");
591 $mode_perms_search .= $entry->[0];
592}
Joe Perches00180462018-02-06 15:38:55 -0800593$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700594
Joe Perches9189c7e2018-09-07 15:26:18 -0700595our %deprecated_apis = (
596 "synchronize_rcu_bh" => "synchronize_rcu",
597 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
598 "call_rcu_bh" => "call_rcu",
599 "rcu_barrier_bh" => "rcu_barrier",
600 "synchronize_sched" => "synchronize_rcu",
601 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
602 "call_rcu_sched" => "call_rcu",
603 "rcu_barrier_sched" => "rcu_barrier",
604 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
605 "cond_synchronize_sched" => "cond_synchronize_rcu",
606);
607
608#Create a search pattern for all these strings to speed up a loop below
609our $deprecated_apis_search = "";
610foreach my $entry (keys %deprecated_apis) {
611 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
612 $deprecated_apis_search .= $entry;
613}
614$deprecated_apis_search = "(?:${deprecated_apis_search})";
615
Joe Perchesb392c642015-04-16 12:44:16 -0700616our $mode_perms_world_writable = qr{
617 S_IWUGO |
618 S_IWOTH |
619 S_IRWXUGO |
620 S_IALLUGO |
621 0[0-7][0-7][2367]
622}x;
623
Joe Perchesf90774e2016-10-11 13:51:47 -0700624our %mode_permission_string_types = (
625 "S_IRWXU" => 0700,
626 "S_IRUSR" => 0400,
627 "S_IWUSR" => 0200,
628 "S_IXUSR" => 0100,
629 "S_IRWXG" => 0070,
630 "S_IRGRP" => 0040,
631 "S_IWGRP" => 0020,
632 "S_IXGRP" => 0010,
633 "S_IRWXO" => 0007,
634 "S_IROTH" => 0004,
635 "S_IWOTH" => 0002,
636 "S_IXOTH" => 0001,
637 "S_IRWXUGO" => 0777,
638 "S_IRUGO" => 0444,
639 "S_IWUGO" => 0222,
640 "S_IXUGO" => 0111,
641);
642
643#Create a search pattern for all these strings to speed up a loop below
644our $mode_perms_string_search = "";
645foreach my $entry (keys %mode_permission_string_types) {
646 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
647 $mode_perms_string_search .= $entry;
648}
Joe Perches00180462018-02-06 15:38:55 -0800649our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
650our $multi_mode_perms_string_search = qr{
651 ${single_mode_perms_string_search}
652 (?:\s*\|\s*${single_mode_perms_string_search})*
653}x;
654
655sub perms_to_octal {
656 my ($string) = @_;
657
658 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
659
660 my $val = "";
661 my $oval = "";
662 my $to = 0;
663 my $curpos = 0;
664 my $lastpos = 0;
665 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
666 $curpos = pos($string);
667 my $match = $2;
668 my $omatch = $1;
669 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
670 $lastpos = $curpos;
671 $to |= $mode_permission_string_types{$match};
672 $val .= '\s*\|\s*' if ($val ne "");
673 $val .= $match;
674 $oval .= $omatch;
675 }
676 $oval =~ s/^\s*\|\s*//;
677 $oval =~ s/\s*\|\s*$//;
678 return sprintf("%04o", $to);
679}
Joe Perchesf90774e2016-10-11 13:51:47 -0700680
Wolfram Sang7840a942010-08-09 17:20:57 -0700681our $allowed_asm_includes = qr{(?x:
682 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700683 memory|
684 time|
685 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700686)};
687# memory.h: ARM has a custom one
688
Kees Cook66b47b42014-10-13 15:51:57 -0700689# Load common spelling mistakes and build regular expression list.
690my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700691my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700692
Joe Perches36061e32014-12-10 15:51:43 -0800693if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800694 while (<$spelling>) {
695 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700696
Joe Perches36061e32014-12-10 15:51:43 -0800697 $line =~ s/\s*\n?$//g;
698 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700699
Joe Perches36061e32014-12-10 15:51:43 -0800700 next if ($line =~ m/^\s*#/);
701 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700702
Joe Perches36061e32014-12-10 15:51:43 -0800703 my ($suspect, $fix) = split(/\|\|/, $line);
704
Joe Perches36061e32014-12-10 15:51:43 -0800705 $spelling_fix{$suspect} = $fix;
706 }
707 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800708} else {
709 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700710}
Kees Cook66b47b42014-10-13 15:51:57 -0700711
Joe Perchesebfd7d62015-04-16 12:44:14 -0700712if ($codespell) {
713 if (open(my $spelling, '<', $codespellfile)) {
714 while (<$spelling>) {
715 my $line = $_;
716
717 $line =~ s/\s*\n?$//g;
718 $line =~ s/^\s*//g;
719
720 next if ($line =~ m/^\s*#/);
721 next if ($line =~ m/^\s*$/);
722 next if ($line =~ m/, disabled/i);
723
724 $line =~ s/,.*$//;
725
726 my ($suspect, $fix) = split(/->/, $line);
727
728 $spelling_fix{$suspect} = $fix;
729 }
730 close($spelling);
731 } else {
732 warn "No codespell typos will be found - file '$codespellfile': $!\n";
733 }
734}
735
736$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
737
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700738sub read_words {
739 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700740
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700741 if (open(my $words, '<', $file)) {
742 while (<$words>) {
743 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700744
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700745 $line =~ s/\s*\n?$//g;
746 $line =~ s/^\s*//g;
747
748 next if ($line =~ m/^\s*#/);
749 next if ($line =~ m/^\s*$/);
750 if ($line =~ /\s/) {
751 print("$file: '$line' invalid - ignored\n");
752 next;
753 }
754
755 $$wordsRef .= '|' if ($$wordsRef ne "");
756 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700757 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700758 close($file);
759 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700760 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700761
762 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700763}
764
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700765my $const_structs = "";
766read_words(\$const_structs, $conststructsfile)
767 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
768
769my $typeOtherTypedefs = "";
770if (length($typedefsfile)) {
771 read_words(\$typeOtherTypedefs, $typedefsfile)
772 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
773}
774$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
775
Andy Whitcroft8905a672007-11-28 16:21:06 -0800776sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700777 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
778 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700779 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700780 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700781 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700782 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700783 (?:$typeTypedefs\b)|
784 (?:${all}\b)
785 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800786 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700787 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800788 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800789 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700790 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700791 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800792 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700793 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800794 }x;
Joe Perches18130872014-08-06 16:11:22 -0700795 $NonptrTypeMisordered = qr{
796 (?:$Modifier\s+|const\s+)*
797 (?:
798 (?:${Misordered}\b)
799 )
800 (?:\s+$Modifier|\s+const)*
801 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700802 $NonptrTypeWithAttr = qr{
803 (?:$Modifier\s+|const\s+)*
804 (?:
805 (?:typeof|__typeof__)\s*\([^\)]*\)|
806 (?:$typeTypedefs\b)|
807 (?:${allWithAttr}\b)
808 )
809 (?:\s+$Modifier|\s+const)*
810 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800811 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700812 $NonptrType
Antonio Borneo7b184962020-04-06 20:11:04 -0700813 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700814 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800815 }x;
Joe Perches18130872014-08-06 16:11:22 -0700816 $TypeMisordered = qr{
817 $NonptrTypeMisordered
Antonio Borneo7b184962020-04-06 20:11:04 -0700818 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Perches18130872014-08-06 16:11:22 -0700819 (?:\s+$Inline|\s+$Modifier)*
820 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700821 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700822 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800823}
824build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700825
Joe Perches7d2367a2011-07-25 17:13:22 -0700826our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700827
828# Using $balanced_parens, $LvalOrFunc, or $FuncArg
829# requires at least perl version v5.10.0
830# Any use must be runtime checked with $^V
831
832our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700833our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800834our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700835
Joe Perchesf8422302014-08-06 16:11:31 -0700836our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700837 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700838 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700839 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
840 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700841)};
842
Joe Perches7d2367a2011-07-25 17:13:22 -0700843sub deparenthesize {
844 my ($string) = @_;
845 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700846
847 while ($string =~ /^\s*\(.*\)\s*$/) {
848 $string =~ s@^\s*\(\s*@@;
849 $string =~ s@\s*\)\s*$@@;
850 }
851
Joe Perches7d2367a2011-07-25 17:13:22 -0700852 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700853
Joe Perches7d2367a2011-07-25 17:13:22 -0700854 return $string;
855}
856
Joe Perches34456862013-07-03 15:05:34 -0700857sub seed_camelcase_file {
858 my ($file) = @_;
859
860 return if (!(-f $file));
861
862 local $/;
863
864 open(my $include_file, '<', "$file")
865 or warn "$P: Can't read '$file' $!\n";
866 my $text = <$include_file>;
867 close($include_file);
868
869 my @lines = split('\n', $text);
870
871 foreach my $line (@lines) {
872 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
873 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
874 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800875 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
876 $camelcase{$1} = 1;
877 } 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 -0700878 $camelcase{$1} = 1;
879 }
880 }
881}
882
Joe Perchescd28b112019-12-04 16:52:09 -0800883our %maintained_status = ();
884
Joe Perches85b0ee12016-10-11 13:51:44 -0700885sub is_maintained_obsolete {
886 my ($filename) = @_;
887
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800888 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700889
Joe Perchescd28b112019-12-04 16:52:09 -0800890 if (!exists($maintained_status{$filename})) {
891 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
892 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700893
Joe Perchescd28b112019-12-04 16:52:09 -0800894 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700895}
896
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700897sub is_SPDX_License_valid {
898 my ($license) = @_;
899
Joe Perches56294112018-08-21 21:58:04 -0700900 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700901
Joe Perches56294112018-08-21 21:58:04 -0700902 my $root_path = abs_path($root);
903 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700904 return 0 if ($status ne "");
905 return 1;
906}
907
Joe Perches34456862013-07-03 15:05:34 -0700908my $camelcase_seeded = 0;
909sub seed_camelcase_includes {
910 return if ($camelcase_seeded);
911
912 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700913 my $camelcase_cache = "";
914 my @include_files = ();
915
916 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700917
Richard Genoud3645e322014-02-10 14:25:32 -0800918 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700919 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -0700920 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700921 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700922 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700923 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700924 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700925 @include_files = split('\n', $files);
926 foreach my $file (@include_files) {
927 my $date = POSIX::strftime("%Y%m%d%H%M",
928 localtime((stat $file)[9]));
929 $last_mod_date = $date if ($last_mod_date < $date);
930 }
931 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700932 }
Joe Perchesc707a812013-07-08 16:00:43 -0700933
934 if ($camelcase_cache ne "" && -f $camelcase_cache) {
935 open(my $camelcase_file, '<', "$camelcase_cache")
936 or warn "$P: Can't read '$camelcase_cache' $!\n";
937 while (<$camelcase_file>) {
938 chomp;
939 $camelcase{$_} = 1;
940 }
941 close($camelcase_file);
942
943 return;
944 }
945
Richard Genoud3645e322014-02-10 14:25:32 -0800946 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700947 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700948 @include_files = split('\n', $files);
949 }
950
Joe Perches34456862013-07-03 15:05:34 -0700951 foreach my $file (@include_files) {
952 seed_camelcase_file($file);
953 }
Joe Perches351b2a12013-07-03 15:05:36 -0700954
Joe Perchesc707a812013-07-08 16:00:43 -0700955 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700956 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700957 open(my $camelcase_file, '>', "$camelcase_cache")
958 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700959 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
960 print $camelcase_file ("$_\n");
961 }
962 close($camelcase_file);
963 }
Joe Perches34456862013-07-03 15:05:34 -0700964}
965
Joe Perchesd311cd42014-08-06 16:10:57 -0700966sub git_commit_info {
967 my ($commit, $id, $desc) = @_;
968
969 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
970
Joe Perchesdbbf8692019-09-25 16:46:52 -0700971 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -0700972 $output =~ s/^\s*//gm;
973 my @lines = split("\n", $output);
974
Joe Perches0d7835f2015-02-13 14:38:35 -0800975 return ($id, $desc) if ($#lines < 0);
976
Sean Christopherson5a7f4452019-09-25 16:46:49 -0700977 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -0700978# Maybe one day convert this block of bash into something that returns
979# all matching commit ids, but it's very slow...
980#
981# echo "checking commits $1..."
982# git rev-list --remotes | grep -i "^$1" |
983# while read line ; do
984# git log --format='%H %s' -1 $line |
985# echo "commit $(cut -c 1-12,41-)"
986# done
987 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700988 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700989 } else {
990 $id = substr($lines[0], 0, 12);
991 $desc = substr($lines[0], 41);
992 }
993
994 return ($id, $desc);
995}
996
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700997$chk_signoff = 0 if ($file);
998
Andy Whitcroft00df3442007-06-08 13:47:06 -0700999my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001000my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001001my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001002my @fixed_inserted = ();
1003my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001004my $fixlinenr = -1;
1005
Du, Changbin4a593c32016-05-20 17:04:16 -07001006# If input is git commits, extract all commits from the commit expressions.
1007# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1008die "$P: No git repository found\n" if ($git && !-e ".git");
1009
1010if ($git) {
1011 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001012 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001013 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001014 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1015 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001016 } elsif ($commit_expr =~ m/\.\./) {
1017 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001018 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001019 $git_range = "-1 $commit_expr";
1020 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001021 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001022 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001023 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1024 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001025 my $sha1 = $1;
1026 my $subject = $2;
1027 unshift(@commits, $sha1);
1028 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001029 }
1030 }
1031 die "$P: no git commits after extraction!\n" if (@commits == 0);
1032 @ARGV = @commits;
1033}
1034
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001035my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001036$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001037for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001038 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -07001039 if ($git) {
1040 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1041 die "$P: $filename: git format-patch failed - $!\n";
1042 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001043 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001044 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001045 } elsif ($filename eq '-') {
1046 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001047 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001048 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001049 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001050 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001051 if ($filename eq '-') {
1052 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001053 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001054 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001055 } else {
1056 $vname = $filename;
1057 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001058 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001059 chomp;
1060 push(@rawlines, $_);
1061 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001062 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001063
1064 if ($#ARGV > 0 && $quiet == 0) {
1065 print '-' x length($vname) . "\n";
1066 print "$vname\n";
1067 print '-' x length($vname) . "\n";
1068 }
1069
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001070 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001071 $exit = 1;
1072 }
1073 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001074 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001075 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001076 @fixed_inserted = ();
1077 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001078 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001079 @modifierListFile = ();
1080 @typeListFile = ();
1081 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001082}
1083
Joe Perchesd8469f12015-06-25 15:03:00 -07001084if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001085 hash_show_words(\%use_type, "Used");
1086 hash_show_words(\%ignore_type, "Ignored");
1087
Joe Perches5b579802018-08-21 21:57:33 -07001088 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001089 print << "EOM"
1090
1091NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001092 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001093EOM
1094 }
1095 if ($exit) {
1096 print << "EOM"
1097
1098NOTE: If any of the errors are false positives, please report
1099 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1100EOM
1101 }
1102}
1103
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001104exit($exit);
1105
1106sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001107 my ($root) = @_;
1108
1109 my @tree_check = (
1110 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1111 "README", "Documentation", "arch", "include", "drivers",
1112 "fs", "init", "ipc", "kernel", "lib", "scripts",
1113 );
1114
1115 foreach my $check (@tree_check) {
1116 if (! -e $root . '/' . $check) {
1117 return 0;
1118 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001119 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001120 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001121}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001122
Joe Perches20112472011-07-25 17:13:23 -07001123sub parse_email {
1124 my ($formatted_email) = @_;
1125
1126 my $name = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001127 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001128 my $address = "";
1129 my $comment = "";
1130
1131 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1132 $name = $1;
1133 $address = $2;
1134 $comment = $3 if defined $3;
1135 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1136 $address = $1;
1137 $comment = $2 if defined $2;
1138 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1139 $address = $1;
1140 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001141 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001142 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001143 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001144 $name =~ s/^\"|\"$//g;
1145 # If there's a name left after stripping spaces and
1146 # leading quotes, and the address doesn't have both
1147 # leading and trailing angle brackets, the address
1148 # is invalid. ie:
1149 # "joe smith joe@smith.com" bad
1150 # "joe smith <joe@smith.com" bad
1151 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1152 $name = "";
1153 $address = "";
1154 $comment = "";
1155 }
1156 }
1157
Joe Perches3705ce52013-07-03 15:05:31 -07001158 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001159 $name =~ s/^\"|\"$//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001160 $name =~ s/(\s*\([^\)]+\))\s*//;
1161 if (defined($1)) {
1162 $name_comment = trim($1);
1163 }
Joe Perches3705ce52013-07-03 15:05:31 -07001164 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001165 $address =~ s/^\<|\>$//g;
1166
1167 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1168 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1169 $name = "\"$name\"";
1170 }
1171
Joe Perchesdfa05c22020-04-06 20:10:48 -07001172 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001173}
1174
1175sub format_email {
1176 my ($name, $address) = @_;
1177
1178 my $formatted_email;
1179
Joe Perches3705ce52013-07-03 15:05:31 -07001180 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001181 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001182 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001183
1184 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1185 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1186 $name = "\"$name\"";
1187 }
1188
1189 if ("$name" eq "") {
1190 $formatted_email = "$address";
1191 } else {
1192 $formatted_email = "$name <$address>";
1193 }
1194
1195 return $formatted_email;
1196}
1197
Joe Perchesdfa05c22020-04-06 20:10:48 -07001198sub reformat_email {
1199 my ($email) = @_;
1200
1201 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1202 return format_email($email_name, $email_address);
1203}
1204
1205sub same_email_addresses {
1206 my ($email1, $email2) = @_;
1207
1208 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1209 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1210
1211 return $email1_name eq $email2_name &&
1212 $email1_address eq $email2_address;
1213}
1214
Joe Perchesd311cd42014-08-06 16:10:57 -07001215sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001216 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001217
Joe Perchesbd474ca2014-08-06 16:11:10 -07001218 foreach my $path (split(/:/, $ENV{PATH})) {
1219 if (-e "$path/$bin") {
1220 return "$path/$bin";
1221 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001222 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001223
Joe Perchesbd474ca2014-08-06 16:11:10 -07001224 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001225}
1226
Joe Perches000d1cc12011-07-25 17:13:25 -07001227sub which_conf {
1228 my ($conf) = @_;
1229
1230 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1231 if (-e "$path/$conf") {
1232 return "$path/$conf";
1233 }
1234 }
1235
1236 return "";
1237}
1238
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001239sub expand_tabs {
1240 my ($str) = @_;
1241
1242 my $res = '';
1243 my $n = 0;
1244 for my $c (split(//, $str)) {
1245 if ($c eq "\t") {
1246 $res .= ' ';
1247 $n++;
Antonio Borneo713a09d2020-04-06 20:11:07 -07001248 for (; ($n % $tabsize) != 0; $n++) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001249 $res .= ' ';
1250 }
1251 next;
1252 }
1253 $res .= $c;
1254 $n++;
1255 }
1256
1257 return $res;
1258}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001259sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001260 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001261 return $res;
1262}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001263
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001264sub line_stats {
1265 my ($line) = @_;
1266
1267 # Drop the diff line leader and expand tabs
1268 $line =~ s/^.//;
1269 $line = expand_tabs($line);
1270
1271 # Pick the indent from the front of the line.
1272 my ($white) = ($line =~ /^(\s*)/);
1273
1274 return (length($line), length($white));
1275}
1276
Andy Whitcroft773647a2008-03-28 14:15:58 -07001277my $sanitise_quote = '';
1278
1279sub sanitise_line_reset {
1280 my ($in_comment) = @_;
1281
1282 if ($in_comment) {
1283 $sanitise_quote = '*/';
1284 } else {
1285 $sanitise_quote = '';
1286 }
1287}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001288sub sanitise_line {
1289 my ($line) = @_;
1290
1291 my $res = '';
1292 my $l = '';
1293
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001294 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001295 my $off = 0;
1296 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001297
Andy Whitcroft773647a2008-03-28 14:15:58 -07001298 # Always copy over the diff marker.
1299 $res = substr($line, 0, 1);
1300
1301 for ($off = 1; $off < length($line); $off++) {
1302 $c = substr($line, $off, 1);
1303
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001304 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001305 # and end, all to $;.
1306 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1307 $sanitise_quote = '*/';
1308
1309 substr($res, $off, 2, "$;$;");
1310 $off++;
1311 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001312 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001313 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001314 $sanitise_quote = '';
1315 substr($res, $off, 2, "$;$;");
1316 $off++;
1317 next;
1318 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001319 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1320 $sanitise_quote = '//';
1321
1322 substr($res, $off, 2, $sanitise_quote);
1323 $off++;
1324 next;
1325 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001326
1327 # A \ in a string means ignore the next character.
1328 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1329 $c eq "\\") {
1330 substr($res, $off, 2, 'XX');
1331 $off++;
1332 next;
1333 }
1334 # Regular quotes.
1335 if ($c eq "'" || $c eq '"') {
1336 if ($sanitise_quote eq '') {
1337 $sanitise_quote = $c;
1338
1339 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001340 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001341 } elsif ($sanitise_quote eq $c) {
1342 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001343 }
1344 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001345
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001346 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001347 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1348 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001349 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1350 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001351 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1352 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001353 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001354 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001355 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001356 }
1357
Daniel Walker113f04a2009-09-21 17:04:35 -07001358 if ($sanitise_quote eq '//') {
1359 $sanitise_quote = '';
1360 }
1361
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001362 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001363 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001364 my $clean = 'X' x length($1);
1365 $res =~ s@\<.*\>@<$clean>@;
1366
1367 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001368 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001369 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001370 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001371 }
1372
Joe Perchesdadf6802016-08-02 14:04:33 -07001373 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1374 my $match = $1;
1375 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1376 }
1377
Andy Whitcroft00df3442007-06-08 13:47:06 -07001378 return $res;
1379}
1380
Joe Perchesa6962d72013-04-29 16:18:13 -07001381sub get_quoted_string {
1382 my ($line, $rawline) = @_;
1383
Joe Perches478b1792018-04-10 16:33:34 -07001384 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001385 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001386 return substr($rawline, $-[0], $+[0] - $-[0]);
1387}
1388
Andy Whitcroft8905a672007-11-28 16:21:06 -08001389sub ctx_statement_block {
1390 my ($linenr, $remain, $off) = @_;
1391 my $line = $linenr - 1;
1392 my $blk = '';
1393 my $soff = $off;
1394 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001395 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001396
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001397 my $loff = 0;
1398
Andy Whitcroft8905a672007-11-28 16:21:06 -08001399 my $type = '';
1400 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001401 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001402 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001403 my $c;
1404 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001405
1406 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001407 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001408 @stack = (['', 0]) if ($#stack == -1);
1409
Andy Whitcroft773647a2008-03-28 14:15:58 -07001410 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001411 # If we are about to drop off the end, pull in more
1412 # context.
1413 if ($off >= $len) {
1414 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001415 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001416 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001417 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001418 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001419 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001420 $len = length($blk);
1421 $line++;
1422 last;
1423 }
1424 # Bail if there is no further context.
1425 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001426 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001427 last;
1428 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001429 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1430 $level++;
1431 $type = '#';
1432 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001433 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001434 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001435 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001436 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001437
Andy Whitcroft773647a2008-03-28 14:15:58 -07001438 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001439
1440 # Handle nested #if/#else.
1441 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1442 push(@stack, [ $type, $level ]);
1443 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1444 ($type, $level) = @{$stack[$#stack - 1]};
1445 } elsif ($remainder =~ /^#\s*endif\b/) {
1446 ($type, $level) = @{pop(@stack)};
1447 }
1448
Andy Whitcroft8905a672007-11-28 16:21:06 -08001449 # Statement ends at the ';' or a close '}' at the
1450 # outermost level.
1451 if ($level == 0 && $c eq ';') {
1452 last;
1453 }
1454
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001455 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001456 if ($level == 0 && $coff_set == 0 &&
1457 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1458 $remainder =~ /^(else)(?:\s|{)/ &&
1459 $remainder !~ /^else\s+if\b/) {
1460 $coff = $off + length($1) - 1;
1461 $coff_set = 1;
1462 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1463 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001464 }
1465
Andy Whitcroft8905a672007-11-28 16:21:06 -08001466 if (($type eq '' || $type eq '(') && $c eq '(') {
1467 $level++;
1468 $type = '(';
1469 }
1470 if ($type eq '(' && $c eq ')') {
1471 $level--;
1472 $type = ($level != 0)? '(' : '';
1473
1474 if ($level == 0 && $coff < $soff) {
1475 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001476 $coff_set = 1;
1477 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001478 }
1479 }
1480 if (($type eq '' || $type eq '{') && $c eq '{') {
1481 $level++;
1482 $type = '{';
1483 }
1484 if ($type eq '{' && $c eq '}') {
1485 $level--;
1486 $type = ($level != 0)? '{' : '';
1487
1488 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001489 if (substr($blk, $off + 1, 1) eq ';') {
1490 $off++;
1491 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001492 last;
1493 }
1494 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001495 # Preprocessor commands end at the newline unless escaped.
1496 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1497 $level--;
1498 $type = '';
1499 $off++;
1500 last;
1501 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001502 $off++;
1503 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001504 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001505 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001506 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001507 $line++;
1508 $remain--;
1509 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001510
1511 my $statement = substr($blk, $soff, $off - $soff + 1);
1512 my $condition = substr($blk, $soff, $coff - $soff + 1);
1513
1514 #warn "STATEMENT<$statement>\n";
1515 #warn "CONDITION<$condition>\n";
1516
Andy Whitcroft773647a2008-03-28 14:15:58 -07001517 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001518
1519 return ($statement, $condition,
1520 $line, $remain + 1, $off - $loff + 1, $level);
1521}
1522
Andy Whitcroftcf655042008-03-04 14:28:20 -08001523sub statement_lines {
1524 my ($stmt) = @_;
1525
1526 # Strip the diff line prefixes and rip blank lines at start and end.
1527 $stmt =~ s/(^|\n)./$1/g;
1528 $stmt =~ s/^\s*//;
1529 $stmt =~ s/\s*$//;
1530
1531 my @stmt_lines = ($stmt =~ /\n/g);
1532
1533 return $#stmt_lines + 2;
1534}
1535
1536sub statement_rawlines {
1537 my ($stmt) = @_;
1538
1539 my @stmt_lines = ($stmt =~ /\n/g);
1540
1541 return $#stmt_lines + 2;
1542}
1543
1544sub statement_block_size {
1545 my ($stmt) = @_;
1546
1547 $stmt =~ s/(^|\n)./$1/g;
1548 $stmt =~ s/^\s*{//;
1549 $stmt =~ s/}\s*$//;
1550 $stmt =~ s/^\s*//;
1551 $stmt =~ s/\s*$//;
1552
1553 my @stmt_lines = ($stmt =~ /\n/g);
1554 my @stmt_statements = ($stmt =~ /;/g);
1555
1556 my $stmt_lines = $#stmt_lines + 2;
1557 my $stmt_statements = $#stmt_statements + 1;
1558
1559 if ($stmt_lines > $stmt_statements) {
1560 return $stmt_lines;
1561 } else {
1562 return $stmt_statements;
1563 }
1564}
1565
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001566sub ctx_statement_full {
1567 my ($linenr, $remain, $off) = @_;
1568 my ($statement, $condition, $level);
1569
1570 my (@chunks);
1571
Andy Whitcroftcf655042008-03-04 14:28:20 -08001572 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001573 ($statement, $condition, $linenr, $remain, $off, $level) =
1574 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001575 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001576 push(@chunks, [ $condition, $statement ]);
1577 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1578 return ($level, $linenr, @chunks);
1579 }
1580
1581 # Pull in the following conditional/block pairs and see if they
1582 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001583 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001584 ($statement, $condition, $linenr, $remain, $off, $level) =
1585 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001586 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001587 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001588 #print "C: push\n";
1589 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001590 }
1591
1592 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001593}
1594
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001595sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001596 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001597 my $line;
1598 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001599 my $blk = '';
1600 my @o;
1601 my @c;
1602 my @res = ();
1603
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001604 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001605 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001606 for ($line = $start; $remain > 0; $line++) {
1607 next if ($rawlines[$line] =~ /^-/);
1608 $remain--;
1609
1610 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001611
1612 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001613 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001614 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001615 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001616 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001617 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001618 $level = pop(@stack);
1619 }
1620
Andy Whitcroft01464f32010-10-26 14:23:19 -07001621 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001622 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1623 if ($off > 0) {
1624 $off--;
1625 next;
1626 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001627
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001628 if ($c eq $close && $level > 0) {
1629 $level--;
1630 last if ($level == 0);
1631 } elsif ($c eq $open) {
1632 $level++;
1633 }
1634 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001635
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001636 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001637 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001638 }
1639
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001640 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001641 }
1642
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001643 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001644}
1645sub ctx_block_outer {
1646 my ($linenr, $remain) = @_;
1647
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001648 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1649 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001650}
1651sub ctx_block {
1652 my ($linenr, $remain) = @_;
1653
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001654 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1655 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001656}
1657sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001658 my ($linenr, $remain, $off) = @_;
1659
1660 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1661 return @r;
1662}
1663sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001664 my ($linenr, $remain) = @_;
1665
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001666 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001667}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001668sub ctx_statement_level {
1669 my ($linenr, $remain, $off) = @_;
1670
1671 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1672}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001673
1674sub ctx_locate_comment {
1675 my ($first_line, $end_line) = @_;
1676
1677 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001678 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001679 return $current_comment if (defined $current_comment);
1680
1681 # Look through the context and try and figure out if there is a
1682 # comment.
1683 my $in_comment = 0;
1684 $current_comment = '';
1685 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001686 my $line = $rawlines[$linenr - 1];
1687 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001688 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1689 $in_comment = 1;
1690 }
1691 if ($line =~ m@/\*@) {
1692 $in_comment = 1;
1693 }
1694 if (!$in_comment && $current_comment ne '') {
1695 $current_comment = '';
1696 }
1697 $current_comment .= $line . "\n" if ($in_comment);
1698 if ($line =~ m@\*/@) {
1699 $in_comment = 0;
1700 }
1701 }
1702
1703 chomp($current_comment);
1704 return($current_comment);
1705}
1706sub ctx_has_comment {
1707 my ($first_line, $end_line) = @_;
1708 my $cmt = ctx_locate_comment($first_line, $end_line);
1709
Andy Whitcroft00df3442007-06-08 13:47:06 -07001710 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001711 ##print "CMMT: $cmt\n";
1712
1713 return ($cmt ne '');
1714}
1715
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001716sub raw_line {
1717 my ($linenr, $cnt) = @_;
1718
1719 my $offset = $linenr - 1;
1720 $cnt++;
1721
1722 my $line;
1723 while ($cnt) {
1724 $line = $rawlines[$offset++];
1725 next if (defined($line) && $line =~ /^-/);
1726 $cnt--;
1727 }
1728
1729 return $line;
1730}
1731
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001732sub get_stat_real {
1733 my ($linenr, $lc) = @_;
1734
1735 my $stat_real = raw_line($linenr, 0);
1736 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1737 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1738 }
1739
1740 return $stat_real;
1741}
1742
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001743sub get_stat_here {
1744 my ($linenr, $cnt, $here) = @_;
1745
1746 my $herectx = $here . "\n";
1747 for (my $n = 0; $n < $cnt; $n++) {
1748 $herectx .= raw_line($linenr, $n) . "\n";
1749 }
1750
1751 return $herectx;
1752}
1753
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001754sub cat_vet {
1755 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001756 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001757
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001758 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001759 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1760 $res .= $1;
1761 if ($2 ne '') {
1762 $coded = sprintf("^%c", unpack('C', $2) + 64);
1763 $res .= $coded;
1764 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001765 }
1766 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001767
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001768 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001769}
1770
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001771my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001772my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001773my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001774my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001775
1776sub annotate_reset {
1777 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001778 $av_pending = '_';
1779 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001780 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001781}
1782
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001783sub annotate_values {
1784 my ($stream, $type) = @_;
1785
1786 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001787 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001788 my $cur = $stream;
1789
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001790 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001791
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001792 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001793 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001794 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001795 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001796 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001797 print "WS($1)\n" if ($dbg_values > 1);
1798 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001799 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001800 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001801 }
1802
Florian Micklerc023e4732011-01-12 16:59:58 -08001803 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001804 print "CAST($1)\n" if ($dbg_values > 1);
1805 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001806 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001807
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001808 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001809 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001810 $type = 'T';
1811
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001812 } elsif ($cur =~ /^($Modifier)\s*/) {
1813 print "MODIFIER($1)\n" if ($dbg_values > 1);
1814 $type = 'T';
1815
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001816 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001817 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001818 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001819 push(@av_paren_type, $type);
1820 if ($2 ne '') {
1821 $av_pending = 'N';
1822 }
1823 $type = 'E';
1824
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001825 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001826 print "UNDEF($1)\n" if ($dbg_values > 1);
1827 $av_preprocessor = 1;
1828 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001829
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001830 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001831 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001832 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001833
1834 push(@av_paren_type, $type);
1835 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001836 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001837
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001838 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001839 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1840 $av_preprocessor = 1;
1841
1842 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1843
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001844 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001845
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001846 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001847 print "PRE_END($1)\n" if ($dbg_values > 1);
1848
1849 $av_preprocessor = 1;
1850
1851 # Assume all arms of the conditional end as this
1852 # one does, and continue as if the #endif was not here.
1853 pop(@av_paren_type);
1854 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001855 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001856
1857 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001858 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001859
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001860 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1861 print "ATTR($1)\n" if ($dbg_values > 1);
1862 $av_pending = $type;
1863 $type = 'N';
1864
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001865 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001866 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001867 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001868 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001869 }
1870 $type = 'N';
1871
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001872 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001873 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001874 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001875 $type = 'N';
1876
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001877 } elsif ($cur =~/^(case)/o) {
1878 print "CASE($1)\n" if ($dbg_values > 1);
1879 $av_pend_colon = 'C';
1880 $type = 'N';
1881
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001882 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001883 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001884 $type = 'N';
1885
1886 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001887 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001888 push(@av_paren_type, $av_pending);
1889 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001890 $type = 'N';
1891
1892 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001893 my $new_type = pop(@av_paren_type);
1894 if ($new_type ne '_') {
1895 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001896 print "PAREN('$1') -> $type\n"
1897 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001898 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001899 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001900 }
1901
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001902 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001903 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001904 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001905 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001906
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001907 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1908 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001909 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001910 } elsif ($type eq 'E') {
1911 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001912 }
1913 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1914 $type = 'V';
1915
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001916 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001917 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001918 $type = 'V';
1919
1920 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001921 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001922 $type = 'N';
1923
Andy Whitcroftcf655042008-03-04 14:28:20 -08001924 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001925 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001926 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001927 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001928
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001929 } elsif ($cur =~/^(,)/) {
1930 print "COMMA($1)\n" if ($dbg_values > 1);
1931 $type = 'C';
1932
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001933 } elsif ($cur =~ /^(\?)/o) {
1934 print "QUESTION($1)\n" if ($dbg_values > 1);
1935 $type = 'N';
1936
1937 } elsif ($cur =~ /^(:)/o) {
1938 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1939
1940 substr($var, length($res), 1, $av_pend_colon);
1941 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1942 $type = 'E';
1943 } else {
1944 $type = 'N';
1945 }
1946 $av_pend_colon = 'O';
1947
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001948 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001949 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001950 $type = 'N';
1951
Andy Whitcroft0d413862008-10-15 22:02:16 -07001952 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001953 my $variant;
1954
1955 print "OPV($1)\n" if ($dbg_values > 1);
1956 if ($type eq 'V') {
1957 $variant = 'B';
1958 } else {
1959 $variant = 'U';
1960 }
1961
1962 substr($var, length($res), 1, $variant);
1963 $type = 'N';
1964
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001965 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001966 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001967 if ($1 ne '++' && $1 ne '--') {
1968 $type = 'N';
1969 }
1970
1971 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001972 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001973 }
1974 if (defined $1) {
1975 $cur = substr($cur, length($1));
1976 $res .= $type x length($1);
1977 }
1978 }
1979
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001980 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001981}
1982
Andy Whitcroft8905a672007-11-28 16:21:06 -08001983sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001984 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001985 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001986 ^(?:
1987 $Modifier|
1988 $Storage|
1989 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001990 DEFINE_\S+
1991 )$|
1992 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001993 goto|
1994 return|
1995 case|
1996 else|
1997 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001998 do|
1999 \#|
2000 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002001 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07002002 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002003 )}x;
2004 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2005 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002006 # Check for modifiers.
2007 $possible =~ s/\s*$Storage\s*//g;
2008 $possible =~ s/\s*$Sparse\s*//g;
2009 if ($possible =~ /^\s*$/) {
2010
2011 } elsif ($possible =~ /\s/) {
2012 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002013 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002014 if ($modifier !~ $notPermitted) {
2015 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002016 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002017 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002018 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002019
2020 } else {
2021 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002022 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002023 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002024 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002025 } else {
2026 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002027 }
2028}
2029
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002030my $prefix = '';
2031
Joe Perches000d1cc12011-07-25 17:13:25 -07002032sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002033 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002034
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002035 $type =~ tr/[a-z]/[A-Z]/;
2036
Joe Perchescbec18a2014-04-03 14:49:19 -07002037 return defined $use_type{$type} if (scalar keys %use_type > 0);
2038
2039 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002040}
2041
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002042sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002043 my ($level, $type, $msg) = @_;
2044
2045 if (!show_type($type) ||
2046 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002047 return 0;
2048 }
Joe Perches57230292015-06-25 15:03:03 -07002049 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002050 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002051 if ($level eq 'ERROR') {
2052 $output .= RED;
2053 } elsif ($level eq 'WARNING') {
2054 $output .= YELLOW;
2055 } else {
2056 $output .= GREEN;
2057 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002058 }
Joe Perches57230292015-06-25 15:03:03 -07002059 $output .= $prefix . $level . ':';
2060 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002061 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002062 $output .= "$type:";
2063 }
John Brooks737c0762017-07-10 15:52:24 -07002064 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002065 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002066
2067 if ($showfile) {
2068 my @lines = split("\n", $output, -1);
2069 splice(@lines, 1, 1);
2070 $output = join("\n", @lines);
2071 }
Joe Perches57230292015-06-25 15:03:03 -07002072 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002073
Joe Perches57230292015-06-25 15:03:03 -07002074 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002075
2076 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002077}
Joe Perchescbec18a2014-04-03 14:49:19 -07002078
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002079sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002080 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002081}
Joe Perches000d1cc12011-07-25 17:13:25 -07002082
Joe Perchesd752fcc2014-08-06 16:11:05 -07002083sub fixup_current_range {
2084 my ($lineRef, $offset, $length) = @_;
2085
2086 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2087 my $o = $1;
2088 my $l = $2;
2089 my $no = $o + $offset;
2090 my $nl = $l + $length;
2091 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2092 }
2093}
2094
2095sub fix_inserted_deleted_lines {
2096 my ($linesRef, $insertedRef, $deletedRef) = @_;
2097
2098 my $range_last_linenr = 0;
2099 my $delta_offset = 0;
2100
2101 my $old_linenr = 0;
2102 my $new_linenr = 0;
2103
2104 my $next_insert = 0;
2105 my $next_delete = 0;
2106
2107 my @lines = ();
2108
2109 my $inserted = @{$insertedRef}[$next_insert++];
2110 my $deleted = @{$deletedRef}[$next_delete++];
2111
2112 foreach my $old_line (@{$linesRef}) {
2113 my $save_line = 1;
2114 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002115 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002116 $delta_offset = 0;
2117 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2118 $range_last_linenr = $new_linenr;
2119 fixup_current_range(\$line, $delta_offset, 0);
2120 }
2121
2122 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2123 $deleted = @{$deletedRef}[$next_delete++];
2124 $save_line = 0;
2125 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2126 }
2127
2128 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2129 push(@lines, ${$inserted}{'LINE'});
2130 $inserted = @{$insertedRef}[$next_insert++];
2131 $new_linenr++;
2132 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2133 }
2134
2135 if ($save_line) {
2136 push(@lines, $line);
2137 $new_linenr++;
2138 }
2139
2140 $old_linenr++;
2141 }
2142
2143 return @lines;
2144}
2145
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002146sub fix_insert_line {
2147 my ($linenr, $line) = @_;
2148
2149 my $inserted = {
2150 LINENR => $linenr,
2151 LINE => $line,
2152 };
2153 push(@fixed_inserted, $inserted);
2154}
2155
2156sub fix_delete_line {
2157 my ($linenr, $line) = @_;
2158
2159 my $deleted = {
2160 LINENR => $linenr,
2161 LINE => $line,
2162 };
2163
2164 push(@fixed_deleted, $deleted);
2165}
2166
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002167sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002168 my ($type, $msg) = @_;
2169
2170 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002171 our $clean = 0;
2172 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002173 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002174 }
Joe Perches3705ce52013-07-03 15:05:31 -07002175 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002176}
2177sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002178 my ($type, $msg) = @_;
2179
2180 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002181 our $clean = 0;
2182 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002183 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002184 }
Joe Perches3705ce52013-07-03 15:05:31 -07002185 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002186}
2187sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002188 my ($type, $msg) = @_;
2189
2190 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002191 our $clean = 0;
2192 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002193 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002194 }
Joe Perches3705ce52013-07-03 15:05:31 -07002195 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002196}
2197
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002198sub check_absolute_file {
2199 my ($absolute, $herecurr) = @_;
2200 my $file = $absolute;
2201
2202 ##print "absolute<$absolute>\n";
2203
2204 # See if any suffix of this path is a path within the tree.
2205 while ($file =~ s@^[^/]*/@@) {
2206 if (-f "$root/$file") {
2207 ##print "file<$file>\n";
2208 last;
2209 }
2210 }
2211 if (! -f _) {
2212 return 0;
2213 }
2214
2215 # It is, so see if the prefix is acceptable.
2216 my $prefix = $absolute;
2217 substr($prefix, -length($file)) = '';
2218
2219 ##print "prefix<$prefix>\n";
2220 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002221 WARN("USE_RELATIVE_PATH",
2222 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002223 }
2224}
2225
Joe Perches3705ce52013-07-03 15:05:31 -07002226sub trim {
2227 my ($string) = @_;
2228
Joe Perchesb34c6482013-09-11 14:24:01 -07002229 $string =~ s/^\s+|\s+$//g;
2230
2231 return $string;
2232}
2233
2234sub ltrim {
2235 my ($string) = @_;
2236
2237 $string =~ s/^\s+//;
2238
2239 return $string;
2240}
2241
2242sub rtrim {
2243 my ($string) = @_;
2244
2245 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002246
2247 return $string;
2248}
2249
Joe Perches52ea8502013-11-12 15:10:09 -08002250sub string_find_replace {
2251 my ($string, $find, $replace) = @_;
2252
2253 $string =~ s/$find/$replace/g;
2254
2255 return $string;
2256}
2257
Joe Perches3705ce52013-07-03 15:05:31 -07002258sub tabify {
2259 my ($leading) = @_;
2260
Antonio Borneo713a09d2020-04-06 20:11:07 -07002261 my $source_indent = $tabsize;
Joe Perches3705ce52013-07-03 15:05:31 -07002262 my $max_spaces_before_tab = $source_indent - 1;
2263 my $spaces_to_tab = " " x $source_indent;
2264
2265 #convert leading spaces to tabs
2266 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2267 #Remove spaces before a tab
2268 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2269
2270 return "$leading";
2271}
2272
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002273sub pos_last_openparen {
2274 my ($line) = @_;
2275
2276 my $pos = 0;
2277
2278 my $opens = $line =~ tr/\(/\(/;
2279 my $closes = $line =~ tr/\)/\)/;
2280
2281 my $last_openparen = 0;
2282
2283 if (($opens == 0) || ($closes >= $opens)) {
2284 return -1;
2285 }
2286
2287 my $len = length($line);
2288
2289 for ($pos = 0; $pos < $len; $pos++) {
2290 my $string = substr($line, $pos);
2291 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2292 $pos += length($1) - 1;
2293 } elsif (substr($line, $pos, 1) eq '(') {
2294 $last_openparen = $pos;
2295 } elsif (index($string, '(') == -1) {
2296 last;
2297 }
2298 }
2299
Joe Perches91cb5192014-04-03 14:49:32 -07002300 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002301}
2302
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002303sub get_raw_comment {
2304 my ($line, $rawline) = @_;
2305 my $comment = '';
2306
2307 for my $i (0 .. (length($line) - 1)) {
2308 if (substr($line, $i, 1) eq "$;") {
2309 $comment .= substr($rawline, $i, 1);
2310 }
2311 }
2312
2313 return $comment;
2314}
2315
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002316sub process {
2317 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002318
2319 my $linenr=0;
2320 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002321 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002322 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002323 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002324
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002325 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002326 my $indent;
2327 my $previndent=0;
2328 my $stashindent=0;
2329
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002330 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002331 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002332 my $author = '';
2333 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002334 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002335 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002336 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002337 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002338 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002339 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002340 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002341 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002342 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002343 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002344 my $non_utf8_charset = 0;
2345
Joe Perches365dd4e2014-08-06 16:10:42 -07002346 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002347 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002348
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002349 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002350 our $cnt_lines = 0;
2351 our $cnt_error = 0;
2352 our $cnt_warn = 0;
2353 our $cnt_chk = 0;
2354
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002355 # Trace the real file/line as we go.
2356 my $realfile = '';
2357 my $realline = 0;
2358 my $realcnt = 0;
2359 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002360 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002361 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002362 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002363 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002364 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002365
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002366 my $prev_values = 'E';
2367
2368 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002369 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002370 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002371 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002372 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002373
Joe Perches7e51f192013-09-11 14:23:57 -07002374 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002375
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002376 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002377 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002378 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002379 my @setup_docs = ();
2380 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002381
Joe Perchesd8b07712013-11-12 15:10:06 -08002382 my $camelcase_file_seeded = 0;
2383
Rob Herring9f3a8992018-04-10 16:33:13 -07002384 my $checklicenseline = 1;
2385
Andy Whitcroft773647a2008-03-28 14:15:58 -07002386 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002387 my $line;
2388 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002389 $linenr++;
2390 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002391
Joe Perches3705ce52013-07-03 15:05:31 -07002392 push(@fixed, $rawline) if ($fix);
2393
Andy Whitcroft773647a2008-03-28 14:15:58 -07002394 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002395 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002396 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002397 $setup_docs = 1;
2398 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002399 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002400 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002401 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002402 $realline=$1-1;
2403 if (defined $2) {
2404 $realcnt=$3+1;
2405 } else {
2406 $realcnt=1+1;
2407 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002408 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002409
2410 # Guestimate if this is a continuing comment. Run
2411 # the context looking for a comment "edge". If this
2412 # edge is a close comment then we must be in a comment
2413 # at context start.
2414 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002415 my $cnt = $realcnt;
2416 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2417 next if (defined $rawlines[$ln - 1] &&
2418 $rawlines[$ln - 1] =~ /^-/);
2419 $cnt--;
2420 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002421 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002422 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2423 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2424 ($edge) = $1;
2425 last;
2426 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002427 }
2428 if (defined $edge && $edge eq '*/') {
2429 $in_comment = 1;
2430 }
2431
2432 # Guestimate if this is a continuing comment. If this
2433 # is the start of a diff block and this line starts
2434 # ' *' then it is very likely a comment.
2435 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002436 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002437 {
2438 $in_comment = 1;
2439 }
2440
2441 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2442 sanitise_line_reset($in_comment);
2443
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002444 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002445 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002446 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002447 $line = sanitise_line($rawline);
2448 }
2449 push(@lines, $line);
2450
2451 if ($realcnt > 1) {
2452 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2453 } else {
2454 $realcnt = 0;
2455 }
2456
2457 #print "==>$rawline\n";
2458 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002459
2460 if ($setup_docs && $line =~ /^\+/) {
2461 push(@setup_docs, $line);
2462 }
2463 }
2464
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002465 $prefix = '';
2466
Andy Whitcroft773647a2008-03-28 14:15:58 -07002467 $realcnt = 0;
2468 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002469 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002470 foreach my $line (@lines) {
2471 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002472 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002473 my $sline = $line; #copy of $line
2474 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002475
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002476 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002477 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002478
Joe Perches12c253a2018-06-07 17:10:58 -07002479# check if it's a mode change, rename or start of a patch
2480 if (!$in_commit_log &&
2481 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2482 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2483 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2484 $is_patch = 1;
2485 }
2486
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002487#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002488 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002489 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2490 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002491 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002492 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002493 $realline=$1-1;
2494 if (defined $2) {
2495 $realcnt=$3+1;
2496 } else {
2497 $realcnt=1+1;
2498 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002499 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002500 $prev_values = 'E';
2501
Andy Whitcroft773647a2008-03-28 14:15:58 -07002502 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002503 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002504 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002505 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002506 if ($context =~ /\b(\w+)\s*\(/) {
2507 $context_function = $1;
2508 } else {
2509 undef $context_function;
2510 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002511 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002512
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002513# track the line number as we move through the hunk, note that
2514# new versions of GNU diff omit the leading space on completely
2515# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002516 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002517 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002518 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002519
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002520 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002521 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002522
2523 # Track the previous line.
2524 ($prevline, $stashline) = ($stashline, $line);
2525 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002526 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2527
Andy Whitcroft773647a2008-03-28 14:15:58 -07002528 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002529
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002530 } elsif ($realcnt == 1) {
2531 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002532 }
2533
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002534 my $hunk_line = ($realcnt != 0);
2535
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002536 $here = "#$linenr: " if (!$file);
2537 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002538
Joe Perches2ac73b42014-06-04 16:12:05 -07002539 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002540 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002541 if ($line =~ /^diff --git.*?(\S+)$/) {
2542 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002543 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002544 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002545 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002546 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002547 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002548 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002549 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002550
2551 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002552 if (!$file && $tree && $p1_prefix ne '' &&
2553 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002554 WARN("PATCH_PREFIX",
2555 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002556 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002557
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002558 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002559 ERROR("MODIFIED_INCLUDE_ASM",
2560 "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 -07002561 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002562 $found_file = 1;
2563 }
2564
Joe Perches34d88152015-06-25 15:03:05 -07002565#make up the handle for any error we report on this line
2566 if ($showfile) {
2567 $prefix = "$realfile:$realline: "
2568 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002569 if ($file) {
2570 $prefix = "$filename:$realline: ";
2571 } else {
2572 $prefix = "$filename:$linenr: ";
2573 }
Joe Perches34d88152015-06-25 15:03:05 -07002574 }
2575
Joe Perches2ac73b42014-06-04 16:12:05 -07002576 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002577 if (is_maintained_obsolete($realfile)) {
2578 WARN("OBSOLETE",
2579 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2580 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002581 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002582 $check = 1;
2583 } else {
2584 $check = $check_orig;
2585 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002586 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002587
2588 if ($realfile !~ /^MAINTAINERS/) {
2589 my $last_binding_patch = $is_binding_patch;
2590
2591 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2592
2593 if (($last_binding_patch != -1) &&
2594 ($last_binding_patch ^ $is_binding_patch)) {
2595 WARN("DT_SPLIT_BINDING_PATCH",
2596 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2597 }
2598 }
2599
Andy Whitcroft773647a2008-03-28 14:15:58 -07002600 next;
2601 }
2602
Randy Dunlap389834b2007-06-08 13:47:03 -07002603 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002604
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002605 my $hereline = "$here\n$rawline\n";
2606 my $herecurr = "$here\n$rawline\n";
2607 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002608
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002609 $cnt_lines++ if ($realcnt != 0);
2610
Joe Perches490b2922018-08-21 21:58:01 -07002611# Verify the existence of a commit log if appropriate
2612# 2 is used because a $signature is counted in $commit_log_lines
2613 if ($in_commit_log) {
2614 if ($line !~ /^\s*$/) {
2615 $commit_log_lines++; #could be a $signature
2616 }
2617 } elsif ($has_commit_log && $commit_log_lines < 2) {
2618 WARN("COMMIT_MESSAGE",
2619 "Missing commit description - Add an appropriate one\n");
2620 $commit_log_lines = 2; #warn only once
2621 }
2622
Joe Perchese518e9a2015-06-25 15:03:27 -07002623# Check if the commit log has what seems like a diff which can confuse patch
2624 if ($in_commit_log && !$commit_log_has_diff &&
2625 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2626 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2627 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2628 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2629 ERROR("DIFF_IN_COMMIT_MSG",
2630 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2631 $commit_log_has_diff = 1;
2632 }
2633
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002634# Check for incorrect file permissions
2635 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2636 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002637 if ($realfile !~ m@scripts/@ &&
2638 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002639 ERROR("EXECUTE_PERMISSIONS",
2640 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002641 }
2642 }
2643
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002644# Check the patch for a From:
2645 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2646 $author = $1;
2647 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2648 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002649 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002650 }
2651
Joe Perches20112472011-07-25 17:13:23 -07002652# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002653 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002654 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002655 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002656 if ($author ne '') {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002657 if (same_email_addresses($1, $author)) {
2658 $authorsignoff = 1;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002659 }
2660 }
Joe Perches20112472011-07-25 17:13:23 -07002661 }
2662
Joe Perchese0d975b2014-12-10 15:51:49 -08002663# Check if MAINTAINERS is being updated. If so, there's probably no need to
2664# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2665 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2666 $reported_maintainer_file = 1;
2667 }
2668
Joe Perches20112472011-07-25 17:13:23 -07002669# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002670 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002671 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002672 my $space_before = $1;
2673 my $sign_off = $2;
2674 my $space_after = $3;
2675 my $email = $4;
2676 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2677
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002678 if ($sign_off !~ /$signature_tags/) {
2679 WARN("BAD_SIGN_OFF",
2680 "Non-standard signature: $sign_off\n" . $herecurr);
2681 }
Joe Perches20112472011-07-25 17:13:23 -07002682 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002683 if (WARN("BAD_SIGN_OFF",
2684 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2685 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002686 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002687 "$ucfirst_sign_off $email";
2688 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002689 }
Joe Perches20112472011-07-25 17:13:23 -07002690 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002691 if (WARN("BAD_SIGN_OFF",
2692 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2693 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002694 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002695 "$ucfirst_sign_off $email";
2696 }
2697
Joe Perches20112472011-07-25 17:13:23 -07002698 }
2699 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002700 if (WARN("BAD_SIGN_OFF",
2701 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2702 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002703 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002704 "$ucfirst_sign_off $email";
2705 }
Joe Perches20112472011-07-25 17:13:23 -07002706 }
2707
Joe Perchesdfa05c22020-04-06 20:10:48 -07002708 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Joe Perches20112472011-07-25 17:13:23 -07002709 my $suggested_email = format_email(($email_name, $email_address));
2710 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002711 ERROR("BAD_SIGN_OFF",
2712 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002713 } else {
2714 my $dequoted = $suggested_email;
2715 $dequoted =~ s/^"//;
2716 $dequoted =~ s/" </ </;
2717 # Don't force email to have quotes
2718 # Allow just an angle bracketed address
Joe Perchesdfa05c22020-04-06 20:10:48 -07002719 if (!same_email_addresses($email, $suggested_email)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002720 WARN("BAD_SIGN_OFF",
2721 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002722 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002723 }
Joe Perches7e51f192013-09-11 14:23:57 -07002724
2725# Check for duplicate signatures
2726 my $sig_nospace = $line;
2727 $sig_nospace =~ s/\s//g;
2728 $sig_nospace = lc($sig_nospace);
2729 if (defined $signatures{$sig_nospace}) {
2730 WARN("BAD_SIGN_OFF",
2731 "Duplicate signature\n" . $herecurr);
2732 } else {
2733 $signatures{$sig_nospace} = 1;
2734 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002735
2736# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2737 if ($sign_off =~ /^co-developed-by:$/i) {
2738 if ($email eq $author) {
2739 WARN("BAD_SIGN_OFF",
2740 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2741 }
2742 if (!defined $lines[$linenr]) {
2743 WARN("BAD_SIGN_OFF",
2744 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2745 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2746 WARN("BAD_SIGN_OFF",
2747 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2748 } elsif ($1 ne $email) {
2749 WARN("BAD_SIGN_OFF",
2750 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2751 }
2752 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002753 }
2754
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002755# Check email subject for common tools that don't need to be mentioned
2756 if ($in_header_lines &&
2757 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2758 WARN("EMAIL_SUBJECT",
2759 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2760 }
2761
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002762# Check for unwanted Gerrit info
2763 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2764 ERROR("GERRIT_CHANGE_ID",
2765 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2766 }
2767
Joe Perches369c8dd2015-11-06 16:31:34 -08002768# Check if the commit log is in a possible stack dump
2769 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2770 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2771 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2772 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07002773 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2774 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2775 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2776 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08002777 $commit_log_possible_stack_dump = 1;
2778 }
2779
Joe Perches2a076f42015-04-16 12:44:28 -07002780# Check for line lengths > 75 in commit log, warn once
2781 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002782 length($line) > 75 &&
2783 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2784 # file delta changes
2785 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2786 # filename then :
2787 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2788 # A Fixes: or Link: line
2789 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002790 WARN("COMMIT_LOG_LONG_LINE",
2791 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2792 $commit_log_long_line = 1;
2793 }
2794
Joe Perchesbf4daf12015-09-09 15:37:50 -07002795# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002796 if ($in_commit_log && $commit_log_possible_stack_dump &&
2797 $line =~ /^\s*$/) {
2798 $commit_log_possible_stack_dump = 0;
2799 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002800
Joe Perches0d7835f2015-02-13 14:38:35 -08002801# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002802 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07002803 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002804 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002805 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002806 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002807 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2808 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002809 my $init_char = "c";
2810 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002811 my $short = 1;
2812 my $long = 0;
2813 my $case = 1;
2814 my $space = 1;
2815 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002816 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002817 my $id = '0123456789ab';
2818 my $orig_desc = "commit description";
2819 my $description = "";
2820
Joe Perchesfe043ea2015-09-09 15:37:25 -07002821 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2822 $init_char = $1;
2823 $orig_commit = lc($2);
2824 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2825 $orig_commit = lc($1);
2826 }
2827
Joe Perches0d7835f2015-02-13 14:38:35 -08002828 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2829 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2830 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2831 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2832 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2833 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002834 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002835 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2836 defined $rawlines[$linenr] &&
2837 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2838 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002839 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002840 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2841 defined $rawlines[$linenr] &&
2842 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2843 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2844 $orig_desc = $1;
2845 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2846 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002847 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002848 }
2849
2850 ($id, $description) = git_commit_info($orig_commit,
2851 $id, $orig_desc);
2852
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002853 if (defined($id) &&
2854 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002855 ERROR("GIT_COMMIT_ID",
2856 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2857 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002858 }
2859
Joe Perches13f19372014-08-06 16:10:59 -07002860# Check for added, moved or deleted files
2861 if (!$reported_maintainer_file && !$in_commit_log &&
2862 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2863 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2864 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2865 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002866 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002867 $reported_maintainer_file = 1;
2868 WARN("FILE_PATH_CHANGES",
2869 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2870 }
2871
Rob Herringe400edb2019-09-12 15:18:20 +01002872# Check for adding new DT bindings not in schema format
2873 if (!$in_commit_log &&
2874 ($line =~ /^new file mode\s*\d+\s*$/) &&
2875 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2876 WARN("DT_SCHEMA_BINDING_PATCH",
2877 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2878 }
2879
Andy Whitcroft00df3442007-06-08 13:47:06 -07002880# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002881 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002882 ERROR("CORRUPTED_PATCH",
2883 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002884 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002885 }
2886
2887# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2888 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002889 $rawline !~ m/^$UTF8*$/) {
2890 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2891
2892 my $blank = copy_spacing($rawline);
2893 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2894 my $hereptr = "$hereline$ptr\n";
2895
Joe Perches34d99212011-07-25 17:13:26 -07002896 CHK("INVALID_UTF8",
2897 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002898 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002899
Joe Perches15662b32011-10-31 17:13:12 -07002900# Check if it's the start of a commit log
2901# (not a header line and we haven't seen the patch filename)
2902 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002903 !($rawline =~ /^\s+(?:\S|$)/ ||
2904 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002905 $in_header_lines = 0;
2906 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002907 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002908 }
2909
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002910# Check if there is UTF-8 in a commit log when a mail header has explicitly
2911# declined it, i.e defined some charset where it is missing.
2912 if ($in_header_lines &&
2913 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2914 $1 !~ /utf-8/i) {
2915 $non_utf8_charset = 1;
2916 }
2917
2918 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002919 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002920 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002921 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2922 }
2923
Joe Perchesd6430f72016-12-12 16:46:28 -08002924# Check for absolute kernel paths in commit message
2925 if ($tree && $in_commit_log) {
2926 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2927 my $file = $1;
2928
2929 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2930 check_absolute_file($1, $herecurr)) {
2931 #
2932 } else {
2933 check_absolute_file($file, $herecurr);
2934 }
2935 }
2936 }
2937
Kees Cook66b47b42014-10-13 15:51:57 -07002938# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002939 if (defined($misspellings) &&
2940 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002941 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002942 my $typo = $1;
2943 my $typo_fix = $spelling_fix{lc($typo)};
2944 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2945 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002946 my $msg_level = \&WARN;
2947 $msg_level = \&CHK if ($file);
2948 if (&{$msg_level}("TYPO_SPELLING",
2949 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002950 $fix) {
2951 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2952 }
2953 }
2954 }
2955
Matteo Crocea8dd86b2019-09-25 16:46:38 -07002956# check for invalid commit id
2957 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2958 my $id;
2959 my $description;
2960 ($id, $description) = git_commit_info($2, undef, undef);
2961 if (!defined($id)) {
2962 WARN("UNKNOWN_COMMIT_ID",
2963 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2964 }
2965 }
2966
Andy Whitcroft306708542008-10-15 22:02:28 -07002967# ignore non-hunk lines and lines being removed
2968 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002969
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002970#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002971 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002972 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002973 if (ERROR("DOS_LINE_ENDINGS",
2974 "DOS line endings\n" . $herevet) &&
2975 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002976 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002977 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002978 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2979 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002980 if (ERROR("TRAILING_WHITESPACE",
2981 "trailing whitespace\n" . $herevet) &&
2982 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002983 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002984 }
2985
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002986 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002987 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002988
Josh Triplett4783f892013-11-12 15:10:12 -08002989# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002990 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002991 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002992 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2993 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002994 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07002995 my $msg_level = \&ERROR;
2996 $msg_level = \&CHK if ($file);
2997 &{$msg_level}("FSF_MAILING_ADDRESS",
2998 "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 -08002999 }
3000
Andi Kleen33549572010-05-24 14:33:29 -07003001# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003002# Only applies when adding the entry originally, after that we do not have
3003# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003004 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003005 # 'choice' is usually the last thing on the line (though
3006 # Kconfig supports named choices), so use a word boundary
3007 # (\b) rather than a whitespace character (\s)
3008 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003009 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003010 my $cnt = $realcnt;
3011 my $ln = $linenr + 1;
3012 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003013 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003014 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003015 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003016 $f = $lines[$ln - 1];
3017 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3018 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003019
3020 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003021 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003022
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003023 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003024 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01003025 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
3026 if ($lines[$ln - 1] =~ "---help---") {
3027 WARN("CONFIG_DESCRIPTION",
3028 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
3029 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003030 $length = -1;
3031 }
3032
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003033 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003034 $f =~ s/#.*//;
3035 $f =~ s/^\s+//;
3036 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003037
3038 # This only checks context lines in the patch
3039 # and so hopefully shouldn't trigger false
3040 # positives, even though some of these are
3041 # common words in help texts
3042 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3043 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003044 $is_end = 1;
3045 last;
3046 }
Andi Kleen33549572010-05-24 14:33:29 -07003047 $length++;
3048 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003049 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3050 WARN("CONFIG_DESCRIPTION",
3051 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3052 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003053 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003054 }
3055
Joe Perches628f91a2017-07-10 15:52:07 -07003056# check for MAINTAINERS entries that don't have the right form
3057 if ($realfile =~ /^MAINTAINERS$/ &&
3058 $rawline =~ /^\+[A-Z]:/ &&
3059 $rawline !~ /^\+[A-Z]:\t\S/) {
3060 if (WARN("MAINTAINERS_STYLE",
3061 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3062 $fix) {
3063 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3064 }
3065 }
3066
Christoph Jaeger327953e2015-02-13 14:38:29 -08003067# discourage the use of boolean for type definition attributes of Kconfig options
3068 if ($realfile =~ /Kconfig/ &&
3069 $line =~ /^\+\s*\bboolean\b/) {
3070 WARN("CONFIG_TYPE_BOOLEAN",
3071 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3072 }
3073
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003074 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3075 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3076 my $flag = $1;
3077 my $replacement = {
3078 'EXTRA_AFLAGS' => 'asflags-y',
3079 'EXTRA_CFLAGS' => 'ccflags-y',
3080 'EXTRA_CPPFLAGS' => 'cppflags-y',
3081 'EXTRA_LDFLAGS' => 'ldflags-y',
3082 };
3083
3084 WARN("DEPRECATED_VARIABLE",
3085 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3086 }
3087
Rob Herringbff5da42014-01-23 15:54:51 -08003088# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003089 if (defined $root &&
3090 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3091 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3092
Rob Herringbff5da42014-01-23 15:54:51 -08003093 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3094
Florian Vaussardcc933192014-04-03 14:49:27 -07003095 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003096 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003097
Rob Herringbff5da42014-01-23 15:54:51 -08003098 foreach my $compat (@compats) {
3099 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003100 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3101 my $compat3 = $compat;
3102 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3103 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003104 if ( $? >> 8 ) {
3105 WARN("UNDOCUMENTED_DT_STRING",
3106 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3107 }
3108
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003109 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3110 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003111 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003112 if ( $? >> 8 ) {
3113 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003114 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003115 }
3116 }
3117 }
3118
Rob Herring9f3a8992018-04-10 16:33:13 -07003119# check for using SPDX license tag at beginning of files
3120 if ($realline == $checklicenseline) {
3121 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3122 $checklicenseline = 2;
3123 } elsif ($rawline =~ /^\+/) {
3124 my $comment = "";
3125 if ($realfile =~ /\.(h|s|S)$/) {
3126 $comment = '/*';
3127 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3128 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003129 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003130 $comment = '#';
3131 } elsif ($realfile =~ /\.rst$/) {
3132 $comment = '..';
3133 }
3134
Joe Perchesfdf13692019-03-07 16:28:32 -08003135# check SPDX comment style for .[chsS] files
3136 if ($realfile =~ /\.[chsS]$/ &&
3137 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003138 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003139 WARN("SPDX_LICENSE_TAG",
3140 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3141 }
3142
Rob Herring9f3a8992018-04-10 16:33:13 -07003143 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003144 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3145 WARN("SPDX_LICENSE_TAG",
3146 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003147 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003148 my $spdx_license = $1;
3149 if (!is_SPDX_License_valid($spdx_license)) {
3150 WARN("SPDX_LICENSE_TAG",
3151 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3152 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003153 }
3154 }
3155 }
3156
Andy Whitcroft5368df202008-10-15 22:02:27 -07003157# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003158 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003159
Joe Perchesa8da38a2019-03-07 16:28:42 -08003160# check for using SPDX-License-Identifier on the wrong line number
3161 if ($realline != $checklicenseline &&
3162 $rawline =~ /\bSPDX-License-Identifier:/ &&
3163 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3164 WARN("SPDX_LICENSE_TAG",
3165 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3166 }
3167
Joe Perches47e0c882015-06-25 15:02:57 -07003168# line length limit (with some exclusions)
3169#
3170# There are a few types of lines that may extend beyond $max_line_length:
3171# logging functions like pr_info that end in a string
3172# lines with a single string
3173# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003174# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003175#
3176# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003177# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003178# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3179# LONG_LINE all other lines longer than $max_line_length
3180#
3181# if LONG_LINE is ignored, the other 2 types are also ignored
3182#
3183
Joe Perchesb4749e92015-07-17 16:24:01 -07003184 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003185 my $msg_type = "LONG_LINE";
3186
3187 # Check the allowed long line types first
3188
3189 # logging functions that end in a string that starts
3190 # before $max_line_length
3191 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3192 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3193 $msg_type = "";
3194
3195 # lines with only strings (w/ possible termination)
3196 # #defines with only strings
3197 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3198 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3199 $msg_type = "";
3200
Joe Perchescc147502017-11-17 15:28:44 -08003201 # More special cases
3202 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3203 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003204 $msg_type = "";
3205
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003206 # URL ($rawline is used in case the URL is in a comment)
3207 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3208 $msg_type = "";
3209
Joe Perches47e0c882015-06-25 15:02:57 -07003210 # Otherwise set the alternate message types
3211
3212 # a comment starts before $max_line_length
3213 } elsif ($line =~ /($;[\s$;]*)$/ &&
3214 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3215 $msg_type = "LONG_LINE_COMMENT"
3216
3217 # a quoted string starts before $max_line_length
3218 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3219 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3220 $msg_type = "LONG_LINE_STRING"
3221 }
3222
3223 if ($msg_type ne "" &&
3224 (show_type("LONG_LINE") || show_type($msg_type))) {
3225 WARN($msg_type,
3226 "line over $max_line_length characters\n" . $herecurr);
3227 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003228 }
3229
Andy Whitcroft8905a672007-11-28 16:21:06 -08003230# check for adding lines without a newline.
3231 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003232 WARN("MISSING_EOF_NEWLINE",
3233 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003234 }
3235
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003236# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003237 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003238
3239# at the beginning of a line any tabs must come first and anything
Antonio Borneo713a09d2020-04-06 20:11:07 -07003240# more than $tabsize must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003241 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3242 $rawline =~ /^\+\s* \s*/) {
3243 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003244 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003245 if (ERROR("CODE_INDENT",
3246 "code indent should use tabs where possible\n" . $herevet) &&
3247 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003248 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003249 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003250 }
3251
Alberto Panizzo08e44362010-03-05 13:43:54 -08003252# check for space before tabs.
3253 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3254 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003255 if (WARN("SPACE_BEFORE_TAB",
3256 "please, no space before tabs\n" . $herevet) &&
3257 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003258 while ($fixed[$fixlinenr] =~
Antonio Borneo713a09d2020-04-06 20:11:07 -07003259 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003260 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003261 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003262 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003263 }
3264
Joe Perches6a487212018-04-10 16:34:04 -07003265# check for assignments on the start of a line
3266 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3267 CHK("ASSIGNMENT_CONTINUATIONS",
3268 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3269 }
3270
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003271# check for && or || at the start of a line
3272 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3273 CHK("LOGICAL_CONTINUATIONS",
3274 "Logical continuations should be on the previous line\n" . $hereprev);
3275 }
3276
Joe Perchesa91e8992016-05-20 17:04:05 -07003277# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003278 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003279 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003280 my $indent = length($1);
Antonio Borneo713a09d2020-04-06 20:11:07 -07003281 if ($indent % $tabsize) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003282 if (WARN("TABSTOP",
3283 "Statements should start on a tabstop\n" . $herecurr) &&
3284 $fix) {
Antonio Borneo713a09d2020-04-06 20:11:07 -07003285 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Joe Perchesa91e8992016-05-20 17:04:05 -07003286 }
3287 }
3288 }
3289
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003290# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003291 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003292 $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 -07003293 $prevline =~ /^\+(\t*)(.*)$/;
3294 my $oldindent = $1;
3295 my $rest = $2;
3296
3297 my $pos = pos_last_openparen($rest);
3298 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003299 $line =~ /^(\+| )([ \t]*)/;
3300 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003301
3302 my $goodtabindent = $oldindent .
Antonio Borneo713a09d2020-04-06 20:11:07 -07003303 "\t" x ($pos / $tabsize) .
3304 " " x ($pos % $tabsize);
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003305 my $goodspaceindent = $oldindent . " " x $pos;
3306
3307 if ($newindent ne $goodtabindent &&
3308 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003309
3310 if (CHK("PARENTHESIS_ALIGNMENT",
3311 "Alignment should match open parenthesis\n" . $hereprev) &&
3312 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003313 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003314 s/^\+[ \t]*/\+$goodtabindent/;
3315 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003316 }
3317 }
3318 }
3319
Joe Perches6ab3a972015-04-16 12:44:05 -07003320# check for space after cast like "(int) foo" or "(struct foo) bar"
3321# avoid checking a few false positives:
3322# "sizeof(<type>)" or "__alignof__(<type>)"
3323# function pointer declarations like "(*foo)(int) = bar;"
3324# structure definitions like "(struct foo) { 0 };"
3325# multiline macros that define functions
3326# known attributes or the __attribute__ keyword
3327 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3328 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003329 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003330 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003331 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003332 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003333 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003334 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003335 }
3336
Joe Perches86406b12015-09-09 15:37:41 -07003337# Block comment styles
3338# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003339 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003340 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003341 $rawline =~ /^\+[ \t]*\*/ &&
3342 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003343 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3344 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3345 }
3346
Joe Perches86406b12015-09-09 15:37:41 -07003347# Block comments use * on subsequent lines
3348 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3349 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003350 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003351 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003352 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003353 WARN("BLOCK_COMMENT_STYLE",
3354 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003355 }
3356
Joe Perches86406b12015-09-09 15:37:41 -07003357# Block comments use */ on trailing lines
3358 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003359 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3360 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3361 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003362 WARN("BLOCK_COMMENT_STYLE",
3363 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003364 }
3365
Joe Perches08eb9b82016-10-11 13:51:50 -07003366# Block comment * alignment
3367 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003368 $line =~ /^\+[ \t]*$;/ && #leading comment
3369 $rawline =~ /^\+[ \t]*\*/ && #leading *
3370 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003371 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003372 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3373 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003374 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003375 if (defined($1)) {
3376 $oldindent = expand_tabs($1);
3377 } else {
3378 $prevrawline =~ m@^\+(.*/?)\*@;
3379 $oldindent = expand_tabs($1);
3380 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003381 $rawline =~ m@^\+([ \t]*)\*@;
3382 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003383 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003384 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003385 WARN("BLOCK_COMMENT_STYLE",
3386 "Block comments should align the * on each line\n" . $hereprev);
3387 }
3388 }
3389
Joe Perches7f619192014-08-06 16:10:39 -07003390# check for missing blank lines after struct/union declarations
3391# with exceptions for various attributes and macros
3392 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3393 $line =~ /^\+/ &&
3394 !($line =~ /^\+\s*$/ ||
3395 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3396 $line =~ /^\+\s*MODULE_/i ||
3397 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3398 $line =~ /^\+[a-z_]*init/ ||
3399 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3400 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003401 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003402 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003403 if (CHK("LINE_SPACING",
3404 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3405 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003406 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003407 }
Joe Perches7f619192014-08-06 16:10:39 -07003408 }
3409
Joe Perches365dd4e2014-08-06 16:10:42 -07003410# check for multiple consecutive blank lines
3411 if ($prevline =~ /^[\+ ]\s*$/ &&
3412 $line =~ /^\+\s*$/ &&
3413 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003414 if (CHK("LINE_SPACING",
3415 "Please don't use multiple blank lines\n" . $hereprev) &&
3416 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003417 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003418 }
3419
Joe Perches365dd4e2014-08-06 16:10:42 -07003420 $last_blank_line = $linenr;
3421 }
3422
Joe Perches3b617e32014-04-03 14:49:28 -07003423# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003424 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3425 # actual declarations
3426 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003427 # function pointer declarations
3428 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003429 # foo bar; where foo is some local typedef or #define
3430 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3431 # known declaration macros
3432 $prevline =~ /^\+\s+$declaration_macros/) &&
3433 # for "else if" which can look like "$Ident $Ident"
3434 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3435 # other possible extensions of declaration lines
3436 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3437 # not starting a section or a macro "\" extended line
3438 $prevline =~ /(?:\{\s*|\\)$/) &&
3439 # looks like a declaration
3440 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003441 # function pointer declarations
3442 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003443 # foo bar; where foo is some local typedef or #define
3444 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3445 # known declaration macros
3446 $sline =~ /^\+\s+$declaration_macros/ ||
3447 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003448 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003449 # start or end of block or continuation of declaration
3450 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3451 # bitfield continuation
3452 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3453 # other possible extensions of declaration lines
3454 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3455 # indentation of previous and current line are the same
3456 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003457 if (WARN("LINE_SPACING",
3458 "Missing a blank line after declarations\n" . $hereprev) &&
3459 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003460 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003461 }
Joe Perches3b617e32014-04-03 14:49:28 -07003462 }
3463
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003464# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003465# Exceptions:
3466# 1) within comments
3467# 2) indented preprocessor commands
3468# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003469 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003470 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003471 if (WARN("LEADING_SPACE",
3472 "please, no spaces at the start of a line\n" . $herevet) &&
3473 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003474 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003475 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003476 }
3477
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003478# check we are in a valid C source file if not then ignore this hunk
3479 next if ($realfile !~ /\.(h|c)$/);
3480
Joe Perches5751a242017-11-17 15:28:52 -08003481# check for unusual line ending [ or (
3482 if ($line =~ /^\+.*([\[\(])\s*$/) {
3483 CHK("OPEN_ENDED_LINE",
3484 "Lines should not end with a '$1'\n" . $herecurr);
3485 }
3486
Joe Perches4dbed762017-05-08 15:55:39 -07003487# check if this appears to be the start function declaration, save the name
3488 if ($sline =~ /^\+\{\s*$/ &&
3489 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3490 $context_function = $1;
3491 }
3492
3493# check if this appears to be the end of function declaration
3494 if ($sline =~ /^\+\}\s*$/) {
3495 undef $context_function;
3496 }
3497
Joe Perches032a4c02014-08-06 16:10:29 -07003498# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003499# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003500# if the previous line is a break or return and is indented 1 tab more...
3501 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3502 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003503 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3504 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3505 defined $lines[$linenr] &&
3506 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003507 WARN("UNNECESSARY_ELSE",
3508 "else is not generally useful after a break or return\n" . $hereprev);
3509 }
3510 }
3511
Joe Perchesc00df192014-08-06 16:11:01 -07003512# check indentation of a line with a break;
3513# if the previous line is a goto or return and is indented the same # of tabs
3514 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3515 my $tabs = $1;
3516 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3517 WARN("UNNECESSARY_BREAK",
3518 "break is not useful after a goto or return\n" . $hereprev);
3519 }
3520 }
3521
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003522# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003523 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003524 WARN("CVS_KEYWORD",
3525 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003526 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003527
Joe Perches56e77d72013-02-21 16:44:14 -08003528# check for old HOTPLUG __dev<foo> section markings
3529 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3530 WARN("HOTPLUG_SECTION",
3531 "Using $1 is unnecessary\n" . $herecurr);
3532 }
3533
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003534# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003535 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3536 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003537#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003538 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003539 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003540 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003541 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003542 $stat =~ s/\n./\n /g;
3543 $cond =~ s/\n./\n /g;
3544
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003545#print "linenr<$linenr> <$stat>\n";
3546 # If this statement has no statement boundaries within
3547 # it there is no point in retrying a statement scan
3548 # until we hit end of it.
3549 my $frag = $stat; $frag =~ s/;+\s*$//;
3550 if ($frag !~ /(?:{|;)/) {
3551#print "skip<$line_nr_next>\n";
3552 $suppress_statement = $line_nr_next;
3553 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003554
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003555 # Find the real next line.
3556 $realline_next = $line_nr_next;
3557 if (defined $realline_next &&
3558 (!defined $lines[$realline_next - 1] ||
3559 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3560 $realline_next++;
3561 }
3562
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003563 my $s = $stat;
3564 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003565
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003566 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003567 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003568
3569 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003570 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003571
Andy Whitcroft463f2862009-09-21 17:04:34 -07003572 } elsif ($s =~ /^.\s*else\b/s) {
3573
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003574 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003575 } 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 -07003576 my $type = $1;
3577 $type =~ s/\s+/ /g;
3578 possible($type, "A:" . $s);
3579
Andy Whitcroft8905a672007-11-28 16:21:06 -08003580 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003581 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003582 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003583 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003584
3585 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003586 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003587 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003588 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003589
3590 # Check for any sort of function declaration.
3591 # int foo(something bar, other baz);
3592 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003593 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 -08003594 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003595
Andy Whitcroftcf655042008-03-04 14:28:20 -08003596 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003597 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003598 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003599
Andy Whitcroft8905a672007-11-28 16:21:06 -08003600 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003601 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003602
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003603 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003604 }
3605 }
3606 }
3607
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003608 }
3609
Andy Whitcroft653d4872007-06-23 17:16:34 -07003610#
3611# Checks which may be anchored in the context.
3612#
3613
3614# Check for switch () and associated case and default
3615# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003616 if ($line=~/\bswitch\s*\(.*\)/) {
3617 my $err = '';
3618 my $sep = '';
3619 my @ctx = ctx_block_outer($linenr, $realcnt);
3620 shift(@ctx);
3621 for my $ctx (@ctx) {
3622 my ($clen, $cindent) = line_stats($ctx);
3623 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3624 $indent != $cindent) {
3625 $err .= "$sep$ctx\n";
3626 $sep = '';
3627 } else {
3628 $sep = "[...]\n";
3629 }
3630 }
3631 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003632 ERROR("SWITCH_CASE_INDENT_LEVEL",
3633 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003634 }
3635 }
3636
3637# if/while/etc brace do not go on next line, unless defining a do while loop,
3638# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003639 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 -07003640 my $pre_ctx = "$1$2";
3641
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003642 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003643
3644 if ($line =~ /^\+\t{6,}/) {
3645 WARN("DEEP_INDENTATION",
3646 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3647 }
3648
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003649 my $ctx_cnt = $realcnt - $#ctx - 1;
3650 my $ctx = join("\n", @ctx);
3651
Andy Whitcroft548596d2008-07-23 21:29:01 -07003652 my $ctx_ln = $linenr;
3653 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003654
Andy Whitcroft548596d2008-07-23 21:29:01 -07003655 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3656 defined $lines[$ctx_ln - 1] &&
3657 $lines[$ctx_ln - 1] =~ /^-/)) {
3658 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3659 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003660 $ctx_ln++;
3661 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003662
Andy Whitcroft53210162008-07-23 21:29:03 -07003663 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3664 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003665
Joe Perchesd752fcc2014-08-06 16:11:05 -07003666 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003667 ERROR("OPEN_BRACE",
3668 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003669 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003670 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003671 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3672 $ctx =~ /\)\s*\;\s*$/ &&
3673 defined $lines[$ctx_ln - 1])
3674 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003675 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3676 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003677 WARN("TRAILING_SEMICOLON",
3678 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003679 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003680 }
3681 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003682 }
3683
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003684# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003685 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 -08003686 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3687 ctx_statement_block($linenr, $realcnt, 0)
3688 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003689 my ($s, $c) = ($stat, $cond);
3690
3691 substr($s, 0, length($c), '');
3692
Joe Perches9f5af482015-09-09 15:37:30 -07003693 # remove inline comments
3694 $s =~ s/$;/ /g;
3695 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003696
3697 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003698 my @newlines = ($c =~ /\n/gs);
3699 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003700
Joe Perches9f5af482015-09-09 15:37:30 -07003701 # Make sure we remove the line prefixes as we have
3702 # none on the first line, and are going to readd them
3703 # where necessary.
3704 $s =~ s/\n./\n/gs;
3705 while ($s =~ /\n\s+\\\n/) {
3706 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3707 }
3708
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003709 # We want to check the first line inside the block
3710 # starting at the end of the conditional, so remove:
3711 # 1) any blank line termination
3712 # 2) any opening brace { on end of the line
3713 # 3) any do (...) {
3714 my $continuation = 0;
3715 my $check = 0;
3716 $s =~ s/^.*\bdo\b//;
3717 $s =~ s/^\s*{//;
3718 if ($s =~ s/^\s*\\//) {
3719 $continuation = 1;
3720 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003721 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003722 $check = 1;
3723 $cond_lines++;
3724 }
3725
3726 # Also ignore a loop construct at the end of a
3727 # preprocessor statement.
3728 if (($prevline =~ /^.\s*#\s*define\s/ ||
3729 $prevline =~ /\\\s*$/) && $continuation == 0) {
3730 $check = 0;
3731 }
3732
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003733 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003734 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003735 while ($cond_ptr != $cond_lines) {
3736 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003737
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003738 # If we see an #else/#elif then the code
3739 # is not linear.
3740 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3741 $check = 0;
3742 }
3743
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003744 # Ignore:
3745 # 1) blank lines, they should be at 0,
3746 # 2) preprocessor lines, and
3747 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003748 if ($continuation ||
3749 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003750 $s =~ /^\s*#\s*?/ ||
3751 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003752 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003753 if ($s =~ s/^.*?\n//) {
3754 $cond_lines++;
3755 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003756 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003757 }
3758
3759 my (undef, $sindent) = line_stats("+" . $s);
3760 my $stat_real = raw_line($linenr, $cond_lines);
3761
3762 # Check if either of these lines are modified, else
3763 # this is not this patch's fault.
3764 if (!defined($stat_real) ||
3765 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3766 $check = 0;
3767 }
3768 if (defined($stat_real) && $cond_lines > 1) {
3769 $stat_real = "[...]\n$stat_real";
3770 }
3771
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003772 #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 -07003773
Joe Perches9f5af482015-09-09 15:37:30 -07003774 if ($check && $s ne '' &&
Antonio Borneo713a09d2020-04-06 20:11:07 -07003775 (($sindent % $tabsize) != 0 ||
Joe Perches9f5af482015-09-09 15:37:30 -07003776 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003777 ($sindent == $indent &&
3778 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Antonio Borneo713a09d2020-04-06 20:11:07 -07003779 ($sindent > $indent + $tabsize))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003780 WARN("SUSPECT_CODE_INDENT",
3781 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003782 }
3783 }
3784
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003785 # Track the 'values' across context and added lines.
3786 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003787 my ($curr_values, $curr_vars) =
3788 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003789 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003790 if ($dbg_values) {
3791 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003792 print "$linenr > .$outline\n";
3793 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003794 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003795 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003796 $prev_values = substr($curr_values, -1);
3797
Andy Whitcroft00df3442007-06-08 13:47:06 -07003798#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003799 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003800
Joe Perches11ca40a2016-12-12 16:46:31 -08003801# check for dereferences that span multiple lines
3802 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3803 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3804 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3805 my $ref = $1;
3806 $line =~ /^.\s*($Lval)/;
3807 $ref .= $1;
3808 $ref =~ s/\s//g;
3809 WARN("MULTILINE_DEREFERENCE",
3810 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3811 }
3812
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003813# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003814 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 -07003815 my $type = $1;
3816 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003817 $var = "" if (!defined $var);
3818 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003819 my $sign = $1;
3820 my $pointer = $2;
3821
3822 $pointer = "" if (!defined $pointer);
3823
3824 if (WARN("UNSPECIFIED_INT",
3825 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3826 $fix) {
3827 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003828 my $comp_pointer = $pointer;
3829 $comp_pointer =~ s/\s//g;
3830 $decl .= $comp_pointer;
3831 $decl = rtrim($decl) if ($var eq "");
3832 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003833 }
3834 }
3835 }
3836
Andy Whitcroft653d4872007-06-23 17:16:34 -07003837# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003838 if ($dbg_type) {
3839 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003840 ERROR("TEST_TYPE",
3841 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003842 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003843 ERROR("TEST_NOT_TYPE",
3844 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003845 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003846 next;
3847 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003848# TEST: allow direct testing of the attribute matcher.
3849 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003850 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003851 ERROR("TEST_ATTR",
3852 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003853 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003854 ERROR("TEST_NOT_ATTR",
3855 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003856 }
3857 next;
3858 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003859
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003860# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003861 if ($line =~ /^.\s*{/ &&
3862 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003863 if (ERROR("OPEN_BRACE",
3864 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003865 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3866 fix_delete_line($fixlinenr - 1, $prevrawline);
3867 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003868 my $fixedline = $prevrawline;
3869 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003870 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003871 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003872 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003873 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003874 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003875 }
3876
Andy Whitcroft653d4872007-06-23 17:16:34 -07003877#
3878# Checks which are anchored on the added line.
3879#
3880
3881# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003882 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003883 my $path = $1;
3884 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003885 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003886 "malformed #include filename\n" . $herecurr);
3887 }
3888 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3889 ERROR("UAPI_INCLUDE",
3890 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003891 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003892 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003893
3894# no C99 // comments
3895 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003896 if (ERROR("C99_COMMENTS",
3897 "do not use C99 // comments\n" . $herecurr) &&
3898 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003899 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003900 if ($line =~ /\/\/(.*)$/) {
3901 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003902 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003903 }
3904 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003905 }
3906 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003907 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003908 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003909
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003910# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3911# the whole statement.
3912#print "APW <$lines[$realline_next - 1]>\n";
3913 if (defined $realline_next &&
3914 exists $lines[$realline_next - 1] &&
3915 !defined $suppress_export{$realline_next} &&
3916 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3917 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003918 # Handle definitions which produce identifiers with
3919 # a prefix:
3920 # XXX(foo);
3921 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003922 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003923 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003924 $name =~ /^${Ident}_$2/) {
3925#print "FOO C name<$name>\n";
3926 $suppress_export{$realline_next} = 1;
3927
3928 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003929 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003930 ^.DEFINE_$Ident\(\Q$name\E\)|
3931 ^.DECLARE_$Ident\(\Q$name\E\)|
3932 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003933 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3934 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003935 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003936#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3937 $suppress_export{$realline_next} = 2;
3938 } else {
3939 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003940 }
3941 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003942 if (!defined $suppress_export{$linenr} &&
3943 $prevline =~ /^.\s*$/ &&
3944 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3945 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3946#print "FOO B <$lines[$linenr - 1]>\n";
3947 $suppress_export{$linenr} = 2;
3948 }
3949 if (defined $suppress_export{$linenr} &&
3950 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003951 WARN("EXPORT_SYMBOL",
3952 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003953 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003954
Joe Eloff5150bda2010-08-09 17:21:00 -07003955# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003956 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003957 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003958 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003959 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003960 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003961 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003962 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003963# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003964 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003965 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003966 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003967 $herecurr) &&
3968 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003969 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003970 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003971 }
3972
Joe Perches18130872014-08-06 16:11:22 -07003973# check for misordered declarations of char/short/int/long with signed/unsigned
3974 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3975 my $tmp = trim($1);
3976 WARN("MISORDERED_TYPE",
3977 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3978 }
3979
Joe Perches809e0822018-08-21 21:58:12 -07003980# check for unnecessary <signed> int declarations of short/long/long long
3981 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3982 my $type = trim($1);
3983 next if ($type !~ /\bint\b/);
3984 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3985 my $new_type = $type;
3986 $new_type =~ s/\b\s*int\s*\b/ /;
3987 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3988 $new_type =~ s/^const\s+//;
3989 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3990 $new_type = "const $new_type" if ($type =~ /^const\b/);
3991 $new_type =~ s/\s+/ /g;
3992 $new_type = trim($new_type);
3993 if (WARN("UNNECESSARY_INT",
3994 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3995 $fix) {
3996 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3997 }
3998 }
3999
Joe Perchescb710ec2010-10-26 14:23:20 -07004000# check for static const char * arrays.
4001 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004002 WARN("STATIC_CONST_CHAR_ARRAY",
4003 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004004 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004005 }
4006
4007# check for initialized const char arrays that should be static const
4008 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4009 if (WARN("STATIC_CONST_CHAR_ARRAY",
4010 "const array should probably be static const\n" . $herecurr) &&
4011 $fix) {
4012 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4013 }
4014 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004015
4016# check for static char foo[] = "bar" declarations.
4017 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004018 WARN("STATIC_CONST_CHAR_ARRAY",
4019 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004020 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004021 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004022
Joe Perchesab7e23f2015-04-16 12:44:22 -07004023# check for const <foo> const where <foo> is not a pointer or array type
4024 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4025 my $found = $1;
4026 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4027 WARN("CONST_CONST",
4028 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4029 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4030 WARN("CONST_CONST",
4031 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4032 }
4033 }
4034
Joe Perches9b0fa602014-04-03 14:49:18 -07004035# check for non-global char *foo[] = {"bar", ...} declarations.
4036 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4037 WARN("STATIC_CONST_CHAR_ARRAY",
4038 "char * array declaration might be better as static const\n" .
4039 $herecurr);
4040 }
4041
Joe Perchesb598b672015-04-16 12:44:36 -07004042# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4043 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4044 my $array = $1;
4045 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4046 my $array_div = $1;
4047 if (WARN("ARRAY_SIZE",
4048 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4049 $fix) {
4050 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4051 }
4052 }
4053 }
4054
Joe Perchesb36190c2014-01-27 17:07:18 -08004055# check for function declarations without arguments like "int foo()"
4056 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
4057 if (ERROR("FUNCTION_WITHOUT_ARGS",
4058 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4059 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004060 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004061 }
4062 }
4063
Andy Whitcroft653d4872007-06-23 17:16:34 -07004064# check for new typedefs, only function parameters and sparse annotations
4065# make sense.
4066 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004067 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004068 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004069 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004070 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004071 WARN("NEW_TYPEDEFS",
4072 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004073 }
4074
4075# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004076 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004077 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4078 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004079 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004080
Andy Whitcroft65863862009-01-06 14:41:21 -08004081 # Should start with a space.
4082 $to =~ s/^(\S)/ $1/;
4083 # Should not end with a space.
4084 $to =~ s/\s+$//;
4085 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004086 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004087 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004088
Joe Perches3705ce52013-07-03 15:05:31 -07004089## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004090 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004091 if (ERROR("POINTER_LOCATION",
4092 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4093 $fix) {
4094 my $sub_from = $ident;
4095 my $sub_to = $ident;
4096 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004097 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004098 s@\Q$sub_from\E@$sub_to@;
4099 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004100 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004101 }
4102 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4103 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004104 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004105
Andy Whitcroft65863862009-01-06 14:41:21 -08004106 # Should start with a space.
4107 $to =~ s/^(\S)/ $1/;
4108 # Should not end with a space.
4109 $to =~ s/\s+$//;
4110 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004111 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004112 }
4113 # Modifiers should have spaces.
4114 $to =~ s/(\b$Modifier$)/$1 /;
4115
Joe Perches3705ce52013-07-03 15:05:31 -07004116## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004117 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004118 if (ERROR("POINTER_LOCATION",
4119 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4120 $fix) {
4121
4122 my $sub_from = $match;
4123 my $sub_to = $match;
4124 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004125 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004126 s@\Q$sub_from\E@$sub_to@;
4127 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004128 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004129 }
4130
Joe Perches9d3e3c72015-09-09 15:37:27 -07004131# avoid BUG() or BUG_ON()
4132 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004133 my $msg_level = \&WARN;
4134 $msg_level = \&CHK if ($file);
4135 &{$msg_level}("AVOID_BUG",
4136 "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 -07004137 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004138
Joe Perches9d3e3c72015-09-09 15:37:27 -07004139# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004140 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004141 WARN("LINUX_VERSION_CODE",
4142 "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 -08004143 }
4144
Joe Perches17441222011-06-15 15:08:17 -07004145# check for uses of printk_ratelimit
4146 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004147 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004148 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004149 }
4150
Joe Percheseeef5732017-11-17 15:28:41 -08004151# printk should use KERN_* levels
4152 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4153 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4154 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004155 }
4156
Joe Perches243f3802012-05-31 16:26:09 -07004157 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4158 my $orig = $1;
4159 my $level = lc($orig);
4160 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004161 my $level2 = $level;
4162 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004163 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004164 "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 -07004165 }
4166
Joe Perchesdc139312013-02-21 16:44:13 -08004167 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4168 my $orig = $1;
4169 my $level = lc($orig);
4170 $level = "warn" if ($level eq "warning");
4171 $level = "dbg" if ($level eq "debug");
4172 WARN("PREFER_DEV_LEVEL",
4173 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4174 }
4175
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004176# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4177# number of false positives, but assembly files are not checked, so at
4178# least the arch entry code will not trigger this warning.
4179 if ($line =~ /\bENOSYS\b/) {
4180 WARN("ENOSYS",
4181 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4182 }
4183
Andy Whitcroft653d4872007-06-23 17:16:34 -07004184# function brace can't be on same line, except for #defines of do while,
4185# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004186 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004187 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4188 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4189 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004190 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004191 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004192 $fix) {
4193 fix_delete_line($fixlinenr, $rawline);
4194 my $fixed_line = $rawline;
4195 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4196 my $line1 = $1;
4197 my $line2 = $2;
4198 fix_insert_line($fixlinenr, ltrim($line1));
4199 fix_insert_line($fixlinenr, "\+{");
4200 if ($line2 !~ /^\s*$/) {
4201 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4202 }
4203 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004204 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004205
Andy Whitcroft8905a672007-11-28 16:21:06 -08004206# open braces for enum, union and struct go on the same line.
4207 if ($line =~ /^.\s*{/ &&
4208 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004209 if (ERROR("OPEN_BRACE",
4210 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4211 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4212 fix_delete_line($fixlinenr - 1, $prevrawline);
4213 fix_delete_line($fixlinenr, $rawline);
4214 my $fixedline = rtrim($prevrawline) . " {";
4215 fix_insert_line($fixlinenr, $fixedline);
4216 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004217 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004218 if ($fixedline !~ /^\+\s*$/) {
4219 fix_insert_line($fixlinenr, $fixedline);
4220 }
4221 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004222 }
4223
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004224# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004225 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4226 if (WARN("SPACING",
4227 "missing space after $1 definition\n" . $herecurr) &&
4228 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004229 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004230 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4231 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004232 }
4233
Joe Perches31070b52014-01-23 15:54:49 -08004234# Function pointer declarations
4235# check spacing between type, funcptr, and args
4236# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004237 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004238 my $declare = $1;
4239 my $pre_pointer_space = $2;
4240 my $post_pointer_space = $3;
4241 my $funcname = $4;
4242 my $post_funcname_space = $5;
4243 my $pre_args_space = $6;
4244
Joe Perches91f72e92014-04-03 14:49:12 -07004245# the $Declare variable will capture all spaces after the type
4246# so check it for a missing trailing missing space but pointer return types
4247# don't need a space so don't warn for those.
4248 my $post_declare_space = "";
4249 if ($declare =~ /(\s+)$/) {
4250 $post_declare_space = $1;
4251 $declare = rtrim($declare);
4252 }
4253 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004254 WARN("SPACING",
4255 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004256 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004257 }
4258
4259# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004260# This test is not currently implemented because these declarations are
4261# equivalent to
4262# int foo(int bar, ...)
4263# and this is form shouldn't/doesn't generate a checkpatch warning.
4264#
4265# elsif ($declare =~ /\s{2,}$/) {
4266# WARN("SPACING",
4267# "Multiple spaces after return type\n" . $herecurr);
4268# }
Joe Perches31070b52014-01-23 15:54:49 -08004269
4270# unnecessary space "type ( *funcptr)(args...)"
4271 if (defined $pre_pointer_space &&
4272 $pre_pointer_space =~ /^\s/) {
4273 WARN("SPACING",
4274 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4275 }
4276
4277# unnecessary space "type (* funcptr)(args...)"
4278 if (defined $post_pointer_space &&
4279 $post_pointer_space =~ /^\s/) {
4280 WARN("SPACING",
4281 "Unnecessary space before function pointer name\n" . $herecurr);
4282 }
4283
4284# unnecessary space "type (*funcptr )(args...)"
4285 if (defined $post_funcname_space &&
4286 $post_funcname_space =~ /^\s/) {
4287 WARN("SPACING",
4288 "Unnecessary space after function pointer name\n" . $herecurr);
4289 }
4290
4291# unnecessary space "type (*funcptr) (args...)"
4292 if (defined $pre_args_space &&
4293 $pre_args_space =~ /^\s/) {
4294 WARN("SPACING",
4295 "Unnecessary space before function pointer arguments\n" . $herecurr);
4296 }
4297
4298 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004299 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004300 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004301 }
4302 }
4303
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004304# check for spacing round square brackets; allowed:
4305# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004306# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4307# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004308 while ($line =~ /(.*?\s)\[/g) {
4309 my ($where, $prefix) = ($-[1], $1);
4310 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004311 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004312 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004313 if (ERROR("BRACKET_SPACE",
4314 "space prohibited before open square bracket '['\n" . $herecurr) &&
4315 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004316 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004317 s/^(\+.*?)\s+\[/$1\[/;
4318 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004319 }
4320 }
4321
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004322# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004323 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004324 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004325 my $ctx_before = substr($line, 0, $-[1]);
4326 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004327
4328 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004329 if ($name =~ /^(?:
4330 if|for|while|switch|return|case|
4331 volatile|__volatile__|
4332 __attribute__|format|__extension__|
4333 asm|__asm__)$/x)
4334 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004335 # cpp #define statements have non-optional spaces, ie
4336 # if there is a space between the name and the open
4337 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004338 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004339
4340 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004341 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004342
4343 # If this whole things ends with a type its most
4344 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004345 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004346
4347 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004348 if (WARN("SPACING",
4349 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4350 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004351 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004352 s/\b$name\s+\(/$name\(/;
4353 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004354 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004355 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004356
Andy Whitcroft653d4872007-06-23 17:16:34 -07004357# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004358 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004359 my $fixed_line = "";
4360 my $line_fixed = 0;
4361
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004362 my $ops = qr{
4363 <<=|>>=|<=|>=|==|!=|
4364 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4365 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004366 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004367 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004368 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004369 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004370
4371## print("element count: <" . $#elements . ">\n");
4372## foreach my $el (@elements) {
4373## print("el: <$el>\n");
4374## }
4375
4376 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004377 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004378
Joe Perches3705ce52013-07-03 15:05:31 -07004379 foreach my $el (@elements) {
4380 push(@fix_elements, substr($rawline, $off, length($el)));
4381 $off += length($el);
4382 }
4383
4384 $off = 0;
4385
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004386 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004387 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004388
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004389 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004390
4391 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4392
4393## print("n: <$n> good: <$good>\n");
4394
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004395 $off += length($elements[$n]);
4396
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004397 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004398 my $ca = substr($opline, 0, $off);
4399 my $cc = '';
4400 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4401 $cc = substr($opline, $off + length($elements[$n + 1]));
4402 }
4403 my $cb = "$ca$;$cc";
4404
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004405 my $a = '';
4406 $a = 'V' if ($elements[$n] ne '');
4407 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004408 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004409 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4410 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004411 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004412
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004413 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004414
4415 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004416 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004417 $c = 'V' if ($elements[$n + 2] ne '');
4418 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004419 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004420 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4421 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004422 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004423 } else {
4424 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004425 }
4426
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004427 my $ctx = "${a}x${c}";
4428
4429 my $at = "(ctx:$ctx)";
4430
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004431 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004432 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004433
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004434 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004435 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004436
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004437 # Get the full operator variant.
4438 my $opv = $op . substr($curr_vars, $off, 1);
4439
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004440 # Ignore operators passed as parameters.
4441 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004442 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004443
Andy Whitcroftcf655042008-03-04 14:28:20 -08004444# # Ignore comments
4445# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004446
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004447 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004448 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004449 if ($ctx !~ /.x[WEBC]/ &&
4450 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004451 if (ERROR("SPACING",
4452 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004453 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004454 $line_fixed = 1;
4455 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004456 }
4457
4458 # // is a comment
4459 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004460
Joe Perchesb00e4812014-04-03 14:49:33 -07004461 # : when part of a bitfield
4462 } elsif ($opv eq ':B') {
4463 # skip the bitfield test for now
4464
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004465 # No spaces for:
4466 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004467 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004468 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004469 if (ERROR("SPACING",
4470 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004471 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004472 if (defined $fix_elements[$n + 2]) {
4473 $fix_elements[$n + 2] =~ s/^\s+//;
4474 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004475 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004476 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004477 }
4478
Joe Perches23810972014-12-10 15:51:32 -08004479 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004480 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004481 my $rtrim_before = 0;
4482 my $space_after = 0;
4483 if ($ctx =~ /Wx./) {
4484 if (ERROR("SPACING",
4485 "space prohibited before that '$op' $at\n" . $hereptr)) {
4486 $line_fixed = 1;
4487 $rtrim_before = 1;
4488 }
4489 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004490 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004491 if (ERROR("SPACING",
4492 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004493 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004494 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004495 $space_after = 1;
4496 }
4497 }
4498 if ($rtrim_before || $space_after) {
4499 if ($rtrim_before) {
4500 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4501 } else {
4502 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4503 }
4504 if ($space_after) {
4505 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004506 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004507 }
4508
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004509 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004510 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004511 #warn "'*' is part of type\n";
4512
4513 # unary operators should have a space before and
4514 # none after. May be left adjacent to another
4515 # unary operator, or a cast
4516 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004517 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004518 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004519 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004520 if (ERROR("SPACING",
4521 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004522 if ($n != $last_after + 2) {
4523 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4524 $line_fixed = 1;
4525 }
Joe Perches3705ce52013-07-03 15:05:31 -07004526 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004527 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004528 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004529 # A unary '*' may be const
4530
4531 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004532 if (ERROR("SPACING",
4533 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004534 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004535 if (defined $fix_elements[$n + 2]) {
4536 $fix_elements[$n + 2] =~ s/^\s+//;
4537 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004538 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004539 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004540 }
4541
4542 # unary ++ and unary -- are allowed no space on one side.
4543 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004544 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004545 if (ERROR("SPACING",
4546 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004547 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004548 $line_fixed = 1;
4549 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004550 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004551 if ($ctx =~ /Wx[BE]/ ||
4552 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004553 if (ERROR("SPACING",
4554 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004555 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004556 $line_fixed = 1;
4557 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004558 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004559 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004560 if (ERROR("SPACING",
4561 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004562 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004563 if (defined $fix_elements[$n + 2]) {
4564 $fix_elements[$n + 2] =~ s/^\s+//;
4565 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004566 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004567 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004568 }
4569
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004570 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004571 } elsif ($op eq '<<' or $op eq '>>' or
4572 $op eq '&' or $op eq '^' or $op eq '|' or
4573 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004574 $op eq '*' or $op eq '/' or
4575 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004576 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004577 if ($check) {
4578 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4579 if (CHK("SPACING",
4580 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4581 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4582 $fix_elements[$n + 2] =~ s/^\s+//;
4583 $line_fixed = 1;
4584 }
4585 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4586 if (CHK("SPACING",
4587 "space preferred before that '$op' $at\n" . $hereptr)) {
4588 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4589 $line_fixed = 1;
4590 }
4591 }
4592 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004593 if (ERROR("SPACING",
4594 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004595 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4596 if (defined $fix_elements[$n + 2]) {
4597 $fix_elements[$n + 2] =~ s/^\s+//;
4598 }
Joe Perches3705ce52013-07-03 15:05:31 -07004599 $line_fixed = 1;
4600 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004601 }
4602
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004603 # A colon needs no spaces before when it is
4604 # terminating a case value or a label.
4605 } elsif ($opv eq ':C' || $opv eq ':L') {
4606 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004607 if (ERROR("SPACING",
4608 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004609 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004610 $line_fixed = 1;
4611 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004612 }
4613
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004614 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004615 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004616 my $ok = 0;
4617
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004618 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004619 if (($op eq '<' &&
4620 $cc =~ /^\S+\@\S+>/) ||
4621 ($op eq '>' &&
4622 $ca =~ /<\S+\@\S+$/))
4623 {
Antonio Borneo342d3d22020-04-06 20:11:01 -07004624 $ok = 1;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004625 }
4626
Joe Perchese0df7e12015-04-16 12:44:53 -07004627 # for asm volatile statements
4628 # ignore a colon with another
4629 # colon immediately before or after
4630 if (($op eq ':') &&
4631 ($ca =~ /:$/ || $cc =~ /^:/)) {
4632 $ok = 1;
4633 }
4634
Joe Perches84731622013-11-12 15:10:05 -08004635 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004636 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004637 my $msg_level = \&ERROR;
4638 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004639
Jean Delvare0675a8f2017-09-08 16:16:07 -07004640 if (&{$msg_level}("SPACING",
4641 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004642 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4643 if (defined $fix_elements[$n + 2]) {
4644 $fix_elements[$n + 2] =~ s/^\s+//;
4645 }
Joe Perches3705ce52013-07-03 15:05:31 -07004646 $line_fixed = 1;
4647 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004648 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004649 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004650 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004651
4652## print("n: <$n> GOOD: <$good>\n");
4653
4654 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004655 }
Joe Perches3705ce52013-07-03 15:05:31 -07004656
4657 if (($#elements % 2) == 0) {
4658 $fixed_line = $fixed_line . $fix_elements[$#elements];
4659 }
4660
Joe Perches194f66f2014-08-06 16:11:03 -07004661 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4662 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004663 }
4664
4665
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004666 }
4667
Joe Perches786b6322013-07-03 15:05:32 -07004668# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004669 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004670 if (WARN("SPACING",
4671 "space prohibited before semicolon\n" . $herecurr) &&
4672 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004673 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004674 s/^(\+.*\S)\s+;/$1;/;
4675 }
4676 }
4677
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004678# check for multiple assignments
4679 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004680 CHK("MULTIPLE_ASSIGNMENTS",
4681 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004682 }
4683
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004684## # check for multiple declarations, allowing for a function declaration
4685## # continuation.
4686## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4687## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4688##
4689## # Remove any bracketed sections to ensure we do not
4690## # falsly report the parameters of functions.
4691## my $ln = $line;
4692## while ($ln =~ s/\([^\(\)]*\)//g) {
4693## }
4694## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004695## WARN("MULTIPLE_DECLARATION",
4696## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004697## }
4698## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004699
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004700#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004701 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004702 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004703 if (ERROR("SPACING",
4704 "space required before the open brace '{'\n" . $herecurr) &&
4705 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004706 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004707 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004708 }
4709
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004710## # check for blank lines before declarations
4711## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4712## $prevrawline =~ /^.\s*$/) {
4713## WARN("SPACING",
4714## "No blank lines before declarations\n" . $hereprev);
4715## }
4716##
4717
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004718# closing brace should have a space following it when it has anything
4719# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07004720 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004721 if (ERROR("SPACING",
4722 "space required after that close brace '}'\n" . $herecurr) &&
4723 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004724 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004725 s/}((?!(?:,|;|\)))\S)/} $1/;
4726 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004727 }
4728
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004729# check spacing on square brackets
4730 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004731 if (ERROR("SPACING",
4732 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4733 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004734 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004735 s/\[\s+/\[/;
4736 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004737 }
4738 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004739 if (ERROR("SPACING",
4740 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4741 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004742 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004743 s/\s+\]/\]/;
4744 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004745 }
4746
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004747# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004748 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4749 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004750 if (ERROR("SPACING",
4751 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4752 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004753 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004754 s/\(\s+/\(/;
4755 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004756 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004757 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004758 $line !~ /for\s*\(.*;\s+\)/ &&
4759 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004760 if (ERROR("SPACING",
4761 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4762 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004763 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004764 s/\s+\)/\)/;
4765 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004766 }
4767
Joe Perchese2826fd2014-08-06 16:10:48 -07004768# check unnecessary parentheses around addressof/dereference single $Lvals
4769# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4770
4771 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004772 my $var = $1;
4773 if (CHK("UNNECESSARY_PARENTHESES",
4774 "Unnecessary parentheses around $var\n" . $herecurr) &&
4775 $fix) {
4776 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4777 }
4778 }
4779
4780# check for unnecessary parentheses around function pointer uses
4781# ie: (foo->bar)(); should be foo->bar();
4782# but not "if (foo->bar) (" to avoid some false positives
4783 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4784 my $var = $2;
4785 if (CHK("UNNECESSARY_PARENTHESES",
4786 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4787 $fix) {
4788 my $var2 = deparenthesize($var);
4789 $var2 =~ s/\s//g;
4790 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4791 }
4792 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004793
Joe Perches63b7c732017-09-08 16:16:01 -07004794# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004795# when !drivers/staging or command-line uses --strict
4796 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004797 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004798 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4799 my $if_stat = $1;
4800 my $test = substr($2, 1, -1);
4801 my $herectx;
4802 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4803 my $match = $1;
4804 # avoid parentheses around potential macro args
4805 next if ($match =~ /^\s*\w+\s*$/);
4806 if (!defined($herectx)) {
4807 $herectx = $here . "\n";
4808 my $cnt = statement_rawlines($if_stat);
4809 for (my $n = 0; $n < $cnt; $n++) {
4810 my $rl = raw_line($linenr, $n);
4811 $herectx .= $rl . "\n";
4812 last if $rl =~ /^[ \+].*\{/;
4813 }
4814 }
4815 CHK("UNNECESSARY_PARENTHESES",
4816 "Unnecessary parentheses around '$match'\n" . $herectx);
4817 }
4818 }
4819
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004820#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004821 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004822 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004823 if (WARN("INDENTED_LABEL",
4824 "labels should not be indented\n" . $herecurr) &&
4825 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004826 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004827 s/^(.)\s+/$1/;
4828 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004829 }
4830
Joe Perches5b9553a2014-04-03 14:49:21 -07004831# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004832 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004833 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004834 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004835 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4836 my $value = $1;
4837 $value = deparenthesize($value);
4838 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4839 ERROR("RETURN_PARENTHESES",
4840 "return is not a function, parentheses are not required\n" . $herecurr);
4841 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004842 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004843 ERROR("SPACING",
4844 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004845 }
4846 }
Joe Perches507e5142013-11-12 15:10:13 -08004847
Joe Perchesb43ae212014-06-23 13:22:07 -07004848# unnecessary return in a void function
4849# at end-of-function, with the previous line a single leading tab, then return;
4850# and the line before that not a goto label target like "out:"
4851 if ($sline =~ /^[ \+]}\s*$/ &&
4852 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4853 $linenr >= 3 &&
4854 $lines[$linenr - 3] =~ /^[ +]/ &&
4855 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004856 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004857 "void function return statements are not generally useful\n" . $hereprev);
4858 }
Joe Perches9819cf22014-06-04 16:12:09 -07004859
Joe Perches189248d2014-01-23 15:54:47 -08004860# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004861 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004862 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4863 my $openparens = $1;
4864 my $count = $openparens =~ tr@\(@\(@;
4865 my $msg = "";
4866 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4867 my $comp = $4; #Not $1 because of $LvalOrFunc
4868 $msg = " - maybe == should be = ?" if ($comp eq "==");
4869 WARN("UNNECESSARY_PARENTHESES",
4870 "Unnecessary parentheses$msg\n" . $herecurr);
4871 }
4872 }
4873
Joe Perchesc5595fa2015-09-09 15:37:58 -07004874# comparisons with a constant or upper case identifier on the left
4875# avoid cases like "foo + BAR < baz"
4876# only fix matches surrounded by parentheses to avoid incorrect
4877# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004878 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004879 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4880 my $lead = $1;
4881 my $const = $2;
4882 my $comp = $3;
4883 my $to = $4;
4884 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004885 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004886 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4887 WARN("CONSTANT_COMPARISON",
4888 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4889 $fix) {
4890 if ($comp eq "<") {
4891 $newcomp = ">";
4892 } elsif ($comp eq "<=") {
4893 $newcomp = ">=";
4894 } elsif ($comp eq ">") {
4895 $newcomp = "<";
4896 } elsif ($comp eq ">=") {
4897 $newcomp = "<=";
4898 }
4899 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4900 }
4901 }
4902
Joe Perchesf34e4a42015-04-16 12:44:19 -07004903# Return of what appears to be an errno should normally be negative
4904 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004905 my $name = $1;
4906 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004907 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004908 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004909 }
4910 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004911
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004912# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004913 if ($line =~ /\b(if|while|for|switch)\(/) {
4914 if (ERROR("SPACING",
4915 "space required before the open parenthesis '('\n" . $herecurr) &&
4916 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004917 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004918 s/\b(if|while|for|switch)\(/$1 \(/;
4919 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004920 }
4921
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004922# Check for illegal assignment in if conditional -- and check for trailing
4923# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004924 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004925 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4926 ctx_statement_block($linenr, $realcnt, 0)
4927 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004928 my ($stat_next) = ctx_statement_block($line_nr_next,
4929 $remain_next, $off_next);
4930 $stat_next =~ s/\n./\n /g;
4931 ##print "stat<$stat> stat_next<$stat_next>\n";
4932
4933 if ($stat_next =~ /^\s*while\b/) {
4934 # If the statement carries leading newlines,
4935 # then count those as offsets.
4936 my ($whitespace) =
4937 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4938 my $offset =
4939 statement_rawlines($whitespace) - 1;
4940
4941 $suppress_whiletrailers{$line_nr_next +
4942 $offset} = 1;
4943 }
4944 }
4945 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004946 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004947 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004948 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004949
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004950 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004951 ERROR("ASSIGN_IN_IF",
4952 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004953 }
4954
4955 # Find out what is on the end of the line after the
4956 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004957 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004958 $s =~ s/\n.*//g;
Antonio Borneo342d3d22020-04-06 20:11:01 -07004959 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004960 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4961 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004962 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004963 # Find out how long the conditional actually is.
4964 my @newlines = ($c =~ /\n/gs);
4965 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004966 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004967
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004968 $stat_real = raw_line($linenr, $cond_lines)
4969 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004970 if (defined($stat_real) && $cond_lines > 1) {
4971 $stat_real = "[...]\n$stat_real";
4972 }
4973
Joe Perches000d1cc12011-07-25 17:13:25 -07004974 ERROR("TRAILING_STATEMENTS",
4975 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004976 }
4977 }
4978
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004979# Check for bitwise tests written as boolean
4980 if ($line =~ /
4981 (?:
4982 (?:\[|\(|\&\&|\|\|)
4983 \s*0[xX][0-9]+\s*
4984 (?:\&\&|\|\|)
4985 |
4986 (?:\&\&|\|\|)
4987 \s*0[xX][0-9]+\s*
4988 (?:\&\&|\|\||\)|\])
4989 )/x)
4990 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004991 WARN("HEXADECIMAL_BOOLEAN_TEST",
4992 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004993 }
4994
Andy Whitcroft8905a672007-11-28 16:21:06 -08004995# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004996 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4997 my $s = $1;
Antonio Borneo342d3d22020-04-06 20:11:01 -07004998 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004999 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005000 ERROR("TRAILING_STATEMENTS",
5001 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005002 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005003 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005004# if should not continue a brace
5005 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005006 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005007 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005008 $herecurr);
5009 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005010# case and default should not have general statements after them
5011 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5012 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005013 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005014 \s*return\s+
5015 )/xg)
5016 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005017 ERROR("TRAILING_STATEMENTS",
5018 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005019 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005020
5021 # Check for }<nl>else {, these must be at the same
5022 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005023 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5024 $previndent == $indent) {
5025 if (ERROR("ELSE_AFTER_BRACE",
5026 "else should follow close brace '}'\n" . $hereprev) &&
5027 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5028 fix_delete_line($fixlinenr - 1, $prevrawline);
5029 fix_delete_line($fixlinenr, $rawline);
5030 my $fixedline = $prevrawline;
5031 $fixedline =~ s/}\s*$//;
5032 if ($fixedline !~ /^\+\s*$/) {
5033 fix_insert_line($fixlinenr, $fixedline);
5034 }
5035 $fixedline = $rawline;
5036 $fixedline =~ s/^(.\s*)else/$1} else/;
5037 fix_insert_line($fixlinenr, $fixedline);
5038 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005039 }
5040
Joe Perches8b8856f2014-08-06 16:11:14 -07005041 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5042 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005043 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5044
5045 # Find out what is on the end of the line after the
5046 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005047 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005048 $s =~ s/\n.*//g;
5049
5050 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005051 if (ERROR("WHILE_AFTER_BRACE",
5052 "while should follow close brace '}'\n" . $hereprev) &&
5053 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5054 fix_delete_line($fixlinenr - 1, $prevrawline);
5055 fix_delete_line($fixlinenr, $rawline);
5056 my $fixedline = $prevrawline;
5057 my $trailing = $rawline;
5058 $trailing =~ s/^\+//;
5059 $trailing = trim($trailing);
5060 $fixedline =~ s/}\s*$/} $trailing/;
5061 fix_insert_line($fixlinenr, $fixedline);
5062 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005063 }
5064 }
5065
Joe Perches95e2c602013-07-03 15:05:20 -07005066#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005067 while ($line =~ m{($Constant|$Lval)}g) {
5068 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005069
Joe Perches95e2c602013-07-03 15:05:20 -07005070#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005071 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005072 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005073#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005074 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005075#Ignore SI style variants like nS, mV and dB
5076#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5077 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005078#Ignore some three character SI units explicitly, like MiB and KHz
5079 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005080 while ($var =~ m{($Ident)}g) {
5081 my $word = $1;
5082 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005083 if ($check) {
5084 seed_camelcase_includes();
5085 if (!$file && !$camelcase_file_seeded) {
5086 seed_camelcase_file($realfile);
5087 $camelcase_file_seeded = 1;
5088 }
5089 }
Joe Perches7e781f62013-09-11 14:23:55 -07005090 if (!defined $camelcase{$word}) {
5091 $camelcase{$word} = 1;
5092 CHK("CAMELCASE",
5093 "Avoid CamelCase: <$word>\n" . $herecurr);
5094 }
Joe Perches34456862013-07-03 15:05:34 -07005095 }
Joe Perches323c1262012-12-17 16:02:07 -08005096 }
5097 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005098
5099#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005100 if ($line =~ /\#\s*define.*\\\s+$/) {
5101 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5102 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5103 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005104 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005105 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005106 }
5107
Fabian Frederick0e212e02015-04-16 12:44:25 -07005108# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5109# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005110 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005111 my $file = "$1.h";
5112 my $checkfile = "include/linux/$file";
5113 if (-f "$root/$checkfile" &&
5114 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005115 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005116 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005117 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5118 if ($asminclude > 0) {
5119 if ($realfile =~ m{^arch/}) {
5120 CHK("ARCH_INCLUDE_LINUX",
5121 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5122 } else {
5123 WARN("INCLUDE_LINUX",
5124 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5125 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005126 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005127 }
5128 }
5129
Andy Whitcroft653d4872007-06-23 17:16:34 -07005130# multi-statement macros should be enclosed in a do while loop, grab the
5131# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005132# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005133 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5134 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005135 my $ln = $linenr;
5136 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005137 my ($off, $dstat, $dcond, $rest);
5138 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005139 my $has_flow_statement = 0;
5140 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005141 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005142 ctx_statement_block($linenr, $realcnt, 0);
5143 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005144 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005145 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005146
Joe Perches08a28432014-10-13 15:51:55 -07005147 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005148 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005149
Joe Perchesf59b64b2016-10-11 13:52:08 -07005150 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5151 my $define_args = $1;
5152 my $define_stmt = $dstat;
5153 my @def_args = ();
5154
5155 if (defined $define_args && $define_args ne "") {
5156 $define_args = substr($define_args, 1, length($define_args) - 2);
5157 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005158 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005159 @def_args = split(",", $define_args);
5160 }
5161
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005162 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005163 $dstat =~ s/\\\n.//g;
5164 $dstat =~ s/^\s*//s;
5165 $dstat =~ s/\s*$//s;
5166
5167 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005168 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5169 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005170 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005171 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005172 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005173
Antonio Borneo342d3d22020-04-06 20:11:01 -07005174 # Flatten any obvious string concatenation.
Joe Perches33acb542015-06-25 15:02:54 -07005175 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5176 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005177 {
5178 }
5179
Joe Perches42e15292016-03-15 14:58:01 -07005180 # Make asm volatile uses seem like a generic function
5181 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5182
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005183 my $exceptions = qr{
5184 $Declare|
5185 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005186 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005187 DECLARE_PER_CPU|
5188 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005189 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005190 union|
5191 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005192 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005193 ^\"|\"$|
5194 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005195 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005196 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005197
5198 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005199 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005200 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005201
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005202 if ($dstat ne '' &&
5203 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5204 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005205 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005206 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005207 $dstat !~ /$exceptions/ &&
5208 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005209 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005210 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005211 $dstat !~ /^for\s*$Constant$/ && # for (...)
5212 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5213 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005214 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005215 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005216 {
Joe Perchese7955562017-05-08 15:55:48 -07005217 if ($dstat =~ /^\s*if\b/) {
5218 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5219 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5220 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005221 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5222 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5223 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005224 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005225 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005226 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005227
5228 }
Joe Perches52076492016-10-11 13:52:14 -07005229
5230 # Make $define_stmt single line, comment-free, etc
5231 my @stmt_array = split('\n', $define_stmt);
5232 my $first = 1;
5233 $define_stmt = "";
5234 foreach my $l (@stmt_array) {
5235 $l =~ s/\\$//;
5236 if ($first) {
5237 $define_stmt = $l;
5238 $first = 0;
5239 } elsif ($l =~ /^[\+ ]/) {
5240 $define_stmt .= substr($l, 1);
5241 }
5242 }
5243 $define_stmt =~ s/$;//g;
5244 $define_stmt =~ s/\s+/ /g;
5245 $define_stmt = trim($define_stmt);
5246
Joe Perchesf59b64b2016-10-11 13:52:08 -07005247# check if any macro arguments are reused (ignore '...' and 'type')
5248 foreach my $arg (@def_args) {
5249 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005250 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005251 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005252 $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 -07005253 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5254 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005255 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005256 if ($use_cnt > 1) {
5257 CHK("MACRO_ARG_REUSE",
5258 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005259 }
5260# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005261 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005262 ((defined($1) && $1 ne ',') ||
5263 (defined($2) && $2 ne ','))) {
5264 CHK("MACRO_ARG_PRECEDENCE",
5265 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005266 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005267 }
Joe Perches5023d342012-12-17 16:01:47 -08005268
Joe Perches08a28432014-10-13 15:51:55 -07005269# check for macros with flow control, but without ## concatenation
5270# ## concatenation is commonly a macro that defines a function so ignore those
5271 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005272 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005273 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005274
Joe Perches08a28432014-10-13 15:51:55 -07005275 WARN("MACRO_WITH_FLOW_CONTROL",
5276 "Macros with flow control statements should be avoided\n" . "$herectx");
5277 }
5278
Joe Perches481eb482012-12-17 16:01:56 -08005279# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005280
5281 } else {
5282 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005283 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5284 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005285 $line =~ /^\+.*\\$/) {
5286 WARN("LINE_CONTINUATIONS",
5287 "Avoid unnecessary line continuations\n" . $herecurr);
5288 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005289 }
5290
Joe Perchesb13edf72012-07-30 14:41:24 -07005291# do {} while (0) macro tests:
5292# single-statement macros do not need to be enclosed in do while (0) loop,
5293# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005294 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005295 $realfile !~ m@/vmlinux.lds.h$@ &&
5296 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5297 my $ln = $linenr;
5298 my $cnt = $realcnt;
5299 my ($off, $dstat, $dcond, $rest);
5300 my $ctx = '';
5301 ($dstat, $dcond, $ln, $cnt, $off) =
5302 ctx_statement_block($linenr, $realcnt, 0);
5303 $ctx = $dstat;
5304
5305 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005306 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005307
5308 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5309 my $stmts = $2;
5310 my $semis = $3;
5311
5312 $ctx =~ s/\n*$//;
5313 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005314 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005315
Joe Perchesac8e97f2012-08-21 16:15:53 -07005316 if (($stmts =~ tr/;/;/) == 1 &&
5317 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005318 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5319 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5320 }
5321 if (defined $semis && $semis ne "") {
5322 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5323 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5324 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005325 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5326 $ctx =~ s/\n*$//;
5327 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005328 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005329
5330 WARN("TRAILING_SEMICOLON",
5331 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005332 }
5333 }
5334
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005335# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005336 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5337 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005338 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005339 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005340 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5341 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005342 my @allowed = ();
5343 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005344 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005345 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005346 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005347 for my $chunk (@chunks) {
5348 my ($cond, $block) = @{$chunk};
5349
Andy Whitcroft773647a2008-03-28 14:15:58 -07005350 # If the condition carries leading newlines, then count those as offsets.
5351 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5352 my $offset = statement_rawlines($whitespace) - 1;
5353
Joe Perchesaad4f612012-03-23 15:02:19 -07005354 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005355 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5356
5357 # We have looked at and allowed this specific line.
5358 $suppress_ifbraces{$ln + $offset} = 1;
5359
5360 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005361 $ln += statement_rawlines($block) - 1;
5362
Andy Whitcroft773647a2008-03-28 14:15:58 -07005363 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005364
5365 $seen++ if ($block =~ /^\s*{/);
5366
Joe Perchesaad4f612012-03-23 15:02:19 -07005367 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005368 if (statement_lines($cond) > 1) {
5369 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005370 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005371 }
5372 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005373 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005374 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005375 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005376 if (statement_block_size($block) > 1) {
5377 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005378 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005379 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005380 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005381 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005382 if ($seen) {
5383 my $sum_allowed = 0;
5384 foreach (@allowed) {
5385 $sum_allowed += $_;
5386 }
5387 if ($sum_allowed == 0) {
5388 WARN("BRACES",
5389 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5390 } elsif ($sum_allowed != $allow &&
5391 $seen != $allow) {
5392 CHK("BRACES",
5393 "braces {} should be used on all arms of this statement\n" . $herectx);
5394 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005395 }
5396 }
5397 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005398 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005399 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005400 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005401
Andy Whitcroftcf655042008-03-04 14:28:20 -08005402 # Check the pre-context.
5403 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5404 #print "APW: ALLOWED: pre<$1>\n";
5405 $allowed = 1;
5406 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005407
5408 my ($level, $endln, @chunks) =
5409 ctx_statement_full($linenr, $realcnt, $-[0]);
5410
Andy Whitcroftcf655042008-03-04 14:28:20 -08005411 # Check the condition.
5412 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005413 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005414 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005415 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005416 }
5417 if (statement_lines($cond) > 1) {
5418 #print "APW: ALLOWED: cond<$cond>\n";
5419 $allowed = 1;
5420 }
5421 if ($block =~/\b(?:if|for|while)\b/) {
5422 #print "APW: ALLOWED: block<$block>\n";
5423 $allowed = 1;
5424 }
5425 if (statement_block_size($block) > 1) {
5426 #print "APW: ALLOWED: lines block<$block>\n";
5427 $allowed = 1;
5428 }
5429 # Check the post-context.
5430 if (defined $chunks[1]) {
5431 my ($cond, $block) = @{$chunks[1]};
5432 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005433 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005434 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005435 if ($block =~ /^\s*\{/) {
5436 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5437 $allowed = 1;
5438 }
5439 }
5440 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005441 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005442 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005443
Joe Perches000d1cc12011-07-25 17:13:25 -07005444 WARN("BRACES",
5445 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005446 }
5447 }
5448
Joe Perchese4c5bab2017-02-24 15:01:41 -08005449# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005450 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5451 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005452 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5453 }
5454
Joe Perches0979ae62012-12-17 16:01:59 -08005455# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005456 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005457 if (CHK("BRACES",
5458 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5459 $fix && $prevrawline =~ /^\+/) {
5460 fix_delete_line($fixlinenr - 1, $prevrawline);
5461 }
Joe Perches0979ae62012-12-17 16:01:59 -08005462 }
Joe Perches77b9a532013-07-03 15:05:29 -07005463 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005464 if (CHK("BRACES",
5465 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5466 $fix) {
5467 fix_delete_line($fixlinenr, $rawline);
5468 }
Joe Perches0979ae62012-12-17 16:01:59 -08005469 }
5470
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005471# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005472 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5473 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005474 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005475 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005476 }
5477
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005478# Check for user-visible strings broken across lines, which breaks the ability
5479# to grep for the string. Make exceptions when the previous string ends in a
5480# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5481# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005482 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005483 $prevline =~ /"\s*$/ &&
5484 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5485 if (WARN("SPLIT_STRING",
5486 "quoted string split across lines\n" . $hereprev) &&
5487 $fix &&
5488 $prevrawline =~ /^\+.*"\s*$/ &&
5489 $last_coalesced_string_linenr != $linenr - 1) {
5490 my $extracted_string = get_quoted_string($line, $rawline);
5491 my $comma_close = "";
5492 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5493 $comma_close = $1;
5494 }
5495
5496 fix_delete_line($fixlinenr - 1, $prevrawline);
5497 fix_delete_line($fixlinenr, $rawline);
5498 my $fixedline = $prevrawline;
5499 $fixedline =~ s/"\s*$//;
5500 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5501 fix_insert_line($fixlinenr - 1, $fixedline);
5502 $fixedline = $rawline;
5503 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5504 if ($fixedline !~ /\+\s*$/) {
5505 fix_insert_line($fixlinenr, $fixedline);
5506 }
5507 $last_coalesced_string_linenr = $linenr;
5508 }
5509 }
5510
5511# check for missing a space in a string concatenation
5512 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5513 WARN('MISSING_SPACE',
5514 "break quoted strings at a space character\n" . $hereprev);
5515 }
5516
Joe Perchese4b7d302017-05-08 15:55:51 -07005517# check for an embedded function name in a string when the function is known
5518# This does not work very well for -f --file checking as it depends on patch
5519# context providing the function name or a single line form for in-file
5520# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005521 if ($line =~ /^\+.*$String/ &&
5522 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005523 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5524 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005525 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005526 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005527 }
5528
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005529# check for spaces before a quoted newline
5530 if ($rawline =~ /^.*\".*\s\\n/) {
5531 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5532 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5533 $fix) {
5534 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5535 }
5536
5537 }
5538
Joe Perchesf17dba42014-10-13 15:51:51 -07005539# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005540 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5541 if (CHK("CONCATENATED_STRING",
5542 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5543 $fix) {
5544 while ($line =~ /($String)/g) {
5545 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5546 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5547 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5548 }
5549 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005550 }
5551
Joe Perches90ad30e2014-12-10 15:51:59 -08005552# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005553 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005554 if (WARN("STRING_FRAGMENTS",
5555 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5556 $fix) {
5557 while ($line =~ /($String)(?=\s*")/g) {
5558 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5559 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5560 }
5561 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005562 }
5563
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005564# check for non-standard and hex prefixed decimal printf formats
5565 my $show_L = 1; #don't show the same defect twice
5566 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005567 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005568 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005569 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005570 # check for %L
5571 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005572 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005573 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5574 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005575 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005576 # check for %Z
5577 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5578 WARN("PRINTF_Z",
5579 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5580 $show_Z = 0;
5581 }
5582 # check for 0x<decimal>
5583 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5584 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005585 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5586 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005587 }
5588
5589# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005590 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005591 WARN("LINE_CONTINUATIONS",
5592 "Avoid line continuations in quoted strings\n" . $herecurr);
5593 }
5594
Andy Whitcroft00df3442007-06-08 13:47:06 -07005595# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005596 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005597 WARN("IF_0",
5598 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5599 }
5600
5601# warn about #if 1
5602 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5603 WARN("IF_1",
5604 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005605 }
5606
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005607# check for needless "if (<foo>) fn(<foo>)" uses
5608 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005609 my $tested = quotemeta($1);
5610 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5611 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5612 my $func = $1;
5613 if (WARN('NEEDLESS_IF',
5614 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5615 $fix) {
5616 my $do_fix = 1;
5617 my $leading_tabs = "";
5618 my $new_leading_tabs = "";
5619 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5620 $leading_tabs = $1;
5621 } else {
5622 $do_fix = 0;
5623 }
5624 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5625 $new_leading_tabs = $1;
5626 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5627 $do_fix = 0;
5628 }
5629 } else {
5630 $do_fix = 0;
5631 }
5632 if ($do_fix) {
5633 fix_delete_line($fixlinenr - 1, $prevrawline);
5634 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5635 }
5636 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005637 }
5638 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005639
Joe Perchesebfdc402014-08-06 16:10:27 -07005640# check for unnecessary "Out of Memory" messages
5641 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5642 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5643 (defined $1 || defined $3) &&
5644 $linenr > 3) {
5645 my $testval = $2;
5646 my $testline = $lines[$linenr - 3];
5647
5648 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5649# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5650
Joe Perchese29a70f2019-03-07 16:28:35 -08005651 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5652 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005653 WARN("OOM_MESSAGE",
5654 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5655 }
5656 }
5657
Joe Perchesf78d98f2014-10-13 15:52:01 -07005658# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005659 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005660 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5661 my $level = $1;
5662 if (WARN("UNNECESSARY_KERN_LEVEL",
5663 "Possible unnecessary $level\n" . $herecurr) &&
5664 $fix) {
5665 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5666 }
5667 }
5668
Joe Perches45c55e92017-02-24 15:01:31 -08005669# check for logging continuations
5670 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5671 WARN("LOGGING_CONTINUATION",
5672 "Avoid logging continuation uses where feasible\n" . $herecurr);
5673 }
5674
Joe Perchesabb08a52014-12-10 15:51:46 -08005675# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005676 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005677 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5678 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5679 WARN("MASK_THEN_SHIFT",
5680 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5681 }
5682
Joe Perchesb75ac612014-12-10 15:52:02 -08005683# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005684 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005685 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5686 my $val = $1;
5687 my $equal = "!";
5688 $equal = "" if ($4 eq "!=");
5689 if (CHK("COMPARISON_TO_NULL",
5690 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5691 $fix) {
5692 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5693 }
5694 }
5695 }
5696
Joe Perches8716de32013-09-11 14:24:05 -07005697# check for bad placement of section $InitAttribute (e.g.: __initdata)
5698 if ($line =~ /(\b$InitAttribute\b)/) {
5699 my $attr = $1;
5700 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5701 my $ptr = $1;
5702 my $var = $2;
5703 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5704 ERROR("MISPLACED_INIT",
5705 "$attr should be placed after $var\n" . $herecurr)) ||
5706 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5707 WARN("MISPLACED_INIT",
5708 "$attr should be placed after $var\n" . $herecurr))) &&
5709 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005710 $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 -07005711 }
5712 }
5713 }
5714
Joe Perchese970b8842013-11-12 15:10:10 -08005715# check for $InitAttributeData (ie: __initdata) with const
5716 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5717 my $attr = $1;
5718 $attr =~ /($InitAttributePrefix)(.*)/;
5719 my $attr_prefix = $1;
5720 my $attr_type = $2;
5721 if (ERROR("INIT_ATTRIBUTE",
5722 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5723 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005724 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005725 s/$InitAttributeData/${attr_prefix}initconst/;
5726 }
5727 }
5728
5729# check for $InitAttributeConst (ie: __initconst) without const
5730 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5731 my $attr = $1;
5732 if (ERROR("INIT_ATTRIBUTE",
5733 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5734 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005735 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005736 /(^\+\s*(?:static\s+))/;
5737 $lead = rtrim($1);
5738 $lead = "$lead " if ($lead !~ /^\+$/);
5739 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005740 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005741 }
5742 }
5743
Joe Perchesc17893c2015-04-16 12:44:42 -07005744# check for __read_mostly with const non-pointer (should just be const)
5745 if ($line =~ /\b__read_mostly\b/ &&
5746 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5747 if (ERROR("CONST_READ_MOSTLY",
5748 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5749 $fix) {
5750 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5751 }
5752 }
5753
Joe Perchesfbdb8132014-04-03 14:49:14 -07005754# don't use __constant_<foo> functions outside of include/uapi/
5755 if ($realfile !~ m@^include/uapi/@ &&
5756 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5757 my $constant_func = $1;
5758 my $func = $constant_func;
5759 $func =~ s/^__constant_//;
5760 if (WARN("CONSTANT_CONVERSION",
5761 "$constant_func should be $func\n" . $herecurr) &&
5762 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005763 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005764 }
5765 }
5766
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005767# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005768 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005769 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005770 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005771 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005772 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005773 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07005774 }
5775 if ($delay > 2000) {
5776 WARN("LONG_UDELAY",
5777 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005778 }
5779 }
5780
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005781# warn about unexpectedly long msleep's
5782 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5783 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005784 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005785 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005786 }
5787 }
5788
Joe Perches36ec1932013-07-03 15:05:25 -07005789# check for comparisons of jiffies
5790 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5791 WARN("JIFFIES_COMPARISON",
5792 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5793 }
5794
Joe Perches9d7a34a2013-07-03 15:05:26 -07005795# check for comparisons of get_jiffies_64()
5796 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5797 WARN("JIFFIES_COMPARISON",
5798 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5799 }
5800
Andy Whitcroft00df3442007-06-08 13:47:06 -07005801# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005802# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005803# print "#ifdef in C files should be avoided\n";
5804# print "$herecurr";
5805# $clean = 0;
5806# }
5807
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005808# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005809 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005810 if (ERROR("SPACING",
5811 "exactly one space required after that #$1\n" . $herecurr) &&
5812 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005813 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005814 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5815 }
5816
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005817 }
5818
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005819# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005820 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5821 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005822 my $which = $1;
5823 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005824 CHK("UNCOMMENTED_DEFINITION",
5825 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005826 }
5827 }
5828# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005829
5830 my $barriers = qr{
5831 mb|
5832 rmb|
5833 wmb|
5834 read_barrier_depends
5835 }x;
5836 my $barrier_stems = qr{
5837 mb__before_atomic|
5838 mb__after_atomic|
5839 store_release|
5840 load_acquire|
5841 store_mb|
5842 (?:$barriers)
5843 }x;
5844 my $all_barriers = qr{
5845 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005846 smp_(?:$barrier_stems)|
5847 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005848 }x;
5849
5850 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005851 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005852 WARN("MEMORY_BARRIER",
5853 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005854 }
5855 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005856
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005857 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5858
5859 if ($realfile !~ m@^include/asm-generic/@ &&
5860 $realfile !~ m@/barrier\.h$@ &&
5861 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5862 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5863 WARN("MEMORY_BARRIER",
5864 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5865 }
5866
Joe Perchescb426e92015-06-25 15:02:46 -07005867# check for waitqueue_active without a comment.
5868 if ($line =~ /\bwaitqueue_active\s*\(/) {
5869 if (!ctx_has_comment($first_line, $linenr)) {
5870 WARN("WAITQUEUE_ACTIVE",
5871 "waitqueue_active without comment\n" . $herecurr);
5872 }
5873 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005874
Paul E. McKenney91db2592017-11-27 09:37:35 -08005875# check for smp_read_barrier_depends and read_barrier_depends
5876 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5877 WARN("READ_BARRIER_DEPENDS",
5878 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5879 }
5880
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005881# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005882 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005883 CHK("ARCH_DEFINES",
5884 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005885 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005886
Joe Perches596ed452017-07-12 14:37:02 -07005887# check that the storage class is not after a type
5888 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005889 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005890 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5891 }
5892# Check that the storage class is at the beginning of a declaration
5893 if ($line =~ /\b$Storage\b/ &&
5894 $line !~ /^.\s*$Storage/ &&
5895 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5896 $1 !~ /[\,\)]\s*$/) {
5897 WARN("STORAGE_CLASS",
5898 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005899 }
5900
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005901# check the location of the inline attribute, that it is between
5902# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005903 if ($line =~ /\b$Type\s+$Inline\b/ ||
5904 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005905 ERROR("INLINE_LOCATION",
5906 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005907 }
5908
Andy Whitcroft8905a672007-11-28 16:21:06 -08005909# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005910 if ($realfile !~ m@\binclude/uapi/@ &&
5911 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005912 if (WARN("INLINE",
5913 "plain inline is preferred over $1\n" . $herecurr) &&
5914 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005915 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005916
5917 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005918 }
5919
Joe Perches3d130fd2011-01-12 17:00:00 -08005920# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005921 if ($realfile !~ m@\binclude/uapi/@ &&
5922 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005923 WARN("PREFER_PACKED",
5924 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005925 }
5926
Joe Perches39b7e282011-07-25 17:13:24 -07005927# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005928 if ($realfile !~ m@\binclude/uapi/@ &&
5929 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005930 WARN("PREFER_ALIGNED",
5931 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005932 }
5933
Joe Perches462811d2019-09-25 16:46:44 -07005934# Check for __attribute__ section, prefer __section
5935 if ($realfile !~ m@\binclude/uapi/@ &&
5936 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
5937 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
5938 my $new = substr($old, 1, -1);
5939 if (WARN("PREFER_SECTION",
5940 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
5941 $fix) {
5942 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
5943 }
5944 }
5945
Joe Perches5f14d3b2012-01-10 15:09:52 -08005946# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005947 if ($realfile !~ m@\binclude/uapi/@ &&
5948 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005949 if (WARN("PREFER_PRINTF",
5950 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5951 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005952 $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 -07005953
5954 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005955 }
5956
Joe Perches6061d942012-03-23 15:02:16 -07005957# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005958 if ($realfile !~ m@\binclude/uapi/@ &&
5959 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005960 if (WARN("PREFER_SCANF",
5961 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5962 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005963 $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 -07005964 }
Joe Perches6061d942012-03-23 15:02:16 -07005965 }
5966
Joe Perches619a9082014-12-10 15:51:35 -08005967# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07005968 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08005969 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5970 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5971 $line =~ /\b__weak\b/)) {
5972 ERROR("WEAK_DECLARATION",
5973 "Using weak declarations can have unintended link defects\n" . $herecurr);
5974 }
5975
Tomas Winklerfd39f902016-12-12 16:46:34 -08005976# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005977 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005978 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005979 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5980 my $type = $1;
5981 if ($type =~ /\b($typeC99Typedefs)\b/) {
5982 $type = $1;
5983 my $kernel_type = 'u';
5984 $kernel_type = 's' if ($type =~ /^_*[si]/);
5985 $type =~ /(\d+)/;
5986 $kernel_type .= $1;
5987 if (CHK("PREFER_KERNEL_TYPES",
5988 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5989 $fix) {
5990 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5991 }
5992 }
5993 }
5994
Joe Perches938224b2016-01-20 14:59:15 -08005995# check for cast of C90 native int or longer types constants
5996 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5997 my $cast = $1;
5998 my $const = $2;
5999 if (WARN("TYPECAST_INT_CONSTANT",
6000 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6001 $fix) {
6002 my $suffix = "";
6003 my $newconst = $const;
6004 $newconst =~ s/${Int_type}$//;
6005 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6006 if ($cast =~ /\blong\s+long\b/) {
6007 $suffix .= 'LL';
6008 } elsif ($cast =~ /\blong\b/) {
6009 $suffix .= 'L';
6010 }
6011 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6012 }
6013 }
6014
Joe Perches8f53a9b2010-03-05 13:43:48 -08006015# check for sizeof(&)
6016 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006017 WARN("SIZEOF_ADDRESS",
6018 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006019 }
6020
Joe Perches66c80b62012-07-30 14:41:22 -07006021# check for sizeof without parenthesis
6022 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006023 if (WARN("SIZEOF_PARENTHESIS",
6024 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6025 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006026 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006027 }
Joe Perches66c80b62012-07-30 14:41:22 -07006028 }
6029
Joe Perches88982fe2012-12-17 16:02:00 -08006030# check for struct spinlock declarations
6031 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6032 WARN("USE_SPINLOCK_T",
6033 "struct spinlock should be spinlock_t\n" . $herecurr);
6034 }
6035
Joe Perchesa6962d72013-04-29 16:18:13 -07006036# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006037 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006038 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006039 $fmt =~ s/%%//g;
6040 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006041 if (WARN("PREFER_SEQ_PUTS",
6042 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6043 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006044 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006045 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006046 }
6047 }
6048
Joe Perches478b1792018-04-10 16:33:34 -07006049# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006050 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006051 defined $stat &&
6052 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6053 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006054 my $stat_real;
6055
Joe Perches0b523762017-05-08 15:55:36 -07006056 my $lc = $stat =~ tr@\n@@;
6057 $lc = $lc + $linenr;
6058 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006059 my $specifier;
6060 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006061 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006062 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006063 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6064 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006065
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006066 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006067 $specifier = $1;
6068 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006069 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006070 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006071 ($extension eq "f" &&
6072 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006073 $bad_specifier = $specifier;
6074 last;
6075 }
6076 if ($extension eq "x" && !defined($stat_real)) {
6077 if (!defined($stat_real)) {
6078 $stat_real = get_stat_real($linenr, $lc);
6079 }
6080 WARN("VSPRINTF_SPECIFIER_PX",
6081 "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");
6082 }
6083 }
6084 if ($bad_specifier ne "") {
6085 my $stat_real = get_stat_real($linenr, $lc);
6086 my $ext_type = "Invalid";
6087 my $use = "";
6088 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006089 $use = " - use %pS instead";
6090 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6091 }
6092
6093 WARN("VSPRINTF_POINTER_EXTENSION",
6094 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6095 }
Joe Perches0b523762017-05-08 15:55:36 -07006096 }
6097 }
6098
Andy Whitcroft554e1652012-01-10 15:09:57 -08006099# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006100 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006101 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006102 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006103
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006104 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006105 my $ms_val = $7;
6106 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006107
Andy Whitcroft554e1652012-01-10 15:09:57 -08006108 if ($ms_size =~ /^(0x|)0$/i) {
6109 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006110 "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 -08006111 } elsif ($ms_size =~ /^(0x|)1$/i) {
6112 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006113 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6114 }
6115 }
6116
Joe Perches98a9bba2014-01-23 15:54:52 -08006117# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006118# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006119# defined $stat &&
6120# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6121# if (WARN("PREFER_ETHER_ADDR_COPY",
6122# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6123# $fix) {
6124# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6125# }
6126# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006127
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006128# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006129# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006130# defined $stat &&
6131# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6132# WARN("PREFER_ETHER_ADDR_EQUAL",
6133# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6134# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006135
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006136# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6137# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006138# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006139# defined $stat &&
6140# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6141#
6142# my $ms_val = $7;
6143#
6144# if ($ms_val =~ /^(?:0x|)0+$/i) {
6145# if (WARN("PREFER_ETH_ZERO_ADDR",
6146# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6147# $fix) {
6148# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6149# }
6150# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6151# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6152# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6153# $fix) {
6154# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6155# }
6156# }
6157# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006158
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006159# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006160 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006161 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006162 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006163 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006164 my $call = $1;
6165 my $cast1 = deparenthesize($2);
6166 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006167 my $cast2 = deparenthesize($7);
6168 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006169 my $cast;
6170
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006171 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006172 $cast = "$cast1 or $cast2";
6173 } elsif ($cast1 ne "") {
6174 $cast = $cast1;
6175 } else {
6176 $cast = $cast2;
6177 }
6178 WARN("MINMAX",
6179 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006180 }
6181 }
6182
Joe Perches4a273192012-07-30 14:41:20 -07006183# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006184 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006185 defined $stat &&
6186 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6187 my $min = $1;
6188 my $max = $7;
6189 if ($min eq $max) {
6190 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006191 "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 -07006192 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6193 $min > $max) {
6194 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006195 "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 -07006196 }
6197 }
6198
Joe Perches823b7942013-11-12 15:10:15 -08006199# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006200 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006201 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006202 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006203 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6204 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6205 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6206 my $lc = $stat =~ tr@\n@@;
6207 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006208 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006209 WARN("NAKED_SSCANF",
6210 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6211 }
6212
Joe Perchesafc819a2014-06-04 16:12:08 -07006213# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006214 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006215 defined $stat &&
6216 $line =~ /\bsscanf\b/) {
6217 my $lc = $stat =~ tr@\n@@;
6218 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006219 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006220 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6221 my $format = $6;
6222 my $count = $format =~ tr@%@%@;
6223 if ($count == 1 &&
6224 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6225 WARN("SSCANF_TO_KSTRTO",
6226 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6227 }
6228 }
6229 }
6230
Joe Perches70dc8a42013-09-11 14:23:58 -07006231# check for new externs in .h files.
6232 if ($realfile =~ /\.h$/ &&
6233 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006234 if (CHK("AVOID_EXTERNS",
6235 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006236 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006237 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006238 }
6239 }
6240
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006241# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006242 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006243 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006244 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006245 my $function_name = $1;
6246 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006247
6248 my $s = $stat;
6249 if (defined $cond) {
6250 substr($s, 0, length($cond), '');
6251 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006252 if ($s =~ /^\s*;/ &&
6253 $function_name ne 'uninitialized_var')
6254 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006255 WARN("AVOID_EXTERNS",
6256 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006257 }
6258
6259 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006260 WARN("FUNCTION_ARGUMENTS",
6261 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006262 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006263
6264 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6265 $stat =~ /^.\s*extern\s+/)
6266 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006267 WARN("AVOID_EXTERNS",
6268 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006269 }
6270
Joe Perchesa0ad7592017-07-10 15:52:19 -07006271# check for function declarations that have arguments without identifier names
6272 if (defined $stat &&
Miles Chen25bdda22017-11-17 15:28:34 -08006273 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Joe Perchesca0d8922016-10-11 13:52:16 -07006274 $1 ne "void") {
6275 my $args = trim($1);
6276 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6277 my $arg = trim($1);
6278 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6279 WARN("FUNCTION_ARGUMENTS",
6280 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6281 }
6282 }
6283 }
6284
Joe Perchesa0ad7592017-07-10 15:52:19 -07006285# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006286 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006287 defined $stat &&
6288 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6289 $context_function = $1;
6290
6291# check for multiline function definition with misplaced open brace
6292 my $ok = 0;
6293 my $cnt = statement_rawlines($stat);
6294 my $herectx = $here . "\n";
6295 for (my $n = 0; $n < $cnt; $n++) {
6296 my $rl = raw_line($linenr, $n);
6297 $herectx .= $rl . "\n";
6298 $ok = 1 if ($rl =~ /^[ \+]\{/);
6299 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6300 last if $rl =~ /^[ \+].*\{/;
6301 }
6302 if (!$ok) {
6303 ERROR("OPEN_BRACE",
6304 "open brace '{' following function definitions go on the next line\n" . $herectx);
6305 }
6306 }
6307
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006308# checks for new __setup's
6309 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6310 my $name = $1;
6311
6312 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006313 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006314 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006315 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006316 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006317
Joe Perchese29a70f2019-03-07 16:28:35 -08006318# check for pointless casting of alloc functions
6319 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006320 WARN("UNNECESSARY_CASTS",
6321 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006322 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006323
Joe Perchesa640d252013-07-03 15:05:21 -07006324# alloc style
6325# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006326 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006327 $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 -07006328 CHK("ALLOC_SIZEOF_STRUCT",
6329 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6330 }
6331
Joe Perches60a55362014-06-04 16:12:07 -07006332# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006333 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006334 defined $stat &&
6335 $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 -07006336 my $oldfunc = $3;
6337 my $a1 = $4;
6338 my $a2 = $10;
6339 my $newfunc = "kmalloc_array";
6340 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006341 my $r1 = $a1;
6342 my $r2 = $a2;
6343 if ($a1 =~ /^sizeof\s*\S/) {
6344 $r1 = $a2;
6345 $r2 = $a1;
6346 }
6347 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6348 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006349 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006350 my $herectx = get_stat_here($linenr, $cnt, $here);
6351
Joe Perches60a55362014-06-04 16:12:07 -07006352 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006353 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6354 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006355 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006356 $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 -07006357 }
6358 }
6359 }
6360
Joe Perches972fdea2013-04-29 16:18:12 -07006361# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006362 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006363 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6364 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006365 WARN("KREALLOC_ARG_REUSE",
6366 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6367 }
6368
Joe Perches5ce59ae2013-02-21 16:44:18 -08006369# check for alloc argument mismatch
6370 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6371 WARN("ALLOC_ARRAY_ARGS",
6372 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6373 }
6374
Joe Perchescaf2a542011-01-12 16:59:56 -08006375# check for multiple semicolons
6376 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006377 if (WARN("ONE_SEMICOLON",
6378 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6379 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006380 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006381 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006382 }
6383
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006384# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6385 if ($realfile !~ m@^include/uapi/@ &&
6386 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006387 my $ull = "";
6388 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6389 if (CHK("BIT_MACRO",
6390 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6391 $fix) {
6392 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6393 }
6394 }
6395
Joe Perches2d632742016-05-20 17:04:00 -07006396# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6397 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6398 my $config = $1;
6399 if (WARN("PREFER_IS_ENABLED",
6400 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6401 $fix) {
6402 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6403 }
6404 }
6405
Joe Perchese81f2392014-08-06 16:11:25 -07006406# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006407 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6408 my $has_break = 0;
6409 my $has_statement = 0;
6410 my $count = 0;
6411 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006412 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006413 $prevline--;
6414 my $rline = $rawlines[$prevline - 1];
6415 my $fline = $lines[$prevline - 1];
6416 last if ($fline =~ /^\@\@/);
6417 next if ($fline =~ /^\-/);
6418 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6419 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6420 next if ($fline =~ /^.[\s$;]*$/);
6421 $has_statement = 1;
6422 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006423 $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 -08006424 }
6425 if (!$has_break && $has_statement) {
6426 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006427 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006428 }
6429 }
6430
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006431# check for /* fallthrough */ like comment, prefer fallthrough;
6432 my @fallthroughs = (
6433 'fallthrough',
6434 '@fallthrough@',
6435 'lint -fallthrough[ \t]*',
6436 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6437 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6438 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6439 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6440 );
6441 if ($raw_comment ne '') {
6442 foreach my $ft (@fallthroughs) {
6443 if ($raw_comment =~ /$ft/) {
6444 my $msg_level = \&WARN;
6445 $msg_level = \&CHK if ($file);
6446 &{$msg_level}("PREFER_FALLTHROUGH",
6447 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6448 last;
6449 }
6450 }
6451 }
6452
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006453# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006454 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006455 defined $stat &&
6456 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006457 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006458 my $herectx = get_stat_here($linenr, $cnt, $here);
6459
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006460 WARN("DEFAULT_NO_BREAK",
6461 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006462 }
6463
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006464# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006465 if ($line =~ /\b__FUNCTION__\b/) {
6466 if (WARN("USE_FUNC",
6467 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6468 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006469 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006470 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006471 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006472
Joe Perches62ec8182015-02-13 14:38:18 -08006473# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6474 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6475 ERROR("DATE_TIME",
6476 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6477 }
6478
Joe Perches2c924882012-03-23 15:02:20 -07006479# check for use of yield()
6480 if ($line =~ /\byield\s*\(\s*\)/) {
6481 WARN("YIELD",
6482 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6483 }
6484
Joe Perches179f8f42013-07-03 15:05:30 -07006485# check for comparisons against true and false
6486 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6487 my $lead = $1;
6488 my $arg = $2;
6489 my $test = $3;
6490 my $otype = $4;
6491 my $trail = $5;
6492 my $op = "!";
6493
6494 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6495
6496 my $type = lc($otype);
6497 if ($type =~ /^(?:true|false)$/) {
6498 if (("$test" eq "==" && "$type" eq "true") ||
6499 ("$test" eq "!=" && "$type" eq "false")) {
6500 $op = "";
6501 }
6502
6503 CHK("BOOL_COMPARISON",
6504 "Using comparison to $otype is error prone\n" . $herecurr);
6505
6506## maybe suggesting a correct construct would better
6507## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6508
6509 }
6510 }
6511
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006512# check for semaphores initialized locked
6513 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006514 WARN("CONSIDER_COMPLETION",
6515 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006516 }
Joe Perches6712d852012-03-23 15:02:20 -07006517
Joe Perches67d0a072011-10-31 17:13:10 -07006518# recommend kstrto* over simple_strto* and strict_strto*
6519 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006520 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006521 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006522 }
Joe Perches6712d852012-03-23 15:02:20 -07006523
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006524# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006525 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006526 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006527 "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 -07006528 }
Joe Perches6712d852012-03-23 15:02:20 -07006529
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006530# check for spin_is_locked(), suggest lockdep instead
6531 if ($line =~ /\bspin_is_locked\(/) {
6532 WARN("USE_LOCKDEP",
6533 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6534 }
6535
Joe Perches9189c7e2018-09-07 15:26:18 -07006536# check for deprecated apis
6537 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6538 my $deprecated_api = $1;
6539 my $new_api = $deprecated_apis{$deprecated_api};
6540 WARN("DEPRECATED_API",
6541 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6542 }
6543
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006544# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006545# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006546 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006547 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006548 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006549 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006550 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006551
6552# use of NR_CPUS is usually wrong
6553# ignore definitions of NR_CPUS and usage to define arrays as likely right
6554 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006555 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6556 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006557 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6558 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6559 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006560 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006561 WARN("NR_CPUS",
6562 "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 -07006563 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006564
Joe Perches52ea8502013-11-12 15:10:09 -08006565# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6566 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6567 ERROR("DEFINE_ARCH_HAS",
6568 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6569 }
6570
Joe Perchesacd93622015-02-13 14:38:38 -08006571# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006572 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006573 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6574 WARN("LIKELY_MISUSE",
6575 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6576 }
6577
Denis Efremovde3f1862019-09-25 16:49:25 -07006578# nested likely/unlikely calls
6579 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6580 WARN("LIKELY_MISUSE",
6581 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6582 }
6583
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006584# whine mightly about in_atomic
6585 if ($line =~ /\bin_atomic\s*\(/) {
6586 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006587 ERROR("IN_ATOMIC",
6588 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006589 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006590 WARN("IN_ATOMIC",
6591 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006592 }
6593 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006594
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006595# check for mutex_trylock_recursive usage
6596 if ($line =~ /mutex_trylock_recursive/) {
6597 ERROR("LOCKING",
6598 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6599 }
6600
Peter Zijlstra1704f472010-03-19 01:37:42 +01006601# check for lockdep_set_novalidate_class
6602 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6603 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6604 if ($realfile !~ m@^kernel/lockdep@ &&
6605 $realfile !~ m@^include/linux/lockdep@ &&
6606 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006607 ERROR("LOCKDEP",
6608 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006609 }
6610 }
Dave Jones88f88312011-01-12 16:59:59 -08006611
Joe Perchesb392c642015-04-16 12:44:16 -07006612 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6613 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006614 WARN("EXPORTED_WORLD_WRITABLE",
6615 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006616 }
Joe Perches24358802014-04-03 14:49:13 -07006617
Joe Perches00180462018-02-06 15:38:55 -08006618# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6619# and whether or not function naming is typical and if
6620# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006621 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006622 defined $stat &&
6623 $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*\)/) {
6624 my $var = $1;
6625 my $perms = $2;
6626 my $show = $3;
6627 my $store = $4;
6628 my $octal_perms = perms_to_octal($perms);
6629 if ($show =~ /^${var}_show$/ &&
6630 $store =~ /^${var}_store$/ &&
6631 $octal_perms eq "0644") {
6632 if (WARN("DEVICE_ATTR_RW",
6633 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6634 $fix) {
6635 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6636 }
6637 } elsif ($show =~ /^${var}_show$/ &&
6638 $store =~ /^NULL$/ &&
6639 $octal_perms eq "0444") {
6640 if (WARN("DEVICE_ATTR_RO",
6641 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6642 $fix) {
6643 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6644 }
6645 } elsif ($show =~ /^NULL$/ &&
6646 $store =~ /^${var}_store$/ &&
6647 $octal_perms eq "0200") {
6648 if (WARN("DEVICE_ATTR_WO",
6649 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6650 $fix) {
6651 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6652 }
6653 } elsif ($octal_perms eq "0644" ||
6654 $octal_perms eq "0444" ||
6655 $octal_perms eq "0200") {
6656 my $newshow = "$show";
6657 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6658 my $newstore = $store;
6659 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6660 my $rename = "";
6661 if ($show ne $newshow) {
6662 $rename .= " '$show' to '$newshow'";
6663 }
6664 if ($store ne $newstore) {
6665 $rename .= " '$store' to '$newstore'";
6666 }
6667 WARN("DEVICE_ATTR_FUNCTIONS",
6668 "Consider renaming function(s)$rename\n" . $herecurr);
6669 } else {
6670 WARN("DEVICE_ATTR_PERMS",
6671 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6672 }
6673 }
6674
Joe Perches515a2352014-04-03 14:49:24 -07006675# Mode permission misuses where it seems decimal should be octal
6676# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006677# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6678# specific definition of not visible in sysfs.
6679# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6680# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006681 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006682 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006683 $line =~ /$mode_perms_search/) {
6684 foreach my $entry (@mode_permission_funcs) {
6685 my $func = $entry->[0];
6686 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006687
Joe Perches459cf0a2016-10-11 13:52:19 -07006688 my $lc = $stat =~ tr@\n@@;
6689 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006690 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006691
Joe Perches515a2352014-04-03 14:49:24 -07006692 my $skip_args = "";
6693 if ($arg_pos > 1) {
6694 $arg_pos--;
6695 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6696 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006697 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006698 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006699 my $val = $1;
6700 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006701 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6702 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6703 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006704 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006705 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006706 }
6707 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006708 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006709 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006710 }
Joe Perches24358802014-04-03 14:49:13 -07006711 }
6712 }
6713 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006714
Joe Perches459cf0a2016-10-11 13:52:19 -07006715# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006716 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006717 my $oval = $1;
6718 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006719 if (WARN("SYMBOLIC_PERMS",
6720 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6721 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006722 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006723 }
6724 }
6725
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006726# validate content of MODULE_LICENSE against list from include/linux/module.h
6727 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6728 my $extracted_string = get_quoted_string($line, $rawline);
6729 my $valid_licenses = qr{
6730 GPL|
6731 GPL\ v2|
6732 GPL\ and\ additional\ rights|
6733 Dual\ BSD/GPL|
6734 Dual\ MIT/GPL|
6735 Dual\ MPL/GPL|
6736 Proprietary
6737 }x;
6738 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6739 WARN("MODULE_LICENSE",
6740 "unknown module license " . $extracted_string . "\n" . $herecurr);
6741 }
6742 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07006743
6744# check for sysctl duplicate constants
6745 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6746 WARN("DUPLICATED_SYSCTL_CONST",
6747 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6748 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006749 }
6750
6751 # If we have no input at all, then there is nothing to report on
6752 # so just keep quiet.
6753 if ($#rawlines == -1) {
6754 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006755 }
6756
Andy Whitcroft8905a672007-11-28 16:21:06 -08006757 # In mailback mode only produce a report in the negative, for
6758 # things that appear to be patches.
6759 if ($mailback && ($clean == 1 || !$is_patch)) {
6760 exit(0);
6761 }
6762
6763 # This is not a patch, and we are are in 'no-patch' mode so
6764 # just keep quiet.
6765 if (!$chk_patch && !$is_patch) {
6766 exit(0);
6767 }
6768
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006769 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006770 ERROR("NOT_UNIFIED_DIFF",
6771 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006772 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006773 if ($is_patch && $has_commit_log && $chk_signoff) {
6774 if ($signoff == 0) {
6775 ERROR("MISSING_SIGN_OFF",
6776 "Missing Signed-off-by: line(s)\n");
6777 } elsif (!$authorsignoff) {
6778 WARN("NO_AUTHOR_SIGN_OFF",
6779 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6780 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006781 }
6782
Andy Whitcroft8905a672007-11-28 16:21:06 -08006783 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006784 if ($summary && !($clean == 1 && $quiet == 1)) {
6785 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006786 print "total: $cnt_error errors, $cnt_warn warnings, " .
6787 (($check)? "$cnt_chk checks, " : "") .
6788 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006789 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006790
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006791 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006792 # If there were any defects found and not already fixing them
6793 if (!$clean and !$fix) {
6794 print << "EOM"
6795
6796NOTE: For some of the reported defects, checkpatch may be able to
6797 mechanically convert to the typical style using --fix or --fix-inplace.
6798EOM
6799 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006800 # If there were whitespace errors which cleanpatch can fix
6801 # then suggest that.
6802 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006803 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006804 print << "EOM"
6805
6806NOTE: Whitespace errors detected.
6807 You may wish to use scripts/cleanpatch or scripts/cleanfile
6808EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006809 }
6810 }
6811
Joe Perchesd752fcc2014-08-06 16:11:05 -07006812 if ($clean == 0 && $fix &&
6813 ("@rawlines" ne "@fixed" ||
6814 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006815 my $newfile = $filename;
6816 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006817 my $linecount = 0;
6818 my $f;
6819
Joe Perchesd752fcc2014-08-06 16:11:05 -07006820 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6821
Joe Perches3705ce52013-07-03 15:05:31 -07006822 open($f, '>', $newfile)
6823 or die "$P: Can't open $newfile for write\n";
6824 foreach my $fixed_line (@fixed) {
6825 $linecount++;
6826 if ($file) {
6827 if ($linecount > 3) {
6828 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006829 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006830 }
6831 } else {
6832 print $f $fixed_line . "\n";
6833 }
6834 }
6835 close($f);
6836
6837 if (!$quiet) {
6838 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006839
Joe Perches3705ce52013-07-03 15:05:31 -07006840Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6841
6842Do _NOT_ trust the results written to this file.
6843Do _NOT_ submit these changes without inspecting them for correctness.
6844
6845This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6846No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006847EOM
6848 }
6849 }
6850
Joe Perchesd8469f12015-06-25 15:03:00 -07006851 if ($quiet == 0) {
6852 print "\n";
6853 if ($clean == 1) {
6854 print "$vname has no obvious style problems and is ready for submission.\n";
6855 } else {
6856 print "$vname has style problems, please review.\n";
6857 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006858 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006859 return $clean;
6860}