blob: fe13e265a3fe9012845c0dd0c83b5528ce967a1d [file] [log] [blame]
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001#!/usr/bin/env perl
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002# SPDX-License-Identifier: GPL-2.0
3#
Joe Hershberger05622192011-10-18 10:06:59 +00004# (c) 2001, Dave Jones. (the file handling bit)
5# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
6# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
7# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02008# (c) 2010-2018 Joe Perches <joe@perches.com>
Joe Hershberger05622192011-10-18 10:06:59 +00009
10use strict;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020011use warnings;
Tom Rini6b9709d2014-02-27 08:27:28 -050012use POSIX;
Dan Murphyc10e0f52017-01-31 14:15:53 -060013use File::Basename;
14use Cwd 'abs_path';
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020015use Term::ANSIColor qw(:constants);
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +020016use Encode qw(decode encode);
Joe Hershberger05622192011-10-18 10:06:59 +000017
18my $P = $0;
Dan Murphyc10e0f52017-01-31 14:15:53 -060019my $D = dirname(abs_path($P));
Joe Hershberger05622192011-10-18 10:06:59 +000020
21my $V = '0.32';
22
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
Tom Rinie199fb32021-08-03 08:31:56 -040026my $verbose = 0;
27my %verbose_messages = ();
28my %verbose_emitted = ();
Joe Hershberger05622192011-10-18 10:06:59 +000029my $tree = 1;
30my $chk_signoff = 1;
31my $chk_patch = 1;
32my $tst_only;
33my $emacs = 0;
34my $terse = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020035my $showfile = 0;
Joe Hershberger05622192011-10-18 10:06:59 +000036my $file = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020037my $git = 0;
38my %git_commits = ();
Joe Hershberger05622192011-10-18 10:06:59 +000039my $check = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020040my $check_orig = 0;
Joe Hershberger05622192011-10-18 10:06:59 +000041my $summary = 1;
42my $mailback = 0;
43my $summary_file = 0;
44my $show_types = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020045my $list_types = 0;
Tom Rini6b9709d2014-02-27 08:27:28 -050046my $fix = 0;
47my $fix_inplace = 0;
Joe Hershberger05622192011-10-18 10:06:59 +000048my $root;
Tom Rinie199fb32021-08-03 08:31:56 -040049my $gitroot = $ENV{'GIT_DIR'};
50$gitroot = ".git" if !defined($gitroot);
Joe Hershberger05622192011-10-18 10:06:59 +000051my %debug;
Tom Rini6b9709d2014-02-27 08:27:28 -050052my %camelcase = ();
53my %use_type = ();
54my @use = ();
Joe Hershberger05622192011-10-18 10:06:59 +000055my %ignore_type = ();
56my @ignore = ();
57my $help = 0;
58my $configuration_file = ".checkpatch.conf";
Simon Glass048a6482020-05-22 16:32:35 -060059my $max_line_length = 100;
Tom Rini6b9709d2014-02-27 08:27:28 -050060my $ignore_perl_version = 0;
61my $minimum_perl_version = 5.10.0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020062my $min_conf_desc_length = 4;
Dan Murphyc10e0f52017-01-31 14:15:53 -060063my $spelling_file = "$D/spelling.txt";
64my $codespell = 0;
65my $codespellfile = "/usr/share/codespell/dictionary.txt";
Simon Glass587254e2022-01-23 12:55:11 -070066my $user_codespellfile = "";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020067my $conststructsfile = "$D/const_structs.checkpatch";
Simon Glassb77df592020-05-22 16:32:36 -060068my $u_boot = 0;
Tom Rinie199fb32021-08-03 08:31:56 -040069my $docsfile = "$D/../doc/develop/checkpatch.rst";
70my $typedefsfile;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020071my $color = "auto";
Tom Rinic57383b2020-06-16 10:29:46 -040072my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
73# git output parsing needs US English output, so first set backtick child process LANGUAGE
74my $git_command ='export LANGUAGE=en_US.UTF-8; git';
75my $tabsize = 8;
Tom Rinie199fb32021-08-03 08:31:56 -040076my ${CONFIG_} = "CONFIG_";
Joe Hershberger05622192011-10-18 10:06:59 +000077
78sub help {
79 my ($exitcode) = @_;
80
81 print << "EOM";
82Usage: $P [OPTION]... [FILE]...
83Version: $V
84
85Options:
86 -q, --quiet quiet
Tom Rinie199fb32021-08-03 08:31:56 -040087 -v, --verbose verbose mode
Joe Hershberger05622192011-10-18 10:06:59 +000088 --no-tree run without a kernel tree
89 --no-signoff do not check for 'Signed-off-by' line
90 --patch treat FILE as patchfile (default)
91 --emacs emacs compile window format
92 --terse one line per report
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020093 --showfile emit diffed file position, not input file position
94 -g, --git treat FILE as a single commit or git revision range
95 single git commit with:
96 <rev>
97 <rev>^
98 <rev>~n
99 multiple git commits with:
100 <rev1>..<rev2>
101 <rev1>...<rev2>
102 <rev>-<count>
103 git merges are ignored
Joe Hershberger05622192011-10-18 10:06:59 +0000104 -f, --file treat FILE as regular source file
105 --subjective, --strict enable more subjective tests
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200106 --list-types list the possible message types
Tom Rini6b9709d2014-02-27 08:27:28 -0500107 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Hershberger05622192011-10-18 10:06:59 +0000108 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200109 --show-types show the specific message type in the output
Simon Glass048a6482020-05-22 16:32:35 -0600110 --max-line-length=n set the maximum line length, (default $max_line_length)
111 if exceeded, warn on patches
112 requires --strict for use with --file
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200113 --min-conf-desc-length=n set the min description length, if shorter, warn
Tom Rinic57383b2020-06-16 10:29:46 -0400114 --tab-size=n set the number of spaces for tab (default $tabsize)
Joe Hershberger05622192011-10-18 10:06:59 +0000115 --root=PATH PATH to the kernel tree root
116 --no-summary suppress the per-file summary
117 --mailback only produce a report in case of warnings/errors
118 --summary-file include the filename in summary
119 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
120 'values', 'possible', 'type', and 'attr' (default
121 is all off)
122 --test-only=WORD report only warnings/errors containing WORD
123 literally
Tom Rini6b9709d2014-02-27 08:27:28 -0500124 --fix EXPERIMENTAL - may create horrible results
125 If correctable single-line errors exist, create
126 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
127 with potential errors corrected to the preferred
128 checkpatch style
129 --fix-inplace EXPERIMENTAL - may create horrible results
130 Is the same as --fix, but overwrites the input
131 file. It's your fault if there's no backup or git
132 --ignore-perl-version override checking of perl version. expect
133 runtime errors.
Dan Murphyc10e0f52017-01-31 14:15:53 -0600134 --codespell Use the codespell dictionary for spelling/typos
Simon Glass587254e2022-01-23 12:55:11 -0700135 (default:$codespellfile)
Dan Murphyc10e0f52017-01-31 14:15:53 -0600136 --codespellfile Use this codespell dictionary
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200137 --typedefsfile Read additional types from this file
138 --color[=WHEN] Use colors 'always', 'never', or only when output
139 is a terminal ('auto'). Default is 'auto'.
Simon Glassb77df592020-05-22 16:32:36 -0600140 --u-boot Run additional checks for U-Boot
Tom Rinie199fb32021-08-03 08:31:56 -0400141 --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default
142 ${CONFIG_})
Joe Hershberger05622192011-10-18 10:06:59 +0000143 -h, --help, --version display this help and exit
144
145When FILE is - read standard input.
146EOM
147
148 exit($exitcode);
149}
150
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200151sub uniq {
152 my %seen;
153 return grep { !$seen{$_}++ } @_;
154}
155
156sub list_types {
157 my ($exitcode) = @_;
158
159 my $count = 0;
160
161 local $/ = undef;
162
163 open(my $script, '<', abs_path($P)) or
164 die "$P: Can't read '$P' $!\n";
165
166 my $text = <$script>;
167 close($script);
168
Tom Rinie199fb32021-08-03 08:31:56 -0400169 my %types = ();
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200170 # Also catch when type or level is passed through a variable
Tom Rinie199fb32021-08-03 08:31:56 -0400171 while ($text =~ /(?:(\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
172 if (defined($1)) {
173 if (exists($types{$2})) {
174 $types{$2} .= ",$1" if ($types{$2} ne $1);
175 } else {
176 $types{$2} = $1;
177 }
178 } else {
179 $types{$2} = "UNDETERMINED";
180 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200181 }
Tom Rinie199fb32021-08-03 08:31:56 -0400182
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200183 print("#\tMessage type\n\n");
Tom Rinie199fb32021-08-03 08:31:56 -0400184 if ($color) {
185 print(" ( Color coding: ");
186 print(RED . "ERROR" . RESET);
187 print(" | ");
188 print(YELLOW . "WARNING" . RESET);
189 print(" | ");
190 print(GREEN . "CHECK" . RESET);
191 print(" | ");
192 print("Multiple levels / Undetermined");
193 print(" )\n\n");
194 }
195
196 foreach my $type (sort keys %types) {
197 my $orig_type = $type;
198 if ($color) {
199 my $level = $types{$type};
200 if ($level eq "ERROR") {
201 $type = RED . $type . RESET;
202 } elsif ($level eq "WARN") {
203 $type = YELLOW . $type . RESET;
204 } elsif ($level eq "CHK") {
205 $type = GREEN . $type . RESET;
206 }
207 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200208 print(++$count . "\t" . $type . "\n");
Tom Rinie199fb32021-08-03 08:31:56 -0400209 if ($verbose && exists($verbose_messages{$orig_type})) {
210 my $message = $verbose_messages{$orig_type};
211 $message =~ s/\n/\n\t/g;
212 print("\t" . $message . "\n\n");
213 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200214 }
215
216 exit($exitcode);
217}
218
Joe Hershberger05622192011-10-18 10:06:59 +0000219my $conf = which_conf($configuration_file);
220if (-f $conf) {
221 my @conf_args;
222 open(my $conffile, '<', "$conf")
223 or warn "$P: Can't find a readable $configuration_file file $!\n";
224
225 while (<$conffile>) {
226 my $line = $_;
227
228 $line =~ s/\s*\n?$//g;
229 $line =~ s/^\s*//g;
230 $line =~ s/\s+/ /g;
231
232 next if ($line =~ m/^\s*#/);
233 next if ($line =~ m/^\s*$/);
234
235 my @words = split(" ", $line);
236 foreach my $word (@words) {
237 last if ($word =~ m/^#/);
238 push (@conf_args, $word);
239 }
240 }
241 close($conffile);
242 unshift(@ARGV, @conf_args) if @conf_args;
243}
244
Tom Rinie199fb32021-08-03 08:31:56 -0400245sub load_docs {
246 open(my $docs, '<', "$docsfile")
247 or warn "$P: Can't read the documentation file $docsfile $!\n";
248
249 my $type = '';
250 my $desc = '';
251 my $in_desc = 0;
252
253 while (<$docs>) {
254 chomp;
255 my $line = $_;
256 $line =~ s/\s+$//;
257
258 if ($line =~ /^\s*\*\*(.+)\*\*$/) {
259 if ($desc ne '') {
260 $verbose_messages{$type} = trim($desc);
261 }
262 $type = $1;
263 $desc = '';
264 $in_desc = 1;
265 } elsif ($in_desc) {
266 if ($line =~ /^(?:\s{4,}|$)/) {
267 $line =~ s/^\s{4}//;
268 $desc .= $line;
269 $desc .= "\n";
270 } else {
271 $verbose_messages{$type} = trim($desc);
272 $type = '';
273 $desc = '';
274 $in_desc = 0;
275 }
276 }
277 }
278
279 if ($desc ne '') {
280 $verbose_messages{$type} = trim($desc);
281 }
282 close($docs);
283}
284
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200285# Perl's Getopt::Long allows options to take optional arguments after a space.
286# Prevent --color by itself from consuming other arguments
287foreach (@ARGV) {
288 if ($_ eq "--color" || $_ eq "-color") {
289 $_ = "--color=$color";
290 }
291}
292
Joe Hershberger05622192011-10-18 10:06:59 +0000293GetOptions(
294 'q|quiet+' => \$quiet,
Tom Rinie199fb32021-08-03 08:31:56 -0400295 'v|verbose!' => \$verbose,
Joe Hershberger05622192011-10-18 10:06:59 +0000296 'tree!' => \$tree,
297 'signoff!' => \$chk_signoff,
298 'patch!' => \$chk_patch,
299 'emacs!' => \$emacs,
300 'terse!' => \$terse,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200301 'showfile!' => \$showfile,
Joe Hershberger05622192011-10-18 10:06:59 +0000302 'f|file!' => \$file,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200303 'g|git!' => \$git,
Joe Hershberger05622192011-10-18 10:06:59 +0000304 'subjective!' => \$check,
305 'strict!' => \$check,
306 'ignore=s' => \@ignore,
Tom Rini6b9709d2014-02-27 08:27:28 -0500307 'types=s' => \@use,
Joe Hershberger05622192011-10-18 10:06:59 +0000308 'show-types!' => \$show_types,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200309 'list-types!' => \$list_types,
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000310 'max-line-length=i' => \$max_line_length,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200311 'min-conf-desc-length=i' => \$min_conf_desc_length,
Tom Rinic57383b2020-06-16 10:29:46 -0400312 'tab-size=i' => \$tabsize,
Joe Hershberger05622192011-10-18 10:06:59 +0000313 'root=s' => \$root,
314 'summary!' => \$summary,
315 'mailback!' => \$mailback,
316 'summary-file!' => \$summary_file,
Tom Rini6b9709d2014-02-27 08:27:28 -0500317 'fix!' => \$fix,
318 'fix-inplace!' => \$fix_inplace,
319 'ignore-perl-version!' => \$ignore_perl_version,
Joe Hershberger05622192011-10-18 10:06:59 +0000320 'debug=s' => \%debug,
321 'test-only=s' => \$tst_only,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200322 'codespell!' => \$codespell,
Simon Glass587254e2022-01-23 12:55:11 -0700323 'codespellfile=s' => \$user_codespellfile,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200324 'typedefsfile=s' => \$typedefsfile,
Simon Glassb77df592020-05-22 16:32:36 -0600325 'u-boot' => \$u_boot,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200326 'color=s' => \$color,
327 'no-color' => \$color, #keep old behaviors of -nocolor
328 'nocolor' => \$color, #keep old behaviors of -nocolor
Tom Rinie199fb32021-08-03 08:31:56 -0400329 'kconfig-prefix=s' => \${CONFIG_},
Joe Hershberger05622192011-10-18 10:06:59 +0000330 'h|help' => \$help,
331 'version' => \$help
Simon Glass587254e2022-01-23 12:55:11 -0700332) or $help = 2;
Joe Hershberger05622192011-10-18 10:06:59 +0000333
Simon Glass587254e2022-01-23 12:55:11 -0700334if ($user_codespellfile) {
335 # Use the user provided codespell file unconditionally
336 $codespellfile = $user_codespellfile;
337} elsif (!(-f $codespellfile)) {
338 # If /usr/share/codespell/dictionary.txt is not present, try to find it
339 # under codespell's install directory: <codespell_root>/data/dictionary.txt
340 if (($codespell || $help) && which("codespell") ne "" && which("python") ne "") {
341 my $python_codespell_dict = << "EOF";
342
343import os.path as op
344import codespell_lib
345codespell_dir = op.dirname(codespell_lib.__file__)
346codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt')
347print(codespell_file, end='')
348EOF
349
350 my $codespell_dict = `python -c "$python_codespell_dict" 2> /dev/null`;
351 $codespellfile = $codespell_dict if (-f $codespell_dict);
352 }
353}
354
355# $help is 1 if either -h, --help or --version is passed as option - exitcode: 0
356# $help is 2 if invalid option is passed - exitcode: 1
357help($help - 1) if ($help);
Joe Hershberger05622192011-10-18 10:06:59 +0000358
Tom Rinie199fb32021-08-03 08:31:56 -0400359die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
360die "$P: --verbose cannot be used with --terse\n" if ($verbose && $terse);
361
362if ($color =~ /^[01]$/) {
363 $color = !$color;
364} elsif ($color =~ /^always$/i) {
365 $color = 1;
366} elsif ($color =~ /^never$/i) {
367 $color = 0;
368} elsif ($color =~ /^auto$/i) {
369 $color = (-t STDOUT);
370} else {
371 die "$P: Invalid color mode: $color\n";
372}
373
374load_docs() if ($verbose);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200375list_types(0) if ($list_types);
376
Tom Rini6b9709d2014-02-27 08:27:28 -0500377$fix = 1 if ($fix_inplace);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200378$check_orig = $check;
Tom Rini6b9709d2014-02-27 08:27:28 -0500379
Joe Hershberger05622192011-10-18 10:06:59 +0000380my $exit = 0;
381
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200382my $perl_version_ok = 1;
Tom Rini6b9709d2014-02-27 08:27:28 -0500383if ($^V && $^V lt $minimum_perl_version) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200384 $perl_version_ok = 0;
Tom Rini6b9709d2014-02-27 08:27:28 -0500385 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200386 exit(1) if (!$ignore_perl_version);
Tom Rini6b9709d2014-02-27 08:27:28 -0500387}
388
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200389#if no filenames are given, push '-' to read patch from stdin
Joe Hershberger05622192011-10-18 10:06:59 +0000390if ($#ARGV < 0) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200391 push(@ARGV, '-');
392}
393
Tom Rinic57383b2020-06-16 10:29:46 -0400394# skip TAB size 1 to avoid additional checks on $tabsize - 1
Tom Rinie199fb32021-08-03 08:31:56 -0400395die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
Tom Rinic57383b2020-06-16 10:29:46 -0400396
Tom Rini6b9709d2014-02-27 08:27:28 -0500397sub hash_save_array_words {
398 my ($hashRef, $arrayRef) = @_;
Joe Hershberger05622192011-10-18 10:06:59 +0000399
Tom Rini6b9709d2014-02-27 08:27:28 -0500400 my @array = split(/,/, join(',', @$arrayRef));
401 foreach my $word (@array) {
402 $word =~ s/\s*\n?$//g;
403 $word =~ s/^\s*//g;
404 $word =~ s/\s+/ /g;
405 $word =~ tr/[a-z]/[A-Z]/;
Joe Hershberger05622192011-10-18 10:06:59 +0000406
Tom Rini6b9709d2014-02-27 08:27:28 -0500407 next if ($word =~ m/^\s*#/);
408 next if ($word =~ m/^\s*$/);
409
410 $hashRef->{$word}++;
411 }
Joe Hershberger05622192011-10-18 10:06:59 +0000412}
413
Tom Rini6b9709d2014-02-27 08:27:28 -0500414sub hash_show_words {
415 my ($hashRef, $prefix) = @_;
416
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200417 if (keys %$hashRef) {
418 print "\nNOTE: $prefix message types:";
Tom Rini6b9709d2014-02-27 08:27:28 -0500419 foreach my $word (sort keys %$hashRef) {
420 print " $word";
421 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200422 print "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -0500423 }
424}
425
426hash_save_array_words(\%ignore_type, \@ignore);
427hash_save_array_words(\%use_type, \@use);
428
Joe Hershberger05622192011-10-18 10:06:59 +0000429my $dbg_values = 0;
430my $dbg_possible = 0;
431my $dbg_type = 0;
432my $dbg_attr = 0;
433for my $key (keys %debug) {
434 ## no critic
435 eval "\${dbg_$key} = '$debug{$key}';";
436 die "$@" if ($@);
437}
438
439my $rpt_cleaners = 0;
440
441if ($terse) {
442 $emacs = 1;
443 $quiet++;
444}
445
446if ($tree) {
447 if (defined $root) {
448 if (!top_of_kernel_tree($root)) {
449 die "$P: $root: --root does not point at a valid tree\n";
450 }
451 } else {
452 if (top_of_kernel_tree('.')) {
453 $root = '.';
454 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
455 top_of_kernel_tree($1)) {
456 $root = $1;
457 }
458 }
459
460 if (!defined $root) {
461 print "Must be run from the top-level dir. of a kernel tree\n";
462 exit(2);
463 }
464}
465
466my $emitted_corrupt = 0;
467
468our $Ident = qr{
469 [A-Za-z_][A-Za-z\d_]*
470 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
471 }x;
472our $Storage = qr{extern|static|asmlinkage};
473our $Sparse = qr{
474 __user|
475 __kernel|
476 __force|
477 __iomem|
478 __must_check|
Joe Hershberger05622192011-10-18 10:06:59 +0000479 __kprobes|
480 __ref|
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200481 __refconst|
482 __refdata|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200483 __rcu|
484 __private
Joe Hershberger05622192011-10-18 10:06:59 +0000485 }x;
Tom Rini6b9709d2014-02-27 08:27:28 -0500486our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
487our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
488our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
489our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
490our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Hershberger05622192011-10-18 10:06:59 +0000491
492# Notes to $Attribute:
493# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
494our $Attribute = qr{
495 const|
Tom Rinie199fb32021-08-03 08:31:56 -0400496 volatile|
Joe Hershberger05622192011-10-18 10:06:59 +0000497 __percpu|
498 __nocast|
499 __safe|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200500 __bitwise|
Joe Hershberger05622192011-10-18 10:06:59 +0000501 __packed__|
502 __packed2__|
503 __naked|
504 __maybe_unused|
505 __always_unused|
506 __noreturn|
507 __used|
508 __cold|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200509 __pure|
Joe Hershberger05622192011-10-18 10:06:59 +0000510 __noclone|
511 __deprecated|
512 __read_mostly|
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200513 __ro_after_init|
Joe Hershberger05622192011-10-18 10:06:59 +0000514 __kprobes|
Tom Rini6b9709d2014-02-27 08:27:28 -0500515 $InitAttribute|
Joe Hershberger05622192011-10-18 10:06:59 +0000516 ____cacheline_aligned|
517 ____cacheline_aligned_in_smp|
518 ____cacheline_internodealigned_in_smp|
Simon Glass587254e2022-01-23 12:55:11 -0700519 __weak|
520 __alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\)
Joe Hershberger05622192011-10-18 10:06:59 +0000521 }x;
522our $Modifier;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200523our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Joe Hershberger05622192011-10-18 10:06:59 +0000524our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
525our $Lval = qr{$Ident(?:$Member)*};
526
Tom Rini6b9709d2014-02-27 08:27:28 -0500527our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
528our $Binary = qr{(?i)0b[01]+$Int_type?};
529our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
530our $Int = qr{[0-9]+$Int_type?};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200531our $Octal = qr{0[0-7]+$Int_type?};
Simon Glass587254e2022-01-23 12:55:11 -0700532our $String = qr{(?:\b[Lu])?"[X\t]*"};
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000533our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
534our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
535our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
536our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200537our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000538our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200539our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Tom Rini6b9709d2014-02-27 08:27:28 -0500540our $Arithmetic = qr{\+|-|\*|\/|%};
Joe Hershberger05622192011-10-18 10:06:59 +0000541our $Operators = qr{
542 <=|>=|==|!=|
543 =>|->|<<|>>|<|>|!|~|
Tom Rini6b9709d2014-02-27 08:27:28 -0500544 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Joe Hershberger05622192011-10-18 10:06:59 +0000545 }x;
546
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200547our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
548
549our $BasicType;
Joe Hershberger05622192011-10-18 10:06:59 +0000550our $NonptrType;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200551our $NonptrTypeMisordered;
Tom Rini6b9709d2014-02-27 08:27:28 -0500552our $NonptrTypeWithAttr;
Joe Hershberger05622192011-10-18 10:06:59 +0000553our $Type;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200554our $TypeMisordered;
Joe Hershberger05622192011-10-18 10:06:59 +0000555our $Declare;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200556our $DeclareMisordered;
Joe Hershberger05622192011-10-18 10:06:59 +0000557
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000558our $NON_ASCII_UTF8 = qr{
559 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Joe Hershberger05622192011-10-18 10:06:59 +0000560 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
561 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
562 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
563 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
564 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
565 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
566}x;
567
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000568our $UTF8 = qr{
569 [\x09\x0A\x0D\x20-\x7E] # ASCII
570 | $NON_ASCII_UTF8
571}x;
572
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200573our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
574our $typeOtherOSTypedefs = qr{(?x:
575 u_(?:char|short|int|long) | # bsd
576 u(?:nchar|short|int|long) # sysv
577)};
578our $typeKernelTypedefs = qr{(?x:
Joe Hershberger05622192011-10-18 10:06:59 +0000579 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
580 atomic_t
581)};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200582our $typeTypedefs = qr{(?x:
583 $typeC99Typedefs\b|
584 $typeOtherOSTypedefs\b|
585 $typeKernelTypedefs\b
586)};
587
588our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
Joe Hershberger05622192011-10-18 10:06:59 +0000589
590our $logFunctions = qr{(?x:
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200591 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Tom Rini6b9709d2014-02-27 08:27:28 -0500592 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200593 TP_printk|
Joe Hershberger05622192011-10-18 10:06:59 +0000594 WARN(?:_RATELIMIT|_ONCE|)|
595 panic|
James Byrne66b3ccc2019-11-21 14:32:46 +0000596 debug|
597 printf|
Tom Rini6b9709d2014-02-27 08:27:28 -0500598 MODULE_[A-Z_]+|
599 seq_vprintf|seq_printf|seq_puts
Joe Hershberger05622192011-10-18 10:06:59 +0000600)};
601
Tom Rinic57383b2020-06-16 10:29:46 -0400602our $allocFunctions = qr{(?x:
603 (?:(?:devm_)?
Tom Rinie199fb32021-08-03 08:31:56 -0400604 (?:kv|k|v)[czm]alloc(?:_array)?(?:_node)? |
Tom Rinic57383b2020-06-16 10:29:46 -0400605 kstrdup(?:_const)? |
606 kmemdup(?:_nul)?) |
607 (?:\w+)?alloc_skb(?:_ip_align)? |
608 # dev_alloc_skb/netdev_alloc_skb, et al
609 dma_alloc_coherent
610)};
611
Joe Hershberger05622192011-10-18 10:06:59 +0000612our $signature_tags = qr{(?xi:
613 Signed-off-by:|
Tom Rinic57383b2020-06-16 10:29:46 -0400614 Co-developed-by:|
Joe Hershberger05622192011-10-18 10:06:59 +0000615 Acked-by:|
616 Tested-by:|
617 Reviewed-by:|
618 Reported-by:|
Tom Rini6b9709d2014-02-27 08:27:28 -0500619 Suggested-by:|
Joe Hershberger05622192011-10-18 10:06:59 +0000620 To:|
621 Cc:
622)};
623
Tom Rinie199fb32021-08-03 08:31:56 -0400624our $tracing_logging_tags = qr{(?xi:
625 [=-]*> |
626 <[=-]* |
627 \[ |
628 \] |
629 start |
630 called |
631 entered |
632 entry |
633 enter |
634 in |
635 inside |
636 here |
637 begin |
638 exit |
639 end |
640 done |
641 leave |
642 completed |
643 out |
644 return |
645 [\.\!:\s]*
646)};
647
648sub edit_distance_min {
649 my (@arr) = @_;
650 my $len = scalar @arr;
651 if ((scalar @arr) < 1) {
652 # if underflow, return
653 return;
654 }
655 my $min = $arr[0];
656 for my $i (0 .. ($len-1)) {
657 if ($arr[$i] < $min) {
658 $min = $arr[$i];
659 }
660 }
661 return $min;
662}
663
664sub get_edit_distance {
665 my ($str1, $str2) = @_;
666 $str1 = lc($str1);
667 $str2 = lc($str2);
668 $str1 =~ s/-//g;
669 $str2 =~ s/-//g;
670 my $len1 = length($str1);
671 my $len2 = length($str2);
672 # two dimensional array storing minimum edit distance
673 my @distance;
674 for my $i (0 .. $len1) {
675 for my $j (0 .. $len2) {
676 if ($i == 0) {
677 $distance[$i][$j] = $j;
678 } elsif ($j == 0) {
679 $distance[$i][$j] = $i;
680 } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
681 $distance[$i][$j] = $distance[$i - 1][$j - 1];
682 } else {
683 my $dist1 = $distance[$i][$j - 1]; #insert distance
684 my $dist2 = $distance[$i - 1][$j]; # remove
685 my $dist3 = $distance[$i - 1][$j - 1]; #replace
686 $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
687 }
688 }
689 }
690 return $distance[$len1][$len2];
691}
692
693sub find_standard_signature {
694 my ($sign_off) = @_;
695 my @standard_signature_tags = (
696 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
697 'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
698 );
699 foreach my $signature (@standard_signature_tags) {
700 return $signature if (get_edit_distance($sign_off, $signature) <= 2);
701 }
702
703 return "";
704}
705
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200706our @typeListMisordered = (
707 qr{char\s+(?:un)?signed},
708 qr{int\s+(?:(?:un)?signed\s+)?short\s},
709 qr{int\s+short(?:\s+(?:un)?signed)},
710 qr{short\s+int(?:\s+(?:un)?signed)},
711 qr{(?:un)?signed\s+int\s+short},
712 qr{short\s+(?:un)?signed},
713 qr{long\s+int\s+(?:un)?signed},
714 qr{int\s+long\s+(?:un)?signed},
715 qr{long\s+(?:un)?signed\s+int},
716 qr{int\s+(?:un)?signed\s+long},
717 qr{int\s+(?:un)?signed},
718 qr{int\s+long\s+long\s+(?:un)?signed},
719 qr{long\s+long\s+int\s+(?:un)?signed},
720 qr{long\s+long\s+(?:un)?signed\s+int},
721 qr{long\s+long\s+(?:un)?signed},
722 qr{long\s+(?:un)?signed},
723);
724
Joe Hershberger05622192011-10-18 10:06:59 +0000725our @typeList = (
726 qr{void},
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200727 qr{(?:(?:un)?signed\s+)?char},
728 qr{(?:(?:un)?signed\s+)?short\s+int},
729 qr{(?:(?:un)?signed\s+)?short},
730 qr{(?:(?:un)?signed\s+)?int},
731 qr{(?:(?:un)?signed\s+)?long\s+int},
732 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
733 qr{(?:(?:un)?signed\s+)?long\s+long},
734 qr{(?:(?:un)?signed\s+)?long},
735 qr{(?:un)?signed},
Joe Hershberger05622192011-10-18 10:06:59 +0000736 qr{float},
737 qr{double},
738 qr{bool},
739 qr{struct\s+$Ident},
740 qr{union\s+$Ident},
741 qr{enum\s+$Ident},
742 qr{${Ident}_t},
743 qr{${Ident}_handler},
744 qr{${Ident}_handler_fn},
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200745 @typeListMisordered,
Joe Hershberger05622192011-10-18 10:06:59 +0000746);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200747
748our $C90_int_types = qr{(?x:
749 long\s+long\s+int\s+(?:un)?signed|
750 long\s+long\s+(?:un)?signed\s+int|
751 long\s+long\s+(?:un)?signed|
752 (?:(?:un)?signed\s+)?long\s+long\s+int|
753 (?:(?:un)?signed\s+)?long\s+long|
754 int\s+long\s+long\s+(?:un)?signed|
755 int\s+(?:(?:un)?signed\s+)?long\s+long|
756
757 long\s+int\s+(?:un)?signed|
758 long\s+(?:un)?signed\s+int|
759 long\s+(?:un)?signed|
760 (?:(?:un)?signed\s+)?long\s+int|
761 (?:(?:un)?signed\s+)?long|
762 int\s+long\s+(?:un)?signed|
763 int\s+(?:(?:un)?signed\s+)?long|
764
765 int\s+(?:un)?signed|
766 (?:(?:un)?signed\s+)?int
767)};
768
769our @typeListFile = ();
Tom Rini6b9709d2014-02-27 08:27:28 -0500770our @typeListWithAttr = (
771 @typeList,
772 qr{struct\s+$InitAttribute\s+$Ident},
773 qr{union\s+$InitAttribute\s+$Ident},
774);
775
Joe Hershberger05622192011-10-18 10:06:59 +0000776our @modifierList = (
777 qr{fastcall},
778);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200779our @modifierListFile = ();
780
781our @mode_permission_funcs = (
782 ["module_param", 3],
783 ["module_param_(?:array|named|string)", 4],
784 ["module_param_array_named", 5],
785 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
786 ["proc_create(?:_data|)", 2],
787 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
788 ["IIO_DEV_ATTR_[A-Z_]+", 1],
789 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
790 ["SENSOR_TEMPLATE(?:_2|)", 3],
791 ["__ATTR", 2],
792);
793
Tom Rinie199fb32021-08-03 08:31:56 -0400794my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
795
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200796#Create a search pattern for all these functions to speed up a loop below
797our $mode_perms_search = "";
798foreach my $entry (@mode_permission_funcs) {
799 $mode_perms_search .= '|' if ($mode_perms_search ne "");
800 $mode_perms_search .= $entry->[0];
801}
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200802$mode_perms_search = "(?:${mode_perms_search})";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200803
Tom Rinic57383b2020-06-16 10:29:46 -0400804our %deprecated_apis = (
805 "synchronize_rcu_bh" => "synchronize_rcu",
806 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
807 "call_rcu_bh" => "call_rcu",
808 "rcu_barrier_bh" => "rcu_barrier",
809 "synchronize_sched" => "synchronize_rcu",
810 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
811 "call_rcu_sched" => "call_rcu",
812 "rcu_barrier_sched" => "rcu_barrier",
813 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
814 "cond_synchronize_sched" => "cond_synchronize_rcu",
815);
816
817#Create a search pattern for all these strings to speed up a loop below
818our $deprecated_apis_search = "";
819foreach my $entry (keys %deprecated_apis) {
820 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
821 $deprecated_apis_search .= $entry;
822}
823$deprecated_apis_search = "(?:${deprecated_apis_search})";
824
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200825our $mode_perms_world_writable = qr{
826 S_IWUGO |
827 S_IWOTH |
828 S_IRWXUGO |
829 S_IALLUGO |
830 0[0-7][0-7][2367]
831}x;
832
833our %mode_permission_string_types = (
834 "S_IRWXU" => 0700,
835 "S_IRUSR" => 0400,
836 "S_IWUSR" => 0200,
837 "S_IXUSR" => 0100,
838 "S_IRWXG" => 0070,
839 "S_IRGRP" => 0040,
840 "S_IWGRP" => 0020,
841 "S_IXGRP" => 0010,
842 "S_IRWXO" => 0007,
843 "S_IROTH" => 0004,
844 "S_IWOTH" => 0002,
845 "S_IXOTH" => 0001,
846 "S_IRWXUGO" => 0777,
847 "S_IRUGO" => 0444,
848 "S_IWUGO" => 0222,
849 "S_IXUGO" => 0111,
850);
851
852#Create a search pattern for all these strings to speed up a loop below
853our $mode_perms_string_search = "";
854foreach my $entry (keys %mode_permission_string_types) {
855 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
856 $mode_perms_string_search .= $entry;
857}
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200858our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
859our $multi_mode_perms_string_search = qr{
860 ${single_mode_perms_string_search}
861 (?:\s*\|\s*${single_mode_perms_string_search})*
862}x;
863
864sub perms_to_octal {
865 my ($string) = @_;
866
867 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
868
869 my $val = "";
870 my $oval = "";
871 my $to = 0;
872 my $curpos = 0;
873 my $lastpos = 0;
874 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
875 $curpos = pos($string);
876 my $match = $2;
877 my $omatch = $1;
878 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
879 $lastpos = $curpos;
880 $to |= $mode_permission_string_types{$match};
881 $val .= '\s*\|\s*' if ($val ne "");
882 $val .= $match;
883 $oval .= $omatch;
884 }
885 $oval =~ s/^\s*\|\s*//;
886 $oval =~ s/\s*\|\s*$//;
887 return sprintf("%04o", $to);
888}
Joe Hershberger05622192011-10-18 10:06:59 +0000889
890our $allowed_asm_includes = qr{(?x:
891 irq|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200892 memory|
893 time|
894 reboot
Joe Hershberger05622192011-10-18 10:06:59 +0000895)};
896# memory.h: ARM has a custom one
897
Dan Murphyc10e0f52017-01-31 14:15:53 -0600898# Load common spelling mistakes and build regular expression list.
899my $misspellings;
900my %spelling_fix;
901
902if (open(my $spelling, '<', $spelling_file)) {
903 while (<$spelling>) {
904 my $line = $_;
905
906 $line =~ s/\s*\n?$//g;
907 $line =~ s/^\s*//g;
908
909 next if ($line =~ m/^\s*#/);
910 next if ($line =~ m/^\s*$/);
911
912 my ($suspect, $fix) = split(/\|\|/, $line);
913
914 $spelling_fix{$suspect} = $fix;
915 }
916 close($spelling);
917} else {
918 warn "No typos will be found - file '$spelling_file': $!\n";
919}
920
921if ($codespell) {
922 if (open(my $spelling, '<', $codespellfile)) {
923 while (<$spelling>) {
924 my $line = $_;
925
926 $line =~ s/\s*\n?$//g;
927 $line =~ s/^\s*//g;
928
929 next if ($line =~ m/^\s*#/);
930 next if ($line =~ m/^\s*$/);
931 next if ($line =~ m/, disabled/i);
932
933 $line =~ s/,.*$//;
934
935 my ($suspect, $fix) = split(/->/, $line);
936
937 $spelling_fix{$suspect} = $fix;
938 }
939 close($spelling);
940 } else {
941 warn "No codespell typos will be found - file '$codespellfile': $!\n";
942 }
943}
944
945$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
946
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200947sub read_words {
948 my ($wordsRef, $file) = @_;
949
950 if (open(my $words, '<', $file)) {
951 while (<$words>) {
952 my $line = $_;
953
954 $line =~ s/\s*\n?$//g;
955 $line =~ s/^\s*//g;
956
957 next if ($line =~ m/^\s*#/);
958 next if ($line =~ m/^\s*$/);
959 if ($line =~ /\s/) {
960 print("$file: '$line' invalid - ignored\n");
961 next;
962 }
963
Tom Rinie199fb32021-08-03 08:31:56 -0400964 $$wordsRef .= '|' if (defined $$wordsRef);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200965 $$wordsRef .= $line;
966 }
967 close($file);
968 return 1;
969 }
970
971 return 0;
972}
973
Tom Rinie199fb32021-08-03 08:31:56 -0400974my $const_structs;
975if (show_type("CONST_STRUCT")) {
976 read_words(\$const_structs, $conststructsfile)
977 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
978}
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200979
Tom Rinie199fb32021-08-03 08:31:56 -0400980if (defined($typedefsfile)) {
981 my $typeOtherTypedefs;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200982 read_words(\$typeOtherTypedefs, $typedefsfile)
983 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
Tom Rinie199fb32021-08-03 08:31:56 -0400984 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200985}
Dan Murphyc10e0f52017-01-31 14:15:53 -0600986
Joe Hershberger05622192011-10-18 10:06:59 +0000987sub build_types {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200988 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
989 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
990 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Tom Rini6b9709d2014-02-27 08:27:28 -0500991 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Joe Hershberger05622192011-10-18 10:06:59 +0000992 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200993 $BasicType = qr{
994 (?:$typeTypedefs\b)|
995 (?:${all}\b)
996 }x;
Joe Hershberger05622192011-10-18 10:06:59 +0000997 $NonptrType = qr{
998 (?:$Modifier\s+|const\s+)*
999 (?:
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00001000 (?:typeof|__typeof__)\s*\([^\)]*\)|
Joe Hershberger05622192011-10-18 10:06:59 +00001001 (?:$typeTypedefs\b)|
1002 (?:${all}\b)
1003 )
1004 (?:\s+$Modifier|\s+const)*
1005 }x;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001006 $NonptrTypeMisordered = qr{
1007 (?:$Modifier\s+|const\s+)*
1008 (?:
1009 (?:${Misordered}\b)
1010 )
1011 (?:\s+$Modifier|\s+const)*
1012 }x;
Tom Rini6b9709d2014-02-27 08:27:28 -05001013 $NonptrTypeWithAttr = qr{
1014 (?:$Modifier\s+|const\s+)*
1015 (?:
1016 (?:typeof|__typeof__)\s*\([^\)]*\)|
1017 (?:$typeTypedefs\b)|
1018 (?:${allWithAttr}\b)
1019 )
1020 (?:\s+$Modifier|\s+const)*
1021 }x;
Joe Hershberger05622192011-10-18 10:06:59 +00001022 $Type = qr{
1023 $NonptrType
Tom Rinic57383b2020-06-16 10:29:46 -04001024 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Hershberger05622192011-10-18 10:06:59 +00001025 (?:\s+$Inline|\s+$Modifier)*
1026 }x;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001027 $TypeMisordered = qr{
1028 $NonptrTypeMisordered
Tom Rinic57383b2020-06-16 10:29:46 -04001029 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001030 (?:\s+$Inline|\s+$Modifier)*
1031 }x;
1032 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
1033 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Joe Hershberger05622192011-10-18 10:06:59 +00001034}
1035build_types();
1036
Joe Hershberger05622192011-10-18 10:06:59 +00001037our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00001038
1039# Using $balanced_parens, $LvalOrFunc, or $FuncArg
1040# requires at least perl version v5.10.0
1041# Any use must be runtime checked with $^V
1042
1043our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001044our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
1045our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
1046
1047our $declaration_macros = qr{(?x:
1048 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
1049 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02001050 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001051)};
Joe Hershberger05622192011-10-18 10:06:59 +00001052
Tom Rinie199fb32021-08-03 08:31:56 -04001053our %allow_repeated_words = (
1054 add => '',
1055 added => '',
1056 bad => '',
1057 be => '',
1058);
1059
Joe Hershberger05622192011-10-18 10:06:59 +00001060sub deparenthesize {
1061 my ($string) = @_;
1062 return "" if (!defined($string));
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001063
1064 while ($string =~ /^\s*\(.*\)\s*$/) {
1065 $string =~ s@^\s*\(\s*@@;
1066 $string =~ s@\s*\)\s*$@@;
1067 }
1068
Joe Hershberger05622192011-10-18 10:06:59 +00001069 $string =~ s@\s+@ @g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001070
Joe Hershberger05622192011-10-18 10:06:59 +00001071 return $string;
1072}
1073
Tom Rini6b9709d2014-02-27 08:27:28 -05001074sub seed_camelcase_file {
1075 my ($file) = @_;
1076
1077 return if (!(-f $file));
1078
1079 local $/;
1080
1081 open(my $include_file, '<', "$file")
1082 or warn "$P: Can't read '$file' $!\n";
1083 my $text = <$include_file>;
1084 close($include_file);
1085
1086 my @lines = split('\n', $text);
1087
1088 foreach my $line (@lines) {
1089 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
1090 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
1091 $camelcase{$1} = 1;
1092 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
1093 $camelcase{$1} = 1;
1094 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
1095 $camelcase{$1} = 1;
1096 }
1097 }
1098}
1099
Tom Rinic57383b2020-06-16 10:29:46 -04001100our %maintained_status = ();
1101
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001102sub is_maintained_obsolete {
1103 my ($filename) = @_;
1104
1105 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
1106
Tom Rinic57383b2020-06-16 10:29:46 -04001107 if (!exists($maintained_status{$filename})) {
1108 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
1109 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001110
Tom Rinic57383b2020-06-16 10:29:46 -04001111 return $maintained_status{$filename} =~ /obsolete/i;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001112}
1113
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02001114sub is_SPDX_License_valid {
1115 my ($license) = @_;
1116
Simon Glass587254e2022-01-23 12:55:11 -07001117 return 1 if (!$tree || which("python3") eq "" || !(-x "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02001118
1119 my $root_path = abs_path($root);
Simon Glass587254e2022-01-23 12:55:11 -07001120 my $status = `cd "$root_path"; echo "$license" | scripts/spdxcheck.py -`;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02001121 return 0 if ($status ne "");
1122 return 1;
1123}
1124
Tom Rini6b9709d2014-02-27 08:27:28 -05001125my $camelcase_seeded = 0;
1126sub seed_camelcase_includes {
1127 return if ($camelcase_seeded);
1128
1129 my $files;
1130 my $camelcase_cache = "";
1131 my @include_files = ();
1132
1133 $camelcase_seeded = 1;
1134
Tom Rinie199fb32021-08-03 08:31:56 -04001135 if (-e "$gitroot") {
Tom Rinic57383b2020-06-16 10:29:46 -04001136 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Tom Rini6b9709d2014-02-27 08:27:28 -05001137 chomp $git_last_include_commit;
1138 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
1139 } else {
1140 my $last_mod_date = 0;
1141 $files = `find $root/include -name "*.h"`;
1142 @include_files = split('\n', $files);
1143 foreach my $file (@include_files) {
1144 my $date = POSIX::strftime("%Y%m%d%H%M",
1145 localtime((stat $file)[9]));
1146 $last_mod_date = $date if ($last_mod_date < $date);
1147 }
1148 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
1149 }
1150
1151 if ($camelcase_cache ne "" && -f $camelcase_cache) {
1152 open(my $camelcase_file, '<', "$camelcase_cache")
1153 or warn "$P: Can't read '$camelcase_cache' $!\n";
1154 while (<$camelcase_file>) {
1155 chomp;
1156 $camelcase{$_} = 1;
1157 }
1158 close($camelcase_file);
1159
1160 return;
1161 }
1162
Tom Rinie199fb32021-08-03 08:31:56 -04001163 if (-e "$gitroot") {
Tom Rinic57383b2020-06-16 10:29:46 -04001164 $files = `${git_command} ls-files "include/*.h"`;
Tom Rini6b9709d2014-02-27 08:27:28 -05001165 @include_files = split('\n', $files);
1166 }
1167
1168 foreach my $file (@include_files) {
1169 seed_camelcase_file($file);
1170 }
1171
1172 if ($camelcase_cache ne "") {
1173 unlink glob ".checkpatch-camelcase.*";
1174 open(my $camelcase_file, '>', "$camelcase_cache")
1175 or warn "$P: Can't write '$camelcase_cache' $!\n";
1176 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1177 print $camelcase_file ("$_\n");
1178 }
1179 close($camelcase_file);
1180 }
1181}
1182
Tom Rinie199fb32021-08-03 08:31:56 -04001183sub git_is_single_file {
1184 my ($filename) = @_;
1185
1186 return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1187
1188 my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1189 my $count = $output =~ tr/\n//;
1190 return $count eq 1 && $output =~ m{^${filename}$};
1191}
1192
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001193sub git_commit_info {
1194 my ($commit, $id, $desc) = @_;
1195
Tom Rinie199fb32021-08-03 08:31:56 -04001196 return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001197
Tom Rinic57383b2020-06-16 10:29:46 -04001198 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001199 $output =~ s/^\s*//gm;
1200 my @lines = split("\n", $output);
1201
1202 return ($id, $desc) if ($#lines < 0);
1203
Tom Rinic57383b2020-06-16 10:29:46 -04001204 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001205# Maybe one day convert this block of bash into something that returns
1206# all matching commit ids, but it's very slow...
1207#
1208# echo "checking commits $1..."
1209# git rev-list --remotes | grep -i "^$1" |
1210# while read line ; do
1211# git log --format='%H %s' -1 $line |
1212# echo "commit $(cut -c 1-12,41-)"
1213# done
Simon Glass587254e2022-01-23 12:55:11 -07001214 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ ||
1215 $lines[0] =~ /^fatal: bad object $commit/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001216 $id = undef;
1217 } else {
1218 $id = substr($lines[0], 0, 12);
1219 $desc = substr($lines[0], 41);
1220 }
1221
1222 return ($id, $desc);
1223}
1224
Joe Hershberger05622192011-10-18 10:06:59 +00001225$chk_signoff = 0 if ($file);
1226
Joe Hershberger05622192011-10-18 10:06:59 +00001227my @rawlines = ();
1228my @lines = ();
Tom Rini6b9709d2014-02-27 08:27:28 -05001229my @fixed = ();
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001230my @fixed_inserted = ();
1231my @fixed_deleted = ();
Dan Murphyc10e0f52017-01-31 14:15:53 -06001232my $fixlinenr = -1;
1233
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001234# If input is git commits, extract all commits from the commit expressions.
1235# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
Tom Rinie199fb32021-08-03 08:31:56 -04001236die "$P: No git repository found\n" if ($git && !-e "$gitroot");
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001237
1238if ($git) {
1239 my @commits = ();
1240 foreach my $commit_expr (@ARGV) {
1241 my $git_range;
1242 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1243 $git_range = "-$2 $1";
1244 } elsif ($commit_expr =~ m/\.\./) {
1245 $git_range = "$commit_expr";
1246 } else {
1247 $git_range = "-1 $commit_expr";
1248 }
Tom Rinic57383b2020-06-16 10:29:46 -04001249 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001250 foreach my $line (split(/\n/, $lines)) {
1251 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1252 next if (!defined($1) || !defined($2));
1253 my $sha1 = $1;
1254 my $subject = $2;
1255 unshift(@commits, $sha1);
1256 $git_commits{$sha1} = $subject;
1257 }
1258 }
1259 die "$P: no git commits after extraction!\n" if (@commits == 0);
1260 @ARGV = @commits;
1261}
1262
1263my $vname;
Tom Rinic57383b2020-06-16 10:29:46 -04001264$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Joe Hershberger05622192011-10-18 10:06:59 +00001265for my $filename (@ARGV) {
1266 my $FILE;
Tom Rinie199fb32021-08-03 08:31:56 -04001267 my $is_git_file = git_is_single_file($filename);
1268 my $oldfile = $file;
1269 $file = 1 if ($is_git_file);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001270 if ($git) {
1271 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1272 die "$P: $filename: git format-patch failed - $!\n";
1273 } elsif ($file) {
Joe Hershberger05622192011-10-18 10:06:59 +00001274 open($FILE, '-|', "diff -u /dev/null $filename") ||
1275 die "$P: $filename: diff failed - $!\n";
1276 } elsif ($filename eq '-') {
1277 open($FILE, '<&STDIN');
1278 } else {
1279 open($FILE, '<', "$filename") ||
1280 die "$P: $filename: open failed - $!\n";
1281 }
1282 if ($filename eq '-') {
1283 $vname = 'Your patch';
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001284 } elsif ($git) {
1285 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Joe Hershberger05622192011-10-18 10:06:59 +00001286 } else {
1287 $vname = $filename;
1288 }
1289 while (<$FILE>) {
1290 chomp;
1291 push(@rawlines, $_);
Tom Rinie199fb32021-08-03 08:31:56 -04001292 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
Joe Hershberger05622192011-10-18 10:06:59 +00001293 }
1294 close($FILE);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001295
1296 if ($#ARGV > 0 && $quiet == 0) {
1297 print '-' x length($vname) . "\n";
1298 print "$vname\n";
1299 print '-' x length($vname) . "\n";
1300 }
1301
Joe Hershberger05622192011-10-18 10:06:59 +00001302 if (!process($filename)) {
1303 $exit = 1;
1304 }
1305 @rawlines = ();
1306 @lines = ();
Tom Rini6b9709d2014-02-27 08:27:28 -05001307 @fixed = ();
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001308 @fixed_inserted = ();
1309 @fixed_deleted = ();
1310 $fixlinenr = -1;
1311 @modifierListFile = ();
1312 @typeListFile = ();
1313 build_types();
Tom Rinie199fb32021-08-03 08:31:56 -04001314 $file = $oldfile if ($is_git_file);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001315}
1316
1317if (!$quiet) {
1318 hash_show_words(\%use_type, "Used");
1319 hash_show_words(\%ignore_type, "Ignored");
1320
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02001321 if (!$perl_version_ok) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001322 print << "EOM"
1323
1324NOTE: perl $^V is not modern enough to detect all possible issues.
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02001325 An upgrade to at least perl $minimum_perl_version is suggested.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001326EOM
1327 }
1328 if ($exit) {
1329 print << "EOM"
1330
1331NOTE: If any of the errors are false positives, please report
1332 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1333EOM
1334 }
Joe Hershberger05622192011-10-18 10:06:59 +00001335}
1336
1337exit($exit);
1338
1339sub top_of_kernel_tree {
1340 my ($root) = @_;
1341
1342 my @tree_check = (
Tom Rini6b9709d2014-02-27 08:27:28 -05001343 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
Joe Hershberger05622192011-10-18 10:06:59 +00001344 "README", "Documentation", "arch", "include", "drivers",
1345 "fs", "init", "ipc", "kernel", "lib", "scripts",
1346 );
1347
1348 foreach my $check (@tree_check) {
1349 if (! -e $root . '/' . $check) {
1350 return 0;
1351 }
1352 }
1353 return 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00001354}
Joe Hershberger05622192011-10-18 10:06:59 +00001355
1356sub parse_email {
1357 my ($formatted_email) = @_;
1358
1359 my $name = "";
Tom Rinie199fb32021-08-03 08:31:56 -04001360 my $quoted = "";
Tom Rinic57383b2020-06-16 10:29:46 -04001361 my $name_comment = "";
Joe Hershberger05622192011-10-18 10:06:59 +00001362 my $address = "";
1363 my $comment = "";
1364
1365 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1366 $name = $1;
1367 $address = $2;
1368 $comment = $3 if defined $3;
1369 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1370 $address = $1;
1371 $comment = $2 if defined $2;
1372 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1373 $address = $1;
1374 $comment = $2 if defined $2;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02001375 $formatted_email =~ s/\Q$address\E.*$//;
Joe Hershberger05622192011-10-18 10:06:59 +00001376 $name = $formatted_email;
Tom Rini6b9709d2014-02-27 08:27:28 -05001377 $name = trim($name);
Joe Hershberger05622192011-10-18 10:06:59 +00001378 $name =~ s/^\"|\"$//g;
1379 # If there's a name left after stripping spaces and
1380 # leading quotes, and the address doesn't have both
1381 # leading and trailing angle brackets, the address
1382 # is invalid. ie:
1383 # "joe smith joe@smith.com" bad
1384 # "joe smith <joe@smith.com" bad
1385 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1386 $name = "";
1387 $address = "";
1388 $comment = "";
1389 }
1390 }
1391
Tom Rinie199fb32021-08-03 08:31:56 -04001392 # Extract comments from names excluding quoted parts
1393 # "John D. (Doe)" - Do not extract
1394 if ($name =~ s/\"(.+)\"//) {
1395 $quoted = $1;
Tom Rinic57383b2020-06-16 10:29:46 -04001396 }
Tom Rinie199fb32021-08-03 08:31:56 -04001397 while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1398 $name_comment .= trim($1);
1399 }
1400 $name =~ s/^[ \"]+|[ \"]+$//g;
1401 $name = trim("$quoted $name");
1402
Tom Rini6b9709d2014-02-27 08:27:28 -05001403 $address = trim($address);
Joe Hershberger05622192011-10-18 10:06:59 +00001404 $address =~ s/^\<|\>$//g;
Tom Rinie199fb32021-08-03 08:31:56 -04001405 $comment = trim($comment);
Joe Hershberger05622192011-10-18 10:06:59 +00001406
1407 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1408 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1409 $name = "\"$name\"";
1410 }
1411
Tom Rinic57383b2020-06-16 10:29:46 -04001412 return ($name, $name_comment, $address, $comment);
Joe Hershberger05622192011-10-18 10:06:59 +00001413}
1414
1415sub format_email {
Tom Rinie199fb32021-08-03 08:31:56 -04001416 my ($name, $name_comment, $address, $comment) = @_;
Joe Hershberger05622192011-10-18 10:06:59 +00001417
1418 my $formatted_email;
1419
Tom Rinie199fb32021-08-03 08:31:56 -04001420 $name =~ s/^[ \"]+|[ \"]+$//g;
Tom Rini6b9709d2014-02-27 08:27:28 -05001421 $address = trim($address);
Tom Rinie199fb32021-08-03 08:31:56 -04001422 $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
Joe Hershberger05622192011-10-18 10:06:59 +00001423
1424 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1425 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1426 $name = "\"$name\"";
1427 }
1428
Tom Rinie199fb32021-08-03 08:31:56 -04001429 $name_comment = trim($name_comment);
1430 $name_comment = " $name_comment" if ($name_comment ne "");
1431 $comment = trim($comment);
1432 $comment = " $comment" if ($comment ne "");
1433
Joe Hershberger05622192011-10-18 10:06:59 +00001434 if ("$name" eq "") {
1435 $formatted_email = "$address";
1436 } else {
Tom Rinie199fb32021-08-03 08:31:56 -04001437 $formatted_email = "$name$name_comment <$address>";
Joe Hershberger05622192011-10-18 10:06:59 +00001438 }
Tom Rinie199fb32021-08-03 08:31:56 -04001439 $formatted_email .= "$comment";
Joe Hershberger05622192011-10-18 10:06:59 +00001440 return $formatted_email;
1441}
1442
Tom Rinic57383b2020-06-16 10:29:46 -04001443sub reformat_email {
1444 my ($email) = @_;
1445
1446 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Tom Rinie199fb32021-08-03 08:31:56 -04001447 return format_email($email_name, $name_comment, $email_address, $comment);
Tom Rinic57383b2020-06-16 10:29:46 -04001448}
1449
1450sub same_email_addresses {
1451 my ($email1, $email2) = @_;
1452
1453 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1454 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1455
1456 return $email1_name eq $email2_name &&
Tom Rinie199fb32021-08-03 08:31:56 -04001457 $email1_address eq $email2_address &&
1458 $name1_comment eq $name2_comment &&
1459 $comment1 eq $comment2;
Tom Rinic57383b2020-06-16 10:29:46 -04001460}
1461
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001462sub which {
1463 my ($bin) = @_;
1464
1465 foreach my $path (split(/:/, $ENV{PATH})) {
1466 if (-e "$path/$bin") {
1467 return "$path/$bin";
1468 }
1469 }
1470
1471 return "";
1472}
1473
Joe Hershberger05622192011-10-18 10:06:59 +00001474sub which_conf {
1475 my ($conf) = @_;
1476
1477 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1478 if (-e "$path/$conf") {
1479 return "$path/$conf";
1480 }
1481 }
1482
1483 return "";
1484}
1485
1486sub expand_tabs {
1487 my ($str) = @_;
1488
1489 my $res = '';
1490 my $n = 0;
1491 for my $c (split(//, $str)) {
1492 if ($c eq "\t") {
1493 $res .= ' ';
1494 $n++;
Tom Rinic57383b2020-06-16 10:29:46 -04001495 for (; ($n % $tabsize) != 0; $n++) {
Joe Hershberger05622192011-10-18 10:06:59 +00001496 $res .= ' ';
1497 }
1498 next;
1499 }
1500 $res .= $c;
1501 $n++;
1502 }
1503
1504 return $res;
1505}
1506sub copy_spacing {
1507 (my $res = shift) =~ tr/\t/ /c;
1508 return $res;
1509}
1510
1511sub line_stats {
1512 my ($line) = @_;
1513
1514 # Drop the diff line leader and expand tabs
1515 $line =~ s/^.//;
1516 $line = expand_tabs($line);
1517
1518 # Pick the indent from the front of the line.
1519 my ($white) = ($line =~ /^(\s*)/);
1520
1521 return (length($line), length($white));
1522}
1523
1524my $sanitise_quote = '';
1525
1526sub sanitise_line_reset {
1527 my ($in_comment) = @_;
1528
1529 if ($in_comment) {
1530 $sanitise_quote = '*/';
1531 } else {
1532 $sanitise_quote = '';
1533 }
1534}
1535sub sanitise_line {
1536 my ($line) = @_;
1537
1538 my $res = '';
1539 my $l = '';
1540
1541 my $qlen = 0;
1542 my $off = 0;
1543 my $c;
1544
1545 # Always copy over the diff marker.
1546 $res = substr($line, 0, 1);
1547
1548 for ($off = 1; $off < length($line); $off++) {
1549 $c = substr($line, $off, 1);
1550
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02001551 # Comments we are whacking completely including the begin
Joe Hershberger05622192011-10-18 10:06:59 +00001552 # and end, all to $;.
1553 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1554 $sanitise_quote = '*/';
1555
1556 substr($res, $off, 2, "$;$;");
1557 $off++;
1558 next;
1559 }
1560 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1561 $sanitise_quote = '';
1562 substr($res, $off, 2, "$;$;");
1563 $off++;
1564 next;
1565 }
1566 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1567 $sanitise_quote = '//';
1568
1569 substr($res, $off, 2, $sanitise_quote);
1570 $off++;
1571 next;
1572 }
1573
1574 # A \ in a string means ignore the next character.
1575 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1576 $c eq "\\") {
1577 substr($res, $off, 2, 'XX');
1578 $off++;
1579 next;
1580 }
1581 # Regular quotes.
1582 if ($c eq "'" || $c eq '"') {
1583 if ($sanitise_quote eq '') {
1584 $sanitise_quote = $c;
1585
1586 substr($res, $off, 1, $c);
1587 next;
1588 } elsif ($sanitise_quote eq $c) {
1589 $sanitise_quote = '';
1590 }
1591 }
1592
1593 #print "c<$c> SQ<$sanitise_quote>\n";
1594 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1595 substr($res, $off, 1, $;);
1596 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1597 substr($res, $off, 1, $;);
1598 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1599 substr($res, $off, 1, 'X');
1600 } else {
1601 substr($res, $off, 1, $c);
1602 }
1603 }
1604
1605 if ($sanitise_quote eq '//') {
1606 $sanitise_quote = '';
1607 }
1608
1609 # The pathname on a #include may be surrounded by '<' and '>'.
1610 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1611 my $clean = 'X' x length($1);
1612 $res =~ s@\<.*\>@<$clean>@;
1613
1614 # The whole of a #error is a string.
1615 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1616 my $clean = 'X' x length($1);
1617 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1618 }
1619
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001620 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1621 my $match = $1;
1622 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1623 }
1624
Joe Hershberger05622192011-10-18 10:06:59 +00001625 return $res;
1626}
1627
Tom Rini6b9709d2014-02-27 08:27:28 -05001628sub get_quoted_string {
1629 my ($line, $rawline) = @_;
1630
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02001631 return "" if (!defined($line) || !defined($rawline));
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001632 return "" if ($line !~ m/($String)/g);
Tom Rini6b9709d2014-02-27 08:27:28 -05001633 return substr($rawline, $-[0], $+[0] - $-[0]);
1634}
1635
Joe Hershberger05622192011-10-18 10:06:59 +00001636sub ctx_statement_block {
1637 my ($linenr, $remain, $off) = @_;
1638 my $line = $linenr - 1;
1639 my $blk = '';
1640 my $soff = $off;
1641 my $coff = $off - 1;
1642 my $coff_set = 0;
1643
1644 my $loff = 0;
1645
1646 my $type = '';
1647 my $level = 0;
1648 my @stack = ();
1649 my $p;
1650 my $c;
1651 my $len = 0;
1652
1653 my $remainder;
1654 while (1) {
1655 @stack = (['', 0]) if ($#stack == -1);
1656
1657 #warn "CSB: blk<$blk> remain<$remain>\n";
1658 # If we are about to drop off the end, pull in more
1659 # context.
1660 if ($off >= $len) {
1661 for (; $remain > 0; $line++) {
1662 last if (!defined $lines[$line]);
1663 next if ($lines[$line] =~ /^-/);
1664 $remain--;
1665 $loff = $len;
1666 $blk .= $lines[$line] . "\n";
1667 $len = length($blk);
1668 $line++;
1669 last;
1670 }
1671 # Bail if there is no further context.
1672 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1673 if ($off >= $len) {
1674 last;
1675 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00001676 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1677 $level++;
1678 $type = '#';
1679 }
Joe Hershberger05622192011-10-18 10:06:59 +00001680 }
1681 $p = $c;
1682 $c = substr($blk, $off, 1);
1683 $remainder = substr($blk, $off);
1684
1685 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1686
1687 # Handle nested #if/#else.
1688 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1689 push(@stack, [ $type, $level ]);
1690 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1691 ($type, $level) = @{$stack[$#stack - 1]};
1692 } elsif ($remainder =~ /^#\s*endif\b/) {
1693 ($type, $level) = @{pop(@stack)};
1694 }
1695
1696 # Statement ends at the ';' or a close '}' at the
1697 # outermost level.
1698 if ($level == 0 && $c eq ';') {
1699 last;
1700 }
1701
1702 # An else is really a conditional as long as its not else if
1703 if ($level == 0 && $coff_set == 0 &&
1704 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1705 $remainder =~ /^(else)(?:\s|{)/ &&
1706 $remainder !~ /^else\s+if\b/) {
1707 $coff = $off + length($1) - 1;
1708 $coff_set = 1;
1709 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1710 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1711 }
1712
1713 if (($type eq '' || $type eq '(') && $c eq '(') {
1714 $level++;
1715 $type = '(';
1716 }
1717 if ($type eq '(' && $c eq ')') {
1718 $level--;
1719 $type = ($level != 0)? '(' : '';
1720
1721 if ($level == 0 && $coff < $soff) {
1722 $coff = $off;
1723 $coff_set = 1;
1724 #warn "CSB: mark coff<$coff>\n";
1725 }
1726 }
1727 if (($type eq '' || $type eq '{') && $c eq '{') {
1728 $level++;
1729 $type = '{';
1730 }
1731 if ($type eq '{' && $c eq '}') {
1732 $level--;
1733 $type = ($level != 0)? '{' : '';
1734
1735 if ($level == 0) {
1736 if (substr($blk, $off + 1, 1) eq ';') {
1737 $off++;
1738 }
1739 last;
1740 }
1741 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00001742 # Preprocessor commands end at the newline unless escaped.
1743 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1744 $level--;
1745 $type = '';
1746 $off++;
1747 last;
1748 }
Joe Hershberger05622192011-10-18 10:06:59 +00001749 $off++;
1750 }
1751 # We are truly at the end, so shuffle to the next line.
1752 if ($off == $len) {
1753 $loff = $len + 1;
1754 $line++;
1755 $remain--;
1756 }
1757
1758 my $statement = substr($blk, $soff, $off - $soff + 1);
1759 my $condition = substr($blk, $soff, $coff - $soff + 1);
1760
1761 #warn "STATEMENT<$statement>\n";
1762 #warn "CONDITION<$condition>\n";
1763
1764 #print "coff<$coff> soff<$off> loff<$loff>\n";
1765
1766 return ($statement, $condition,
1767 $line, $remain + 1, $off - $loff + 1, $level);
1768}
1769
1770sub statement_lines {
1771 my ($stmt) = @_;
1772
1773 # Strip the diff line prefixes and rip blank lines at start and end.
1774 $stmt =~ s/(^|\n)./$1/g;
1775 $stmt =~ s/^\s*//;
1776 $stmt =~ s/\s*$//;
1777
1778 my @stmt_lines = ($stmt =~ /\n/g);
1779
1780 return $#stmt_lines + 2;
1781}
1782
1783sub statement_rawlines {
1784 my ($stmt) = @_;
1785
1786 my @stmt_lines = ($stmt =~ /\n/g);
1787
1788 return $#stmt_lines + 2;
1789}
1790
1791sub statement_block_size {
1792 my ($stmt) = @_;
1793
1794 $stmt =~ s/(^|\n)./$1/g;
1795 $stmt =~ s/^\s*{//;
1796 $stmt =~ s/}\s*$//;
1797 $stmt =~ s/^\s*//;
1798 $stmt =~ s/\s*$//;
1799
1800 my @stmt_lines = ($stmt =~ /\n/g);
1801 my @stmt_statements = ($stmt =~ /;/g);
1802
1803 my $stmt_lines = $#stmt_lines + 2;
1804 my $stmt_statements = $#stmt_statements + 1;
1805
1806 if ($stmt_lines > $stmt_statements) {
1807 return $stmt_lines;
1808 } else {
1809 return $stmt_statements;
1810 }
1811}
1812
1813sub ctx_statement_full {
1814 my ($linenr, $remain, $off) = @_;
1815 my ($statement, $condition, $level);
1816
1817 my (@chunks);
1818
1819 # Grab the first conditional/block pair.
1820 ($statement, $condition, $linenr, $remain, $off, $level) =
1821 ctx_statement_block($linenr, $remain, $off);
1822 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1823 push(@chunks, [ $condition, $statement ]);
1824 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1825 return ($level, $linenr, @chunks);
1826 }
1827
1828 # Pull in the following conditional/block pairs and see if they
1829 # could continue the statement.
1830 for (;;) {
1831 ($statement, $condition, $linenr, $remain, $off, $level) =
1832 ctx_statement_block($linenr, $remain, $off);
1833 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1834 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1835 #print "C: push\n";
1836 push(@chunks, [ $condition, $statement ]);
1837 }
1838
1839 return ($level, $linenr, @chunks);
1840}
1841
1842sub ctx_block_get {
1843 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1844 my $line;
1845 my $start = $linenr - 1;
1846 my $blk = '';
1847 my @o;
1848 my @c;
1849 my @res = ();
1850
1851 my $level = 0;
1852 my @stack = ($level);
1853 for ($line = $start; $remain > 0; $line++) {
1854 next if ($rawlines[$line] =~ /^-/);
1855 $remain--;
1856
1857 $blk .= $rawlines[$line];
1858
1859 # Handle nested #if/#else.
1860 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1861 push(@stack, $level);
1862 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1863 $level = $stack[$#stack - 1];
1864 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1865 $level = pop(@stack);
1866 }
1867
1868 foreach my $c (split(//, $lines[$line])) {
1869 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1870 if ($off > 0) {
1871 $off--;
1872 next;
1873 }
1874
1875 if ($c eq $close && $level > 0) {
1876 $level--;
1877 last if ($level == 0);
1878 } elsif ($c eq $open) {
1879 $level++;
1880 }
1881 }
1882
1883 if (!$outer || $level <= 1) {
1884 push(@res, $rawlines[$line]);
1885 }
1886
1887 last if ($level == 0);
1888 }
1889
1890 return ($level, @res);
1891}
1892sub ctx_block_outer {
1893 my ($linenr, $remain) = @_;
1894
1895 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1896 return @r;
1897}
1898sub ctx_block {
1899 my ($linenr, $remain) = @_;
1900
1901 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1902 return @r;
1903}
1904sub ctx_statement {
1905 my ($linenr, $remain, $off) = @_;
1906
1907 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1908 return @r;
1909}
1910sub ctx_block_level {
1911 my ($linenr, $remain) = @_;
1912
1913 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1914}
1915sub ctx_statement_level {
1916 my ($linenr, $remain, $off) = @_;
1917
1918 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1919}
1920
1921sub ctx_locate_comment {
1922 my ($first_line, $end_line) = @_;
1923
Tom Rinie199fb32021-08-03 08:31:56 -04001924 # If c99 comment on the current line, or the line before or after
1925 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1926 return $current_comment if (defined $current_comment);
1927 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1928 return $current_comment if (defined $current_comment);
1929 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1930 return $current_comment if (defined $current_comment);
1931
Joe Hershberger05622192011-10-18 10:06:59 +00001932 # Catch a comment on the end of the line itself.
Tom Rinie199fb32021-08-03 08:31:56 -04001933 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Joe Hershberger05622192011-10-18 10:06:59 +00001934 return $current_comment if (defined $current_comment);
1935
1936 # Look through the context and try and figure out if there is a
1937 # comment.
1938 my $in_comment = 0;
1939 $current_comment = '';
1940 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1941 my $line = $rawlines[$linenr - 1];
1942 #warn " $line\n";
1943 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1944 $in_comment = 1;
1945 }
1946 if ($line =~ m@/\*@) {
1947 $in_comment = 1;
1948 }
1949 if (!$in_comment && $current_comment ne '') {
1950 $current_comment = '';
1951 }
1952 $current_comment .= $line . "\n" if ($in_comment);
1953 if ($line =~ m@\*/@) {
1954 $in_comment = 0;
1955 }
1956 }
1957
1958 chomp($current_comment);
1959 return($current_comment);
1960}
1961sub ctx_has_comment {
1962 my ($first_line, $end_line) = @_;
1963 my $cmt = ctx_locate_comment($first_line, $end_line);
1964
1965 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1966 ##print "CMMT: $cmt\n";
1967
1968 return ($cmt ne '');
1969}
1970
1971sub raw_line {
1972 my ($linenr, $cnt) = @_;
1973
1974 my $offset = $linenr - 1;
1975 $cnt++;
1976
1977 my $line;
1978 while ($cnt) {
1979 $line = $rawlines[$offset++];
1980 next if (defined($line) && $line =~ /^-/);
1981 $cnt--;
1982 }
1983
1984 return $line;
1985}
1986
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02001987sub get_stat_real {
1988 my ($linenr, $lc) = @_;
1989
1990 my $stat_real = raw_line($linenr, 0);
1991 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1992 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1993 }
1994
1995 return $stat_real;
1996}
1997
1998sub get_stat_here {
1999 my ($linenr, $cnt, $here) = @_;
2000
2001 my $herectx = $here . "\n";
2002 for (my $n = 0; $n < $cnt; $n++) {
2003 $herectx .= raw_line($linenr, $n) . "\n";
2004 }
2005
2006 return $herectx;
2007}
2008
Joe Hershberger05622192011-10-18 10:06:59 +00002009sub cat_vet {
2010 my ($vet) = @_;
2011 my ($res, $coded);
2012
2013 $res = '';
2014 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
2015 $res .= $1;
2016 if ($2 ne '') {
2017 $coded = sprintf("^%c", unpack('C', $2) + 64);
2018 $res .= $coded;
2019 }
2020 }
2021 $res =~ s/$/\$/;
2022
2023 return $res;
2024}
2025
2026my $av_preprocessor = 0;
2027my $av_pending;
2028my @av_paren_type;
2029my $av_pend_colon;
2030
2031sub annotate_reset {
2032 $av_preprocessor = 0;
2033 $av_pending = '_';
2034 @av_paren_type = ('E');
2035 $av_pend_colon = 'O';
2036}
2037
2038sub annotate_values {
2039 my ($stream, $type) = @_;
2040
2041 my $res;
2042 my $var = '_' x length($stream);
2043 my $cur = $stream;
2044
2045 print "$stream\n" if ($dbg_values > 1);
2046
2047 while (length($cur)) {
2048 @av_paren_type = ('E') if ($#av_paren_type < 0);
2049 print " <" . join('', @av_paren_type) .
2050 "> <$type> <$av_pending>" if ($dbg_values > 1);
2051 if ($cur =~ /^(\s+)/o) {
2052 print "WS($1)\n" if ($dbg_values > 1);
2053 if ($1 =~ /\n/ && $av_preprocessor) {
2054 $type = pop(@av_paren_type);
2055 $av_preprocessor = 0;
2056 }
2057
2058 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
2059 print "CAST($1)\n" if ($dbg_values > 1);
2060 push(@av_paren_type, $type);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002061 $type = 'c';
Joe Hershberger05622192011-10-18 10:06:59 +00002062
2063 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
2064 print "DECLARE($1)\n" if ($dbg_values > 1);
2065 $type = 'T';
2066
2067 } elsif ($cur =~ /^($Modifier)\s*/) {
2068 print "MODIFIER($1)\n" if ($dbg_values > 1);
2069 $type = 'T';
2070
2071 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
2072 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
2073 $av_preprocessor = 1;
2074 push(@av_paren_type, $type);
2075 if ($2 ne '') {
2076 $av_pending = 'N';
2077 }
2078 $type = 'E';
2079
2080 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
2081 print "UNDEF($1)\n" if ($dbg_values > 1);
2082 $av_preprocessor = 1;
2083 push(@av_paren_type, $type);
2084
2085 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
2086 print "PRE_START($1)\n" if ($dbg_values > 1);
2087 $av_preprocessor = 1;
2088
2089 push(@av_paren_type, $type);
2090 push(@av_paren_type, $type);
2091 $type = 'E';
2092
2093 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
2094 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
2095 $av_preprocessor = 1;
2096
2097 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
2098
2099 $type = 'E';
2100
2101 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
2102 print "PRE_END($1)\n" if ($dbg_values > 1);
2103
2104 $av_preprocessor = 1;
2105
2106 # Assume all arms of the conditional end as this
2107 # one does, and continue as if the #endif was not here.
2108 pop(@av_paren_type);
2109 push(@av_paren_type, $type);
2110 $type = 'E';
2111
2112 } elsif ($cur =~ /^(\\\n)/o) {
2113 print "PRECONT($1)\n" if ($dbg_values > 1);
2114
2115 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
2116 print "ATTR($1)\n" if ($dbg_values > 1);
2117 $av_pending = $type;
2118 $type = 'N';
2119
2120 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
2121 print "SIZEOF($1)\n" if ($dbg_values > 1);
2122 if (defined $2) {
2123 $av_pending = 'V';
2124 }
2125 $type = 'N';
2126
2127 } elsif ($cur =~ /^(if|while|for)\b/o) {
2128 print "COND($1)\n" if ($dbg_values > 1);
2129 $av_pending = 'E';
2130 $type = 'N';
2131
2132 } elsif ($cur =~/^(case)/o) {
2133 print "CASE($1)\n" if ($dbg_values > 1);
2134 $av_pend_colon = 'C';
2135 $type = 'N';
2136
2137 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
2138 print "KEYWORD($1)\n" if ($dbg_values > 1);
2139 $type = 'N';
2140
2141 } elsif ($cur =~ /^(\()/o) {
2142 print "PAREN('$1')\n" if ($dbg_values > 1);
2143 push(@av_paren_type, $av_pending);
2144 $av_pending = '_';
2145 $type = 'N';
2146
2147 } elsif ($cur =~ /^(\))/o) {
2148 my $new_type = pop(@av_paren_type);
2149 if ($new_type ne '_') {
2150 $type = $new_type;
2151 print "PAREN('$1') -> $type\n"
2152 if ($dbg_values > 1);
2153 } else {
2154 print "PAREN('$1')\n" if ($dbg_values > 1);
2155 }
2156
2157 } elsif ($cur =~ /^($Ident)\s*\(/o) {
2158 print "FUNC($1)\n" if ($dbg_values > 1);
2159 $type = 'V';
2160 $av_pending = 'V';
2161
2162 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
2163 if (defined $2 && $type eq 'C' || $type eq 'T') {
2164 $av_pend_colon = 'B';
2165 } elsif ($type eq 'E') {
2166 $av_pend_colon = 'L';
2167 }
2168 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
2169 $type = 'V';
2170
2171 } elsif ($cur =~ /^($Ident|$Constant)/o) {
2172 print "IDENT($1)\n" if ($dbg_values > 1);
2173 $type = 'V';
2174
2175 } elsif ($cur =~ /^($Assignment)/o) {
2176 print "ASSIGN($1)\n" if ($dbg_values > 1);
2177 $type = 'N';
2178
2179 } elsif ($cur =~/^(;|{|})/) {
2180 print "END($1)\n" if ($dbg_values > 1);
2181 $type = 'E';
2182 $av_pend_colon = 'O';
2183
2184 } elsif ($cur =~/^(,)/) {
2185 print "COMMA($1)\n" if ($dbg_values > 1);
2186 $type = 'C';
2187
2188 } elsif ($cur =~ /^(\?)/o) {
2189 print "QUESTION($1)\n" if ($dbg_values > 1);
2190 $type = 'N';
2191
2192 } elsif ($cur =~ /^(:)/o) {
2193 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
2194
2195 substr($var, length($res), 1, $av_pend_colon);
2196 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
2197 $type = 'E';
2198 } else {
2199 $type = 'N';
2200 }
2201 $av_pend_colon = 'O';
2202
2203 } elsif ($cur =~ /^(\[)/o) {
2204 print "CLOSE($1)\n" if ($dbg_values > 1);
2205 $type = 'N';
2206
2207 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
2208 my $variant;
2209
2210 print "OPV($1)\n" if ($dbg_values > 1);
2211 if ($type eq 'V') {
2212 $variant = 'B';
2213 } else {
2214 $variant = 'U';
2215 }
2216
2217 substr($var, length($res), 1, $variant);
2218 $type = 'N';
2219
2220 } elsif ($cur =~ /^($Operators)/o) {
2221 print "OP($1)\n" if ($dbg_values > 1);
2222 if ($1 ne '++' && $1 ne '--') {
2223 $type = 'N';
2224 }
2225
2226 } elsif ($cur =~ /(^.)/o) {
2227 print "C($1)\n" if ($dbg_values > 1);
2228 }
2229 if (defined $1) {
2230 $cur = substr($cur, length($1));
2231 $res .= $type x length($1);
2232 }
2233 }
2234
2235 return ($res, $var);
2236}
2237
2238sub possible {
2239 my ($possible, $line) = @_;
2240 my $notPermitted = qr{(?:
2241 ^(?:
2242 $Modifier|
2243 $Storage|
2244 $Type|
2245 DEFINE_\S+
2246 )$|
2247 ^(?:
2248 goto|
2249 return|
2250 case|
2251 else|
2252 asm|__asm__|
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002253 do|
2254 \#|
2255 \#\#|
Joe Hershberger05622192011-10-18 10:06:59 +00002256 )(?:\s|$)|
2257 ^(?:typedef|struct|enum)\b
2258 )}x;
2259 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2260 if ($possible !~ $notPermitted) {
2261 # Check for modifiers.
2262 $possible =~ s/\s*$Storage\s*//g;
2263 $possible =~ s/\s*$Sparse\s*//g;
2264 if ($possible =~ /^\s*$/) {
2265
2266 } elsif ($possible =~ /\s/) {
2267 $possible =~ s/\s*$Type\s*//g;
2268 for my $modifier (split(' ', $possible)) {
2269 if ($modifier !~ $notPermitted) {
2270 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002271 push(@modifierListFile, $modifier);
Joe Hershberger05622192011-10-18 10:06:59 +00002272 }
2273 }
2274
2275 } else {
2276 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002277 push(@typeListFile, $possible);
Joe Hershberger05622192011-10-18 10:06:59 +00002278 }
2279 build_types();
2280 } else {
2281 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
2282 }
2283}
2284
2285my $prefix = '';
2286
2287sub show_type {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002288 my ($type) = @_;
Tom Rini6b9709d2014-02-27 08:27:28 -05002289
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002290 $type =~ tr/[a-z]/[A-Z]/;
2291
2292 return defined $use_type{$type} if (scalar keys %use_type > 0);
2293
2294 return !defined $ignore_type{$type};
Joe Hershberger05622192011-10-18 10:06:59 +00002295}
2296
2297sub report {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002298 my ($level, $type, $msg) = @_;
2299
2300 if (!show_type($type) ||
2301 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002302 return 0;
2303 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002304 my $output = '';
2305 if ($color) {
2306 if ($level eq 'ERROR') {
2307 $output .= RED;
2308 } elsif ($level eq 'WARNING') {
2309 $output .= YELLOW;
2310 } else {
2311 $output .= GREEN;
2312 }
Joe Hershberger05622192011-10-18 10:06:59 +00002313 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002314 $output .= $prefix . $level . ':';
2315 if ($show_types) {
2316 $output .= BLUE if ($color);
2317 $output .= "$type:";
2318 }
2319 $output .= RESET if ($color);
2320 $output .= ' ' . $msg . "\n";
Joe Hershberger05622192011-10-18 10:06:59 +00002321
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002322 if ($showfile) {
2323 my @lines = split("\n", $output, -1);
2324 splice(@lines, 1, 1);
2325 $output = join("\n", @lines);
2326 }
Tom Rinie199fb32021-08-03 08:31:56 -04002327
2328 if ($terse) {
2329 $output = (split('\n', $output))[0] . "\n";
2330 }
2331
2332 if ($verbose && exists($verbose_messages{$type}) &&
2333 !exists($verbose_emitted{$type})) {
2334 $output .= $verbose_messages{$type} . "\n\n";
2335 $verbose_emitted{$type} = 1;
2336 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002337
2338 push(our @report, $output);
Joe Hershberger05622192011-10-18 10:06:59 +00002339
2340 return 1;
2341}
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002342
Joe Hershberger05622192011-10-18 10:06:59 +00002343sub report_dump {
2344 our @report;
2345}
2346
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002347sub fixup_current_range {
2348 my ($lineRef, $offset, $length) = @_;
2349
2350 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2351 my $o = $1;
2352 my $l = $2;
2353 my $no = $o + $offset;
2354 my $nl = $l + $length;
2355 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2356 }
2357}
2358
2359sub fix_inserted_deleted_lines {
2360 my ($linesRef, $insertedRef, $deletedRef) = @_;
2361
2362 my $range_last_linenr = 0;
2363 my $delta_offset = 0;
2364
2365 my $old_linenr = 0;
2366 my $new_linenr = 0;
2367
2368 my $next_insert = 0;
2369 my $next_delete = 0;
2370
2371 my @lines = ();
2372
2373 my $inserted = @{$insertedRef}[$next_insert++];
2374 my $deleted = @{$deletedRef}[$next_delete++];
2375
2376 foreach my $old_line (@{$linesRef}) {
2377 my $save_line = 1;
2378 my $line = $old_line; #don't modify the array
2379 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
2380 $delta_offset = 0;
2381 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2382 $range_last_linenr = $new_linenr;
2383 fixup_current_range(\$line, $delta_offset, 0);
2384 }
2385
2386 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2387 $deleted = @{$deletedRef}[$next_delete++];
2388 $save_line = 0;
2389 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2390 }
2391
2392 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2393 push(@lines, ${$inserted}{'LINE'});
2394 $inserted = @{$insertedRef}[$next_insert++];
2395 $new_linenr++;
2396 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2397 }
2398
2399 if ($save_line) {
2400 push(@lines, $line);
2401 $new_linenr++;
2402 }
2403
2404 $old_linenr++;
2405 }
2406
2407 return @lines;
2408}
2409
2410sub fix_insert_line {
2411 my ($linenr, $line) = @_;
2412
2413 my $inserted = {
2414 LINENR => $linenr,
2415 LINE => $line,
2416 };
2417 push(@fixed_inserted, $inserted);
2418}
2419
2420sub fix_delete_line {
2421 my ($linenr, $line) = @_;
2422
2423 my $deleted = {
2424 LINENR => $linenr,
2425 LINE => $line,
2426 };
2427
2428 push(@fixed_deleted, $deleted);
2429}
2430
Joe Hershberger05622192011-10-18 10:06:59 +00002431sub ERROR {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002432 my ($type, $msg) = @_;
2433
2434 if (report("ERROR", $type, $msg)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002435 our $clean = 0;
2436 our $cnt_error++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002437 return 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002438 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002439 return 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002440}
2441sub WARN {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002442 my ($type, $msg) = @_;
2443
2444 if (report("WARNING", $type, $msg)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002445 our $clean = 0;
2446 our $cnt_warn++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002447 return 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002448 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002449 return 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002450}
2451sub CHK {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002452 my ($type, $msg) = @_;
2453
2454 if ($check && report("CHECK", $type, $msg)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002455 our $clean = 0;
2456 our $cnt_chk++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002457 return 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002458 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002459 return 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002460}
2461
2462sub check_absolute_file {
2463 my ($absolute, $herecurr) = @_;
2464 my $file = $absolute;
2465
2466 ##print "absolute<$absolute>\n";
2467
2468 # See if any suffix of this path is a path within the tree.
2469 while ($file =~ s@^[^/]*/@@) {
2470 if (-f "$root/$file") {
2471 ##print "file<$file>\n";
2472 last;
2473 }
2474 }
2475 if (! -f _) {
2476 return 0;
2477 }
2478
2479 # It is, so see if the prefix is acceptable.
2480 my $prefix = $absolute;
2481 substr($prefix, -length($file)) = '';
2482
2483 ##print "prefix<$prefix>\n";
2484 if ($prefix ne ".../") {
2485 WARN("USE_RELATIVE_PATH",
2486 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2487 }
2488}
2489
Tom Rini6b9709d2014-02-27 08:27:28 -05002490sub trim {
2491 my ($string) = @_;
2492
2493 $string =~ s/^\s+|\s+$//g;
2494
2495 return $string;
2496}
2497
2498sub ltrim {
2499 my ($string) = @_;
2500
2501 $string =~ s/^\s+//;
2502
2503 return $string;
2504}
2505
2506sub rtrim {
2507 my ($string) = @_;
2508
2509 $string =~ s/\s+$//;
2510
2511 return $string;
2512}
2513
2514sub string_find_replace {
2515 my ($string, $find, $replace) = @_;
2516
2517 $string =~ s/$find/$replace/g;
2518
2519 return $string;
2520}
2521
2522sub tabify {
2523 my ($leading) = @_;
2524
Tom Rinic57383b2020-06-16 10:29:46 -04002525 my $source_indent = $tabsize;
Tom Rini6b9709d2014-02-27 08:27:28 -05002526 my $max_spaces_before_tab = $source_indent - 1;
2527 my $spaces_to_tab = " " x $source_indent;
2528
2529 #convert leading spaces to tabs
2530 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2531 #Remove spaces before a tab
2532 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2533
2534 return "$leading";
2535}
2536
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002537sub pos_last_openparen {
2538 my ($line) = @_;
2539
2540 my $pos = 0;
2541
2542 my $opens = $line =~ tr/\(/\(/;
2543 my $closes = $line =~ tr/\)/\)/;
2544
2545 my $last_openparen = 0;
2546
2547 if (($opens == 0) || ($closes >= $opens)) {
2548 return -1;
2549 }
2550
2551 my $len = length($line);
2552
2553 for ($pos = 0; $pos < $len; $pos++) {
2554 my $string = substr($line, $pos);
2555 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2556 $pos += length($1) - 1;
2557 } elsif (substr($line, $pos, 1) eq '(') {
2558 $last_openparen = $pos;
2559 } elsif (index($string, '(') == -1) {
2560 last;
2561 }
2562 }
2563
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002564 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002565}
2566
Tom Rinic57383b2020-06-16 10:29:46 -04002567sub get_raw_comment {
2568 my ($line, $rawline) = @_;
2569 my $comment = '';
2570
2571 for my $i (0 .. (length($line) - 1)) {
2572 if (substr($line, $i, 1) eq "$;") {
2573 $comment .= substr($rawline, $i, 1);
2574 }
2575 }
2576
2577 return $comment;
2578}
2579
Simon Glassb7bbd552020-12-03 16:55:24 -07002580# Args:
2581# line: Patch line to check
2582# auto: Auto variable name, e.g. "per_child_auto"
2583# suffix: Suffix to expect on member, e.g. "_priv"
2584# warning: Warning name, e.g. "PRIV_AUTO"
2585sub u_boot_struct_name {
Evan Bennec6db6c2021-04-01 13:49:30 +11002586 my ($line, $auto, $suffix, $warning, $herecurr) = @_;
Simon Glassb7bbd552020-12-03 16:55:24 -07002587
2588 # Use _priv as a suffix for the device-private data struct
2589 if ($line =~ /^\+\s*\.${auto}\s*=\s*sizeof\(struct\((\w+)\).*/) {
2590 my $struct_name = $1;
2591 if ($struct_name !~ /^\w+${suffix}/) {
Evan Bennec6db6c2021-04-01 13:49:30 +11002592 WARN($warning,
2593 "struct \'$struct_name\' should have a ${suffix} suffix\n"
2594 . $herecurr);
Simon Glassb7bbd552020-12-03 16:55:24 -07002595 }
2596 }
2597}
2598
Simon Glassb77df592020-05-22 16:32:36 -06002599# Checks specific to U-Boot
2600sub u_boot_line {
Simon Glass23552ba2020-07-19 10:16:01 -06002601 my ($realfile, $line, $rawline, $herecurr) = @_;
Simon Glass281236c2020-05-22 16:32:37 -06002602
2603 # ask for a test if a new uclass ID is added
2604 if ($realfile =~ /uclass-id.h/ && $line =~ /^\+/) {
2605 WARN("NEW_UCLASS",
2606 "Possible new uclass - make sure to add a sandbox driver, plus a test in test/dm/<name>.c\n" . $herecurr);
2607 }
Simon Glass7fc7d242020-05-22 16:32:38 -06002608
2609 # try to get people to use the livetree API
2610 if ($line =~ /^\+.*fdtdec_/) {
2611 WARN("LIVETREE",
2612 "Use the livetree API (dev_read_...)\n" . $herecurr);
2613 }
Simon Glass58978112020-05-22 16:32:39 -06002614
2615 # add tests for new commands
2616 if ($line =~ /^\+.*do_($Ident)\(struct cmd_tbl.*/) {
2617 WARN("CMD_TEST",
2618 "Possible new command - make sure you add a test\n" . $herecurr);
2619 }
Simon Glassdd5b0fa2020-05-22 16:32:40 -06002620
2621 # use if instead of #if
Simon Glass8af45b12020-06-14 10:54:08 -06002622 if ($realfile =~ /\.c$/ && $line =~ /^\+#if.*CONFIG.*/) {
Simon Glassdd5b0fa2020-05-22 16:32:40 -06002623 WARN("PREFER_IF",
2624 "Use 'if (IS_ENABLED(CONFIG...))' instead of '#if or #ifdef' where possible\n" . $herecurr);
2625 }
Tom Rinif3e2ebe2020-05-26 14:29:02 -04002626
Sean Andersond9c30502021-03-11 00:15:45 -05002627 # prefer strl(cpy|cat) over strn(cpy|cat)
2628 if ($line =~ /\bstrn(cpy|cat)\s*\(/) {
2629 WARN("STRL",
2630 "strl$1 is preferred over strn$1 because it always produces a nul-terminated string\n" . $herecurr);
2631 }
2632
Tom Rinif3e2ebe2020-05-26 14:29:02 -04002633 # use defconfig to manage CONFIG_CMD options
2634 if ($line =~ /\+\s*#\s*(define|undef)\s+(CONFIG_CMD\w*)\b/) {
2635 ERROR("DEFINE_CONFIG_CMD",
2636 "All commands are managed by Kconfig\n" . $herecurr);
2637 }
Simon Glass23552ba2020-07-19 10:16:01 -06002638
2639 # Don't put common.h and dm.h in header files
2640 if ($realfile =~ /\.h$/ && $rawline =~ /^\+#include\s*<(common|dm)\.h>*/) {
2641 ERROR("BARRED_INCLUDE_IN_HDR",
2642 "Avoid including common.h and dm.h in header files\n" . $herecurr);
2643 }
Tom Rini12178b52020-08-20 08:37:49 -04002644
2645 # Do not disable fdt / initrd relocation
Hou Zhiqiang2f3e8d62021-12-15 14:23:52 +08002646 if ($rawline =~ /^\+.*(fdt|initrd)_high=0xffffffff/) {
Tom Rini12178b52020-08-20 08:37:49 -04002647 ERROR("DISABLE_FDT_OR_INITRD_RELOC",
2648 "fdt or initrd relocation disabled at boot time\n" . $herecurr);
2649 }
Alper Nebi Yasakb9cca2c2020-10-05 09:57:30 +03002650
Wasim Khan402558b2021-02-04 15:44:04 +01002651 # make sure 'skip_board_fixup' is not
2652 if ($rawline =~ /.*skip_board_fixup.*/) {
2653 ERROR("SKIP_BOARD_FIXUP",
2654 "Avoid setting skip_board_fixup env variable\n" . $herecurr);
2655 }
2656
Alper Nebi Yasakb9cca2c2020-10-05 09:57:30 +03002657 # Do not use CONFIG_ prefix in CONFIG_IS_ENABLED() calls
2658 if ($line =~ /^\+.*CONFIG_IS_ENABLED\(CONFIG_\w*\).*/) {
2659 ERROR("CONFIG_IS_ENABLED_CONFIG",
2660 "CONFIG_IS_ENABLED() takes values without the CONFIG_ prefix\n" . $herecurr);
2661 }
Simon Glassb7bbd552020-12-03 16:55:24 -07002662
2663 # Use _priv as a suffix for the device-private data struct
2664 if ($line =~ /^\+\s*\.priv_auto\s*=\s*sizeof\(struct\((\w+)\).*/) {
2665 my $struct_name = $1;
2666 if ($struct_name !~ /^\w+_priv/) {
2667 WARN("PRIV_AUTO", "struct \'$struct_name\' should have a _priv suffix");
2668 }
2669 }
2670
2671 # Check struct names for the 'auto' members of struct driver
Evan Bennec6db6c2021-04-01 13:49:30 +11002672 u_boot_struct_name($line, "priv_auto", "_priv", "PRIV_AUTO", $herecurr);
2673 u_boot_struct_name($line, "plat_auto", "_plat", "PLAT_AUTO", $herecurr);
2674 u_boot_struct_name($line, "per_child_auto", "_priv", "CHILD_PRIV_AUTO", $herecurr);
Simon Glassb7bbd552020-12-03 16:55:24 -07002675 u_boot_struct_name($line, "per_child_plat_auto", "_plat",
Evan Bennec6db6c2021-04-01 13:49:30 +11002676 "CHILD_PLAT_AUTO", $herecurr);
Simon Glassb7bbd552020-12-03 16:55:24 -07002677
2678 # Now the ones for struct uclass, skipping those in common with above
2679 u_boot_struct_name($line, "per_device_auto", "_priv",
Evan Bennec6db6c2021-04-01 13:49:30 +11002680 "DEVICE_PRIV_AUTO", $herecurr);
Simon Glassb7bbd552020-12-03 16:55:24 -07002681 u_boot_struct_name($line, "per_device_plat_auto", "_plat",
Evan Bennec6db6c2021-04-01 13:49:30 +11002682 "DEVICE_PLAT_AUTO", $herecurr);
Simon Glassb77df592020-05-22 16:32:36 -06002683}
2684
Tom Rinie199fb32021-08-03 08:31:56 -04002685sub exclude_global_initialisers {
2686 my ($realfile) = @_;
2687
2688 # Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
2689 return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ ||
2690 $realfile =~ m@^samples/bpf/.*_kern\.c$@ ||
2691 $realfile =~ m@/bpf/.*\.bpf\.c$@;
2692}
2693
Joe Hershberger05622192011-10-18 10:06:59 +00002694sub process {
2695 my $filename = shift;
2696
2697 my $linenr=0;
2698 my $prevline="";
2699 my $prevrawline="";
2700 my $stashline="";
2701 my $stashrawline="";
2702
2703 my $length;
2704 my $indent;
2705 my $previndent=0;
2706 my $stashindent=0;
2707
2708 our $clean = 1;
2709 my $signoff = 0;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002710 my $author = '';
2711 my $authorsignoff = 0;
Tom Rinie199fb32021-08-03 08:31:56 -04002712 my $author_sob = '';
Joe Hershberger05622192011-10-18 10:06:59 +00002713 my $is_patch = 0;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002714 my $is_binding_patch = -1;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002715 my $in_header_lines = $file ? 0 : 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002716 my $in_commit_log = 0; #Scanning lines before patch
Tom Rinic57383b2020-06-16 10:29:46 -04002717 my $has_patch_separator = 0; #Found a --- line
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002718 my $has_commit_log = 0; #Encountered lines before patch
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002719 my $commit_log_lines = 0; #Number of commit log lines
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002720 my $commit_log_possible_stack_dump = 0;
2721 my $commit_log_long_line = 0;
2722 my $commit_log_has_diff = 0;
2723 my $reported_maintainer_file = 0;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002724 my $non_utf8_charset = 0;
2725
Simon Glass587254e2022-01-23 12:55:11 -07002726 my $last_git_commit_id_linenr = -1;
2727
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002728 my $last_blank_line = 0;
2729 my $last_coalesced_string_linenr = -1;
2730
Joe Hershberger05622192011-10-18 10:06:59 +00002731 our @report = ();
2732 our $cnt_lines = 0;
2733 our $cnt_error = 0;
2734 our $cnt_warn = 0;
2735 our $cnt_chk = 0;
2736
2737 # Trace the real file/line as we go.
2738 my $realfile = '';
2739 my $realline = 0;
2740 my $realcnt = 0;
2741 my $here = '';
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002742 my $context_function; #undef'd unless there's a known function
Joe Hershberger05622192011-10-18 10:06:59 +00002743 my $in_comment = 0;
2744 my $comment_edge = 0;
2745 my $first_line = 0;
2746 my $p1_prefix = '';
2747
2748 my $prev_values = 'E';
2749
2750 # suppression flags
2751 my %suppress_ifbraces;
2752 my %suppress_whiletrailers;
2753 my %suppress_export;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002754 my $suppress_statement = 0;
2755
Tom Rini6b9709d2014-02-27 08:27:28 -05002756 my %signatures = ();
Joe Hershberger05622192011-10-18 10:06:59 +00002757
2758 # Pre-scan the patch sanitizing the lines.
2759 # Pre-scan the patch looking for any __setup documentation.
2760 #
2761 my @setup_docs = ();
2762 my $setup_docs = 0;
2763
Tom Rini6b9709d2014-02-27 08:27:28 -05002764 my $camelcase_file_seeded = 0;
2765
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002766 my $checklicenseline = 1;
2767
Joe Hershberger05622192011-10-18 10:06:59 +00002768 sanitise_line_reset();
2769 my $line;
2770 foreach my $rawline (@rawlines) {
2771 $linenr++;
2772 $line = $rawline;
2773
Tom Rini6b9709d2014-02-27 08:27:28 -05002774 push(@fixed, $rawline) if ($fix);
2775
Joe Hershberger05622192011-10-18 10:06:59 +00002776 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2777 $setup_docs = 0;
Tom Rinie199fb32021-08-03 08:31:56 -04002778 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
Joe Hershberger05622192011-10-18 10:06:59 +00002779 $setup_docs = 1;
2780 }
2781 #next;
2782 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002783 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Joe Hershberger05622192011-10-18 10:06:59 +00002784 $realline=$1-1;
2785 if (defined $2) {
2786 $realcnt=$3+1;
2787 } else {
2788 $realcnt=1+1;
2789 }
2790 $in_comment = 0;
2791
2792 # Guestimate if this is a continuing comment. Run
2793 # the context looking for a comment "edge". If this
2794 # edge is a close comment then we must be in a comment
2795 # at context start.
2796 my $edge;
2797 my $cnt = $realcnt;
2798 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2799 next if (defined $rawlines[$ln - 1] &&
2800 $rawlines[$ln - 1] =~ /^-/);
2801 $cnt--;
2802 #print "RAW<$rawlines[$ln - 1]>\n";
2803 last if (!defined $rawlines[$ln - 1]);
2804 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2805 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2806 ($edge) = $1;
2807 last;
2808 }
2809 }
2810 if (defined $edge && $edge eq '*/') {
2811 $in_comment = 1;
2812 }
2813
2814 # Guestimate if this is a continuing comment. If this
2815 # is the start of a diff block and this line starts
2816 # ' *' then it is very likely a comment.
2817 if (!defined $edge &&
2818 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2819 {
2820 $in_comment = 1;
2821 }
2822
2823 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2824 sanitise_line_reset($in_comment);
2825
2826 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2827 # Standardise the strings and chars within the input to
2828 # simplify matching -- only bother with positive lines.
2829 $line = sanitise_line($rawline);
2830 }
2831 push(@lines, $line);
2832
2833 if ($realcnt > 1) {
2834 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2835 } else {
2836 $realcnt = 0;
2837 }
2838
2839 #print "==>$rawline\n";
2840 #print "-->$line\n";
2841
2842 if ($setup_docs && $line =~ /^\+/) {
2843 push(@setup_docs, $line);
2844 }
2845 }
2846
2847 $prefix = '';
2848
2849 $realcnt = 0;
2850 $linenr = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002851 $fixlinenr = -1;
Joe Hershberger05622192011-10-18 10:06:59 +00002852 foreach my $line (@lines) {
2853 $linenr++;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002854 $fixlinenr++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002855 my $sline = $line; #copy of $line
2856 $sline =~ s/$;/ /g; #with comments as spaces
Joe Hershberger05622192011-10-18 10:06:59 +00002857
2858 my $rawline = $rawlines[$linenr - 1];
Tom Rinic57383b2020-06-16 10:29:46 -04002859 my $raw_comment = get_raw_comment($line, $rawline);
Joe Hershberger05622192011-10-18 10:06:59 +00002860
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002861# check if it's a mode change, rename or start of a patch
2862 if (!$in_commit_log &&
2863 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2864 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2865 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2866 $is_patch = 1;
2867 }
2868
Joe Hershberger05622192011-10-18 10:06:59 +00002869#extract the line range in the file after the patch is applied
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002870 if (!$in_commit_log &&
2871 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2872 my $context = $4;
Joe Hershberger05622192011-10-18 10:06:59 +00002873 $is_patch = 1;
2874 $first_line = $linenr + 1;
2875 $realline=$1-1;
2876 if (defined $2) {
2877 $realcnt=$3+1;
2878 } else {
2879 $realcnt=1+1;
2880 }
2881 annotate_reset();
2882 $prev_values = 'E';
2883
2884 %suppress_ifbraces = ();
2885 %suppress_whiletrailers = ();
2886 %suppress_export = ();
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002887 $suppress_statement = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002888 if ($context =~ /\b(\w+)\s*\(/) {
2889 $context_function = $1;
2890 } else {
2891 undef $context_function;
2892 }
Joe Hershberger05622192011-10-18 10:06:59 +00002893 next;
2894
2895# track the line number as we move through the hunk, note that
2896# new versions of GNU diff omit the leading space on completely
2897# blank context lines so we need to count that too.
2898 } elsif ($line =~ /^( |\+|$)/) {
2899 $realline++;
2900 $realcnt-- if ($realcnt != 0);
2901
2902 # Measure the line length and indent.
2903 ($length, $indent) = line_stats($rawline);
2904
2905 # Track the previous line.
2906 ($prevline, $stashline) = ($stashline, $line);
2907 ($previndent, $stashindent) = ($stashindent, $indent);
2908 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2909
2910 #warn "line<$line>\n";
2911
2912 } elsif ($realcnt == 1) {
2913 $realcnt--;
2914 }
2915
2916 my $hunk_line = ($realcnt != 0);
2917
Joe Hershberger05622192011-10-18 10:06:59 +00002918 $here = "#$linenr: " if (!$file);
2919 $here = "#$realline: " if ($file);
2920
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002921 my $found_file = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002922 # extract the filename as it passes
2923 if ($line =~ /^diff --git.*?(\S+)$/) {
2924 $realfile = $1;
Tom Rini6b9709d2014-02-27 08:27:28 -05002925 $realfile =~ s@^([^/]*)/@@ if (!$file);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002926 $in_commit_log = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002927 $found_file = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002928 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2929 $realfile = $1;
Tom Rini6b9709d2014-02-27 08:27:28 -05002930 $realfile =~ s@^([^/]*)/@@ if (!$file);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002931 $in_commit_log = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002932
2933 $p1_prefix = $1;
2934 if (!$file && $tree && $p1_prefix ne '' &&
2935 -e "$root/$p1_prefix") {
2936 WARN("PATCH_PREFIX",
2937 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2938 }
2939
2940 if ($realfile =~ m@^include/asm/@) {
2941 ERROR("MODIFIED_INCLUDE_ASM",
2942 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2943 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002944 $found_file = 1;
2945 }
2946
2947#make up the handle for any error we report on this line
2948 if ($showfile) {
2949 $prefix = "$realfile:$realline: "
2950 } elsif ($emacs) {
2951 if ($file) {
2952 $prefix = "$filename:$realline: ";
2953 } else {
2954 $prefix = "$filename:$linenr: ";
2955 }
2956 }
2957
2958 if ($found_file) {
2959 if (is_maintained_obsolete($realfile)) {
2960 WARN("OBSOLETE",
2961 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2962 }
2963 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
2964 $check = 1;
2965 } else {
2966 $check = $check_orig;
2967 }
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002968 $checklicenseline = 1;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002969
2970 if ($realfile !~ /^MAINTAINERS/) {
2971 my $last_binding_patch = $is_binding_patch;
2972
2973 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2974
2975 if (($last_binding_patch != -1) &&
2976 ($last_binding_patch ^ $is_binding_patch)) {
2977 WARN("DT_SPLIT_BINDING_PATCH",
Tom Rinie199fb32021-08-03 08:31:56 -04002978 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002979 }
2980 }
2981
Joe Hershberger05622192011-10-18 10:06:59 +00002982 next;
2983 }
2984
2985 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2986
2987 my $hereline = "$here\n$rawline\n";
2988 my $herecurr = "$here\n$rawline\n";
2989 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2990
2991 $cnt_lines++ if ($realcnt != 0);
2992
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002993# Verify the existence of a commit log if appropriate
2994# 2 is used because a $signature is counted in $commit_log_lines
2995 if ($in_commit_log) {
2996 if ($line !~ /^\s*$/) {
2997 $commit_log_lines++; #could be a $signature
2998 }
2999 } elsif ($has_commit_log && $commit_log_lines < 2) {
3000 WARN("COMMIT_MESSAGE",
3001 "Missing commit description - Add an appropriate one\n");
3002 $commit_log_lines = 2; #warn only once
3003 }
3004
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003005# Check if the commit log has what seems like a diff which can confuse patch
3006 if ($in_commit_log && !$commit_log_has_diff &&
Tom Rinie199fb32021-08-03 08:31:56 -04003007 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
3008 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003009 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
3010 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
3011 ERROR("DIFF_IN_COMMIT_MSG",
3012 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
3013 $commit_log_has_diff = 1;
3014 }
3015
Joe Hershberger05622192011-10-18 10:06:59 +00003016# Check for incorrect file permissions
3017 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
3018 my $permhere = $here . "FILE: $realfile\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003019 if ($realfile !~ m@scripts/@ &&
3020 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003021 ERROR("EXECUTE_PERMISSIONS",
3022 "do not set execute permissions for source files\n" . $permhere);
3023 }
3024 }
3025
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003026# Check the patch for a From:
3027 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
3028 $author = $1;
Tom Rinie199fb32021-08-03 08:31:56 -04003029 my $curline = $linenr;
3030 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
3031 $author .= $1;
3032 }
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003033 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
3034 $author =~ s/"//g;
Tom Rinic57383b2020-06-16 10:29:46 -04003035 $author = reformat_email($author);
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003036 }
3037
Joe Hershberger05622192011-10-18 10:06:59 +00003038# Check the patch for a signoff:
Tom Rinic57383b2020-06-16 10:29:46 -04003039 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Joe Hershberger05622192011-10-18 10:06:59 +00003040 $signoff++;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003041 $in_commit_log = 0;
Tom Rinie199fb32021-08-03 08:31:56 -04003042 if ($author ne '' && $authorsignoff != 1) {
Tom Rinic57383b2020-06-16 10:29:46 -04003043 if (same_email_addresses($1, $author)) {
3044 $authorsignoff = 1;
Tom Rinie199fb32021-08-03 08:31:56 -04003045 } else {
3046 my $ctx = $1;
3047 my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
3048 my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
3049
Simon Glass587254e2022-01-23 12:55:11 -07003050 if (lc $email_address eq lc $author_address && $email_name eq $author_name) {
Tom Rinie199fb32021-08-03 08:31:56 -04003051 $author_sob = $ctx;
3052 $authorsignoff = 2;
Simon Glass587254e2022-01-23 12:55:11 -07003053 } elsif (lc $email_address eq lc $author_address) {
Tom Rinie199fb32021-08-03 08:31:56 -04003054 $author_sob = $ctx;
3055 $authorsignoff = 3;
3056 } elsif ($email_name eq $author_name) {
3057 $author_sob = $ctx;
3058 $authorsignoff = 4;
3059
3060 my $address1 = $email_address;
3061 my $address2 = $author_address;
3062
3063 if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
3064 $address1 = "$1$2";
3065 }
3066 if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
3067 $address2 = "$1$2";
3068 }
3069 if ($address1 eq $address2) {
3070 $authorsignoff = 5;
3071 }
3072 }
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003073 }
3074 }
Joe Hershberger05622192011-10-18 10:06:59 +00003075 }
3076
Tom Rinic57383b2020-06-16 10:29:46 -04003077# Check for patch separator
3078 if ($line =~ /^---$/) {
3079 $has_patch_separator = 1;
3080 $in_commit_log = 0;
3081 }
3082
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003083# Check if MAINTAINERS is being updated. If so, there's probably no need to
3084# emit the "does MAINTAINERS need updating?" message on file add/move/delete
3085 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
3086 $reported_maintainer_file = 1;
3087 }
3088
Joe Hershberger05622192011-10-18 10:06:59 +00003089# Check signature styles
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003090 if (!$in_header_lines &&
3091 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Hershberger05622192011-10-18 10:06:59 +00003092 my $space_before = $1;
3093 my $sign_off = $2;
3094 my $space_after = $3;
3095 my $email = $4;
3096 my $ucfirst_sign_off = ucfirst(lc($sign_off));
3097
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003098 if ($sign_off !~ /$signature_tags/) {
Tom Rinie199fb32021-08-03 08:31:56 -04003099 my $suggested_signature = find_standard_signature($sign_off);
3100 if ($suggested_signature eq "") {
3101 WARN("BAD_SIGN_OFF",
3102 "Non-standard signature: $sign_off\n" . $herecurr);
3103 } else {
3104 if (WARN("BAD_SIGN_OFF",
3105 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
3106 $fix) {
3107 $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
3108 }
3109 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003110 }
Joe Hershberger05622192011-10-18 10:06:59 +00003111 if (defined $space_before && $space_before ne "") {
Tom Rini6b9709d2014-02-27 08:27:28 -05003112 if (WARN("BAD_SIGN_OFF",
3113 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
3114 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003115 $fixed[$fixlinenr] =
Tom Rini6b9709d2014-02-27 08:27:28 -05003116 "$ucfirst_sign_off $email";
3117 }
Joe Hershberger05622192011-10-18 10:06:59 +00003118 }
3119 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Tom Rini6b9709d2014-02-27 08:27:28 -05003120 if (WARN("BAD_SIGN_OFF",
3121 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
3122 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003123 $fixed[$fixlinenr] =
Tom Rini6b9709d2014-02-27 08:27:28 -05003124 "$ucfirst_sign_off $email";
3125 }
3126
Joe Hershberger05622192011-10-18 10:06:59 +00003127 }
3128 if (!defined $space_after || $space_after ne " ") {
Tom Rini6b9709d2014-02-27 08:27:28 -05003129 if (WARN("BAD_SIGN_OFF",
3130 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
3131 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003132 $fixed[$fixlinenr] =
Tom Rini6b9709d2014-02-27 08:27:28 -05003133 "$ucfirst_sign_off $email";
3134 }
Joe Hershberger05622192011-10-18 10:06:59 +00003135 }
3136
Tom Rinic57383b2020-06-16 10:29:46 -04003137 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Tom Rinie199fb32021-08-03 08:31:56 -04003138 my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
Joe Hershberger05622192011-10-18 10:06:59 +00003139 if ($suggested_email eq "") {
3140 ERROR("BAD_SIGN_OFF",
3141 "Unrecognized email address: '$email'\n" . $herecurr);
3142 } else {
3143 my $dequoted = $suggested_email;
3144 $dequoted =~ s/^"//;
3145 $dequoted =~ s/" </ </;
3146 # Don't force email to have quotes
3147 # Allow just an angle bracketed address
Tom Rinic57383b2020-06-16 10:29:46 -04003148 if (!same_email_addresses($email, $suggested_email)) {
Tom Rinie199fb32021-08-03 08:31:56 -04003149 if (WARN("BAD_SIGN_OFF",
3150 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
3151 $fix) {
3152 $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
3153 }
3154 }
3155
3156 # Address part shouldn't have comments
3157 my $stripped_address = $email_address;
3158 $stripped_address =~ s/\([^\(\)]*\)//g;
3159 if ($email_address ne $stripped_address) {
3160 if (WARN("BAD_SIGN_OFF",
3161 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
3162 $fix) {
3163 $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
3164 }
3165 }
3166
3167 # Only one name comment should be allowed
3168 my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
3169 if ($comment_count > 1) {
Joe Hershberger05622192011-10-18 10:06:59 +00003170 WARN("BAD_SIGN_OFF",
Tom Rinie199fb32021-08-03 08:31:56 -04003171 "Use a single name comment in email: '$email'\n" . $herecurr);
3172 }
3173
3174
3175 # stable@vger.kernel.org or stable@kernel.org shouldn't
3176 # have an email name. In addition comments should strictly
3177 # begin with a #
3178 if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
3179 if (($comment ne "" && $comment !~ /^#.+/) ||
3180 ($email_name ne "")) {
3181 my $cur_name = $email_name;
3182 my $new_comment = $comment;
3183 $cur_name =~ s/[a-zA-Z\s\-\"]+//g;
3184
3185 # Remove brackets enclosing comment text
3186 # and # from start of comments to get comment text
3187 $new_comment =~ s/^\((.*)\)$/$1/;
3188 $new_comment =~ s/^\[(.*)\]$/$1/;
3189 $new_comment =~ s/^[\s\#]+|\s+$//g;
3190
3191 $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
3192 $new_comment = " # $new_comment" if ($new_comment ne "");
3193 my $new_email = "$email_address$new_comment";
3194
3195 if (WARN("BAD_STABLE_ADDRESS_STYLE",
3196 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
3197 $fix) {
3198 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3199 }
3200 }
3201 } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
3202 my $new_comment = $comment;
3203
3204 # Extract comment text from within brackets or
3205 # c89 style /*...*/ comments
3206 $new_comment =~ s/^\[(.*)\]$/$1/;
3207 $new_comment =~ s/^\/\*(.*)\*\/$/$1/;
3208
3209 $new_comment = trim($new_comment);
3210 $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
3211 $new_comment = "($new_comment)" if ($new_comment ne "");
3212 my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
3213
3214 if (WARN("BAD_SIGN_OFF",
3215 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
3216 $fix) {
3217 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3218 }
Joe Hershberger05622192011-10-18 10:06:59 +00003219 }
3220 }
Tom Rini6b9709d2014-02-27 08:27:28 -05003221
3222# Check for duplicate signatures
3223 my $sig_nospace = $line;
3224 $sig_nospace =~ s/\s//g;
3225 $sig_nospace = lc($sig_nospace);
3226 if (defined $signatures{$sig_nospace}) {
3227 WARN("BAD_SIGN_OFF",
3228 "Duplicate signature\n" . $herecurr);
3229 } else {
3230 $signatures{$sig_nospace} = 1;
3231 }
Tom Rinic57383b2020-06-16 10:29:46 -04003232
3233# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
3234 if ($sign_off =~ /^co-developed-by:$/i) {
3235 if ($email eq $author) {
3236 WARN("BAD_SIGN_OFF",
3237 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
3238 }
3239 if (!defined $lines[$linenr]) {
3240 WARN("BAD_SIGN_OFF",
Tom Rinie199fb32021-08-03 08:31:56 -04003241 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
Tom Rinic57383b2020-06-16 10:29:46 -04003242 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
3243 WARN("BAD_SIGN_OFF",
3244 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
3245 } elsif ($1 ne $email) {
3246 WARN("BAD_SIGN_OFF",
3247 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
3248 }
3249 }
Joe Hershberger05622192011-10-18 10:06:59 +00003250 }
3251
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003252# Check email subject for common tools that don't need to be mentioned
3253 if ($in_header_lines &&
3254 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
3255 WARN("EMAIL_SUBJECT",
3256 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
3257 }
3258
Tom Rinic57383b2020-06-16 10:29:46 -04003259# Check for Gerrit Change-Ids not in any patch context
3260 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Tom Rinie199fb32021-08-03 08:31:56 -04003261 if (ERROR("GERRIT_CHANGE_ID",
3262 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
3263 $fix) {
3264 fix_delete_line($fixlinenr, $rawline);
3265 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003266 }
3267
3268# Check if the commit log is in a possible stack dump
3269 if ($in_commit_log && !$commit_log_possible_stack_dump &&
3270 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3271 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3272 # timestamp
Tom Rinic57383b2020-06-16 10:29:46 -04003273 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3274 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3275 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3276 # stack dump address styles
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003277 $commit_log_possible_stack_dump = 1;
3278 }
3279
3280# Check for line lengths > 75 in commit log, warn once
3281 if ($in_commit_log && !$commit_log_long_line &&
3282 length($line) > 75 &&
3283 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3284 # file delta changes
3285 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
3286 # filename then :
Tom Rinie199fb32021-08-03 08:31:56 -04003287 $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
3288 # A Fixes: or Link: line or signature tag line
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003289 $commit_log_possible_stack_dump)) {
3290 WARN("COMMIT_LOG_LONG_LINE",
3291 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
3292 $commit_log_long_line = 1;
3293 }
3294
3295# Reset possible stack dump if a blank line is found
3296 if ($in_commit_log && $commit_log_possible_stack_dump &&
3297 $line =~ /^\s*$/) {
3298 $commit_log_possible_stack_dump = 0;
3299 }
3300
Tom Rinie199fb32021-08-03 08:31:56 -04003301# Check for lines starting with a #
3302 if ($in_commit_log && $line =~ /^#/) {
3303 if (WARN("COMMIT_COMMENT_SYMBOL",
3304 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3305 $fix) {
3306 $fixed[$fixlinenr] =~ s/^/ /;
3307 }
3308 }
3309
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003310# Check for git id commit length and improperly formed commit descriptions
Simon Glass587254e2022-01-23 12:55:11 -07003311# A correctly formed commit description is:
3312# commit <SHA-1 hash length 12+ chars> ("Complete commit subject")
3313# with the commit subject '("' prefix and '")' suffix
3314# This is a fairly compilicated block as it tests for what appears to be
3315# bare SHA-1 hash with minimum length of 5. It also avoids several types of
3316# possible SHA-1 matches.
3317# A commit match can span multiple lines so this block attempts to find a
3318# complete typical commit on a maximum of 3 lines
3319 if ($perl_version_ok &&
3320 $in_commit_log && !$commit_log_possible_stack_dump &&
Tom Rinic57383b2020-06-16 10:29:46 -04003321 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003322 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Simon Glass587254e2022-01-23 12:55:11 -07003323 (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
3324 ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003325 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
3326 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3327 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
3328 my $init_char = "c";
3329 my $orig_commit = "";
3330 my $short = 1;
3331 my $long = 0;
3332 my $case = 1;
3333 my $space = 1;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003334 my $id = '0123456789ab';
3335 my $orig_desc = "commit description";
3336 my $description = "";
Simon Glass587254e2022-01-23 12:55:11 -07003337 my $herectx = $herecurr;
3338 my $has_parens = 0;
3339 my $has_quotes = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003340
Simon Glass587254e2022-01-23 12:55:11 -07003341 my $input = $line;
3342 if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) {
3343 for (my $n = 0; $n < 2; $n++) {
3344 if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) {
3345 $orig_desc = $1;
3346 $has_parens = 1;
3347 # Always strip leading/trailing parens then double quotes if existing
3348 $orig_desc = substr($orig_desc, 1, -1);
3349 if ($orig_desc =~ /^".*"$/) {
3350 $orig_desc = substr($orig_desc, 1, -1);
3351 $has_quotes = 1;
3352 }
3353 last;
3354 }
3355 last if ($#lines < $linenr + $n);
3356 $input .= " " . trim($rawlines[$linenr + $n]);
3357 $herectx .= "$rawlines[$linenr + $n]\n";
3358 }
3359 $herectx = $herecurr if (!$has_parens);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003360 }
3361
Simon Glass587254e2022-01-23 12:55:11 -07003362 if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3363 $init_char = $1;
3364 $orig_commit = lc($2);
3365 $short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i);
3366 $long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i);
3367 $space = 0 if ($input =~ /\bcommit [0-9a-f]/i);
3368 $case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
3369 } elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) {
3370 $orig_commit = lc($1);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003371 }
3372
3373 ($id, $description) = git_commit_info($orig_commit,
3374 $id, $orig_desc);
3375
3376 if (defined($id) &&
Simon Glass587254e2022-01-23 12:55:11 -07003377 ($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) &&
3378 $last_git_commit_id_linenr != $linenr - 1) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003379 ERROR("GIT_COMMIT_ID",
Simon Glass587254e2022-01-23 12:55:11 -07003380 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herectx);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003381 }
Simon Glass587254e2022-01-23 12:55:11 -07003382 #don't report the next line if this line ends in commit and the sha1 hash is the next line
3383 $last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003384 }
3385
3386# Check for added, moved or deleted files
3387 if (!$reported_maintainer_file && !$in_commit_log &&
3388 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
3389 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
3390 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
3391 (defined($1) || defined($2))))) {
3392 $is_patch = 1;
3393 $reported_maintainer_file = 1;
3394 WARN("FILE_PATH_CHANGES",
3395 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
3396 }
3397
Tom Rinic57383b2020-06-16 10:29:46 -04003398# Check for adding new DT bindings not in schema format
3399 if (!$in_commit_log &&
3400 ($line =~ /^new file mode\s*\d+\s*$/) &&
3401 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3402 WARN("DT_SCHEMA_BINDING_PATCH",
Tom Rinie199fb32021-08-03 08:31:56 -04003403 "DT bindings should be in DT schema format. See: Documentation/devicetree/bindings/writing-schema.rst\n");
Tom Rinic57383b2020-06-16 10:29:46 -04003404 }
3405
Joe Hershberger05622192011-10-18 10:06:59 +00003406# Check for wrappage within a valid hunk of the file
3407 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
3408 ERROR("CORRUPTED_PATCH",
3409 "patch seems to be corrupt (line wrapped?)\n" .
3410 $herecurr) if (!$emitted_corrupt++);
3411 }
3412
Joe Hershberger05622192011-10-18 10:06:59 +00003413# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3414 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
3415 $rawline !~ m/^$UTF8*$/) {
3416 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3417
3418 my $blank = copy_spacing($rawline);
3419 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3420 my $hereptr = "$hereline$ptr\n";
3421
3422 CHK("INVALID_UTF8",
3423 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
3424 }
3425
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003426# Check if it's the start of a commit log
3427# (not a header line and we haven't seen the patch filename)
3428 if ($in_header_lines && $realfile =~ /^$/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003429 !($rawline =~ /^\s+(?:\S|$)/ ||
3430 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003431 $in_header_lines = 0;
3432 $in_commit_log = 1;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003433 $has_commit_log = 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003434 }
3435
3436# Check if there is UTF-8 in a commit log when a mail header has explicitly
3437# declined it, i.e defined some charset where it is missing.
3438 if ($in_header_lines &&
3439 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3440 $1 !~ /utf-8/i) {
3441 $non_utf8_charset = 1;
3442 }
3443
3444 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
3445 $rawline =~ /$NON_ASCII_UTF8/) {
3446 WARN("UTF8_BEFORE_PATCH",
3447 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3448 }
3449
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003450# Check for absolute kernel paths in commit message
3451 if ($tree && $in_commit_log) {
3452 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3453 my $file = $1;
3454
3455 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3456 check_absolute_file($1, $herecurr)) {
3457 #
3458 } else {
3459 check_absolute_file($file, $herecurr);
3460 }
3461 }
3462 }
3463
Dan Murphyc10e0f52017-01-31 14:15:53 -06003464# Check for various typo / spelling mistakes
3465 if (defined($misspellings) &&
3466 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Tom Rinie199fb32021-08-03 08:31:56 -04003467 while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
Dan Murphyc10e0f52017-01-31 14:15:53 -06003468 my $typo = $1;
Tom Rinie199fb32021-08-03 08:31:56 -04003469 my $blank = copy_spacing($rawline);
3470 my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
3471 my $hereptr = "$hereline$ptr\n";
Dan Murphyc10e0f52017-01-31 14:15:53 -06003472 my $typo_fix = $spelling_fix{lc($typo)};
3473 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3474 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003475 my $msg_level = \&WARN;
3476 $msg_level = \&CHK if ($file);
3477 if (&{$msg_level}("TYPO_SPELLING",
Tom Rinie199fb32021-08-03 08:31:56 -04003478 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
Dan Murphyc10e0f52017-01-31 14:15:53 -06003479 $fix) {
3480 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3481 }
3482 }
3483 }
3484
Tom Rinic57383b2020-06-16 10:29:46 -04003485# check for invalid commit id
3486 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3487 my $id;
3488 my $description;
3489 ($id, $description) = git_commit_info($2, undef, undef);
3490 if (!defined($id)) {
3491 WARN("UNKNOWN_COMMIT_ID",
3492 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3493 }
3494 }
3495
Tom Rinie199fb32021-08-03 08:31:56 -04003496# check for repeated words separated by a single space
3497# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3498 if (($rawline =~ /^\+/ || $in_commit_log) &&
3499 $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
3500 pos($rawline) = 1 if (!$in_commit_log);
3501 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3502
3503 my $first = $1;
3504 my $second = $2;
3505 my $start_pos = $-[1];
3506 my $end_pos = $+[2];
3507 if ($first =~ /(?:struct|union|enum)/) {
3508 pos($rawline) += length($first) + length($second) + 1;
3509 next;
3510 }
3511
3512 next if (lc($first) ne lc($second));
3513 next if ($first eq 'long');
3514
3515 # check for character before and after the word matches
3516 my $start_char = '';
3517 my $end_char = '';
3518 $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
3519 $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3520
3521 next if ($start_char =~ /^\S$/);
3522 next if (index(" \t.,;?!", $end_char) == -1);
3523
3524 # avoid repeating hex occurrences like 'ff ff fe 09 ...'
3525 if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3526 next if (!exists($allow_repeated_words{lc($first)}));
3527 }
3528
3529 if (WARN("REPEATED_WORD",
3530 "Possible repeated word: '$first'\n" . $herecurr) &&
3531 $fix) {
3532 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3533 }
3534 }
3535
3536 # if it's a repeated word on consecutive lines in a comment block
3537 if ($prevline =~ /$;+\s*$/ &&
3538 $prevrawline =~ /($word_pattern)\s*$/) {
3539 my $last_word = $1;
3540 if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3541 if (WARN("REPEATED_WORD",
3542 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3543 $fix) {
3544 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3545 }
3546 }
3547 }
3548 }
3549
Joe Hershberger05622192011-10-18 10:06:59 +00003550# ignore non-hunk lines and lines being removed
3551 next if (!$hunk_line || $line =~ /^-/);
3552
3553#trailing whitespace
3554 if ($line =~ /^\+.*\015/) {
3555 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003556 if (ERROR("DOS_LINE_ENDINGS",
3557 "DOS line endings\n" . $herevet) &&
3558 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003559 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Tom Rini6b9709d2014-02-27 08:27:28 -05003560 }
Joe Hershberger05622192011-10-18 10:06:59 +00003561 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3562 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003563 if (ERROR("TRAILING_WHITESPACE",
3564 "trailing whitespace\n" . $herevet) &&
3565 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003566 $fixed[$fixlinenr] =~ s/\s+$//;
Tom Rini6b9709d2014-02-27 08:27:28 -05003567 }
3568
Joe Hershberger05622192011-10-18 10:06:59 +00003569 $rpt_cleaners = 1;
3570 }
3571
Tom Rini6b9709d2014-02-27 08:27:28 -05003572# Check for FSF mailing addresses.
3573 if ($rawline =~ /\bwrite to the Free/i ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003574 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Tom Rini6b9709d2014-02-27 08:27:28 -05003575 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3576 $rawline =~ /\b51\s+Franklin\s+St/i) {
3577 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003578 my $msg_level = \&ERROR;
3579 $msg_level = \&CHK if ($file);
3580 &{$msg_level}("FSF_MAILING_ADDRESS",
3581 "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)
Tom Rini6b9709d2014-02-27 08:27:28 -05003582 }
3583
Joe Hershberger05622192011-10-18 10:06:59 +00003584# check for Kconfig help text having a real description
3585# Only applies when adding the entry originally, after that we do not have
3586# sufficient context to determine whether it is indeed long enough.
3587 if ($realfile =~ /Kconfig/ &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003588 # 'choice' is usually the last thing on the line (though
3589 # Kconfig supports named choices), so use a word boundary
3590 # (\b) rather than a whitespace character (\s)
3591 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003592 my $length = 0;
3593 my $cnt = $realcnt;
3594 my $ln = $linenr + 1;
3595 my $f;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003596 my $is_start = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00003597 my $is_end = 0;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003598 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Joe Hershberger05622192011-10-18 10:06:59 +00003599 $f = $lines[$ln - 1];
3600 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3601 $is_end = $lines[$ln - 1] =~ /^\+/;
Joe Hershberger05622192011-10-18 10:06:59 +00003602
3603 next if ($f =~ /^-/);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003604 last if (!$file && $f =~ /^\@\@/);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003605
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003606 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003607 $is_start = 1;
Tom Rinie199fb32021-08-03 08:31:56 -04003608 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003609 $length = -1;
3610 }
3611
Joe Hershberger05622192011-10-18 10:06:59 +00003612 $f =~ s/^.//;
3613 $f =~ s/#.*//;
3614 $f =~ s/^\s+//;
3615 next if ($f =~ /^$/);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003616
3617 # This only checks context lines in the patch
3618 # and so hopefully shouldn't trigger false
3619 # positives, even though some of these are
3620 # common words in help texts
3621 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3622 if|endif|menu|endmenu|source)\b/x) {
Joe Hershberger05622192011-10-18 10:06:59 +00003623 $is_end = 1;
3624 last;
3625 }
3626 $length++;
3627 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003628 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3629 WARN("CONFIG_DESCRIPTION",
3630 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3631 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003632 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
3633 }
3634
Tom Rinie199fb32021-08-03 08:31:56 -04003635# check MAINTAINERS entries
3636 if ($realfile =~ /^MAINTAINERS$/) {
3637# check MAINTAINERS entries for the right form
3638 if ($rawline =~ /^\+[A-Z]:/ &&
3639 $rawline !~ /^\+[A-Z]:\t\S/) {
3640 if (WARN("MAINTAINERS_STYLE",
3641 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3642 $fix) {
3643 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3644 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003645 }
Tom Rinie199fb32021-08-03 08:31:56 -04003646# check MAINTAINERS entries for the right ordering too
3647 my $preferred_order = 'MRLSWQBCPTFXNK';
3648 if ($rawline =~ /^\+[A-Z]:/ &&
3649 $prevrawline =~ /^[\+ ][A-Z]:/) {
3650 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3651 my $cur = $1;
3652 my $curval = $2;
3653 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3654 my $prev = $1;
3655 my $prevval = $2;
3656 my $curindex = index($preferred_order, $cur);
3657 my $previndex = index($preferred_order, $prev);
3658 if ($curindex < 0) {
3659 WARN("MAINTAINERS_STYLE",
3660 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3661 } else {
3662 if ($previndex >= 0 && $curindex < $previndex) {
3663 WARN("MAINTAINERS_STYLE",
3664 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3665 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3666 ($prev eq 'X' && $cur eq 'X')) &&
3667 ($prevval cmp $curval) > 0) {
3668 WARN("MAINTAINERS_STYLE",
3669 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3670 }
3671 }
3672 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003673 }
3674
3675 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3676 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3677 my $flag = $1;
3678 my $replacement = {
3679 'EXTRA_AFLAGS' => 'asflags-y',
3680 'EXTRA_CFLAGS' => 'ccflags-y',
3681 'EXTRA_CPPFLAGS' => 'cppflags-y',
3682 'EXTRA_LDFLAGS' => 'ldflags-y',
3683 };
3684
3685 WARN("DEPRECATED_VARIABLE",
3686 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
Joe Hershberger05622192011-10-18 10:06:59 +00003687 }
3688
Tom Rini6b9709d2014-02-27 08:27:28 -05003689# check for DT compatible documentation
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003690 if (defined $root &&
3691 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3692 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3693
Tom Rini6b9709d2014-02-27 08:27:28 -05003694 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3695
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003696 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Tom Rinic57383b2020-06-16 10:29:46 -04003697 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003698
Tom Rini6b9709d2014-02-27 08:27:28 -05003699 foreach my $compat (@compats) {
3700 my $compat2 = $compat;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003701 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3702 my $compat3 = $compat;
3703 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3704 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Tom Rini6b9709d2014-02-27 08:27:28 -05003705 if ( $? >> 8 ) {
3706 WARN("UNDOCUMENTED_DT_STRING",
3707 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3708 }
3709
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003710 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3711 my $vendor = $1;
Tom Rinic57383b2020-06-16 10:29:46 -04003712 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Tom Rini6b9709d2014-02-27 08:27:28 -05003713 if ( $? >> 8 ) {
3714 WARN("UNDOCUMENTED_DT_STRING",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003715 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Tom Rini6b9709d2014-02-27 08:27:28 -05003716 }
3717 }
3718 }
3719
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003720# check for using SPDX license tag at beginning of files
3721 if ($realline == $checklicenseline) {
3722 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3723 $checklicenseline = 2;
3724 } elsif ($rawline =~ /^\+/) {
3725 my $comment = "";
3726 if ($realfile =~ /\.(h|s|S)$/) {
3727 $comment = '/*';
3728 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3729 $comment = '//';
Tom Rinic57383b2020-06-16 10:29:46 -04003730 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003731 $comment = '#';
3732 } elsif ($realfile =~ /\.rst$/) {
3733 $comment = '..';
3734 }
3735
Tom Rinic57383b2020-06-16 10:29:46 -04003736# check SPDX comment style for .[chsS] files
3737 if ($realfile =~ /\.[chsS]$/ &&
3738 $rawline =~ /SPDX-License-Identifier:/ &&
3739 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3740 WARN("SPDX_LICENSE_TAG",
3741 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3742 }
3743
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003744 if ($comment !~ /^$/ &&
Tom Rinic57383b2020-06-16 10:29:46 -04003745 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3746 WARN("SPDX_LICENSE_TAG",
3747 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003748 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Tom Rinic57383b2020-06-16 10:29:46 -04003749 my $spdx_license = $1;
3750 if (!is_SPDX_License_valid($spdx_license)) {
3751 WARN("SPDX_LICENSE_TAG",
3752 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3753 }
3754 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3755 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3756 my $msg_level = \&WARN;
3757 $msg_level = \&CHK if ($file);
3758 if (&{$msg_level}("SPDX_LICENSE_TAG",
3759
3760 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3761 $fix) {
3762 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3763 }
3764 }
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003765 }
3766 }
3767 }
3768
Tom Rinie199fb32021-08-03 08:31:56 -04003769# check for embedded filenames
3770 if ($rawline =~ /^\+.*\Q$realfile\E/) {
3771 WARN("EMBEDDED_FILENAME",
3772 "It's generally not useful to have the filename in the file\n" . $herecurr);
3773 }
3774
Joe Hershberger05622192011-10-18 10:06:59 +00003775# check we are in a valid source file if not then ignore this hunk
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003776 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Joe Hershberger05622192011-10-18 10:06:59 +00003777
Tom Rinic57383b2020-06-16 10:29:46 -04003778# check for using SPDX-License-Identifier on the wrong line number
3779 if ($realline != $checklicenseline &&
3780 $rawline =~ /\bSPDX-License-Identifier:/ &&
3781 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3782 WARN("SPDX_LICENSE_TAG",
3783 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3784 }
3785
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003786# line length limit (with some exclusions)
3787#
3788# There are a few types of lines that may extend beyond $max_line_length:
3789# logging functions like pr_info that end in a string
3790# lines with a single string
3791# #defines that are a single string
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003792# lines with an RFC3986 like URL
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003793#
3794# There are 3 different line length message types:
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003795# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003796# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3797# LONG_LINE all other lines longer than $max_line_length
3798#
3799# if LONG_LINE is ignored, the other 2 types are also ignored
3800#
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003801
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003802 if ($line =~ /^\+/ && $length > $max_line_length) {
3803 my $msg_type = "LONG_LINE";
Joe Hershberger05622192011-10-18 10:06:59 +00003804
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003805 # Check the allowed long line types first
3806
3807 # logging functions that end in a string that starts
3808 # before $max_line_length
3809 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3810 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3811 $msg_type = "";
3812
3813 # lines with only strings (w/ possible termination)
3814 # #defines with only strings
3815 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3816 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3817 $msg_type = "";
3818
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003819 # More special cases
3820 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3821 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3822 $msg_type = "";
3823
3824 # URL ($rawline is used in case the URL is in a comment)
3825 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003826 $msg_type = "";
3827
3828 # Otherwise set the alternate message types
3829
3830 # a comment starts before $max_line_length
3831 } elsif ($line =~ /($;[\s$;]*)$/ &&
3832 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3833 $msg_type = "LONG_LINE_COMMENT"
3834
3835 # a quoted string starts before $max_line_length
3836 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3837 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3838 $msg_type = "LONG_LINE_STRING"
Tom Rini6b9709d2014-02-27 08:27:28 -05003839 }
3840
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003841 if ($msg_type ne "" &&
3842 (show_type("LONG_LINE") || show_type($msg_type))) {
Simon Glass048a6482020-05-22 16:32:35 -06003843 my $msg_level = \&WARN;
3844 $msg_level = \&CHK if ($file);
3845 &{$msg_level}($msg_type,
3846 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003847 }
Joe Hershberger05622192011-10-18 10:06:59 +00003848 }
3849
3850# check for adding lines without a newline.
3851 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Tom Rinie199fb32021-08-03 08:31:56 -04003852 if (WARN("MISSING_EOF_NEWLINE",
3853 "adding a line without newline at end of file\n" . $herecurr) &&
3854 $fix) {
3855 fix_delete_line($fixlinenr+1, "No newline at end of file");
3856 }
3857 }
3858
3859# check for .L prefix local symbols in .S files
3860 if ($realfile =~ /\.S$/ &&
3861 $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) {
3862 WARN("AVOID_L_PREFIX",
3863 "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/asm-annotations.rst\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00003864 }
3865
Simon Glassb77df592020-05-22 16:32:36 -06003866 if ($u_boot) {
Simon Glass23552ba2020-07-19 10:16:01 -06003867 u_boot_line($realfile, $line, $rawline, $herecurr);
Simon Glassb77df592020-05-22 16:32:36 -06003868 }
3869
Joe Hershberger05622192011-10-18 10:06:59 +00003870# check we are in a valid source file C or perl if not then ignore this hunk
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003871 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Joe Hershberger05622192011-10-18 10:06:59 +00003872
3873# at the beginning of a line any tabs must come first and anything
Tom Rinic57383b2020-06-16 10:29:46 -04003874# more than $tabsize must use tabs.
Joe Hershberger05622192011-10-18 10:06:59 +00003875 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3876 $rawline =~ /^\+\s* \s*/) {
3877 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Hershberger05622192011-10-18 10:06:59 +00003878 $rpt_cleaners = 1;
Tom Rini6b9709d2014-02-27 08:27:28 -05003879 if (ERROR("CODE_INDENT",
3880 "code indent should use tabs where possible\n" . $herevet) &&
3881 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003882 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Tom Rini6b9709d2014-02-27 08:27:28 -05003883 }
Joe Hershberger05622192011-10-18 10:06:59 +00003884 }
3885
3886# check for space before tabs.
3887 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3888 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003889 if (WARN("SPACE_BEFORE_TAB",
3890 "please, no space before tabs\n" . $herevet) &&
3891 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003892 while ($fixed[$fixlinenr] =~
Tom Rinic57383b2020-06-16 10:29:46 -04003893 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003894 while ($fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05003895 s/(^\+.*) +\t/$1\t/) {}
3896 }
Joe Hershberger05622192011-10-18 10:06:59 +00003897 }
3898
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003899# check for assignments on the start of a line
3900 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
Tom Rinie199fb32021-08-03 08:31:56 -04003901 my $operator = $1;
3902 if (CHK("ASSIGNMENT_CONTINUATIONS",
3903 "Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3904 $fix && $prevrawline =~ /^\+/) {
3905 # add assignment operator to the previous line, remove from current line
3906 $fixed[$fixlinenr - 1] .= " $operator";
3907 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3908 }
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003909 }
3910
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003911# check for && or || at the start of a line
3912 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
Tom Rinie199fb32021-08-03 08:31:56 -04003913 my $operator = $1;
3914 if (CHK("LOGICAL_CONTINUATIONS",
3915 "Logical continuations should be on the previous line\n" . $hereprev) &&
3916 $fix && $prevrawline =~ /^\+/) {
3917 # insert logical operator at last non-comment, non-whitepsace char on previous line
3918 $prevline =~ /[\s$;]*$/;
3919 my $line_end = substr($prevrawline, $-[0]);
3920 $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
3921 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3922 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003923 }
3924
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003925# check indentation starts on a tab stop
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003926 if ($perl_version_ok &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003927 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003928 my $indent = length($1);
Tom Rinic57383b2020-06-16 10:29:46 -04003929 if ($indent % $tabsize) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003930 if (WARN("TABSTOP",
3931 "Statements should start on a tabstop\n" . $herecurr) &&
3932 $fix) {
Tom Rinic57383b2020-06-16 10:29:46 -04003933 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003934 }
3935 }
3936 }
3937
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003938# check multi-line statement indentation matches previous line
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003939 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003940 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003941 $prevline =~ /^\+(\t*)(.*)$/;
3942 my $oldindent = $1;
3943 my $rest = $2;
3944
3945 my $pos = pos_last_openparen($rest);
3946 if ($pos >= 0) {
3947 $line =~ /^(\+| )([ \t]*)/;
3948 my $newindent = $2;
3949
3950 my $goodtabindent = $oldindent .
Tom Rinic57383b2020-06-16 10:29:46 -04003951 "\t" x ($pos / $tabsize) .
3952 " " x ($pos % $tabsize);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003953 my $goodspaceindent = $oldindent . " " x $pos;
3954
3955 if ($newindent ne $goodtabindent &&
3956 $newindent ne $goodspaceindent) {
Tom Rini6b9709d2014-02-27 08:27:28 -05003957
3958 if (CHK("PARENTHESIS_ALIGNMENT",
3959 "Alignment should match open parenthesis\n" . $hereprev) &&
3960 $fix && $line =~ /^\+/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003961 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05003962 s/^\+[ \t]*/\+$goodtabindent/;
3963 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003964 }
3965 }
3966 }
3967
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003968# check for space after cast like "(int) foo" or "(struct foo) bar"
3969# avoid checking a few false positives:
3970# "sizeof(<type>)" or "__alignof__(<type>)"
3971# function pointer declarations like "(*foo)(int) = bar;"
3972# structure definitions like "(struct foo) { 0 };"
3973# multiline macros that define functions
3974# known attributes or the __attribute__ keyword
3975 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3976 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05003977 if (CHK("SPACING",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003978 "No space is necessary after a cast\n" . $herecurr) &&
Tom Rini6b9709d2014-02-27 08:27:28 -05003979 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003980 $fixed[$fixlinenr] =~
3981 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Tom Rini6b9709d2014-02-27 08:27:28 -05003982 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003983 }
3984
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003985# Block comment styles
3986# Networking with an initial /*
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003987 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Tom Rini6b9709d2014-02-27 08:27:28 -05003988 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003989 $rawline =~ /^\+[ \t]*\*/ &&
Tom Rinie199fb32021-08-03 08:31:56 -04003990 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003991 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3992 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3993 }
3994
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003995# Block comments use * on subsequent lines
3996 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3997 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Tom Rini6b9709d2014-02-27 08:27:28 -05003998 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
3999 $rawline =~ /^\+/ && #line is new
4000 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004001 WARN("BLOCK_COMMENT_STYLE",
4002 "Block comments use * on subsequent lines\n" . $hereprev);
Tom Rini6b9709d2014-02-27 08:27:28 -05004003 }
4004
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004005# Block comments use */ on trailing lines
4006 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004007 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
4008 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
4009 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004010 WARN("BLOCK_COMMENT_STYLE",
4011 "Block comments use a trailing */ on a separate line\n" . $herecurr);
4012 }
4013
4014# Block comment * alignment
4015 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
4016 $line =~ /^\+[ \t]*$;/ && #leading comment
4017 $rawline =~ /^\+[ \t]*\*/ && #leading *
4018 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
4019 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
4020 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
4021 my $oldindent;
4022 $prevrawline =~ m@^\+([ \t]*/?)\*@;
4023 if (defined($1)) {
4024 $oldindent = expand_tabs($1);
4025 } else {
4026 $prevrawline =~ m@^\+(.*/?)\*@;
4027 $oldindent = expand_tabs($1);
4028 }
4029 $rawline =~ m@^\+([ \t]*)\*@;
4030 my $newindent = $1;
4031 $newindent = expand_tabs($newindent);
4032 if (length($oldindent) ne length($newindent)) {
4033 WARN("BLOCK_COMMENT_STYLE",
4034 "Block comments should align the * on each line\n" . $hereprev);
4035 }
4036 }
4037
4038# check for missing blank lines after struct/union declarations
4039# with exceptions for various attributes and macros
4040 if ($prevline =~ /^[\+ ]};?\s*$/ &&
4041 $line =~ /^\+/ &&
4042 !($line =~ /^\+\s*$/ ||
4043 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
4044 $line =~ /^\+\s*MODULE_/i ||
4045 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
4046 $line =~ /^\+[a-z_]*init/ ||
4047 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
4048 $line =~ /^\+\s*DECLARE/ ||
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004049 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004050 $line =~ /^\+\s*__setup/)) {
4051 if (CHK("LINE_SPACING",
4052 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
4053 $fix) {
4054 fix_insert_line($fixlinenr, "\+");
4055 }
4056 }
4057
4058# check for multiple consecutive blank lines
4059 if ($prevline =~ /^[\+ ]\s*$/ &&
4060 $line =~ /^\+\s*$/ &&
4061 $last_blank_line != ($linenr - 1)) {
4062 if (CHK("LINE_SPACING",
4063 "Please don't use multiple blank lines\n" . $hereprev) &&
4064 $fix) {
4065 fix_delete_line($fixlinenr, $rawline);
4066 }
4067
4068 $last_blank_line = $linenr;
4069 }
4070
4071# check for missing blank lines after declarations
Tom Rinie199fb32021-08-03 08:31:56 -04004072# (declarations must have the same indentation and not be at the start of line)
4073 if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
4074 # use temporaries
4075 my $sl = $sline;
4076 my $pl = $prevline;
4077 # remove $Attribute/$Sparse uses to simplify comparisons
4078 $sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
4079 $pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
4080 if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004081 # function pointer declarations
Tom Rinie199fb32021-08-03 08:31:56 -04004082 $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004083 # foo bar; where foo is some local typedef or #define
Tom Rinie199fb32021-08-03 08:31:56 -04004084 $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004085 # known declaration macros
Tom Rinie199fb32021-08-03 08:31:56 -04004086 $pl =~ /^\+\s+$declaration_macros/) &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004087 # for "else if" which can look like "$Ident $Ident"
Tom Rinie199fb32021-08-03 08:31:56 -04004088 !($pl =~ /^\+\s+$c90_Keywords\b/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004089 # other possible extensions of declaration lines
Tom Rinie199fb32021-08-03 08:31:56 -04004090 $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004091 # not starting a section or a macro "\" extended line
Tom Rinie199fb32021-08-03 08:31:56 -04004092 $pl =~ /(?:\{\s*|\\)$/) &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004093 # looks like a declaration
Tom Rinie199fb32021-08-03 08:31:56 -04004094 !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004095 # function pointer declarations
Tom Rinie199fb32021-08-03 08:31:56 -04004096 $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004097 # foo bar; where foo is some local typedef or #define
Tom Rinie199fb32021-08-03 08:31:56 -04004098 $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004099 # known declaration macros
Tom Rinie199fb32021-08-03 08:31:56 -04004100 $sl =~ /^\+\s+$declaration_macros/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004101 # start of struct or union or enum
Tom Rinie199fb32021-08-03 08:31:56 -04004102 $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004103 # start or end of block or continuation of declaration
Tom Rinie199fb32021-08-03 08:31:56 -04004104 $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004105 # bitfield continuation
Tom Rinie199fb32021-08-03 08:31:56 -04004106 $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004107 # other possible extensions of declaration lines
Tom Rinie199fb32021-08-03 08:31:56 -04004108 $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
4109 if (WARN("LINE_SPACING",
4110 "Missing a blank line after declarations\n" . $hereprev) &&
4111 $fix) {
4112 fix_insert_line($fixlinenr, "\+");
4113 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004114 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004115 }
4116
Joe Hershberger05622192011-10-18 10:06:59 +00004117# check for spaces at the beginning of a line.
4118# Exceptions:
4119# 1) within comments
4120# 2) indented preprocessor commands
4121# 3) hanging labels
Tom Rini6b9709d2014-02-27 08:27:28 -05004122 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Joe Hershberger05622192011-10-18 10:06:59 +00004123 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05004124 if (WARN("LEADING_SPACE",
4125 "please, no spaces at the start of a line\n" . $herevet) &&
4126 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004127 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Tom Rini6b9709d2014-02-27 08:27:28 -05004128 }
Joe Hershberger05622192011-10-18 10:06:59 +00004129 }
4130
4131# check we are in a valid C source file if not then ignore this hunk
4132 next if ($realfile !~ /\.(h|c)$/);
4133
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004134# check for unusual line ending [ or (
4135 if ($line =~ /^\+.*([\[\(])\s*$/) {
4136 CHK("OPEN_ENDED_LINE",
4137 "Lines should not end with a '$1'\n" . $herecurr);
4138 }
4139
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004140# check if this appears to be the start function declaration, save the name
4141 if ($sline =~ /^\+\{\s*$/ &&
4142 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
4143 $context_function = $1;
4144 }
4145
4146# check if this appears to be the end of function declaration
4147 if ($sline =~ /^\+\}\s*$/) {
4148 undef $context_function;
4149 }
4150
4151# check indentation of any line with a bare else
4152# (but not if it is a multiple line "if (foo) return bar; else return baz;")
4153# if the previous line is a break or return and is indented 1 tab more...
4154 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
4155 my $tabs = length($1) + 1;
4156 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
4157 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
4158 defined $lines[$linenr] &&
4159 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
4160 WARN("UNNECESSARY_ELSE",
4161 "else is not generally useful after a break or return\n" . $hereprev);
4162 }
4163 }
4164
4165# check indentation of a line with a break;
Tom Rinie199fb32021-08-03 08:31:56 -04004166# if the previous line is a goto, return or break
4167# and is indented the same # of tabs
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004168 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
4169 my $tabs = $1;
Tom Rinie199fb32021-08-03 08:31:56 -04004170 if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
4171 if (WARN("UNNECESSARY_BREAK",
4172 "break is not useful after a $1\n" . $hereprev) &&
4173 $fix) {
4174 fix_delete_line($fixlinenr, $rawline);
4175 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004176 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004177 }
4178
Joe Hershberger05622192011-10-18 10:06:59 +00004179# check for RCS/CVS revision markers
4180 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
4181 WARN("CVS_KEYWORD",
4182 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
4183 }
4184
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004185# check for old HOTPLUG __dev<foo> section markings
4186 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
4187 WARN("HOTPLUG_SECTION",
4188 "Using $1 is unnecessary\n" . $herecurr);
4189 }
4190
Joe Hershberger05622192011-10-18 10:06:59 +00004191# Check for potential 'bare' types
4192 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
4193 $realline_next);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004194#print "LINE<$line>\n";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004195 if ($linenr > $suppress_statement &&
Tom Rini6b9709d2014-02-27 08:27:28 -05004196 $realcnt && $sline =~ /.\s*\S/) {
Joe Hershberger05622192011-10-18 10:06:59 +00004197 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4198 ctx_statement_block($linenr, $realcnt, 0);
4199 $stat =~ s/\n./\n /g;
4200 $cond =~ s/\n./\n /g;
4201
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004202#print "linenr<$linenr> <$stat>\n";
4203 # If this statement has no statement boundaries within
4204 # it there is no point in retrying a statement scan
4205 # until we hit end of it.
4206 my $frag = $stat; $frag =~ s/;+\s*$//;
4207 if ($frag !~ /(?:{|;)/) {
4208#print "skip<$line_nr_next>\n";
4209 $suppress_statement = $line_nr_next;
4210 }
4211
Joe Hershberger05622192011-10-18 10:06:59 +00004212 # Find the real next line.
4213 $realline_next = $line_nr_next;
4214 if (defined $realline_next &&
4215 (!defined $lines[$realline_next - 1] ||
4216 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
4217 $realline_next++;
4218 }
4219
4220 my $s = $stat;
4221 $s =~ s/{.*$//s;
4222
4223 # Ignore goto labels.
4224 if ($s =~ /$Ident:\*$/s) {
4225
4226 # Ignore functions being called
4227 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
4228
4229 } elsif ($s =~ /^.\s*else\b/s) {
4230
4231 # declarations always start with types
4232 } 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) {
4233 my $type = $1;
4234 $type =~ s/\s+/ /g;
4235 possible($type, "A:" . $s);
4236
4237 # definitions in global scope can only start with types
4238 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
4239 possible($1, "B:" . $s);
4240 }
4241
4242 # any (foo ... *) is a pointer cast, and foo is a type
4243 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
4244 possible($1, "C:" . $s);
4245 }
4246
4247 # Check for any sort of function declaration.
4248 # int foo(something bar, other baz);
4249 # void (*store_gdt)(x86_descr_ptr *);
4250 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
4251 my ($name_len) = length($1);
4252
4253 my $ctx = $s;
4254 substr($ctx, 0, $name_len + 1, '');
4255 $ctx =~ s/\)[^\)]*$//;
4256
4257 for my $arg (split(/\s*,\s*/, $ctx)) {
4258 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
4259
4260 possible($1, "D:" . $s);
4261 }
4262 }
4263 }
4264
4265 }
4266
4267#
4268# Checks which may be anchored in the context.
4269#
4270
4271# Check for switch () and associated case and default
4272# statements should be at the same indent.
4273 if ($line=~/\bswitch\s*\(.*\)/) {
4274 my $err = '';
4275 my $sep = '';
4276 my @ctx = ctx_block_outer($linenr, $realcnt);
4277 shift(@ctx);
4278 for my $ctx (@ctx) {
4279 my ($clen, $cindent) = line_stats($ctx);
4280 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
4281 $indent != $cindent) {
4282 $err .= "$sep$ctx\n";
4283 $sep = '';
4284 } else {
4285 $sep = "[...]\n";
4286 }
4287 }
4288 if ($err ne '') {
4289 ERROR("SWITCH_CASE_INDENT_LEVEL",
4290 "switch and case should be at the same indent\n$hereline$err");
4291 }
4292 }
4293
4294# if/while/etc brace do not go on next line, unless defining a do while loop,
4295# or if that brace on the next line is for something else
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004296 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Joe Hershberger05622192011-10-18 10:06:59 +00004297 my $pre_ctx = "$1$2";
4298
4299 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004300
4301 if ($line =~ /^\+\t{6,}/) {
4302 WARN("DEEP_INDENTATION",
4303 "Too many leading tabs - consider code refactoring\n" . $herecurr);
4304 }
4305
Joe Hershberger05622192011-10-18 10:06:59 +00004306 my $ctx_cnt = $realcnt - $#ctx - 1;
4307 my $ctx = join("\n", @ctx);
4308
4309 my $ctx_ln = $linenr;
4310 my $ctx_skip = $realcnt;
4311
4312 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4313 defined $lines[$ctx_ln - 1] &&
4314 $lines[$ctx_ln - 1] =~ /^-/)) {
4315 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4316 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
4317 $ctx_ln++;
4318 }
4319
4320 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
4321 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
4322
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004323 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Hershberger05622192011-10-18 10:06:59 +00004324 ERROR("OPEN_BRACE",
4325 "that open brace { should be on the previous line\n" .
4326 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
4327 }
4328 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4329 $ctx =~ /\)\s*\;\s*$/ &&
4330 defined $lines[$ctx_ln - 1])
4331 {
4332 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
4333 if ($nindent > $indent) {
4334 WARN("TRAILING_SEMICOLON",
4335 "trailing semicolon indicates no statements, indent implies otherwise\n" .
4336 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
4337 }
4338 }
4339 }
4340
4341# Check relative indent for conditionals and blocks.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004342 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004343 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4344 ctx_statement_block($linenr, $realcnt, 0)
4345 if (!defined $stat);
Joe Hershberger05622192011-10-18 10:06:59 +00004346 my ($s, $c) = ($stat, $cond);
4347
4348 substr($s, 0, length($c), '');
4349
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004350 # remove inline comments
4351 $s =~ s/$;/ /g;
4352 $c =~ s/$;/ /g;
Joe Hershberger05622192011-10-18 10:06:59 +00004353
4354 # Find out how long the conditional actually is.
4355 my @newlines = ($c =~ /\n/gs);
4356 my $cond_lines = 1 + $#newlines;
4357
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004358 # Make sure we remove the line prefixes as we have
4359 # none on the first line, and are going to readd them
4360 # where necessary.
4361 $s =~ s/\n./\n/gs;
4362 while ($s =~ /\n\s+\\\n/) {
4363 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
4364 }
4365
Joe Hershberger05622192011-10-18 10:06:59 +00004366 # We want to check the first line inside the block
4367 # starting at the end of the conditional, so remove:
4368 # 1) any blank line termination
4369 # 2) any opening brace { on end of the line
4370 # 3) any do (...) {
4371 my $continuation = 0;
4372 my $check = 0;
4373 $s =~ s/^.*\bdo\b//;
4374 $s =~ s/^\s*{//;
4375 if ($s =~ s/^\s*\\//) {
4376 $continuation = 1;
4377 }
4378 if ($s =~ s/^\s*?\n//) {
4379 $check = 1;
4380 $cond_lines++;
4381 }
4382
4383 # Also ignore a loop construct at the end of a
4384 # preprocessor statement.
4385 if (($prevline =~ /^.\s*#\s*define\s/ ||
4386 $prevline =~ /\\\s*$/) && $continuation == 0) {
4387 $check = 0;
4388 }
4389
4390 my $cond_ptr = -1;
4391 $continuation = 0;
4392 while ($cond_ptr != $cond_lines) {
4393 $cond_ptr = $cond_lines;
4394
4395 # If we see an #else/#elif then the code
4396 # is not linear.
4397 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4398 $check = 0;
4399 }
4400
4401 # Ignore:
4402 # 1) blank lines, they should be at 0,
4403 # 2) preprocessor lines, and
4404 # 3) labels.
4405 if ($continuation ||
4406 $s =~ /^\s*?\n/ ||
4407 $s =~ /^\s*#\s*?/ ||
4408 $s =~ /^\s*$Ident\s*:/) {
4409 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
4410 if ($s =~ s/^.*?\n//) {
4411 $cond_lines++;
4412 }
4413 }
4414 }
4415
4416 my (undef, $sindent) = line_stats("+" . $s);
4417 my $stat_real = raw_line($linenr, $cond_lines);
4418
4419 # Check if either of these lines are modified, else
4420 # this is not this patch's fault.
4421 if (!defined($stat_real) ||
4422 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
4423 $check = 0;
4424 }
4425 if (defined($stat_real) && $cond_lines > 1) {
4426 $stat_real = "[...]\n$stat_real";
4427 }
4428
4429 #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";
4430
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004431 if ($check && $s ne '' &&
Tom Rinic57383b2020-06-16 10:29:46 -04004432 (($sindent % $tabsize) != 0 ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004433 ($sindent < $indent) ||
4434 ($sindent == $indent &&
4435 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Tom Rinic57383b2020-06-16 10:29:46 -04004436 ($sindent > $indent + $tabsize))) {
Joe Hershberger05622192011-10-18 10:06:59 +00004437 WARN("SUSPECT_CODE_INDENT",
4438 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
4439 }
4440 }
4441
4442 # Track the 'values' across context and added lines.
4443 my $opline = $line; $opline =~ s/^./ /;
4444 my ($curr_values, $curr_vars) =
4445 annotate_values($opline . "\n", $prev_values);
4446 $curr_values = $prev_values . $curr_values;
4447 if ($dbg_values) {
4448 my $outline = $opline; $outline =~ s/\t/ /g;
4449 print "$linenr > .$outline\n";
4450 print "$linenr > $curr_values\n";
4451 print "$linenr > $curr_vars\n";
4452 }
4453 $prev_values = substr($curr_values, -1);
4454
4455#ignore lines not being added
Tom Rini6b9709d2014-02-27 08:27:28 -05004456 next if ($line =~ /^[^\+]/);
Joe Hershberger05622192011-10-18 10:06:59 +00004457
Tom Rinie199fb32021-08-03 08:31:56 -04004458# check for self assignments used to avoid compiler warnings
4459# e.g.: int foo = foo, *bar = NULL;
4460# struct foo bar = *(&(bar));
4461 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
4462 my $var = $1;
4463 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
4464 WARN("SELF_ASSIGNMENT",
4465 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
4466 }
4467 }
4468
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004469# check for dereferences that span multiple lines
4470 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
4471 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
4472 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
4473 my $ref = $1;
4474 $line =~ /^.\s*($Lval)/;
4475 $ref .= $1;
4476 $ref =~ s/\s//g;
4477 WARN("MULTILINE_DEREFERENCE",
4478 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
4479 }
4480
4481# check for declarations of signed or unsigned without int
4482 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
4483 my $type = $1;
4484 my $var = $2;
4485 $var = "" if (!defined $var);
4486 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
4487 my $sign = $1;
4488 my $pointer = $2;
4489
4490 $pointer = "" if (!defined $pointer);
4491
4492 if (WARN("UNSPECIFIED_INT",
4493 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4494 $fix) {
4495 my $decl = trim($sign) . " int ";
4496 my $comp_pointer = $pointer;
4497 $comp_pointer =~ s/\s//g;
4498 $decl .= $comp_pointer;
4499 $decl = rtrim($decl) if ($var eq "");
4500 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
4501 }
4502 }
4503 }
4504
Joe Hershberger05622192011-10-18 10:06:59 +00004505# TEST: allow direct testing of the type matcher.
4506 if ($dbg_type) {
4507 if ($line =~ /^.\s*$Declare\s*$/) {
4508 ERROR("TEST_TYPE",
4509 "TEST: is type\n" . $herecurr);
4510 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
4511 ERROR("TEST_NOT_TYPE",
4512 "TEST: is not type ($1 is)\n". $herecurr);
4513 }
4514 next;
4515 }
4516# TEST: allow direct testing of the attribute matcher.
4517 if ($dbg_attr) {
4518 if ($line =~ /^.\s*$Modifier\s*$/) {
4519 ERROR("TEST_ATTR",
4520 "TEST: is attr\n" . $herecurr);
4521 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
4522 ERROR("TEST_NOT_ATTR",
4523 "TEST: is not attr ($1 is)\n". $herecurr);
4524 }
4525 next;
4526 }
4527
4528# check for initialisation to aggregates open brace on the next line
4529 if ($line =~ /^.\s*{/ &&
4530 $prevline =~ /(?:^|[^=])=\s*$/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004531 if (ERROR("OPEN_BRACE",
4532 "that open brace { should be on the previous line\n" . $hereprev) &&
4533 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4534 fix_delete_line($fixlinenr - 1, $prevrawline);
4535 fix_delete_line($fixlinenr, $rawline);
4536 my $fixedline = $prevrawline;
4537 $fixedline =~ s/\s*=\s*$/ = {/;
4538 fix_insert_line($fixlinenr, $fixedline);
4539 $fixedline = $line;
4540 $fixedline =~ s/^(.\s*)\{\s*/$1/;
4541 fix_insert_line($fixlinenr, $fixedline);
4542 }
Joe Hershberger05622192011-10-18 10:06:59 +00004543 }
4544
4545#
4546# Checks which are anchored on the added line.
4547#
4548
4549# check for malformed paths in #include statements (uses RAW line)
4550 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4551 my $path = $1;
4552 if ($path =~ m{//}) {
4553 ERROR("MALFORMED_INCLUDE",
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004554 "malformed #include filename\n" . $herecurr);
4555 }
4556 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4557 ERROR("UAPI_INCLUDE",
4558 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00004559 }
4560 }
4561
4562# no C99 // comments
4563 if ($line =~ m{//}) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004564 if (ERROR("C99_COMMENTS",
4565 "do not use C99 // comments\n" . $herecurr) &&
4566 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004567 my $line = $fixed[$fixlinenr];
Tom Rini6b9709d2014-02-27 08:27:28 -05004568 if ($line =~ /\/\/(.*)$/) {
4569 my $comment = trim($1);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004570 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Tom Rini6b9709d2014-02-27 08:27:28 -05004571 }
4572 }
Joe Hershberger05622192011-10-18 10:06:59 +00004573 }
4574 # Remove C99 comments.
4575 $line =~ s@//.*@@;
4576 $opline =~ s@//.*@@;
4577
4578# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4579# the whole statement.
4580#print "APW <$lines[$realline_next - 1]>\n";
4581 if (defined $realline_next &&
4582 exists $lines[$realline_next - 1] &&
4583 !defined $suppress_export{$realline_next} &&
Tom Rinie199fb32021-08-03 08:31:56 -04004584 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
Joe Hershberger05622192011-10-18 10:06:59 +00004585 # Handle definitions which produce identifiers with
4586 # a prefix:
4587 # XXX(foo);
4588 # EXPORT_SYMBOL(something_foo);
4589 my $name = $1;
Simon Glass587254e2022-01-23 12:55:11 -07004590 $name =~ s/^\s*($Ident).*/$1/;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004591 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Joe Hershberger05622192011-10-18 10:06:59 +00004592 $name =~ /^${Ident}_$2/) {
4593#print "FOO C name<$name>\n";
4594 $suppress_export{$realline_next} = 1;
4595
4596 } elsif ($stat !~ /(?:
4597 \n.}\s*$|
4598 ^.DEFINE_$Ident\(\Q$name\E\)|
4599 ^.DECLARE_$Ident\(\Q$name\E\)|
4600 ^.LIST_HEAD\(\Q$name\E\)|
4601 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4602 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
4603 )/x) {
4604#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4605 $suppress_export{$realline_next} = 2;
4606 } else {
4607 $suppress_export{$realline_next} = 1;
4608 }
4609 }
4610 if (!defined $suppress_export{$linenr} &&
4611 $prevline =~ /^.\s*$/ &&
Tom Rinie199fb32021-08-03 08:31:56 -04004612 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
Joe Hershberger05622192011-10-18 10:06:59 +00004613#print "FOO B <$lines[$linenr - 1]>\n";
4614 $suppress_export{$linenr} = 2;
4615 }
4616 if (defined $suppress_export{$linenr} &&
4617 $suppress_export{$linenr} == 2) {
4618 WARN("EXPORT_SYMBOL",
4619 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
4620 }
4621
4622# check for global initialisers.
Tom Rinie199fb32021-08-03 08:31:56 -04004623 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
4624 !exclude_global_initialisers($realfile)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004625 if (ERROR("GLOBAL_INITIALISERS",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004626 "do not initialise globals to $1\n" . $herecurr) &&
Tom Rini6b9709d2014-02-27 08:27:28 -05004627 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004628 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Tom Rini6b9709d2014-02-27 08:27:28 -05004629 }
Joe Hershberger05622192011-10-18 10:06:59 +00004630 }
4631# check for static initialisers.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004632 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004633 if (ERROR("INITIALISED_STATIC",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004634 "do not initialise statics to $1\n" .
Tom Rini6b9709d2014-02-27 08:27:28 -05004635 $herecurr) &&
4636 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004637 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Tom Rini6b9709d2014-02-27 08:27:28 -05004638 }
Joe Hershberger05622192011-10-18 10:06:59 +00004639 }
4640
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004641# check for misordered declarations of char/short/int/long with signed/unsigned
4642 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4643 my $tmp = trim($1);
4644 WARN("MISORDERED_TYPE",
4645 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4646 }
4647
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004648# check for unnecessary <signed> int declarations of short/long/long long
4649 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4650 my $type = trim($1);
4651 next if ($type !~ /\bint\b/);
4652 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4653 my $new_type = $type;
4654 $new_type =~ s/\b\s*int\s*\b/ /;
4655 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4656 $new_type =~ s/^const\s+//;
4657 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4658 $new_type = "const $new_type" if ($type =~ /^const\b/);
4659 $new_type =~ s/\s+/ /g;
4660 $new_type = trim($new_type);
4661 if (WARN("UNNECESSARY_INT",
4662 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4663 $fix) {
4664 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4665 }
4666 }
4667
Joe Hershberger05622192011-10-18 10:06:59 +00004668# check for static const char * arrays.
4669 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4670 WARN("STATIC_CONST_CHAR_ARRAY",
4671 "static const char * array should probably be static const char * const\n" .
4672 $herecurr);
Tom Rinic57383b2020-06-16 10:29:46 -04004673 }
4674
4675# check for initialized const char arrays that should be static const
4676 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4677 if (WARN("STATIC_CONST_CHAR_ARRAY",
4678 "const array should probably be static const\n" . $herecurr) &&
4679 $fix) {
4680 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4681 }
4682 }
Joe Hershberger05622192011-10-18 10:06:59 +00004683
4684# check for static char foo[] = "bar" declarations.
4685 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4686 WARN("STATIC_CONST_CHAR_ARRAY",
4687 "static char array declaration should probably be static const char\n" .
4688 $herecurr);
Tom Rinic57383b2020-06-16 10:29:46 -04004689 }
Joe Hershberger05622192011-10-18 10:06:59 +00004690
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004691# check for const <foo> const where <foo> is not a pointer or array type
4692 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4693 my $found = $1;
4694 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4695 WARN("CONST_CONST",
4696 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4697 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4698 WARN("CONST_CONST",
4699 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4700 }
4701 }
4702
Tom Rinie199fb32021-08-03 08:31:56 -04004703# check for const static or static <non ptr type> const declarations
4704# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
4705 if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
4706 $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
4707 if (WARN("STATIC_CONST",
4708 "Move const after static - use 'static const $1'\n" . $herecurr) &&
4709 $fix) {
4710 $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
4711 $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
4712 }
4713 }
4714
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004715# check for non-global char *foo[] = {"bar", ...} declarations.
4716 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4717 WARN("STATIC_CONST_CHAR_ARRAY",
4718 "char * array declaration might be better as static const\n" .
4719 $herecurr);
Tom Rinie199fb32021-08-03 08:31:56 -04004720 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004721
4722# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4723 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4724 my $array = $1;
4725 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4726 my $array_div = $1;
4727 if (WARN("ARRAY_SIZE",
4728 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4729 $fix) {
4730 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4731 }
4732 }
4733 }
4734
Tom Rini6b9709d2014-02-27 08:27:28 -05004735# check for function declarations without arguments like "int foo()"
Tom Rinic57383b2020-06-16 10:29:46 -04004736 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004737 if (ERROR("FUNCTION_WITHOUT_ARGS",
4738 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4739 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004740 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Tom Rini6b9709d2014-02-27 08:27:28 -05004741 }
Joe Hershberger05622192011-10-18 10:06:59 +00004742 }
4743
4744# check for new typedefs, only function parameters and sparse annotations
4745# make sense.
4746 if ($line =~ /\btypedef\s/ &&
4747 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4748 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
4749 $line !~ /\b$typeTypedefs\b/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004750 $line !~ /\b__bitwise\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00004751 WARN("NEW_TYPEDEFS",
4752 "do not add new typedefs\n" . $herecurr);
4753 }
4754
4755# * goes on variable not on type
4756 # (char*[ const])
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004757 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4758 #print "AA<$1>\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05004759 my ($ident, $from, $to) = ($1, $2, $2);
Joe Hershberger05622192011-10-18 10:06:59 +00004760
4761 # Should start with a space.
4762 $to =~ s/^(\S)/ $1/;
4763 # Should not end with a space.
4764 $to =~ s/\s+$//;
4765 # '*'s should not have spaces between.
4766 while ($to =~ s/\*\s+\*/\*\*/) {
4767 }
4768
Tom Rini6b9709d2014-02-27 08:27:28 -05004769## print "1: from<$from> to<$to> ident<$ident>\n";
Joe Hershberger05622192011-10-18 10:06:59 +00004770 if ($from ne $to) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004771 if (ERROR("POINTER_LOCATION",
4772 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4773 $fix) {
4774 my $sub_from = $ident;
4775 my $sub_to = $ident;
4776 $sub_to =~ s/\Q$from\E/$to/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004777 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004778 s@\Q$sub_from\E@$sub_to@;
4779 }
Joe Hershberger05622192011-10-18 10:06:59 +00004780 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004781 }
4782 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4783 #print "BB<$1>\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05004784 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Joe Hershberger05622192011-10-18 10:06:59 +00004785
4786 # Should start with a space.
4787 $to =~ s/^(\S)/ $1/;
4788 # Should not end with a space.
4789 $to =~ s/\s+$//;
4790 # '*'s should not have spaces between.
4791 while ($to =~ s/\*\s+\*/\*\*/) {
4792 }
4793 # Modifiers should have spaces.
4794 $to =~ s/(\b$Modifier$)/$1 /;
4795
Tom Rini6b9709d2014-02-27 08:27:28 -05004796## print "2: from<$from> to<$to> ident<$ident>\n";
Joe Hershberger05622192011-10-18 10:06:59 +00004797 if ($from ne $to && $ident !~ /^$Modifier$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004798 if (ERROR("POINTER_LOCATION",
4799 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4800 $fix) {
4801
4802 my $sub_from = $match;
4803 my $sub_to = $match;
4804 $sub_to =~ s/\Q$from\E/$to/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004805 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004806 s@\Q$sub_from\E@$sub_to@;
4807 }
Joe Hershberger05622192011-10-18 10:06:59 +00004808 }
4809 }
4810
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004811# avoid BUG() or BUG_ON()
4812 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004813 my $msg_level = \&WARN;
4814 $msg_level = \&CHK if ($file);
4815 &{$msg_level}("AVOID_BUG",
4816 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004817 }
Joe Hershberger05622192011-10-18 10:06:59 +00004818
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004819# avoid LINUX_VERSION_CODE
Joe Hershberger05622192011-10-18 10:06:59 +00004820 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4821 WARN("LINUX_VERSION_CODE",
4822 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
4823 }
4824
4825# check for uses of printk_ratelimit
4826 if ($line =~ /\bprintk_ratelimit\s*\(/) {
4827 WARN("PRINTK_RATELIMITED",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004828 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00004829 }
4830
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004831# printk should use KERN_* levels
4832 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4833 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4834 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00004835 }
4836
Tom Rinie199fb32021-08-03 08:31:56 -04004837# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4838 if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4839 my $printk = $1;
4840 my $modifier = $2;
4841 my $orig = $3;
4842 $modifier = "" if (!defined($modifier));
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004843 my $level = lc($orig);
4844 $level = "warn" if ($level eq "warning");
4845 my $level2 = $level;
4846 $level2 = "dbg" if ($level eq "debug");
Tom Rinie199fb32021-08-03 08:31:56 -04004847 $level .= $modifier;
4848 $level2 .= $modifier;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004849 WARN("PREFER_PR_LEVEL",
Tom Rinie199fb32021-08-03 08:31:56 -04004850 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to $printk(KERN_$orig ...\n" . $herecurr);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004851 }
4852
Tom Rinie199fb32021-08-03 08:31:56 -04004853# prefer dev_<level> to dev_printk(KERN_<LEVEL>
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004854 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4855 my $orig = $1;
4856 my $level = lc($orig);
4857 $level = "warn" if ($level eq "warning");
4858 $level = "dbg" if ($level eq "debug");
4859 WARN("PREFER_DEV_LEVEL",
4860 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4861 }
4862
Tom Rinie199fb32021-08-03 08:31:56 -04004863# trace_printk should not be used in production code.
4864 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4865 WARN("TRACE_PRINTK",
4866 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4867 }
4868
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004869# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4870# number of false positives, but assembly files are not checked, so at
4871# least the arch entry code will not trigger this warning.
4872 if ($line =~ /\bENOSYS\b/) {
4873 WARN("ENOSYS",
4874 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4875 }
4876
Tom Rinie199fb32021-08-03 08:31:56 -04004877# ENOTSUPP is not a standard error code and should be avoided in new patches.
4878# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4879# Similarly to ENOSYS warning a small number of false positives is expected.
4880 if (!$file && $line =~ /\bENOTSUPP\b/) {
4881 if (WARN("ENOTSUPP",
4882 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4883 $fix) {
4884 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4885 }
4886 }
4887
Joe Hershberger05622192011-10-18 10:06:59 +00004888# function brace can't be on same line, except for #defines of do while,
4889# or if closed on same line
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004890 if ($perl_version_ok &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004891 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4892 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4893 $sline !~ /}/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004894 if (ERROR("OPEN_BRACE",
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004895 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004896 $fix) {
4897 fix_delete_line($fixlinenr, $rawline);
4898 my $fixed_line = $rawline;
Tom Rinie199fb32021-08-03 08:31:56 -04004899 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004900 my $line1 = $1;
4901 my $line2 = $2;
4902 fix_insert_line($fixlinenr, ltrim($line1));
4903 fix_insert_line($fixlinenr, "\+{");
4904 if ($line2 !~ /^\s*$/) {
4905 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4906 }
4907 }
Joe Hershberger05622192011-10-18 10:06:59 +00004908 }
4909
4910# open braces for enum, union and struct go on the same line.
4911 if ($line =~ /^.\s*{/ &&
4912 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004913 if (ERROR("OPEN_BRACE",
4914 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4915 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4916 fix_delete_line($fixlinenr - 1, $prevrawline);
4917 fix_delete_line($fixlinenr, $rawline);
4918 my $fixedline = rtrim($prevrawline) . " {";
4919 fix_insert_line($fixlinenr, $fixedline);
4920 $fixedline = $rawline;
4921 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
4922 if ($fixedline !~ /^\+\s*$/) {
4923 fix_insert_line($fixlinenr, $fixedline);
4924 }
4925 }
Joe Hershberger05622192011-10-18 10:06:59 +00004926 }
4927
4928# missing space after union, struct or enum definition
Tom Rini6b9709d2014-02-27 08:27:28 -05004929 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4930 if (WARN("SPACING",
4931 "missing space after $1 definition\n" . $herecurr) &&
4932 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004933 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004934 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4935 }
4936 }
4937
4938# Function pointer declarations
4939# check spacing between type, funcptr, and args
4940# canonical declaration is "type (*funcptr)(args...)"
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004941 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004942 my $declare = $1;
4943 my $pre_pointer_space = $2;
4944 my $post_pointer_space = $3;
4945 my $funcname = $4;
4946 my $post_funcname_space = $5;
4947 my $pre_args_space = $6;
4948
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004949# the $Declare variable will capture all spaces after the type
4950# so check it for a missing trailing missing space but pointer return types
4951# don't need a space so don't warn for those.
4952 my $post_declare_space = "";
4953 if ($declare =~ /(\s+)$/) {
4954 $post_declare_space = $1;
4955 $declare = rtrim($declare);
4956 }
4957 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004958 WARN("SPACING",
4959 "missing space after return type\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004960 $post_declare_space = " ";
Tom Rini6b9709d2014-02-27 08:27:28 -05004961 }
4962
4963# unnecessary space "type (*funcptr)(args...)"
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004964# This test is not currently implemented because these declarations are
4965# equivalent to
4966# int foo(int bar, ...)
4967# and this is form shouldn't/doesn't generate a checkpatch warning.
4968#
4969# elsif ($declare =~ /\s{2,}$/) {
4970# WARN("SPACING",
4971# "Multiple spaces after return type\n" . $herecurr);
4972# }
Tom Rini6b9709d2014-02-27 08:27:28 -05004973
4974# unnecessary space "type ( *funcptr)(args...)"
4975 if (defined $pre_pointer_space &&
4976 $pre_pointer_space =~ /^\s/) {
4977 WARN("SPACING",
4978 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4979 }
4980
4981# unnecessary space "type (* funcptr)(args...)"
4982 if (defined $post_pointer_space &&
4983 $post_pointer_space =~ /^\s/) {
4984 WARN("SPACING",
4985 "Unnecessary space before function pointer name\n" . $herecurr);
4986 }
4987
4988# unnecessary space "type (*funcptr )(args...)"
4989 if (defined $post_funcname_space &&
4990 $post_funcname_space =~ /^\s/) {
4991 WARN("SPACING",
4992 "Unnecessary space after function pointer name\n" . $herecurr);
4993 }
4994
4995# unnecessary space "type (*funcptr) (args...)"
4996 if (defined $pre_args_space &&
4997 $pre_args_space =~ /^\s/) {
4998 WARN("SPACING",
4999 "Unnecessary space before function pointer arguments\n" . $herecurr);
5000 }
5001
5002 if (show_type("SPACING") && $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005003 $fixed[$fixlinenr] =~
5004 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Tom Rini6b9709d2014-02-27 08:27:28 -05005005 }
Joe Hershberger05622192011-10-18 10:06:59 +00005006 }
5007
5008# check for spacing round square brackets; allowed:
5009# 1. with a type on the left -- int [] a;
5010# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
5011# 3. inside a curly brace -- = { [0...10] = 5 }
5012 while ($line =~ /(.*?\s)\[/g) {
5013 my ($where, $prefix) = ($-[1], $1);
5014 if ($prefix !~ /$Type\s+$/ &&
5015 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005016 $prefix !~ /[{,:]\s+$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005017 if (ERROR("BRACKET_SPACE",
5018 "space prohibited before open square bracket '['\n" . $herecurr) &&
5019 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005020 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005021 s/^(\+.*?)\s+\[/$1\[/;
5022 }
Joe Hershberger05622192011-10-18 10:06:59 +00005023 }
5024 }
5025
5026# check for spaces between functions and their parentheses.
5027 while ($line =~ /($Ident)\s+\(/g) {
5028 my $name = $1;
5029 my $ctx_before = substr($line, 0, $-[1]);
5030 my $ctx = "$ctx_before$name";
5031
5032 # Ignore those directives where spaces _are_ permitted.
5033 if ($name =~ /^(?:
5034 if|for|while|switch|return|case|
5035 volatile|__volatile__|
5036 __attribute__|format|__extension__|
5037 asm|__asm__)$/x)
5038 {
Joe Hershberger05622192011-10-18 10:06:59 +00005039 # cpp #define statements have non-optional spaces, ie
5040 # if there is a space between the name and the open
5041 # parenthesis it is simply not a parameter group.
5042 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
5043
5044 # cpp #elif statement condition may start with a (
5045 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
5046
5047 # If this whole things ends with a type its most
5048 # likely a typedef for a function.
5049 } elsif ($ctx =~ /$Type$/) {
5050
5051 } else {
Tom Rini6b9709d2014-02-27 08:27:28 -05005052 if (WARN("SPACING",
5053 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
5054 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005055 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005056 s/\b$name\s+\(/$name\(/;
5057 }
Joe Hershberger05622192011-10-18 10:06:59 +00005058 }
5059 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005060
Joe Hershberger05622192011-10-18 10:06:59 +00005061# Check operator spacing.
5062 if (!($line=~/\#\s*include/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005063 my $fixed_line = "";
5064 my $line_fixed = 0;
5065
Joe Hershberger05622192011-10-18 10:06:59 +00005066 my $ops = qr{
5067 <<=|>>=|<=|>=|==|!=|
5068 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
5069 =>|->|<<|>>|<|>|=|!|~|
5070 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Tom Rini6b9709d2014-02-27 08:27:28 -05005071 \?:|\?|:
Joe Hershberger05622192011-10-18 10:06:59 +00005072 }x;
5073 my @elements = split(/($ops|;)/, $opline);
Tom Rini6b9709d2014-02-27 08:27:28 -05005074
5075## print("element count: <" . $#elements . ">\n");
5076## foreach my $el (@elements) {
5077## print("el: <$el>\n");
5078## }
5079
5080 my @fix_elements = ();
Joe Hershberger05622192011-10-18 10:06:59 +00005081 my $off = 0;
5082
Tom Rini6b9709d2014-02-27 08:27:28 -05005083 foreach my $el (@elements) {
5084 push(@fix_elements, substr($rawline, $off, length($el)));
5085 $off += length($el);
5086 }
5087
5088 $off = 0;
5089
Joe Hershberger05622192011-10-18 10:06:59 +00005090 my $blank = copy_spacing($opline);
Tom Rini6b9709d2014-02-27 08:27:28 -05005091 my $last_after = -1;
Joe Hershberger05622192011-10-18 10:06:59 +00005092
5093 for (my $n = 0; $n < $#elements; $n += 2) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005094
5095 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
5096
5097## print("n: <$n> good: <$good>\n");
5098
Joe Hershberger05622192011-10-18 10:06:59 +00005099 $off += length($elements[$n]);
5100
5101 # Pick up the preceding and succeeding characters.
5102 my $ca = substr($opline, 0, $off);
5103 my $cc = '';
5104 if (length($opline) >= ($off + length($elements[$n + 1]))) {
5105 $cc = substr($opline, $off + length($elements[$n + 1]));
5106 }
5107 my $cb = "$ca$;$cc";
5108
5109 my $a = '';
5110 $a = 'V' if ($elements[$n] ne '');
5111 $a = 'W' if ($elements[$n] =~ /\s$/);
5112 $a = 'C' if ($elements[$n] =~ /$;$/);
5113 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
5114 $a = 'O' if ($elements[$n] eq '');
5115 $a = 'E' if ($ca =~ /^\s*$/);
5116
5117 my $op = $elements[$n + 1];
5118
5119 my $c = '';
5120 if (defined $elements[$n + 2]) {
5121 $c = 'V' if ($elements[$n + 2] ne '');
5122 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
5123 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
5124 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
5125 $c = 'O' if ($elements[$n + 2] eq '');
5126 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
5127 } else {
5128 $c = 'E';
5129 }
5130
5131 my $ctx = "${a}x${c}";
5132
5133 my $at = "(ctx:$ctx)";
5134
5135 my $ptr = substr($blank, 0, $off) . "^";
5136 my $hereptr = "$hereline$ptr\n";
5137
5138 # Pull out the value of this operator.
5139 my $op_type = substr($curr_values, $off + 1, 1);
5140
5141 # Get the full operator variant.
5142 my $opv = $op . substr($curr_vars, $off, 1);
5143
5144 # Ignore operators passed as parameters.
5145 if ($op_type ne 'V' &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005146 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Joe Hershberger05622192011-10-18 10:06:59 +00005147
5148# # Ignore comments
5149# } elsif ($op =~ /^$;+$/) {
5150
5151 # ; should have either the end of line or a space or \ after it
5152 } elsif ($op eq ';') {
5153 if ($ctx !~ /.x[WEBC]/ &&
5154 $cc !~ /^\\/ && $cc !~ /^;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005155 if (ERROR("SPACING",
5156 "space required after that '$op' $at\n" . $hereptr)) {
5157 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
5158 $line_fixed = 1;
5159 }
Joe Hershberger05622192011-10-18 10:06:59 +00005160 }
5161
5162 # // is a comment
5163 } elsif ($op eq '//') {
5164
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005165 # : when part of a bitfield
5166 } elsif ($opv eq ':B') {
5167 # skip the bitfield test for now
5168
Joe Hershberger05622192011-10-18 10:06:59 +00005169 # No spaces for:
5170 # ->
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005171 } elsif ($op eq '->') {
Joe Hershberger05622192011-10-18 10:06:59 +00005172 if ($ctx =~ /Wx.|.xW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005173 if (ERROR("SPACING",
5174 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
5175 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
5176 if (defined $fix_elements[$n + 2]) {
5177 $fix_elements[$n + 2] =~ s/^\s+//;
5178 }
5179 $line_fixed = 1;
5180 }
Joe Hershberger05622192011-10-18 10:06:59 +00005181 }
5182
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005183 # , must not have a space before and must have a space on the right.
Joe Hershberger05622192011-10-18 10:06:59 +00005184 } elsif ($op eq ',') {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005185 my $rtrim_before = 0;
5186 my $space_after = 0;
5187 if ($ctx =~ /Wx./) {
5188 if (ERROR("SPACING",
5189 "space prohibited before that '$op' $at\n" . $hereptr)) {
5190 $line_fixed = 1;
5191 $rtrim_before = 1;
5192 }
5193 }
Joe Hershberger05622192011-10-18 10:06:59 +00005194 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005195 if (ERROR("SPACING",
5196 "space required after that '$op' $at\n" . $hereptr)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005197 $line_fixed = 1;
5198 $last_after = $n;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005199 $space_after = 1;
5200 }
5201 }
5202 if ($rtrim_before || $space_after) {
5203 if ($rtrim_before) {
5204 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
5205 } else {
5206 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
5207 }
5208 if ($space_after) {
5209 $good .= " ";
Tom Rini6b9709d2014-02-27 08:27:28 -05005210 }
Joe Hershberger05622192011-10-18 10:06:59 +00005211 }
5212
5213 # '*' as part of a type definition -- reported already.
5214 } elsif ($opv eq '*_') {
5215 #warn "'*' is part of type\n";
5216
5217 # unary operators should have a space before and
5218 # none after. May be left adjacent to another
5219 # unary operator, or a cast
5220 } elsif ($op eq '!' || $op eq '~' ||
5221 $opv eq '*U' || $opv eq '-U' ||
5222 $opv eq '&U' || $opv eq '&&U') {
5223 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005224 if (ERROR("SPACING",
5225 "space required before that '$op' $at\n" . $hereptr)) {
5226 if ($n != $last_after + 2) {
5227 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
5228 $line_fixed = 1;
5229 }
5230 }
Joe Hershberger05622192011-10-18 10:06:59 +00005231 }
5232 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
5233 # A unary '*' may be const
5234
5235 } elsif ($ctx =~ /.xW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005236 if (ERROR("SPACING",
5237 "space prohibited after that '$op' $at\n" . $hereptr)) {
5238 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
5239 if (defined $fix_elements[$n + 2]) {
5240 $fix_elements[$n + 2] =~ s/^\s+//;
5241 }
5242 $line_fixed = 1;
5243 }
Joe Hershberger05622192011-10-18 10:06:59 +00005244 }
5245
5246 # unary ++ and unary -- are allowed no space on one side.
5247 } elsif ($op eq '++' or $op eq '--') {
5248 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005249 if (ERROR("SPACING",
5250 "space required one side of that '$op' $at\n" . $hereptr)) {
5251 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
5252 $line_fixed = 1;
5253 }
Joe Hershberger05622192011-10-18 10:06:59 +00005254 }
5255 if ($ctx =~ /Wx[BE]/ ||
5256 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005257 if (ERROR("SPACING",
5258 "space prohibited before that '$op' $at\n" . $hereptr)) {
5259 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
5260 $line_fixed = 1;
5261 }
Joe Hershberger05622192011-10-18 10:06:59 +00005262 }
5263 if ($ctx =~ /ExW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005264 if (ERROR("SPACING",
5265 "space prohibited after that '$op' $at\n" . $hereptr)) {
5266 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
5267 if (defined $fix_elements[$n + 2]) {
5268 $fix_elements[$n + 2] =~ s/^\s+//;
5269 }
5270 $line_fixed = 1;
5271 }
Joe Hershberger05622192011-10-18 10:06:59 +00005272 }
5273
Joe Hershberger05622192011-10-18 10:06:59 +00005274 # << and >> may either have or not have spaces both sides
5275 } elsif ($op eq '<<' or $op eq '>>' or
5276 $op eq '&' or $op eq '^' or $op eq '|' or
5277 $op eq '+' or $op eq '-' or
5278 $op eq '*' or $op eq '/' or
5279 $op eq '%')
5280 {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005281 if ($check) {
5282 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
5283 if (CHK("SPACING",
5284 "spaces preferred around that '$op' $at\n" . $hereptr)) {
5285 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5286 $fix_elements[$n + 2] =~ s/^\s+//;
5287 $line_fixed = 1;
5288 }
5289 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
5290 if (CHK("SPACING",
5291 "space preferred before that '$op' $at\n" . $hereptr)) {
5292 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
5293 $line_fixed = 1;
5294 }
5295 }
5296 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005297 if (ERROR("SPACING",
5298 "need consistent spacing around '$op' $at\n" . $hereptr)) {
5299 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5300 if (defined $fix_elements[$n + 2]) {
5301 $fix_elements[$n + 2] =~ s/^\s+//;
5302 }
5303 $line_fixed = 1;
5304 }
Joe Hershberger05622192011-10-18 10:06:59 +00005305 }
5306
5307 # A colon needs no spaces before when it is
5308 # terminating a case value or a label.
5309 } elsif ($opv eq ':C' || $opv eq ':L') {
Tom Rinie199fb32021-08-03 08:31:56 -04005310 if ($ctx =~ /Wx./ and $realfile !~ m@.*\.lds\.h$@) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005311 if (ERROR("SPACING",
5312 "space prohibited before that '$op' $at\n" . $hereptr)) {
5313 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
5314 $line_fixed = 1;
5315 }
Joe Hershberger05622192011-10-18 10:06:59 +00005316 }
5317
5318 # All the others need spaces both sides.
5319 } elsif ($ctx !~ /[EWC]x[CWE]/) {
5320 my $ok = 0;
5321
5322 # Ignore email addresses <foo@bar>
5323 if (($op eq '<' &&
5324 $cc =~ /^\S+\@\S+>/) ||
5325 ($op eq '>' &&
5326 $ca =~ /<\S+\@\S+$/))
5327 {
Tom Rinic57383b2020-06-16 10:29:46 -04005328 $ok = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00005329 }
5330
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005331 # for asm volatile statements
5332 # ignore a colon with another
5333 # colon immediately before or after
5334 if (($op eq ':') &&
5335 ($ca =~ /:$/ || $cc =~ /^:/)) {
5336 $ok = 1;
5337 }
5338
Tom Rini6b9709d2014-02-27 08:27:28 -05005339 # messages are ERROR, but ?: are CHK
Joe Hershberger05622192011-10-18 10:06:59 +00005340 if ($ok == 0) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005341 my $msg_level = \&ERROR;
5342 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Tom Rini6b9709d2014-02-27 08:27:28 -05005343
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005344 if (&{$msg_level}("SPACING",
5345 "spaces required around that '$op' $at\n" . $hereptr)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005346 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5347 if (defined $fix_elements[$n + 2]) {
5348 $fix_elements[$n + 2] =~ s/^\s+//;
5349 }
5350 $line_fixed = 1;
5351 }
Joe Hershberger05622192011-10-18 10:06:59 +00005352 }
5353 }
5354 $off += length($elements[$n + 1]);
Tom Rini6b9709d2014-02-27 08:27:28 -05005355
5356## print("n: <$n> GOOD: <$good>\n");
5357
5358 $fixed_line = $fixed_line . $good;
5359 }
5360
5361 if (($#elements % 2) == 0) {
5362 $fixed_line = $fixed_line . $fix_elements[$#elements];
5363 }
5364
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005365 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5366 $fixed[$fixlinenr] = $fixed_line;
Tom Rini6b9709d2014-02-27 08:27:28 -05005367 }
5368
5369
5370 }
5371
5372# check for whitespace before a non-naked semicolon
5373 if ($line =~ /^\+.*\S\s+;\s*$/) {
5374 if (WARN("SPACING",
5375 "space prohibited before semicolon\n" . $herecurr) &&
5376 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005377 1 while $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005378 s/^(\+.*\S)\s+;/$1;/;
Joe Hershberger05622192011-10-18 10:06:59 +00005379 }
5380 }
5381
5382# check for multiple assignments
5383 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
5384 CHK("MULTIPLE_ASSIGNMENTS",
5385 "multiple assignments should be avoided\n" . $herecurr);
5386 }
5387
5388## # check for multiple declarations, allowing for a function declaration
5389## # continuation.
5390## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
5391## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
5392##
5393## # Remove any bracketed sections to ensure we do not
Tom Rinie199fb32021-08-03 08:31:56 -04005394## # falsely report the parameters of functions.
Joe Hershberger05622192011-10-18 10:06:59 +00005395## my $ln = $line;
5396## while ($ln =~ s/\([^\(\)]*\)//g) {
5397## }
5398## if ($ln =~ /,/) {
5399## WARN("MULTIPLE_DECLARATION",
5400## "declaring multiple variables together should be avoided\n" . $herecurr);
5401## }
5402## }
5403
5404#need space before brace following if, while, etc
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005405 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005406 $line =~ /\b(?:else|do)\{/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005407 if (ERROR("SPACING",
5408 "space required before the open brace '{'\n" . $herecurr) &&
5409 $fix) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005410 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Tom Rini6b9709d2014-02-27 08:27:28 -05005411 }
Joe Hershberger05622192011-10-18 10:06:59 +00005412 }
5413
Tom Rini6b9709d2014-02-27 08:27:28 -05005414## # check for blank lines before declarations
5415## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5416## $prevrawline =~ /^.\s*$/) {
5417## WARN("SPACING",
5418## "No blank lines before declarations\n" . $hereprev);
5419## }
5420##
5421
Joe Hershberger05622192011-10-18 10:06:59 +00005422# closing brace should have a space following it when it has anything
5423# on the line
Tom Rinic57383b2020-06-16 10:29:46 -04005424 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005425 if (ERROR("SPACING",
5426 "space required after that close brace '}'\n" . $herecurr) &&
5427 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005428 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005429 s/}((?!(?:,|;|\)))\S)/} $1/;
5430 }
Joe Hershberger05622192011-10-18 10:06:59 +00005431 }
5432
5433# check spacing on square brackets
5434 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005435 if (ERROR("SPACING",
5436 "space prohibited after that open square bracket '['\n" . $herecurr) &&
5437 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005438 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005439 s/\[\s+/\[/;
5440 }
Joe Hershberger05622192011-10-18 10:06:59 +00005441 }
5442 if ($line =~ /\s\]/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005443 if (ERROR("SPACING",
5444 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
5445 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005446 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005447 s/\s+\]/\]/;
5448 }
Joe Hershberger05622192011-10-18 10:06:59 +00005449 }
5450
5451# check spacing on parentheses
5452 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
5453 $line !~ /for\s*\(\s+;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005454 if (ERROR("SPACING",
5455 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
5456 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005457 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005458 s/\(\s+/\(/;
5459 }
Joe Hershberger05622192011-10-18 10:06:59 +00005460 }
5461 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
5462 $line !~ /for\s*\(.*;\s+\)/ &&
5463 $line !~ /:\s+\)/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005464 if (ERROR("SPACING",
5465 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
5466 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005467 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005468 s/\s+\)/\)/;
5469 }
Joe Hershberger05622192011-10-18 10:06:59 +00005470 }
5471
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005472# check unnecessary parentheses around addressof/dereference single $Lvals
5473# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5474
5475 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
5476 my $var = $1;
5477 if (CHK("UNNECESSARY_PARENTHESES",
5478 "Unnecessary parentheses around $var\n" . $herecurr) &&
5479 $fix) {
5480 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5481 }
5482 }
5483
5484# check for unnecessary parentheses around function pointer uses
5485# ie: (foo->bar)(); should be foo->bar();
5486# but not "if (foo->bar) (" to avoid some false positives
5487 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5488 my $var = $2;
5489 if (CHK("UNNECESSARY_PARENTHESES",
5490 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5491 $fix) {
5492 my $var2 = deparenthesize($var);
5493 $var2 =~ s/\s//g;
5494 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5495 }
5496 }
5497
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005498# check for unnecessary parentheses around comparisons in if uses
5499# when !drivers/staging or command-line uses --strict
5500 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005501 $perl_version_ok && defined($stat) &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005502 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
5503 my $if_stat = $1;
5504 my $test = substr($2, 1, -1);
5505 my $herectx;
5506 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
5507 my $match = $1;
5508 # avoid parentheses around potential macro args
5509 next if ($match =~ /^\s*\w+\s*$/);
5510 if (!defined($herectx)) {
5511 $herectx = $here . "\n";
5512 my $cnt = statement_rawlines($if_stat);
5513 for (my $n = 0; $n < $cnt; $n++) {
5514 my $rl = raw_line($linenr, $n);
5515 $herectx .= $rl . "\n";
5516 last if $rl =~ /^[ \+].*\{/;
5517 }
5518 }
5519 CHK("UNNECESSARY_PARENTHESES",
5520 "Unnecessary parentheses around '$match'\n" . $herectx);
5521 }
5522 }
5523
Simon Glass587254e2022-01-23 12:55:11 -07005524# check that goto labels aren't indented (allow a single space indentation)
5525# and ignore bitfield definitions like foo:1
5526# Strictly, labels can have whitespace after the identifier and before the :
5527# but this is not allowed here as many ?: uses would appear to be labels
5528 if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ &&
5529 $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ &&
5530 $sline !~ /^.\s+default:/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005531 if (WARN("INDENTED_LABEL",
5532 "labels should not be indented\n" . $herecurr) &&
5533 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005534 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005535 s/^(.)\s+/$1/;
5536 }
Joe Hershberger05622192011-10-18 10:06:59 +00005537 }
5538
Tom Rinie199fb32021-08-03 08:31:56 -04005539# check if a statement with a comma should be two statements like:
5540# foo = bar(), /* comma should be semicolon */
5541# bar = baz();
5542 if (defined($stat) &&
5543 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5544 my $cnt = statement_rawlines($stat);
5545 my $herectx = get_stat_here($linenr, $cnt, $here);
5546 WARN("SUSPECT_COMMA_SEMICOLON",
5547 "Possible comma where semicolon could be used\n" . $herectx);
5548 }
5549
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005550# return is not a function
Tom Rini6b9709d2014-02-27 08:27:28 -05005551 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Joe Hershberger05622192011-10-18 10:06:59 +00005552 my $spacing = $1;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005553 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005554 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5555 my $value = $1;
5556 $value = deparenthesize($value);
5557 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5558 ERROR("RETURN_PARENTHESES",
5559 "return is not a function, parentheses are not required\n" . $herecurr);
5560 }
Joe Hershberger05622192011-10-18 10:06:59 +00005561 } elsif ($spacing !~ /\s+/) {
5562 ERROR("SPACING",
5563 "space required before the open parenthesis '('\n" . $herecurr);
5564 }
5565 }
Tom Rini6b9709d2014-02-27 08:27:28 -05005566
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005567# unnecessary return in a void function
5568# at end-of-function, with the previous line a single leading tab, then return;
5569# and the line before that not a goto label target like "out:"
5570 if ($sline =~ /^[ \+]}\s*$/ &&
5571 $prevline =~ /^\+\treturn\s*;\s*$/ &&
5572 $linenr >= 3 &&
5573 $lines[$linenr - 3] =~ /^[ +]/ &&
5574 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
5575 WARN("RETURN_VOID",
5576 "void function return statements are not generally useful\n" . $hereprev);
Tom Rinie199fb32021-08-03 08:31:56 -04005577 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005578
Tom Rini6b9709d2014-02-27 08:27:28 -05005579# if statements using unnecessary parentheses - ie: if ((foo == bar))
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005580 if ($perl_version_ok &&
Tom Rini6b9709d2014-02-27 08:27:28 -05005581 $line =~ /\bif\s*((?:\(\s*){2,})/) {
5582 my $openparens = $1;
5583 my $count = $openparens =~ tr@\(@\(@;
5584 my $msg = "";
5585 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5586 my $comp = $4; #Not $1 because of $LvalOrFunc
5587 $msg = " - maybe == should be = ?" if ($comp eq "==");
5588 WARN("UNNECESSARY_PARENTHESES",
5589 "Unnecessary parentheses$msg\n" . $herecurr);
5590 }
5591 }
5592
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005593# comparisons with a constant or upper case identifier on the left
5594# avoid cases like "foo + BAR < baz"
5595# only fix matches surrounded by parentheses to avoid incorrect
5596# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005597 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005598 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5599 my $lead = $1;
5600 my $const = $2;
5601 my $comp = $3;
5602 my $to = $4;
5603 my $newcomp = $comp;
5604 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
5605 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5606 WARN("CONSTANT_COMPARISON",
5607 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5608 $fix) {
5609 if ($comp eq "<") {
5610 $newcomp = ">";
5611 } elsif ($comp eq "<=") {
5612 $newcomp = ">=";
5613 } elsif ($comp eq ">") {
5614 $newcomp = "<";
5615 } elsif ($comp eq ">=") {
5616 $newcomp = "<=";
5617 }
5618 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5619 }
5620 }
5621
5622# Return of what appears to be an errno should normally be negative
5623 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Joe Hershberger05622192011-10-18 10:06:59 +00005624 my $name = $1;
Simon Glass587254e2022-01-23 12:55:11 -07005625 if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) {
Joe Hershberger05622192011-10-18 10:06:59 +00005626 WARN("USE_NEGATIVE_ERRNO",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005627 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005628 }
5629 }
5630
Joe Hershberger05622192011-10-18 10:06:59 +00005631# Need a space before open parenthesis after if, while etc
Tom Rini6b9709d2014-02-27 08:27:28 -05005632 if ($line =~ /\b(if|while|for|switch)\(/) {
5633 if (ERROR("SPACING",
5634 "space required before the open parenthesis '('\n" . $herecurr) &&
5635 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005636 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005637 s/\b(if|while|for|switch)\(/$1 \(/;
5638 }
Joe Hershberger05622192011-10-18 10:06:59 +00005639 }
5640
5641# Check for illegal assignment in if conditional -- and check for trailing
5642# statements after the conditional.
5643 if ($line =~ /do\s*(?!{)/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005644 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5645 ctx_statement_block($linenr, $realcnt, 0)
5646 if (!defined $stat);
Joe Hershberger05622192011-10-18 10:06:59 +00005647 my ($stat_next) = ctx_statement_block($line_nr_next,
5648 $remain_next, $off_next);
5649 $stat_next =~ s/\n./\n /g;
5650 ##print "stat<$stat> stat_next<$stat_next>\n";
5651
5652 if ($stat_next =~ /^\s*while\b/) {
5653 # If the statement carries leading newlines,
5654 # then count those as offsets.
5655 my ($whitespace) =
5656 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5657 my $offset =
5658 statement_rawlines($whitespace) - 1;
5659
5660 $suppress_whiletrailers{$line_nr_next +
5661 $offset} = 1;
5662 }
5663 }
5664 if (!defined $suppress_whiletrailers{$linenr} &&
Tom Rini6b9709d2014-02-27 08:27:28 -05005665 defined($stat) && defined($cond) &&
Joe Hershberger05622192011-10-18 10:06:59 +00005666 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5667 my ($s, $c) = ($stat, $cond);
5668
5669 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Tom Rinie199fb32021-08-03 08:31:56 -04005670 if (ERROR("ASSIGN_IN_IF",
5671 "do not use assignment in if condition\n" . $herecurr) &&
5672 $fix && $perl_version_ok) {
5673 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5674 my $space = $1;
5675 my $not = $2;
5676 my $statement = $3;
5677 my $assigned = $4;
5678 my $test = $8;
5679 my $against = $9;
5680 my $brace = $15;
5681 fix_delete_line($fixlinenr, $rawline);
5682 fix_insert_line($fixlinenr, "$space$statement;");
5683 my $newline = "${space}if (";
5684 $newline .= '!' if defined($not);
5685 $newline .= '(' if (defined $not && defined($test) && defined($against));
5686 $newline .= "$assigned";
5687 $newline .= " $test $against" if (defined($test) && defined($against));
5688 $newline .= ')' if (defined $not && defined($test) && defined($against));
5689 $newline .= ')';
5690 $newline .= " {" if (defined($brace));
5691 fix_insert_line($fixlinenr + 1, $newline);
5692 }
5693 }
Joe Hershberger05622192011-10-18 10:06:59 +00005694 }
5695
5696 # Find out what is on the end of the line after the
5697 # conditional.
5698 substr($s, 0, length($c), '');
5699 $s =~ s/\n.*//g;
Tom Rinic57383b2020-06-16 10:29:46 -04005700 $s =~ s/$;//g; # Remove any comments
Joe Hershberger05622192011-10-18 10:06:59 +00005701 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5702 $c !~ /}\s*while\s*/)
5703 {
5704 # Find out how long the conditional actually is.
5705 my @newlines = ($c =~ /\n/gs);
5706 my $cond_lines = 1 + $#newlines;
5707 my $stat_real = '';
5708
5709 $stat_real = raw_line($linenr, $cond_lines)
5710 . "\n" if ($cond_lines);
5711 if (defined($stat_real) && $cond_lines > 1) {
5712 $stat_real = "[...]\n$stat_real";
5713 }
5714
5715 ERROR("TRAILING_STATEMENTS",
5716 "trailing statements should be on next line\n" . $herecurr . $stat_real);
5717 }
5718 }
5719
5720# Check for bitwise tests written as boolean
5721 if ($line =~ /
5722 (?:
5723 (?:\[|\(|\&\&|\|\|)
5724 \s*0[xX][0-9]+\s*
5725 (?:\&\&|\|\|)
5726 |
5727 (?:\&\&|\|\|)
5728 \s*0[xX][0-9]+\s*
5729 (?:\&\&|\|\||\)|\])
5730 )/x)
5731 {
5732 WARN("HEXADECIMAL_BOOLEAN_TEST",
5733 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
5734 }
5735
5736# if and else should not have general statements after it
5737 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5738 my $s = $1;
Tom Rinic57383b2020-06-16 10:29:46 -04005739 $s =~ s/$;//g; # Remove any comments
Joe Hershberger05622192011-10-18 10:06:59 +00005740 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5741 ERROR("TRAILING_STATEMENTS",
5742 "trailing statements should be on next line\n" . $herecurr);
5743 }
5744 }
5745# if should not continue a brace
5746 if ($line =~ /}\s*if\b/) {
5747 ERROR("TRAILING_STATEMENTS",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005748 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Joe Hershberger05622192011-10-18 10:06:59 +00005749 $herecurr);
5750 }
5751# case and default should not have general statements after them
5752 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5753 $line !~ /\G(?:
5754 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5755 \s*return\s+
5756 )/xg)
5757 {
5758 ERROR("TRAILING_STATEMENTS",
5759 "trailing statements should be on next line\n" . $herecurr);
5760 }
5761
5762 # Check for }<nl>else {, these must be at the same
5763 # indent level to be relevant to each other.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005764 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5765 $previndent == $indent) {
5766 if (ERROR("ELSE_AFTER_BRACE",
5767 "else should follow close brace '}'\n" . $hereprev) &&
5768 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5769 fix_delete_line($fixlinenr - 1, $prevrawline);
5770 fix_delete_line($fixlinenr, $rawline);
5771 my $fixedline = $prevrawline;
5772 $fixedline =~ s/}\s*$//;
5773 if ($fixedline !~ /^\+\s*$/) {
5774 fix_insert_line($fixlinenr, $fixedline);
5775 }
5776 $fixedline = $rawline;
5777 $fixedline =~ s/^(.\s*)else/$1} else/;
5778 fix_insert_line($fixlinenr, $fixedline);
5779 }
Joe Hershberger05622192011-10-18 10:06:59 +00005780 }
5781
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005782 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5783 $previndent == $indent) {
Joe Hershberger05622192011-10-18 10:06:59 +00005784 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5785
5786 # Find out what is on the end of the line after the
5787 # conditional.
5788 substr($s, 0, length($c), '');
5789 $s =~ s/\n.*//g;
5790
5791 if ($s =~ /^\s*;/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005792 if (ERROR("WHILE_AFTER_BRACE",
5793 "while should follow close brace '}'\n" . $hereprev) &&
5794 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5795 fix_delete_line($fixlinenr - 1, $prevrawline);
5796 fix_delete_line($fixlinenr, $rawline);
5797 my $fixedline = $prevrawline;
5798 my $trailing = $rawline;
5799 $trailing =~ s/^\+//;
5800 $trailing = trim($trailing);
5801 $fixedline =~ s/}\s*$/} $trailing/;
5802 fix_insert_line($fixlinenr, $fixedline);
5803 }
Joe Hershberger05622192011-10-18 10:06:59 +00005804 }
5805 }
5806
Tom Rini6b9709d2014-02-27 08:27:28 -05005807#Specific variable tests
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005808 while ($line =~ m{($Constant|$Lval)}g) {
5809 my $var = $1;
Tom Rini6b9709d2014-02-27 08:27:28 -05005810
Tom Rini6b9709d2014-02-27 08:27:28 -05005811#CamelCase
5812 if ($var !~ /^$Constant$/ &&
5813 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Tom Rinie199fb32021-08-03 08:31:56 -04005814#Ignore some autogenerated defines and enum values
5815 $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
Tom Rini6b9709d2014-02-27 08:27:28 -05005816#Ignore Page<foo> variants
5817 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Tom Rinic57383b2020-06-16 10:29:46 -04005818#Ignore SI style variants like nS, mV and dB
5819#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5820 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005821#Ignore some three character SI units explicitly, like MiB and KHz
5822 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005823 while ($var =~ m{($Ident)}g) {
5824 my $word = $1;
5825 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5826 if ($check) {
5827 seed_camelcase_includes();
5828 if (!$file && !$camelcase_file_seeded) {
5829 seed_camelcase_file($realfile);
5830 $camelcase_file_seeded = 1;
5831 }
5832 }
5833 if (!defined $camelcase{$word}) {
5834 $camelcase{$word} = 1;
5835 CHK("CAMELCASE",
5836 "Avoid CamelCase: <$word>\n" . $herecurr);
5837 }
5838 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005839 }
5840 }
Joe Hershberger05622192011-10-18 10:06:59 +00005841
5842#no spaces allowed after \ in define
Tom Rini6b9709d2014-02-27 08:27:28 -05005843 if ($line =~ /\#\s*define.*\\\s+$/) {
5844 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5845 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5846 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005847 $fixed[$fixlinenr] =~ s/\s+$//;
Tom Rini6b9709d2014-02-27 08:27:28 -05005848 }
Joe Hershberger05622192011-10-18 10:06:59 +00005849 }
5850
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005851# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5852# itself <asm/foo.h> (uses RAW line)
Joe Hershberger05622192011-10-18 10:06:59 +00005853 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5854 my $file = "$1.h";
5855 my $checkfile = "include/linux/$file";
5856 if (-f "$root/$checkfile" &&
5857 $realfile ne $checkfile &&
5858 $1 !~ /$allowed_asm_includes/)
5859 {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005860 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5861 if ($asminclude > 0) {
5862 if ($realfile =~ m{^arch/}) {
5863 CHK("ARCH_INCLUDE_LINUX",
5864 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5865 } else {
5866 WARN("INCLUDE_LINUX",
5867 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5868 }
Joe Hershberger05622192011-10-18 10:06:59 +00005869 }
5870 }
5871 }
5872
5873# multi-statement macros should be enclosed in a do while loop, grab the
5874# first statement and ensure its the whole macro if its not enclosed
5875# in a known good container
5876 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5877 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5878 my $ln = $linenr;
5879 my $cnt = $realcnt;
5880 my ($off, $dstat, $dcond, $rest);
5881 my $ctx = '';
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005882 my $has_flow_statement = 0;
5883 my $has_arg_concat = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00005884 ($dstat, $dcond, $ln, $cnt, $off) =
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005885 ctx_statement_block($linenr, $realcnt, 0);
5886 $ctx = $dstat;
Joe Hershberger05622192011-10-18 10:06:59 +00005887 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5888 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5889
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005890 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
5891 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
5892
5893 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5894 my $define_args = $1;
5895 my $define_stmt = $dstat;
5896 my @def_args = ();
5897
5898 if (defined $define_args && $define_args ne "") {
5899 $define_args = substr($define_args, 1, length($define_args) - 2);
5900 $define_args =~ s/\s*//g;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005901 $define_args =~ s/\\\+?//g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005902 @def_args = split(",", $define_args);
5903 }
5904
Joe Hershberger05622192011-10-18 10:06:59 +00005905 $dstat =~ s/$;//g;
5906 $dstat =~ s/\\\n.//g;
5907 $dstat =~ s/^\s*//s;
5908 $dstat =~ s/\s*$//s;
5909
5910 # Flatten any parentheses and braces
Tom Rinie199fb32021-08-03 08:31:56 -04005911 while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5912 $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5913 $dstat =~ s/.\[[^\[\]]*\]/1u/)
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005914 {
5915 }
5916
Tom Rinic57383b2020-06-16 10:29:46 -04005917 # Flatten any obvious string concatenation.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005918 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5919 $dstat =~ s/$Ident\s*($String)/$1/)
Joe Hershberger05622192011-10-18 10:06:59 +00005920 {
5921 }
5922
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005923 # Make asm volatile uses seem like a generic function
5924 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5925
Joe Hershberger05622192011-10-18 10:06:59 +00005926 my $exceptions = qr{
5927 $Declare|
5928 module_param_named|
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005929 MODULE_PARM_DESC|
Joe Hershberger05622192011-10-18 10:06:59 +00005930 DECLARE_PER_CPU|
5931 DEFINE_PER_CPU|
5932 __typeof__\(|
5933 union|
5934 struct|
5935 \.$Ident\s*=\s*|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005936 ^\"|\"$|
5937 ^\[
Joe Hershberger05622192011-10-18 10:06:59 +00005938 }x;
5939 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005940
5941 $ctx =~ s/\n*$//;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005942 my $stmt_cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005943 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005944
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005945 if ($dstat ne '' &&
5946 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5947 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Tom Rini6b9709d2014-02-27 08:27:28 -05005948 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005949 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005950 $dstat !~ /$exceptions/ &&
5951 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Tom Rini6b9709d2014-02-27 08:27:28 -05005952 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005953 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Tom Rinie199fb32021-08-03 08:31:56 -04005954 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...}
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005955 $dstat !~ /^for\s*$Constant$/ && # for (...)
5956 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5957 $dstat !~ /^do\s*{/ && # do {...
Heiko Schocherd8a1a302016-01-04 09:17:19 +01005958 $dstat !~ /^\(\{/ && # ({...
Tom Rini6b9709d2014-02-27 08:27:28 -05005959 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005960 {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005961 if ($dstat =~ /^\s*if\b/) {
5962 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5963 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5964 } elsif ($dstat =~ /;/) {
5965 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5966 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5967 } else {
5968 ERROR("COMPLEX_MACRO",
5969 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5970 }
5971
5972 }
5973
5974 # Make $define_stmt single line, comment-free, etc
5975 my @stmt_array = split('\n', $define_stmt);
5976 my $first = 1;
5977 $define_stmt = "";
5978 foreach my $l (@stmt_array) {
5979 $l =~ s/\\$//;
5980 if ($first) {
5981 $define_stmt = $l;
5982 $first = 0;
5983 } elsif ($l =~ /^[\+ ]/) {
5984 $define_stmt .= substr($l, 1);
5985 }
5986 }
5987 $define_stmt =~ s/$;//g;
5988 $define_stmt =~ s/\s+/ /g;
5989 $define_stmt = trim($define_stmt);
5990
5991# check if any macro arguments are reused (ignore '...' and 'type')
5992 foreach my $arg (@def_args) {
5993 next if ($arg =~ /\.\.\./);
5994 next if ($arg =~ /^type$/i);
5995 my $tmp_stmt = $define_stmt;
Tom Rinie199fb32021-08-03 08:31:56 -04005996 $tmp_stmt =~ s/\b(__must_be_array|offsetof|sizeof|sizeof_field|__stringify|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005997 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5998 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005999 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006000 if ($use_cnt > 1) {
6001 CHK("MACRO_ARG_REUSE",
6002 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
6003 }
6004# check if any macro arguments may have other precedence issues
6005 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
6006 ((defined($1) && $1 ne ',') ||
6007 (defined($2) && $2 ne ','))) {
6008 CHK("MACRO_ARG_PRECEDENCE",
6009 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
6010 }
6011 }
6012
6013# check for macros with flow control, but without ## concatenation
6014# ## concatenation is commonly a macro that defines a function so ignore those
6015 if ($has_flow_statement && !$has_arg_concat) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006016 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006017 my $herectx = get_stat_here($linenr, $cnt, $here);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006018
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006019 WARN("MACRO_WITH_FLOW_CONTROL",
6020 "Macros with flow control statements should be avoided\n" . "$herectx");
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006021 }
6022
6023# check for line continuations outside of #defines, preprocessor #, and asm
6024
6025 } else {
6026 if ($prevline !~ /^..*\\$/ &&
6027 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
6028 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
6029 $line =~ /^\+.*\\$/) {
6030 WARN("LINE_CONTINUATIONS",
6031 "Avoid unnecessary line continuations\n" . $herecurr);
6032 }
6033 }
6034
6035# do {} while (0) macro tests:
6036# single-statement macros do not need to be enclosed in do while (0) loop,
6037# macro should not end with a semicolon
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006038 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006039 $realfile !~ m@/vmlinux.lds.h$@ &&
6040 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
6041 my $ln = $linenr;
6042 my $cnt = $realcnt;
6043 my ($off, $dstat, $dcond, $rest);
6044 my $ctx = '';
6045 ($dstat, $dcond, $ln, $cnt, $off) =
6046 ctx_statement_block($linenr, $realcnt, 0);
6047 $ctx = $dstat;
6048
6049 $dstat =~ s/\\\n.//g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006050 $dstat =~ s/$;/ /g;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006051
6052 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
6053 my $stmts = $2;
6054 my $semis = $3;
6055
6056 $ctx =~ s/\n*$//;
6057 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006058 my $herectx = get_stat_here($linenr, $cnt, $here);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006059
6060 if (($stmts =~ tr/;/;/) == 1 &&
6061 $stmts !~ /^\s*(if|while|for|switch)\b/) {
6062 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
6063 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
6064 }
6065 if (defined $semis && $semis ne "") {
6066 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
6067 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
Joe Hershberger05622192011-10-18 10:06:59 +00006068 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006069 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
6070 $ctx =~ s/\n*$//;
6071 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006072 my $herectx = get_stat_here($linenr, $cnt, $here);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006073
6074 WARN("TRAILING_SEMICOLON",
6075 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Hershberger05622192011-10-18 10:06:59 +00006076 }
6077 }
6078
Joe Hershberger05622192011-10-18 10:06:59 +00006079# check for redundant bracing round if etc
6080 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
6081 my ($level, $endln, @chunks) =
6082 ctx_statement_full($linenr, $realcnt, 1);
6083 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
6084 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
6085 if ($#chunks > 0 && $level == 0) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006086 my @allowed = ();
6087 my $allow = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00006088 my $seen = 0;
6089 my $herectx = $here . "\n";
6090 my $ln = $linenr - 1;
6091 for my $chunk (@chunks) {
6092 my ($cond, $block) = @{$chunk};
6093
6094 # If the condition carries leading newlines, then count those as offsets.
6095 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
6096 my $offset = statement_rawlines($whitespace) - 1;
6097
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006098 $allowed[$allow] = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00006099 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
6100
6101 # We have looked at and allowed this specific line.
6102 $suppress_ifbraces{$ln + $offset} = 1;
6103
6104 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
6105 $ln += statement_rawlines($block) - 1;
6106
6107 substr($block, 0, length($cond), '');
6108
6109 $seen++ if ($block =~ /^\s*{/);
6110
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006111 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Joe Hershberger05622192011-10-18 10:06:59 +00006112 if (statement_lines($cond) > 1) {
6113 #print "APW: ALLOWED: cond<$cond>\n";
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006114 $allowed[$allow] = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00006115 }
6116 if ($block =~/\b(?:if|for|while)\b/) {
6117 #print "APW: ALLOWED: block<$block>\n";
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006118 $allowed[$allow] = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00006119 }
6120 if (statement_block_size($block) > 1) {
6121 #print "APW: ALLOWED: lines block<$block>\n";
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006122 $allowed[$allow] = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00006123 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006124 $allow++;
Joe Hershberger05622192011-10-18 10:06:59 +00006125 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006126 if ($seen) {
6127 my $sum_allowed = 0;
6128 foreach (@allowed) {
6129 $sum_allowed += $_;
6130 }
6131 if ($sum_allowed == 0) {
6132 WARN("BRACES",
6133 "braces {} are not necessary for any arm of this statement\n" . $herectx);
6134 } elsif ($sum_allowed != $allow &&
6135 $seen != $allow) {
6136 CHK("BRACES",
6137 "braces {} should be used on all arms of this statement\n" . $herectx);
6138 }
Joe Hershberger05622192011-10-18 10:06:59 +00006139 }
6140 }
6141 }
6142 if (!defined $suppress_ifbraces{$linenr - 1} &&
6143 $line =~ /\b(if|while|for|else)\b/) {
6144 my $allowed = 0;
6145
6146 # Check the pre-context.
6147 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
6148 #print "APW: ALLOWED: pre<$1>\n";
6149 $allowed = 1;
6150 }
6151
6152 my ($level, $endln, @chunks) =
6153 ctx_statement_full($linenr, $realcnt, $-[0]);
6154
6155 # Check the condition.
6156 my ($cond, $block) = @{$chunks[0]};
6157 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
6158 if (defined $cond) {
6159 substr($block, 0, length($cond), '');
6160 }
6161 if (statement_lines($cond) > 1) {
6162 #print "APW: ALLOWED: cond<$cond>\n";
6163 $allowed = 1;
6164 }
6165 if ($block =~/\b(?:if|for|while)\b/) {
6166 #print "APW: ALLOWED: block<$block>\n";
6167 $allowed = 1;
6168 }
6169 if (statement_block_size($block) > 1) {
6170 #print "APW: ALLOWED: lines block<$block>\n";
6171 $allowed = 1;
6172 }
6173 # Check the post-context.
6174 if (defined $chunks[1]) {
6175 my ($cond, $block) = @{$chunks[1]};
6176 if (defined $cond) {
6177 substr($block, 0, length($cond), '');
6178 }
6179 if ($block =~ /^\s*\{/) {
6180 #print "APW: ALLOWED: chunk-1 block<$block>\n";
6181 $allowed = 1;
6182 }
6183 }
6184 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Joe Hershberger05622192011-10-18 10:06:59 +00006185 my $cnt = statement_rawlines($block);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006186 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Hershberger05622192011-10-18 10:06:59 +00006187
6188 WARN("BRACES",
6189 "braces {} are not necessary for single statement blocks\n" . $herectx);
6190 }
6191 }
6192
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006193# check for single line unbalanced braces
6194 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
6195 $sline =~ /^.\s*else\s*\{\s*$/) {
6196 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
6197 }
6198
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006199# check for unnecessary blank lines around braces
Tom Rini6b9709d2014-02-27 08:27:28 -05006200 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006201 if (CHK("BRACES",
6202 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
6203 $fix && $prevrawline =~ /^\+/) {
6204 fix_delete_line($fixlinenr - 1, $prevrawline);
6205 }
Joe Hershberger05622192011-10-18 10:06:59 +00006206 }
Tom Rini6b9709d2014-02-27 08:27:28 -05006207 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006208 if (CHK("BRACES",
6209 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
6210 $fix) {
6211 fix_delete_line($fixlinenr, $rawline);
6212 }
Joe Hershberger05622192011-10-18 10:06:59 +00006213 }
6214
6215# no volatiles please
6216 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
6217 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
6218 WARN("VOLATILE",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006219 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
6220 }
6221
6222# Check for user-visible strings broken across lines, which breaks the ability
6223# to grep for the string. Make exceptions when the previous string ends in a
6224# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
6225# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
6226 if ($line =~ /^\+\s*$String/ &&
6227 $prevline =~ /"\s*$/ &&
6228 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
6229 if (WARN("SPLIT_STRING",
6230 "quoted string split across lines\n" . $hereprev) &&
6231 $fix &&
6232 $prevrawline =~ /^\+.*"\s*$/ &&
6233 $last_coalesced_string_linenr != $linenr - 1) {
6234 my $extracted_string = get_quoted_string($line, $rawline);
6235 my $comma_close = "";
6236 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
6237 $comma_close = $1;
6238 }
6239
6240 fix_delete_line($fixlinenr - 1, $prevrawline);
6241 fix_delete_line($fixlinenr, $rawline);
6242 my $fixedline = $prevrawline;
6243 $fixedline =~ s/"\s*$//;
6244 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
6245 fix_insert_line($fixlinenr - 1, $fixedline);
6246 $fixedline = $rawline;
6247 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
6248 if ($fixedline !~ /\+\s*$/) {
6249 fix_insert_line($fixlinenr, $fixedline);
6250 }
6251 $last_coalesced_string_linenr = $linenr;
6252 }
6253 }
6254
6255# check for missing a space in a string concatenation
6256 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
6257 WARN('MISSING_SPACE',
6258 "break quoted strings at a space character\n" . $hereprev);
6259 }
6260
6261# check for an embedded function name in a string when the function is known
6262# This does not work very well for -f --file checking as it depends on patch
6263# context providing the function name or a single line form for in-file
6264# function declarations
6265 if ($line =~ /^\+.*$String/ &&
6266 defined($context_function) &&
6267 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
6268 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
6269 WARN("EMBEDDED_FUNCTION_NAME",
6270 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
6271 }
6272
Tom Rinie199fb32021-08-03 08:31:56 -04006273# check for unnecessary function tracing like uses
6274# This does not use $logFunctions because there are many instances like
6275# 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions
6276 if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) {
6277 if (WARN("TRACING_LOGGING",
6278 "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) &&
6279 $fix) {
6280 fix_delete_line($fixlinenr, $rawline);
6281 }
6282 }
6283
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006284# check for spaces before a quoted newline
6285 if ($rawline =~ /^.*\".*\s\\n/) {
6286 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
6287 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
6288 $fix) {
6289 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
6290 }
6291
6292 }
6293
6294# concatenated string without spaces between elements
Simon Glass587254e2022-01-23 12:55:11 -07006295 if ($line =~ /$String[A-Z_]/ ||
6296 ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006297 if (CHK("CONCATENATED_STRING",
6298 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
6299 $fix) {
6300 while ($line =~ /($String)/g) {
6301 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6302 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
6303 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
6304 }
6305 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006306 }
6307
6308# uncoalesced string fragments
Simon Glass587254e2022-01-23 12:55:11 -07006309 if ($line =~ /$String\s*[Lu]?"/) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006310 if (WARN("STRING_FRAGMENTS",
6311 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
6312 $fix) {
6313 while ($line =~ /($String)(?=\s*")/g) {
6314 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6315 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
6316 }
6317 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006318 }
6319
6320# check for non-standard and hex prefixed decimal printf formats
6321 my $show_L = 1; #don't show the same defect twice
6322 my $show_Z = 1;
6323 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
6324 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
6325 $string =~ s/%%/__/g;
6326 # check for %L
6327 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
6328 WARN("PRINTF_L",
6329 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6330 $show_L = 0;
6331 }
6332 # check for %Z
6333 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6334 WARN("PRINTF_Z",
6335 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6336 $show_Z = 0;
6337 }
6338 # check for 0x<decimal>
6339 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6340 ERROR("PRINTF_0XDECIMAL",
6341 "Prefixing 0x with decimal output is defective\n" . $herecurr);
6342 }
6343 }
6344
6345# check for line continuations in quoted strings with odd counts of "
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006346 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006347 WARN("LINE_CONTINUATIONS",
6348 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006349 }
6350
6351# warn about #if 0
6352 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006353 WARN("IF_0",
6354 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
6355 }
6356
6357# warn about #if 1
6358 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
6359 WARN("IF_1",
6360 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006361 }
6362
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006363# check for needless "if (<foo>) fn(<foo>)" uses
6364 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006365 my $tested = quotemeta($1);
6366 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6367 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6368 my $func = $1;
6369 if (WARN('NEEDLESS_IF',
6370 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6371 $fix) {
6372 my $do_fix = 1;
6373 my $leading_tabs = "";
6374 my $new_leading_tabs = "";
6375 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6376 $leading_tabs = $1;
6377 } else {
6378 $do_fix = 0;
6379 }
6380 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6381 $new_leading_tabs = $1;
6382 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6383 $do_fix = 0;
6384 }
6385 } else {
6386 $do_fix = 0;
6387 }
6388 if ($do_fix) {
6389 fix_delete_line($fixlinenr - 1, $prevrawline);
6390 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6391 }
6392 }
6393 }
6394 }
6395
6396# check for unnecessary "Out of Memory" messages
6397 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6398 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6399 (defined $1 || defined $3) &&
6400 $linenr > 3) {
6401 my $testval = $2;
6402 my $testline = $lines[$linenr - 3];
6403
6404 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6405# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6406
Tom Rinic57383b2020-06-16 10:29:46 -04006407 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6408 $s !~ /\b__GFP_NOWARN\b/ ) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006409 WARN("OOM_MESSAGE",
6410 "Possible unnecessary 'out of memory' message\n" . $hereprev);
6411 }
6412 }
6413
6414# check for logging functions with KERN_<LEVEL>
6415 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
6416 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6417 my $level = $1;
6418 if (WARN("UNNECESSARY_KERN_LEVEL",
6419 "Possible unnecessary $level\n" . $herecurr) &&
6420 $fix) {
6421 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
6422 }
6423 }
6424
6425# check for logging continuations
6426 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
6427 WARN("LOGGING_CONTINUATION",
6428 "Avoid logging continuation uses where feasible\n" . $herecurr);
6429 }
6430
Tom Rinie199fb32021-08-03 08:31:56 -04006431# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
6432 if (defined $stat &&
6433 $line =~ /\b$logFunctions\s*\(/ &&
6434 index($stat, '"') >= 0) {
6435 my $lc = $stat =~ tr@\n@@;
6436 $lc = $lc + $linenr;
6437 my $stat_real = get_stat_real($linenr, $lc);
6438 pos($stat_real) = index($stat_real, '"');
6439 while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
6440 my $pspec = $1;
6441 my $h = $2;
6442 my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
6443 if (WARN("UNNECESSARY_MODIFIER",
6444 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
6445 $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
6446 my $nspec = $pspec;
6447 $nspec =~ s/h//g;
6448 $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
6449 }
6450 }
6451 }
6452
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006453# check for mask then right shift without a parentheses
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006454 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006455 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6456 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6457 WARN("MASK_THEN_SHIFT",
6458 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6459 }
6460
6461# check for pointer comparisons to NULL
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006462 if ($perl_version_ok) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006463 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6464 my $val = $1;
6465 my $equal = "!";
6466 $equal = "" if ($4 eq "!=");
6467 if (CHK("COMPARISON_TO_NULL",
6468 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6469 $fix) {
6470 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6471 }
Joe Hershberger05622192011-10-18 10:06:59 +00006472 }
6473 }
6474
Tom Rini6b9709d2014-02-27 08:27:28 -05006475# check for bad placement of section $InitAttribute (e.g.: __initdata)
6476 if ($line =~ /(\b$InitAttribute\b)/) {
6477 my $attr = $1;
6478 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
6479 my $ptr = $1;
6480 my $var = $2;
6481 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
6482 ERROR("MISPLACED_INIT",
6483 "$attr should be placed after $var\n" . $herecurr)) ||
6484 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
6485 WARN("MISPLACED_INIT",
6486 "$attr should be placed after $var\n" . $herecurr))) &&
6487 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006488 $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;
Tom Rini6b9709d2014-02-27 08:27:28 -05006489 }
6490 }
6491 }
6492
6493# check for $InitAttributeData (ie: __initdata) with const
6494 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6495 my $attr = $1;
6496 $attr =~ /($InitAttributePrefix)(.*)/;
6497 my $attr_prefix = $1;
6498 my $attr_type = $2;
6499 if (ERROR("INIT_ATTRIBUTE",
6500 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6501 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006502 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05006503 s/$InitAttributeData/${attr_prefix}initconst/;
6504 }
6505 }
6506
6507# check for $InitAttributeConst (ie: __initconst) without const
6508 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6509 my $attr = $1;
6510 if (ERROR("INIT_ATTRIBUTE",
6511 "Use of $attr requires a separate use of const\n" . $herecurr) &&
6512 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006513 my $lead = $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05006514 /(^\+\s*(?:static\s+))/;
6515 $lead = rtrim($1);
6516 $lead = "$lead " if ($lead !~ /^\+$/);
6517 $lead = "${lead}const ";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006518 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
6519 }
6520 }
6521
6522# check for __read_mostly with const non-pointer (should just be const)
6523 if ($line =~ /\b__read_mostly\b/ &&
6524 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6525 if (ERROR("CONST_READ_MOSTLY",
6526 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6527 $fix) {
6528 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6529 }
6530 }
6531
6532# don't use __constant_<foo> functions outside of include/uapi/
6533 if ($realfile !~ m@^include/uapi/@ &&
6534 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6535 my $constant_func = $1;
6536 my $func = $constant_func;
6537 $func =~ s/^__constant_//;
6538 if (WARN("CONSTANT_CONVERSION",
6539 "$constant_func should be $func\n" . $herecurr) &&
6540 $fix) {
6541 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Tom Rini6b9709d2014-02-27 08:27:28 -05006542 }
6543 }
6544
Joe Hershberger05622192011-10-18 10:06:59 +00006545# prefer usleep_range over udelay
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006546 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006547 my $delay = $1;
Joe Hershberger05622192011-10-18 10:06:59 +00006548 # ignore udelay's < 10, however
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006549 if (! ($delay < 10) ) {
Joe Hershberger05622192011-10-18 10:06:59 +00006550 CHK("USLEEP_RANGE",
Tom Rinic57383b2020-06-16 10:29:46 -04006551 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006552 }
6553 if ($delay > 2000) {
6554 WARN("LONG_UDELAY",
6555 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006556 }
6557 }
6558
6559# warn about unexpectedly long msleep's
6560 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6561 if ($1 < 20) {
6562 WARN("MSLEEP",
Tom Rinic57383b2020-06-16 10:29:46 -04006563 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006564 }
6565 }
6566
Tom Rini6b9709d2014-02-27 08:27:28 -05006567# check for comparisons of jiffies
6568 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6569 WARN("JIFFIES_COMPARISON",
6570 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6571 }
6572
6573# check for comparisons of get_jiffies_64()
6574 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6575 WARN("JIFFIES_COMPARISON",
6576 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6577 }
6578
Joe Hershberger05622192011-10-18 10:06:59 +00006579# warn about #ifdefs in C files
6580# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
6581# print "#ifdef in C files should be avoided\n";
6582# print "$herecurr";
6583# $clean = 0;
6584# }
6585
6586# warn about spacing in #ifdefs
6587 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006588 if (ERROR("SPACING",
6589 "exactly one space required after that #$1\n" . $herecurr) &&
6590 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006591 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05006592 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6593 }
6594
Joe Hershberger05622192011-10-18 10:06:59 +00006595 }
6596
6597# check for spinlock_t definitions without a comment.
6598 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6599 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
6600 my $which = $1;
6601 if (!ctx_has_comment($first_line, $linenr)) {
6602 CHK("UNCOMMENTED_DEFINITION",
6603 "$1 definition without comment\n" . $herecurr);
6604 }
6605 }
6606# check for memory barriers without a comment.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006607
6608 my $barriers = qr{
6609 mb|
6610 rmb|
Tom Rinie199fb32021-08-03 08:31:56 -04006611 wmb
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006612 }x;
6613 my $barrier_stems = qr{
6614 mb__before_atomic|
6615 mb__after_atomic|
6616 store_release|
6617 load_acquire|
6618 store_mb|
6619 (?:$barriers)
6620 }x;
6621 my $all_barriers = qr{
6622 (?:$barriers)|
6623 smp_(?:$barrier_stems)|
6624 virt_(?:$barrier_stems)
6625 }x;
6626
6627 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006628 if (!ctx_has_comment($first_line, $linenr)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006629 WARN("MEMORY_BARRIER",
6630 "memory barrier without comment\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006631 }
6632 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006633
6634 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6635
6636 if ($realfile !~ m@^include/asm-generic/@ &&
6637 $realfile !~ m@/barrier\.h$@ &&
6638 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6639 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6640 WARN("MEMORY_BARRIER",
6641 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6642 }
6643
6644# check for waitqueue_active without a comment.
6645 if ($line =~ /\bwaitqueue_active\s*\(/) {
6646 if (!ctx_has_comment($first_line, $linenr)) {
6647 WARN("WAITQUEUE_ACTIVE",
6648 "waitqueue_active without comment\n" . $herecurr);
6649 }
6650 }
6651
Tom Rinie199fb32021-08-03 08:31:56 -04006652# check for data_race without a comment.
6653 if ($line =~ /\bdata_race\s*\(/) {
6654 if (!ctx_has_comment($first_line, $linenr)) {
6655 WARN("DATA_RACE",
6656 "data_race without comment\n" . $herecurr);
6657 }
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006658 }
6659
Joe Hershberger05622192011-10-18 10:06:59 +00006660# check of hardware specific defines
6661 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
6662 CHK("ARCH_DEFINES",
6663 "architecture specific defines should be avoided\n" . $herecurr);
6664 }
6665
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006666# check that the storage class is not after a type
6667 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006668 WARN("STORAGE_CLASS",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006669 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6670 }
6671# Check that the storage class is at the beginning of a declaration
6672 if ($line =~ /\b$Storage\b/ &&
6673 $line !~ /^.\s*$Storage/ &&
6674 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6675 $1 !~ /[\,\)]\s*$/) {
6676 WARN("STORAGE_CLASS",
6677 "storage class should be at the beginning of the declaration\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006678 }
6679
6680# check the location of the inline attribute, that it is between
6681# storage class and type.
6682 if ($line =~ /\b$Type\s+$Inline\b/ ||
6683 $line =~ /\b$Inline\s+$Storage\b/) {
6684 ERROR("INLINE_LOCATION",
6685 "inline keyword should sit between storage class and type\n" . $herecurr);
6686 }
6687
6688# Check for __inline__ and __inline, prefer inline
Tom Rini6b9709d2014-02-27 08:27:28 -05006689 if ($realfile !~ m@\binclude/uapi/@ &&
6690 $line =~ /\b(__inline__|__inline)\b/) {
6691 if (WARN("INLINE",
6692 "plain inline is preferred over $1\n" . $herecurr) &&
6693 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006694 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Tom Rini6b9709d2014-02-27 08:27:28 -05006695
6696 }
Joe Hershberger05622192011-10-18 10:06:59 +00006697 }
6698
Tom Rinie199fb32021-08-03 08:31:56 -04006699# Check for compiler attributes
Tom Rini6b9709d2014-02-27 08:27:28 -05006700 if ($realfile !~ m@\binclude/uapi/@ &&
Tom Rinie199fb32021-08-03 08:31:56 -04006701 $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
6702 my $attr = $1;
6703 $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
Joe Hershberger05622192011-10-18 10:06:59 +00006704
Tom Rinie199fb32021-08-03 08:31:56 -04006705 my %attr_list = (
6706 "alias" => "__alias",
6707 "aligned" => "__aligned",
6708 "always_inline" => "__always_inline",
6709 "assume_aligned" => "__assume_aligned",
6710 "cold" => "__cold",
6711 "const" => "__attribute_const__",
6712 "copy" => "__copy",
6713 "designated_init" => "__designated_init",
6714 "externally_visible" => "__visible",
6715 "format" => "printf|scanf",
6716 "gnu_inline" => "__gnu_inline",
6717 "malloc" => "__malloc",
6718 "mode" => "__mode",
6719 "no_caller_saved_registers" => "__no_caller_saved_registers",
6720 "noclone" => "__noclone",
6721 "noinline" => "noinline",
6722 "nonstring" => "__nonstring",
6723 "noreturn" => "__noreturn",
6724 "packed" => "__packed",
6725 "pure" => "__pure",
6726 "section" => "__section",
6727 "used" => "__used",
6728 "weak" => "__weak"
6729 );
Joe Hershberger05622192011-10-18 10:06:59 +00006730
Tom Rinie199fb32021-08-03 08:31:56 -04006731 while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
6732 my $orig_attr = $1;
6733 my $params = '';
6734 $params = $2 if defined($2);
6735 my $curr_attr = $orig_attr;
6736 $curr_attr =~ s/^[\s_]+|[\s_]+$//g;
6737 if (exists($attr_list{$curr_attr})) {
6738 my $new = $attr_list{$curr_attr};
6739 if ($curr_attr eq "format" && $params) {
6740 $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
6741 $new = "__$1\($2";
6742 } else {
6743 $new = "$new$params";
6744 }
6745 if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6746 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
6747 $fix) {
6748 my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6749 $fixed[$fixlinenr] =~ s/$remove//;
6750 $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6751 $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6752 $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
6753 }
6754 }
Tom Rinic57383b2020-06-16 10:29:46 -04006755 }
Tom Rinic57383b2020-06-16 10:29:46 -04006756
Tom Rinie199fb32021-08-03 08:31:56 -04006757 # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
6758 if ($attr =~ /^_*unused/) {
6759 WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6760 "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006761 }
6762 }
6763
6764# Check for __attribute__ weak, or __weak declarations (may have link issues)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006765 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006766 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6767 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6768 $line =~ /\b__weak\b/)) {
6769 ERROR("WEAK_DECLARATION",
6770 "Using weak declarations can have unintended link defects\n" . $herecurr);
6771 }
6772
6773# check for c99 types like uint8_t used outside of uapi/ and tools/
6774 if ($realfile !~ m@\binclude/uapi/@ &&
6775 $realfile !~ m@\btools/@ &&
6776 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6777 my $type = $1;
6778 if ($type =~ /\b($typeC99Typedefs)\b/) {
6779 $type = $1;
6780 my $kernel_type = 'u';
6781 $kernel_type = 's' if ($type =~ /^_*[si]/);
6782 $type =~ /(\d+)/;
6783 $kernel_type .= $1;
6784 if (CHK("PREFER_KERNEL_TYPES",
6785 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6786 $fix) {
6787 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6788 }
6789 }
6790 }
6791
6792# check for cast of C90 native int or longer types constants
6793 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6794 my $cast = $1;
6795 my $const = $2;
Tom Rinie199fb32021-08-03 08:31:56 -04006796 my $suffix = "";
6797 my $newconst = $const;
6798 $newconst =~ s/${Int_type}$//;
6799 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6800 if ($cast =~ /\blong\s+long\b/) {
6801 $suffix .= 'LL';
6802 } elsif ($cast =~ /\blong\b/) {
6803 $suffix .= 'L';
6804 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006805 if (WARN("TYPECAST_INT_CONSTANT",
Tom Rinie199fb32021-08-03 08:31:56 -04006806 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006807 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006808 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
Tom Rini6b9709d2014-02-27 08:27:28 -05006809 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006810 }
6811
Joe Hershberger05622192011-10-18 10:06:59 +00006812# check for sizeof(&)
6813 if ($line =~ /\bsizeof\s*\(\s*\&/) {
6814 WARN("SIZEOF_ADDRESS",
6815 "sizeof(& should be avoided\n" . $herecurr);
6816 }
6817
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006818# check for sizeof without parenthesis
6819 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006820 if (WARN("SIZEOF_PARENTHESIS",
6821 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6822 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006823 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Tom Rini6b9709d2014-02-27 08:27:28 -05006824 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006825 }
6826
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006827# check for struct spinlock declarations
6828 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6829 WARN("USE_SPINLOCK_T",
6830 "struct spinlock should be spinlock_t\n" . $herecurr);
6831 }
6832
Tom Rini6b9709d2014-02-27 08:27:28 -05006833# check for seq_printf uses that could be seq_puts
6834 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6835 my $fmt = get_quoted_string($line, $rawline);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006836 $fmt =~ s/%%//g;
6837 if ($fmt !~ /%/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006838 if (WARN("PREFER_SEQ_PUTS",
6839 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6840 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006841 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Tom Rini6b9709d2014-02-27 08:27:28 -05006842 }
6843 }
6844 }
6845
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006846# check for vsprintf extension %p<foo> misuses
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006847 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006848 defined $stat &&
6849 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6850 $1 !~ /^_*volatile_*$/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006851 my $stat_real;
6852
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006853 my $lc = $stat =~ tr@\n@@;
6854 $lc = $lc + $linenr;
6855 for (my $count = $linenr; $count <= $lc; $count++) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006856 my $specifier;
6857 my $extension;
Tom Rinic57383b2020-06-16 10:29:46 -04006858 my $qualifier;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006859 my $bad_specifier = "";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006860 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6861 $fmt =~ s/%%//g;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006862
Tom Rinic57383b2020-06-16 10:29:46 -04006863 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006864 $specifier = $1;
6865 $extension = $2;
Tom Rinic57383b2020-06-16 10:29:46 -04006866 $qualifier = $3;
Tom Rinie199fb32021-08-03 08:31:56 -04006867 if ($extension !~ /[4SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Tom Rinic57383b2020-06-16 10:29:46 -04006868 ($extension eq "f" &&
Tom Rinie199fb32021-08-03 08:31:56 -04006869 defined $qualifier && $qualifier !~ /^w/) ||
6870 ($extension eq "4" &&
6871 defined $qualifier && $qualifier !~ /^cc/)) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006872 $bad_specifier = $specifier;
6873 last;
6874 }
6875 if ($extension eq "x" && !defined($stat_real)) {
6876 if (!defined($stat_real)) {
6877 $stat_real = get_stat_real($linenr, $lc);
6878 }
6879 WARN("VSPRINTF_SPECIFIER_PX",
6880 "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");
6881 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006882 }
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006883 if ($bad_specifier ne "") {
6884 my $stat_real = get_stat_real($linenr, $lc);
6885 my $ext_type = "Invalid";
6886 my $use = "";
6887 if ($bad_specifier =~ /p[Ff]/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006888 $use = " - use %pS instead";
6889 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6890 }
6891
6892 WARN("VSPRINTF_POINTER_EXTENSION",
6893 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006894 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006895 }
6896 }
6897
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006898# Check for misused memsets
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006899 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006900 defined $stat &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006901 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006902
6903 my $ms_addr = $2;
6904 my $ms_val = $7;
6905 my $ms_size = $12;
6906
6907 if ($ms_size =~ /^(0x|)0$/i) {
6908 ERROR("MEMSET",
6909 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6910 } elsif ($ms_size =~ /^(0x|)1$/i) {
6911 WARN("MEMSET",
6912 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6913 }
6914 }
6915
Tom Rini6b9709d2014-02-27 08:27:28 -05006916# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006917# if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006918# defined $stat &&
6919# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6920# if (WARN("PREFER_ETHER_ADDR_COPY",
6921# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6922# $fix) {
6923# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6924# }
6925# }
6926
6927# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006928# if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006929# defined $stat &&
6930# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6931# WARN("PREFER_ETHER_ADDR_EQUAL",
6932# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6933# }
6934
6935# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6936# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006937# if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006938# defined $stat &&
6939# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6940#
6941# my $ms_val = $7;
6942#
6943# if ($ms_val =~ /^(?:0x|)0+$/i) {
6944# if (WARN("PREFER_ETH_ZERO_ADDR",
6945# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6946# $fix) {
6947# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6948# }
6949# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6950# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6951# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6952# $fix) {
6953# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6954# }
6955# }
6956# }
Tom Rini6b9709d2014-02-27 08:27:28 -05006957
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006958# typecasts on min/max could be min_t/max_t
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006959 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006960 defined $stat &&
6961 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6962 if (defined $2 || defined $7) {
6963 my $call = $1;
6964 my $cast1 = deparenthesize($2);
6965 my $arg1 = $3;
6966 my $cast2 = deparenthesize($7);
6967 my $arg2 = $8;
6968 my $cast;
6969
6970 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6971 $cast = "$cast1 or $cast2";
6972 } elsif ($cast1 ne "") {
6973 $cast = $cast1;
6974 } else {
6975 $cast = $cast2;
6976 }
6977 WARN("MINMAX",
6978 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6979 }
6980 }
6981
6982# check usleep_range arguments
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006983 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006984 defined $stat &&
6985 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6986 my $min = $1;
6987 my $max = $7;
6988 if ($min eq $max) {
6989 WARN("USLEEP_RANGE",
Tom Rinic57383b2020-06-16 10:29:46 -04006990 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006991 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6992 $min > $max) {
6993 WARN("USLEEP_RANGE",
Tom Rinic57383b2020-06-16 10:29:46 -04006994 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006995 }
6996 }
6997
Tom Rini6b9709d2014-02-27 08:27:28 -05006998# check for naked sscanf
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006999 if ($perl_version_ok &&
Tom Rini6b9709d2014-02-27 08:27:28 -05007000 defined $stat &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007001 $line =~ /\bsscanf\b/ &&
Tom Rini6b9709d2014-02-27 08:27:28 -05007002 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
7003 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
7004 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
7005 my $lc = $stat =~ tr@\n@@;
7006 $lc = $lc + $linenr;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02007007 my $stat_real = get_stat_real($linenr, $lc);
Tom Rini6b9709d2014-02-27 08:27:28 -05007008 WARN("NAKED_SSCANF",
7009 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
7010 }
7011
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007012# check for simple sscanf that should be kstrto<foo>
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02007013 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007014 defined $stat &&
7015 $line =~ /\bsscanf\b/) {
7016 my $lc = $stat =~ tr@\n@@;
7017 $lc = $lc + $linenr;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02007018 my $stat_real = get_stat_real($linenr, $lc);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007019 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
7020 my $format = $6;
7021 my $count = $format =~ tr@%@%@;
7022 if ($count == 1 &&
7023 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
7024 WARN("SSCANF_TO_KSTRTO",
7025 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
7026 }
7027 }
7028 }
7029
Tom Rini6b9709d2014-02-27 08:27:28 -05007030# check for new externs in .h files.
7031 if ($realfile =~ /\.h$/ &&
7032 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
7033 if (CHK("AVOID_EXTERNS",
7034 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
7035 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007036 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Tom Rini6b9709d2014-02-27 08:27:28 -05007037 }
7038 }
7039
Joe Hershberger05622192011-10-18 10:06:59 +00007040# check for new externs in .c files.
7041 if ($realfile =~ /\.c$/ && defined $stat &&
7042 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
7043 {
7044 my $function_name = $1;
7045 my $paren_space = $2;
7046
7047 my $s = $stat;
7048 if (defined $cond) {
7049 substr($s, 0, length($cond), '');
7050 }
Tom Rinie199fb32021-08-03 08:31:56 -04007051 if ($s =~ /^\s*;/)
Joe Hershberger05622192011-10-18 10:06:59 +00007052 {
7053 WARN("AVOID_EXTERNS",
7054 "externs should be avoided in .c files\n" . $herecurr);
7055 }
7056
7057 if ($paren_space =~ /\n/) {
7058 WARN("FUNCTION_ARGUMENTS",
7059 "arguments for function declarations should follow identifier\n" . $herecurr);
7060 }
7061
7062 } elsif ($realfile =~ /\.c$/ && defined $stat &&
7063 $stat =~ /^.\s*extern\s+/)
7064 {
7065 WARN("AVOID_EXTERNS",
7066 "externs should be avoided in .c files\n" . $herecurr);
7067 }
7068
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007069# check for function declarations that have arguments without identifier names
7070 if (defined $stat &&
Tom Rinie199fb32021-08-03 08:31:56 -04007071 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
7072 $1 ne "void") {
7073 my $args = trim($1);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007074 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
7075 my $arg = trim($1);
Tom Rinie199fb32021-08-03 08:31:56 -04007076 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007077 WARN("FUNCTION_ARGUMENTS",
7078 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
7079 }
7080 }
7081 }
7082
7083# check for function definitions
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02007084 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007085 defined $stat &&
7086 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
7087 $context_function = $1;
7088
7089# check for multiline function definition with misplaced open brace
7090 my $ok = 0;
7091 my $cnt = statement_rawlines($stat);
7092 my $herectx = $here . "\n";
7093 for (my $n = 0; $n < $cnt; $n++) {
7094 my $rl = raw_line($linenr, $n);
7095 $herectx .= $rl . "\n";
7096 $ok = 1 if ($rl =~ /^[ \+]\{/);
7097 $ok = 1 if ($rl =~ /\{/ && $n == 0);
7098 last if $rl =~ /^[ \+].*\{/;
7099 }
7100 if (!$ok) {
7101 ERROR("OPEN_BRACE",
7102 "open brace '{' following function definitions go on the next line\n" . $herectx);
7103 }
7104 }
7105
Joe Hershberger05622192011-10-18 10:06:59 +00007106# checks for new __setup's
7107 if ($rawline =~ /\b__setup\("([^"]*)"/) {
7108 my $name = $1;
7109
7110 if (!grep(/$name/, @setup_docs)) {
7111 CHK("UNDOCUMENTED_SETUP",
Tom Rinie199fb32021-08-03 08:31:56 -04007112 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00007113 }
7114 }
7115
Tom Rinic57383b2020-06-16 10:29:46 -04007116# check for pointless casting of alloc functions
7117 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00007118 WARN("UNNECESSARY_CASTS",
7119 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
7120 }
7121
Tom Rini6b9709d2014-02-27 08:27:28 -05007122# alloc style
7123# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02007124 if ($perl_version_ok &&
Tom Rinic57383b2020-06-16 10:29:46 -04007125 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05007126 CHK("ALLOC_SIZEOF_STRUCT",
7127 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
7128 }
7129
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007130# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02007131 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007132 defined $stat &&
7133 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
7134 my $oldfunc = $3;
7135 my $a1 = $4;
7136 my $a2 = $10;
7137 my $newfunc = "kmalloc_array";
7138 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
7139 my $r1 = $a1;
7140 my $r2 = $a2;
7141 if ($a1 =~ /^sizeof\s*\S/) {
7142 $r1 = $a2;
7143 $r2 = $a1;
7144 }
7145 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
7146 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007147 my $cnt = statement_rawlines($stat);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02007148 my $herectx = get_stat_here($linenr, $cnt, $here);
7149
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007150 if (WARN("ALLOC_WITH_MULTIPLY",
7151 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
7152 $cnt == 1 &&
7153 $fix) {
7154 $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;
7155 }
7156 }
7157 }
7158
Tom Rini6b9709d2014-02-27 08:27:28 -05007159# check for krealloc arg reuse
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02007160 if ($perl_version_ok &&
7161 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
7162 $1 eq $3) {
Tom Rini6b9709d2014-02-27 08:27:28 -05007163 WARN("KREALLOC_ARG_REUSE",
7164 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
7165 }
7166
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007167# check for alloc argument mismatch
Tom Rinie199fb32021-08-03 08:31:56 -04007168 if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007169 WARN("ALLOC_ARRAY_ARGS",
7170 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00007171 }
7172
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007173# check for multiple semicolons
7174 if ($line =~ /;\s*;\s*$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05007175 if (WARN("ONE_SEMICOLON",
7176 "Statements terminations use 1 semicolon\n" . $herecurr) &&
7177 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007178 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
7179 }
7180 }
7181
7182# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
7183 if ($realfile !~ m@^include/uapi/@ &&
7184 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
7185 my $ull = "";
7186 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
7187 if (CHK("BIT_MACRO",
7188 "Prefer using the BIT$ull macro\n" . $herecurr) &&
7189 $fix) {
7190 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
7191 }
7192 }
7193
Tom Rinie199fb32021-08-03 08:31:56 -04007194# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
7195 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
7196 WARN("IS_ENABLED_CONFIG",
7197 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
Tom Rini6b9709d2014-02-27 08:27:28 -05007198 }
7199
Tom Rinie199fb32021-08-03 08:31:56 -04007200# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
7201 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
7202 my $config = $1;
7203 if (WARN("PREFER_IS_ENABLED",
7204 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
7205 $fix) {
7206 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
Tom Rini6b9709d2014-02-27 08:27:28 -05007207 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007208 }
7209
Tom Rinic57383b2020-06-16 10:29:46 -04007210# check for /* fallthrough */ like comment, prefer fallthrough;
7211 my @fallthroughs = (
7212 'fallthrough',
7213 '@fallthrough@',
7214 'lint -fallthrough[ \t]*',
7215 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
7216 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
7217 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7218 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7219 );
7220 if ($raw_comment ne '') {
7221 foreach my $ft (@fallthroughs) {
7222 if ($raw_comment =~ /$ft/) {
7223 my $msg_level = \&WARN;
7224 $msg_level = \&CHK if ($file);
7225 &{$msg_level}("PREFER_FALLTHROUGH",
7226 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
7227 last;
7228 }
7229 }
7230 }
7231
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007232# check for switch/default statements without a break;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02007233 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007234 defined $stat &&
7235 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007236 my $cnt = statement_rawlines($stat);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02007237 my $herectx = get_stat_here($linenr, $cnt, $here);
7238
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007239 WARN("DEFAULT_NO_BREAK",
7240 "switch default: should use break\n" . $herectx);
Eric Nelson172a3a82012-05-02 20:32:18 -07007241 }
7242
Joe Hershberger05622192011-10-18 10:06:59 +00007243# check for gcc specific __FUNCTION__
Tom Rini6b9709d2014-02-27 08:27:28 -05007244 if ($line =~ /\b__FUNCTION__\b/) {
7245 if (WARN("USE_FUNC",
7246 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
7247 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007248 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Tom Rini6b9709d2014-02-27 08:27:28 -05007249 }
Joe Hershberger05622192011-10-18 10:06:59 +00007250 }
7251
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007252# check for uses of __DATE__, __TIME__, __TIMESTAMP__
7253 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
7254 ERROR("DATE_TIME",
7255 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
7256 }
7257
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007258# check for use of yield()
7259 if ($line =~ /\byield\s*\(\s*\)/) {
7260 WARN("YIELD",
7261 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
7262 }
7263
Tom Rini6b9709d2014-02-27 08:27:28 -05007264# check for comparisons against true and false
7265 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
7266 my $lead = $1;
7267 my $arg = $2;
7268 my $test = $3;
7269 my $otype = $4;
7270 my $trail = $5;
7271 my $op = "!";
7272
7273 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
7274
7275 my $type = lc($otype);
7276 if ($type =~ /^(?:true|false)$/) {
7277 if (("$test" eq "==" && "$type" eq "true") ||
7278 ("$test" eq "!=" && "$type" eq "false")) {
7279 $op = "";
7280 }
7281
7282 CHK("BOOL_COMPARISON",
7283 "Using comparison to $otype is error prone\n" . $herecurr);
7284
7285## maybe suggesting a correct construct would better
7286## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
7287
7288 }
7289 }
7290
Joe Hershberger05622192011-10-18 10:06:59 +00007291# check for semaphores initialized locked
7292 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
7293 WARN("CONSIDER_COMPLETION",
7294 "consider using a completion\n" . $herecurr);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007295 }
Joe Hershberger05622192011-10-18 10:06:59 +00007296
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007297# recommend kstrto* over simple_strto* and strict_strto*
7298 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Hershberger05622192011-10-18 10:06:59 +00007299 WARN("CONSIDER_KSTRTO",
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007300 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00007301 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007302
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007303# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Joe Hershberger05622192011-10-18 10:06:59 +00007304 if ($line =~ /^.\s*__initcall\s*\(/) {
7305 WARN("USE_DEVICE_INITCALL",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007306 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00007307 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007308
Tom Rinic57383b2020-06-16 10:29:46 -04007309# check for spin_is_locked(), suggest lockdep instead
7310 if ($line =~ /\bspin_is_locked\(/) {
7311 WARN("USE_LOCKDEP",
7312 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
7313 }
7314
7315# check for deprecated apis
7316 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
7317 my $deprecated_api = $1;
7318 my $new_api = $deprecated_apis{$deprecated_api};
7319 WARN("DEPRECATED_API",
7320 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
7321 }
7322
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007323# check for various structs that are normally const (ops, kgdb, device_tree)
7324# and avoid what seem like struct definitions 'struct foo {'
Tom Rinie199fb32021-08-03 08:31:56 -04007325 if (defined($const_structs) &&
7326 $line !~ /\bconst\b/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007327 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Hershberger05622192011-10-18 10:06:59 +00007328 WARN("CONST_STRUCT",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007329 "struct $1 should normally be const\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00007330 }
7331
7332# use of NR_CPUS is usually wrong
7333# ignore definitions of NR_CPUS and usage to define arrays as likely right
Tom Rinie199fb32021-08-03 08:31:56 -04007334# ignore designated initializers using NR_CPUS
Joe Hershberger05622192011-10-18 10:06:59 +00007335 if ($line =~ /\bNR_CPUS\b/ &&
7336 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7337 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
7338 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7339 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
Tom Rinie199fb32021-08-03 08:31:56 -04007340 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
7341 $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
Joe Hershberger05622192011-10-18 10:06:59 +00007342 {
7343 WARN("NR_CPUS",
7344 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
7345 }
7346
Tom Rini6b9709d2014-02-27 08:27:28 -05007347# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
7348 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
7349 ERROR("DEFINE_ARCH_HAS",
7350 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
7351 }
7352
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007353# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02007354 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007355 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7356 WARN("LIKELY_MISUSE",
7357 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00007358 }
7359
Tom Rinie199fb32021-08-03 08:31:56 -04007360# return sysfs_emit(foo, fmt, ...) fmt without newline
7361 if ($line =~ /\breturn\s+sysfs_emit\s*\(\s*$FuncArg\s*,\s*($String)/ &&
7362 substr($rawline, $-[6], $+[6] - $-[6]) !~ /\\n"$/) {
7363 my $offset = $+[6] - 1;
7364 if (WARN("SYSFS_EMIT",
7365 "return sysfs_emit(...) formats should include a terminating newline\n" . $herecurr) &&
7366 $fix) {
7367 substr($fixed[$fixlinenr], $offset, 0) = '\\n';
7368 }
7369 }
7370
Tom Rinic57383b2020-06-16 10:29:46 -04007371# nested likely/unlikely calls
7372 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7373 WARN("LIKELY_MISUSE",
7374 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7375 }
7376
Joe Hershberger05622192011-10-18 10:06:59 +00007377# whine mightly about in_atomic
7378 if ($line =~ /\bin_atomic\s*\(/) {
7379 if ($realfile =~ m@^drivers/@) {
7380 ERROR("IN_ATOMIC",
7381 "do not use in_atomic in drivers\n" . $herecurr);
7382 } elsif ($realfile !~ m@^kernel/@) {
7383 WARN("IN_ATOMIC",
7384 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
7385 }
7386 }
7387
7388# check for lockdep_set_novalidate_class
7389 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
7390 $line =~ /__lockdep_no_validate__\s*\)/ ) {
7391 if ($realfile !~ m@^kernel/lockdep@ &&
7392 $realfile !~ m@^include/linux/lockdep@ &&
7393 $realfile !~ m@^drivers/base/core@) {
7394 ERROR("LOCKDEP",
7395 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
7396 }
7397 }
7398
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007399 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7400 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00007401 WARN("EXPORTED_WORLD_WRITABLE",
7402 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
7403 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007404
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02007405# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
7406# and whether or not function naming is typical and if
7407# DEVICE_ATTR permissions uses are unusual too
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02007408 if ($perl_version_ok &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02007409 defined $stat &&
7410 $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*\)/) {
7411 my $var = $1;
7412 my $perms = $2;
7413 my $show = $3;
7414 my $store = $4;
7415 my $octal_perms = perms_to_octal($perms);
7416 if ($show =~ /^${var}_show$/ &&
7417 $store =~ /^${var}_store$/ &&
7418 $octal_perms eq "0644") {
7419 if (WARN("DEVICE_ATTR_RW",
7420 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
7421 $fix) {
7422 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
7423 }
7424 } elsif ($show =~ /^${var}_show$/ &&
7425 $store =~ /^NULL$/ &&
7426 $octal_perms eq "0444") {
7427 if (WARN("DEVICE_ATTR_RO",
7428 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
7429 $fix) {
7430 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
7431 }
7432 } elsif ($show =~ /^NULL$/ &&
7433 $store =~ /^${var}_store$/ &&
7434 $octal_perms eq "0200") {
7435 if (WARN("DEVICE_ATTR_WO",
7436 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
7437 $fix) {
7438 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
7439 }
7440 } elsif ($octal_perms eq "0644" ||
7441 $octal_perms eq "0444" ||
7442 $octal_perms eq "0200") {
7443 my $newshow = "$show";
7444 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
7445 my $newstore = $store;
7446 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
7447 my $rename = "";
7448 if ($show ne $newshow) {
7449 $rename .= " '$show' to '$newshow'";
7450 }
7451 if ($store ne $newstore) {
7452 $rename .= " '$store' to '$newstore'";
7453 }
7454 WARN("DEVICE_ATTR_FUNCTIONS",
7455 "Consider renaming function(s)$rename\n" . $herecurr);
7456 } else {
7457 WARN("DEVICE_ATTR_PERMS",
7458 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
7459 }
7460 }
7461
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007462# Mode permission misuses where it seems decimal should be octal
7463# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02007464# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
7465# specific definition of not visible in sysfs.
7466# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
7467# use the default permissions
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02007468 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007469 defined $stat &&
7470 $line =~ /$mode_perms_search/) {
7471 foreach my $entry (@mode_permission_funcs) {
7472 my $func = $entry->[0];
7473 my $arg_pos = $entry->[1];
7474
7475 my $lc = $stat =~ tr@\n@@;
7476 $lc = $lc + $linenr;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02007477 my $stat_real = get_stat_real($linenr, $lc);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007478
7479 my $skip_args = "";
7480 if ($arg_pos > 1) {
7481 $arg_pos--;
7482 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
7483 }
7484 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
7485 if ($stat =~ /$test/) {
7486 my $val = $1;
7487 $val = $6 if ($skip_args ne "");
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02007488 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
7489 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
7490 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007491 ERROR("NON_OCTAL_PERMISSIONS",
7492 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
7493 }
7494 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
7495 ERROR("EXPORTED_WORLD_WRITABLE",
7496 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
7497 }
7498 }
7499 }
7500 }
7501
7502# check for uses of S_<PERMS> that could be octal for readability
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02007503 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
7504 my $oval = $1;
7505 my $octal = perms_to_octal($oval);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007506 if (WARN("SYMBOLIC_PERMS",
7507 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7508 $fix) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02007509 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007510 }
7511 }
7512
7513# validate content of MODULE_LICENSE against list from include/linux/module.h
7514 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
7515 my $extracted_string = get_quoted_string($line, $rawline);
7516 my $valid_licenses = qr{
7517 GPL|
7518 GPL\ v2|
7519 GPL\ and\ additional\ rights|
7520 Dual\ BSD/GPL|
7521 Dual\ MIT/GPL|
7522 Dual\ MPL/GPL|
7523 Proprietary
7524 }x;
7525 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
7526 WARN("MODULE_LICENSE",
7527 "unknown module license " . $extracted_string . "\n" . $herecurr);
7528 }
7529 }
Tom Rinic57383b2020-06-16 10:29:46 -04007530
7531# check for sysctl duplicate constants
7532 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
7533 WARN("DUPLICATED_SYSCTL_CONST",
7534 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
7535 }
Joe Hershberger05622192011-10-18 10:06:59 +00007536 }
7537
7538 # If we have no input at all, then there is nothing to report on
7539 # so just keep quiet.
7540 if ($#rawlines == -1) {
7541 exit(0);
7542 }
7543
7544 # In mailback mode only produce a report in the negative, for
7545 # things that appear to be patches.
7546 if ($mailback && ($clean == 1 || !$is_patch)) {
7547 exit(0);
7548 }
7549
Tom Rinie199fb32021-08-03 08:31:56 -04007550 # This is not a patch, and we are in 'no-patch' mode so
Joe Hershberger05622192011-10-18 10:06:59 +00007551 # just keep quiet.
7552 if (!$chk_patch && !$is_patch) {
7553 exit(0);
7554 }
7555
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02007556 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Hershberger05622192011-10-18 10:06:59 +00007557 ERROR("NOT_UNIFIED_DIFF",
7558 "Does not appear to be a unified-diff format patch\n");
7559 }
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02007560 if ($is_patch && $has_commit_log && $chk_signoff) {
7561 if ($signoff == 0) {
7562 ERROR("MISSING_SIGN_OFF",
7563 "Missing Signed-off-by: line(s)\n");
Tom Rinie199fb32021-08-03 08:31:56 -04007564 } elsif ($authorsignoff != 1) {
7565 # authorsignoff values:
7566 # 0 -> missing sign off
7567 # 1 -> sign off identical
7568 # 2 -> names and addresses match, comments mismatch
7569 # 3 -> addresses match, names different
7570 # 4 -> names match, addresses different
7571 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
7572
7573 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
7574
7575 if ($authorsignoff == 0) {
7576 ERROR("NO_AUTHOR_SIGN_OFF",
7577 "Missing Signed-off-by: line by nominal patch author '$author'\n");
7578 } elsif ($authorsignoff == 2) {
7579 CHK("FROM_SIGN_OFF_MISMATCH",
7580 "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7581 } elsif ($authorsignoff == 3) {
7582 WARN("FROM_SIGN_OFF_MISMATCH",
7583 "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7584 } elsif ($authorsignoff == 4) {
7585 WARN("FROM_SIGN_OFF_MISMATCH",
7586 "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7587 } elsif ($authorsignoff == 5) {
7588 WARN("FROM_SIGN_OFF_MISMATCH",
7589 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7590 }
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02007591 }
Joe Hershberger05622192011-10-18 10:06:59 +00007592 }
7593
7594 print report_dump();
7595 if ($summary && !($clean == 1 && $quiet == 1)) {
7596 print "$filename " if ($summary_file);
7597 print "total: $cnt_error errors, $cnt_warn warnings, " .
7598 (($check)? "$cnt_chk checks, " : "") .
7599 "$cnt_lines lines checked\n";
Joe Hershberger05622192011-10-18 10:06:59 +00007600 }
7601
7602 if ($quiet == 0) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007603 # If there were any defects found and not already fixing them
7604 if (!$clean and !$fix) {
7605 print << "EOM"
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007606
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007607NOTE: For some of the reported defects, checkpatch may be able to
7608 mechanically convert to the typical style using --fix or --fix-inplace.
7609EOM
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00007610 }
Joe Hershberger05622192011-10-18 10:06:59 +00007611 # If there were whitespace errors which cleanpatch can fix
7612 # then suggest that.
7613 if ($rpt_cleaners) {
Joe Hershberger05622192011-10-18 10:06:59 +00007614 $rpt_cleaners = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007615 print << "EOM"
7616
7617NOTE: Whitespace errors detected.
7618 You may wish to use scripts/cleanpatch or scripts/cleanfile
7619EOM
Joe Hershberger05622192011-10-18 10:06:59 +00007620 }
7621 }
7622
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007623 if ($clean == 0 && $fix &&
7624 ("@rawlines" ne "@fixed" ||
7625 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05007626 my $newfile = $filename;
7627 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
7628 my $linecount = 0;
7629 my $f;
7630
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007631 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7632
Tom Rini6b9709d2014-02-27 08:27:28 -05007633 open($f, '>', $newfile)
7634 or die "$P: Can't open $newfile for write\n";
7635 foreach my $fixed_line (@fixed) {
7636 $linecount++;
7637 if ($file) {
7638 if ($linecount > 3) {
7639 $fixed_line =~ s/^\+//;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007640 print $f $fixed_line . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05007641 }
7642 } else {
7643 print $f $fixed_line . "\n";
7644 }
7645 }
7646 close($f);
7647
7648 if (!$quiet) {
7649 print << "EOM";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007650
Tom Rini6b9709d2014-02-27 08:27:28 -05007651Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
7652
7653Do _NOT_ trust the results written to this file.
7654Do _NOT_ submit these changes without inspecting them for correctness.
7655
7656This EXPERIMENTAL file is simply a convenience to help rewrite patches.
7657No warranties, expressed or implied...
Tom Rini6b9709d2014-02-27 08:27:28 -05007658EOM
7659 }
Joe Hershberger05622192011-10-18 10:06:59 +00007660 }
7661
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02007662 if ($quiet == 0) {
7663 print "\n";
7664 if ($clean == 1) {
7665 print "$vname has no obvious style problems and is ready for submission.\n";
7666 } else {
7667 print "$vname has style problems, please review.\n";
7668 }
Joe Hershberger05622192011-10-18 10:06:59 +00007669 }
Joe Hershberger05622192011-10-18 10:06:59 +00007670 return $clean;
7671}