blob: 7cbe6e72e363b79e8d464c68a73f4892a8382feb [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Joe Perches882ea1d2018-06-07 17:04:33 -07002# SPDX-License-Identifier: GPL-2.0
3#
Dave Jonesdbf004d2010-01-12 16:59:52 -05004# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07005# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08006# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07007# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Joe Perches882ea1d2018-06-07 17:04:33 -07008# (c) 2010-2018 Joe Perches <joe@perches.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07009
10use strict;
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020011use warnings;
Joe Perchesc707a812013-07-08 16:00:43 -070012use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080013use File::Basename;
14use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070015use Term::ANSIColor qw(:constants);
Geert Uytterhoevencd261492018-08-21 21:57:40 -070016use Encode qw(decode encode);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
18my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080019my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070020
Joe Perches000d1cc12011-07-25 17:13:25 -070021my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070022
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
26my $tree = 1;
27my $chk_signoff = 1;
28my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070029my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080031my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070032my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070034my $git = 0;
Joe Perches0dea9f1e2016-05-20 17:04:19 -070035my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070037my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080038my $summary = 1;
39my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080040my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070041my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070042my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070043my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080044my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070045my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080046my %debug;
Joe Perches34456862013-07-03 15:05:34 -070047my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070048my %use_type = ();
49my @use = ();
50my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070051my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070052my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070053my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080054my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070055my $ignore_perl_version = 0;
56my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070057my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070058my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070059my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070060my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070061my $conststructsfile = "$D/const_structs.checkpatch";
Jerome Forissier75ad8c52017-05-08 15:56:00 -070062my $typedefsfile = "";
John Brooks737c0762017-07-10 15:52:24 -070063my $color = "auto";
Vadim Bendebury98005e82019-03-07 16:28:38 -080064my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
Joe Perchesdbbf8692019-09-25 16:46:52 -070065# git output parsing needs US English output, so first set backtick child process LANGUAGE
66my $git_command ='export LANGUAGE=en_US.UTF-8; git';
Hannes Eder77f5b102009-09-21 17:04:37 -070067
68sub help {
69 my ($exitcode) = @_;
70
71 print << "EOM";
72Usage: $P [OPTION]... [FILE]...
73Version: $V
74
75Options:
76 -q, --quiet quiet
77 --no-tree run without a kernel tree
78 --no-signoff do not check for 'Signed-off-by' line
79 --patch treat FILE as patchfile (default)
80 --emacs emacs compile window format
81 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070082 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070083 -g, --git treat FILE as a single commit or git revision range
84 single git commit with:
85 <rev>
86 <rev>^
87 <rev>~n
88 multiple git commits with:
89 <rev1>..<rev2>
90 <rev1>...<rev2>
91 <rev>-<count>
92 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070093 -f, --file treat FILE as regular source file
94 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070095 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070096 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070097 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070098 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080099 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -0700100 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -0700101 --root=PATH PATH to the kernel tree root
102 --no-summary suppress the per-file summary
103 --mailback only produce a report in case of warnings/errors
104 --summary-file include the filename in summary
105 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
106 'values', 'possible', 'type', and 'attr' (default
107 is all off)
108 --test-only=WORD report only warnings/errors containing WORD
109 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700110 --fix EXPERIMENTAL - may create horrible results
111 If correctable single-line errors exist, create
112 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
113 with potential errors corrected to the preferred
114 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800115 --fix-inplace EXPERIMENTAL - may create horrible results
116 Is the same as --fix, but overwrites the input
117 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700118 --ignore-perl-version override checking of perl version. expect
119 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700120 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700121 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700122 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700123 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700124 --color[=WHEN] Use colors 'always', 'never', or only when output
125 is a terminal ('auto'). Default is 'auto'.
Hannes Eder77f5b102009-09-21 17:04:37 -0700126 -h, --help, --version display this help and exit
127
128When FILE is - read standard input.
129EOM
130
131 exit($exitcode);
132}
133
Joe Perches3beb42e2016-05-20 17:04:14 -0700134sub uniq {
135 my %seen;
136 return grep { !$seen{$_}++ } @_;
137}
138
139sub list_types {
140 my ($exitcode) = @_;
141
142 my $count = 0;
143
144 local $/ = undef;
145
146 open(my $script, '<', abs_path($P)) or
147 die "$P: Can't read '$P' $!\n";
148
149 my $text = <$script>;
150 close($script);
151
152 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700153 # Also catch when type or level is passed through a variable
154 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700155 push (@types, $_);
156 }
157 @types = sort(uniq(@types));
158 print("#\tMessage type\n\n");
159 foreach my $type (@types) {
160 print(++$count . "\t" . $type . "\n");
161 }
162
163 exit($exitcode);
164}
165
Joe Perches000d1cc12011-07-25 17:13:25 -0700166my $conf = which_conf($configuration_file);
167if (-f $conf) {
168 my @conf_args;
169 open(my $conffile, '<', "$conf")
170 or warn "$P: Can't find a readable $configuration_file file $!\n";
171
172 while (<$conffile>) {
173 my $line = $_;
174
175 $line =~ s/\s*\n?$//g;
176 $line =~ s/^\s*//g;
177 $line =~ s/\s+/ /g;
178
179 next if ($line =~ m/^\s*#/);
180 next if ($line =~ m/^\s*$/);
181
182 my @words = split(" ", $line);
183 foreach my $word (@words) {
184 last if ($word =~ m/^#/);
185 push (@conf_args, $word);
186 }
187 }
188 close($conffile);
189 unshift(@ARGV, @conf_args) if @conf_args;
190}
191
John Brooks737c0762017-07-10 15:52:24 -0700192# Perl's Getopt::Long allows options to take optional arguments after a space.
193# Prevent --color by itself from consuming other arguments
194foreach (@ARGV) {
195 if ($_ eq "--color" || $_ eq "-color") {
196 $_ = "--color=$color";
197 }
198}
199
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700200GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700201 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700202 'tree!' => \$tree,
203 'signoff!' => \$chk_signoff,
204 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700205 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800206 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700207 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700208 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700209 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700210 'subjective!' => \$check,
211 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700212 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700213 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700214 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700215 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800216 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700217 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700218 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800219 'summary!' => \$summary,
220 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800221 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700222 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800223 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700224 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800225 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700226 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700227 'codespell!' => \$codespell,
228 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700229 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700230 'color=s' => \$color,
231 'no-color' => \$color, #keep old behaviors of -nocolor
232 'nocolor' => \$color, #keep old behaviors of -nocolor
Hannes Eder77f5b102009-09-21 17:04:37 -0700233 'h|help' => \$help,
234 'version' => \$help
235) or help(1);
236
237help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700238
Joe Perches3beb42e2016-05-20 17:04:14 -0700239list_types(0) if ($list_types);
240
Joe Perches9624b8d2014-01-23 15:54:44 -0800241$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700242$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800243
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700244my $exit = 0;
245
Joe Perches5b579802018-08-21 21:57:33 -0700246my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700247if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700248 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700249 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700250 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700251}
252
Allen Hubbe45107ff2016-08-02 14:04:47 -0700253#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700254if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700255 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700256}
257
John Brooks737c0762017-07-10 15:52:24 -0700258if ($color =~ /^[01]$/) {
259 $color = !$color;
260} elsif ($color =~ /^always$/i) {
261 $color = 1;
262} elsif ($color =~ /^never$/i) {
263 $color = 0;
264} elsif ($color =~ /^auto$/i) {
265 $color = (-t STDOUT);
266} else {
267 die "Invalid color mode: $color\n";
268}
269
Joe Perches91bfe482013-09-11 14:23:59 -0700270sub hash_save_array_words {
271 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700272
Joe Perches91bfe482013-09-11 14:23:59 -0700273 my @array = split(/,/, join(',', @$arrayRef));
274 foreach my $word (@array) {
275 $word =~ s/\s*\n?$//g;
276 $word =~ s/^\s*//g;
277 $word =~ s/\s+/ /g;
278 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700279
Joe Perches91bfe482013-09-11 14:23:59 -0700280 next if ($word =~ m/^\s*#/);
281 next if ($word =~ m/^\s*$/);
282
283 $hashRef->{$word}++;
284 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700285}
286
Joe Perches91bfe482013-09-11 14:23:59 -0700287sub hash_show_words {
288 my ($hashRef, $prefix) = @_;
289
Joe Perches3c816e42015-06-25 15:03:29 -0700290 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700291 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700292 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700293 print " $word";
294 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700295 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700296 }
297}
298
299hash_save_array_words(\%ignore_type, \@ignore);
300hash_save_array_words(\%use_type, \@use);
301
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800302my $dbg_values = 0;
303my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700304my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700305my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800306for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800307 ## no critic
308 eval "\${dbg_$key} = '$debug{$key}';";
309 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800310}
311
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700312my $rpt_cleaners = 0;
313
Andy Whitcroft8905a672007-11-28 16:21:06 -0800314if ($terse) {
315 $emacs = 1;
316 $quiet++;
317}
318
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700319if ($tree) {
320 if (defined $root) {
321 if (!top_of_kernel_tree($root)) {
322 die "$P: $root: --root does not point at a valid tree\n";
323 }
324 } else {
325 if (top_of_kernel_tree('.')) {
326 $root = '.';
327 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
328 top_of_kernel_tree($1)) {
329 $root = $1;
330 }
331 }
332
333 if (!defined $root) {
334 print "Must be run from the top-level dir. of a kernel tree\n";
335 exit(2);
336 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700337}
338
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700339my $emitted_corrupt = 0;
340
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700341our $Ident = qr{
342 [A-Za-z_][A-Za-z\d_]*
343 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
344 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700345our $Storage = qr{extern|static|asmlinkage};
346our $Sparse = qr{
347 __user|
348 __kernel|
349 __force|
350 __iomem|
351 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800352 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700353 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700354 __refconst|
355 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800356 __rcu|
357 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700358 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800359our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
360our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
361our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
362our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
363our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700364
Wolfram Sang52131292010-03-05 13:43:51 -0800365# Notes to $Attribute:
366# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700367our $Attribute = qr{
368 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700369 __percpu|
370 __nocast|
371 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200372 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700373 __packed__|
374 __packed2__|
375 __naked|
376 __maybe_unused|
377 __always_unused|
378 __noreturn|
379 __used|
380 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800381 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700382 __noclone|
383 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700384 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700385 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700386 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700387 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700388 ____cacheline_aligned|
389 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800390 ____cacheline_internodealigned_in_smp|
391 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700392 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700393our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700394our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700395our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
396our $Lval = qr{$Ident(?:$Member)*};
397
Joe Perches95e2c602013-07-03 15:05:20 -0700398our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
399our $Binary = qr{(?i)0b[01]+$Int_type?};
400our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
401our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700402our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800403our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800404our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
405our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
406our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800407our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700408our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800409our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700410our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700411our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700412our $Operators = qr{
413 <=|>=|==|!=|
414 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700415 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700416 }x;
417
Joe Perches91cb5192014-04-03 14:49:32 -0700418our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
419
Joe Perchesab7e23f2015-04-16 12:44:22 -0700420our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800421our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700422our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700423our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800424our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700425our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800426our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700427our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800428
Joe Perches15662b32011-10-31 17:13:12 -0700429our $NON_ASCII_UTF8 = qr{
430 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700431 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
432 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
433 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
434 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
435 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
436 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
437}x;
438
Joe Perches15662b32011-10-31 17:13:12 -0700439our $UTF8 = qr{
440 [\x09\x0A\x0D\x20-\x7E] # ASCII
441 | $NON_ASCII_UTF8
442}x;
443
Joe Perchese6176fa2015-06-25 15:02:49 -0700444our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800445our $typeOtherOSTypedefs = qr{(?x:
446 u_(?:char|short|int|long) | # bsd
447 u(?:nchar|short|int|long) # sysv
448)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700449our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700450 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700451 atomic_t
452)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700453our $typeTypedefs = qr{(?x:
454 $typeC99Typedefs\b|
455 $typeOtherOSTypedefs\b|
456 $typeKernelTypedefs\b
457)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700458
Joe Perches6d32f7a2015-11-06 16:31:37 -0800459our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
460
Joe Perches691e6692010-03-05 13:43:51 -0800461our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800462 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700463 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches87bd4992017-11-17 15:28:48 -0800464 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700465 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700466 panic|
Joe Perches06668722013-11-12 15:10:07 -0800467 MODULE_[A-Z_]+|
468 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800469)};
470
Joe Perchese29a70f2019-03-07 16:28:35 -0800471our $allocFunctions = qr{(?x:
472 (?:(?:devm_)?
473 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
474 kstrdup(?:_const)? |
475 kmemdup(?:_nul)?) |
476 (?:\w+)?alloc_skb(?:ip_align)? |
477 # dev_alloc_skb/netdev_alloc_skb, et al
478 dma_alloc_coherent
479)};
480
Joe Perches20112472011-07-25 17:13:23 -0700481our $signature_tags = qr{(?xi:
482 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800483 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700484 Acked-by:|
485 Tested-by:|
486 Reviewed-by:|
487 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700488 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700489 To:|
490 Cc:
491)};
492
Joe Perches18130872014-08-06 16:11:22 -0700493our @typeListMisordered = (
494 qr{char\s+(?:un)?signed},
495 qr{int\s+(?:(?:un)?signed\s+)?short\s},
496 qr{int\s+short(?:\s+(?:un)?signed)},
497 qr{short\s+int(?:\s+(?:un)?signed)},
498 qr{(?:un)?signed\s+int\s+short},
499 qr{short\s+(?:un)?signed},
500 qr{long\s+int\s+(?:un)?signed},
501 qr{int\s+long\s+(?:un)?signed},
502 qr{long\s+(?:un)?signed\s+int},
503 qr{int\s+(?:un)?signed\s+long},
504 qr{int\s+(?:un)?signed},
505 qr{int\s+long\s+long\s+(?:un)?signed},
506 qr{long\s+long\s+int\s+(?:un)?signed},
507 qr{long\s+long\s+(?:un)?signed\s+int},
508 qr{long\s+long\s+(?:un)?signed},
509 qr{long\s+(?:un)?signed},
510);
511
Andy Whitcroft8905a672007-11-28 16:21:06 -0800512our @typeList = (
513 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700514 qr{(?:(?:un)?signed\s+)?char},
515 qr{(?:(?:un)?signed\s+)?short\s+int},
516 qr{(?:(?:un)?signed\s+)?short},
517 qr{(?:(?:un)?signed\s+)?int},
518 qr{(?:(?:un)?signed\s+)?long\s+int},
519 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
520 qr{(?:(?:un)?signed\s+)?long\s+long},
521 qr{(?:(?:un)?signed\s+)?long},
522 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800523 qr{float},
524 qr{double},
525 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800526 qr{struct\s+$Ident},
527 qr{union\s+$Ident},
528 qr{enum\s+$Ident},
529 qr{${Ident}_t},
530 qr{${Ident}_handler},
531 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700532 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800533);
Joe Perches938224b2016-01-20 14:59:15 -0800534
535our $C90_int_types = qr{(?x:
536 long\s+long\s+int\s+(?:un)?signed|
537 long\s+long\s+(?:un)?signed\s+int|
538 long\s+long\s+(?:un)?signed|
539 (?:(?:un)?signed\s+)?long\s+long\s+int|
540 (?:(?:un)?signed\s+)?long\s+long|
541 int\s+long\s+long\s+(?:un)?signed|
542 int\s+(?:(?:un)?signed\s+)?long\s+long|
543
544 long\s+int\s+(?:un)?signed|
545 long\s+(?:un)?signed\s+int|
546 long\s+(?:un)?signed|
547 (?:(?:un)?signed\s+)?long\s+int|
548 (?:(?:un)?signed\s+)?long|
549 int\s+long\s+(?:un)?signed|
550 int\s+(?:(?:un)?signed\s+)?long|
551
552 int\s+(?:un)?signed|
553 (?:(?:un)?signed\s+)?int
554)};
555
Alex Dowad485ff232015-06-25 15:02:52 -0700556our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700557our @typeListWithAttr = (
558 @typeList,
559 qr{struct\s+$InitAttribute\s+$Ident},
560 qr{union\s+$InitAttribute\s+$Ident},
561);
562
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700563our @modifierList = (
564 qr{fastcall},
565);
Alex Dowad485ff232015-06-25 15:02:52 -0700566our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800567
Joe Perches24358802014-04-03 14:49:13 -0700568our @mode_permission_funcs = (
569 ["module_param", 3],
570 ["module_param_(?:array|named|string)", 4],
571 ["module_param_array_named", 5],
572 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
573 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700574 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
575 ["IIO_DEV_ATTR_[A-Z_]+", 1],
576 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
577 ["SENSOR_TEMPLATE(?:_2|)", 3],
578 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700579);
580
Joe Perches515a2352014-04-03 14:49:24 -0700581#Create a search pattern for all these functions to speed up a loop below
582our $mode_perms_search = "";
583foreach my $entry (@mode_permission_funcs) {
584 $mode_perms_search .= '|' if ($mode_perms_search ne "");
585 $mode_perms_search .= $entry->[0];
586}
Joe Perches00180462018-02-06 15:38:55 -0800587$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700588
Joe Perches9189c7e2018-09-07 15:26:18 -0700589our %deprecated_apis = (
590 "synchronize_rcu_bh" => "synchronize_rcu",
591 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
592 "call_rcu_bh" => "call_rcu",
593 "rcu_barrier_bh" => "rcu_barrier",
594 "synchronize_sched" => "synchronize_rcu",
595 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
596 "call_rcu_sched" => "call_rcu",
597 "rcu_barrier_sched" => "rcu_barrier",
598 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
599 "cond_synchronize_sched" => "cond_synchronize_rcu",
600);
601
602#Create a search pattern for all these strings to speed up a loop below
603our $deprecated_apis_search = "";
604foreach my $entry (keys %deprecated_apis) {
605 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
606 $deprecated_apis_search .= $entry;
607}
608$deprecated_apis_search = "(?:${deprecated_apis_search})";
609
Joe Perchesb392c642015-04-16 12:44:16 -0700610our $mode_perms_world_writable = qr{
611 S_IWUGO |
612 S_IWOTH |
613 S_IRWXUGO |
614 S_IALLUGO |
615 0[0-7][0-7][2367]
616}x;
617
Joe Perchesf90774e2016-10-11 13:51:47 -0700618our %mode_permission_string_types = (
619 "S_IRWXU" => 0700,
620 "S_IRUSR" => 0400,
621 "S_IWUSR" => 0200,
622 "S_IXUSR" => 0100,
623 "S_IRWXG" => 0070,
624 "S_IRGRP" => 0040,
625 "S_IWGRP" => 0020,
626 "S_IXGRP" => 0010,
627 "S_IRWXO" => 0007,
628 "S_IROTH" => 0004,
629 "S_IWOTH" => 0002,
630 "S_IXOTH" => 0001,
631 "S_IRWXUGO" => 0777,
632 "S_IRUGO" => 0444,
633 "S_IWUGO" => 0222,
634 "S_IXUGO" => 0111,
635);
636
637#Create a search pattern for all these strings to speed up a loop below
638our $mode_perms_string_search = "";
639foreach my $entry (keys %mode_permission_string_types) {
640 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
641 $mode_perms_string_search .= $entry;
642}
Joe Perches00180462018-02-06 15:38:55 -0800643our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
644our $multi_mode_perms_string_search = qr{
645 ${single_mode_perms_string_search}
646 (?:\s*\|\s*${single_mode_perms_string_search})*
647}x;
648
649sub perms_to_octal {
650 my ($string) = @_;
651
652 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
653
654 my $val = "";
655 my $oval = "";
656 my $to = 0;
657 my $curpos = 0;
658 my $lastpos = 0;
659 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
660 $curpos = pos($string);
661 my $match = $2;
662 my $omatch = $1;
663 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
664 $lastpos = $curpos;
665 $to |= $mode_permission_string_types{$match};
666 $val .= '\s*\|\s*' if ($val ne "");
667 $val .= $match;
668 $oval .= $omatch;
669 }
670 $oval =~ s/^\s*\|\s*//;
671 $oval =~ s/\s*\|\s*$//;
672 return sprintf("%04o", $to);
673}
Joe Perchesf90774e2016-10-11 13:51:47 -0700674
Wolfram Sang7840a942010-08-09 17:20:57 -0700675our $allowed_asm_includes = qr{(?x:
676 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700677 memory|
678 time|
679 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700680)};
681# memory.h: ARM has a custom one
682
Kees Cook66b47b42014-10-13 15:51:57 -0700683# Load common spelling mistakes and build regular expression list.
684my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700685my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700686
Joe Perches36061e32014-12-10 15:51:43 -0800687if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800688 while (<$spelling>) {
689 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700690
Joe Perches36061e32014-12-10 15:51:43 -0800691 $line =~ s/\s*\n?$//g;
692 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700693
Joe Perches36061e32014-12-10 15:51:43 -0800694 next if ($line =~ m/^\s*#/);
695 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700696
Joe Perches36061e32014-12-10 15:51:43 -0800697 my ($suspect, $fix) = split(/\|\|/, $line);
698
Joe Perches36061e32014-12-10 15:51:43 -0800699 $spelling_fix{$suspect} = $fix;
700 }
701 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800702} else {
703 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700704}
Kees Cook66b47b42014-10-13 15:51:57 -0700705
Joe Perchesebfd7d62015-04-16 12:44:14 -0700706if ($codespell) {
707 if (open(my $spelling, '<', $codespellfile)) {
708 while (<$spelling>) {
709 my $line = $_;
710
711 $line =~ s/\s*\n?$//g;
712 $line =~ s/^\s*//g;
713
714 next if ($line =~ m/^\s*#/);
715 next if ($line =~ m/^\s*$/);
716 next if ($line =~ m/, disabled/i);
717
718 $line =~ s/,.*$//;
719
720 my ($suspect, $fix) = split(/->/, $line);
721
722 $spelling_fix{$suspect} = $fix;
723 }
724 close($spelling);
725 } else {
726 warn "No codespell typos will be found - file '$codespellfile': $!\n";
727 }
728}
729
730$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
731
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700732sub read_words {
733 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700734
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700735 if (open(my $words, '<', $file)) {
736 while (<$words>) {
737 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700738
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700739 $line =~ s/\s*\n?$//g;
740 $line =~ s/^\s*//g;
741
742 next if ($line =~ m/^\s*#/);
743 next if ($line =~ m/^\s*$/);
744 if ($line =~ /\s/) {
745 print("$file: '$line' invalid - ignored\n");
746 next;
747 }
748
749 $$wordsRef .= '|' if ($$wordsRef ne "");
750 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700751 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700752 close($file);
753 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700754 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700755
756 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700757}
758
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700759my $const_structs = "";
760read_words(\$const_structs, $conststructsfile)
761 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
762
763my $typeOtherTypedefs = "";
764if (length($typedefsfile)) {
765 read_words(\$typeOtherTypedefs, $typedefsfile)
766 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
767}
768$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
769
Andy Whitcroft8905a672007-11-28 16:21:06 -0800770sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700771 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
772 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700773 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700774 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700775 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700776 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700777 (?:$typeTypedefs\b)|
778 (?:${all}\b)
779 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800780 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700781 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800782 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800783 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700784 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700785 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800786 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700787 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800788 }x;
Joe Perches18130872014-08-06 16:11:22 -0700789 $NonptrTypeMisordered = qr{
790 (?:$Modifier\s+|const\s+)*
791 (?:
792 (?:${Misordered}\b)
793 )
794 (?:\s+$Modifier|\s+const)*
795 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700796 $NonptrTypeWithAttr = qr{
797 (?:$Modifier\s+|const\s+)*
798 (?:
799 (?:typeof|__typeof__)\s*\([^\)]*\)|
800 (?:$typeTypedefs\b)|
801 (?:${allWithAttr}\b)
802 )
803 (?:\s+$Modifier|\s+const)*
804 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800805 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700806 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700807 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700808 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800809 }x;
Joe Perches18130872014-08-06 16:11:22 -0700810 $TypeMisordered = qr{
811 $NonptrTypeMisordered
812 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
813 (?:\s+$Inline|\s+$Modifier)*
814 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700815 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700816 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800817}
818build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700819
Joe Perches7d2367a2011-07-25 17:13:22 -0700820our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700821
822# Using $balanced_parens, $LvalOrFunc, or $FuncArg
823# requires at least perl version v5.10.0
824# Any use must be runtime checked with $^V
825
826our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700827our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800828our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700829
Joe Perchesf8422302014-08-06 16:11:31 -0700830our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700831 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700832 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700833 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
834 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700835)};
836
Joe Perches7d2367a2011-07-25 17:13:22 -0700837sub deparenthesize {
838 my ($string) = @_;
839 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700840
841 while ($string =~ /^\s*\(.*\)\s*$/) {
842 $string =~ s@^\s*\(\s*@@;
843 $string =~ s@\s*\)\s*$@@;
844 }
845
Joe Perches7d2367a2011-07-25 17:13:22 -0700846 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700847
Joe Perches7d2367a2011-07-25 17:13:22 -0700848 return $string;
849}
850
Joe Perches34456862013-07-03 15:05:34 -0700851sub seed_camelcase_file {
852 my ($file) = @_;
853
854 return if (!(-f $file));
855
856 local $/;
857
858 open(my $include_file, '<', "$file")
859 or warn "$P: Can't read '$file' $!\n";
860 my $text = <$include_file>;
861 close($include_file);
862
863 my @lines = split('\n', $text);
864
865 foreach my $line (@lines) {
866 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
867 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
868 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800869 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
870 $camelcase{$1} = 1;
871 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
Joe Perches34456862013-07-03 15:05:34 -0700872 $camelcase{$1} = 1;
873 }
874 }
875}
876
Joe Perchescd28b112019-12-04 16:52:09 -0800877our %maintained_status = ();
878
Joe Perches85b0ee12016-10-11 13:51:44 -0700879sub is_maintained_obsolete {
880 my ($filename) = @_;
881
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800882 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700883
Joe Perchescd28b112019-12-04 16:52:09 -0800884 if (!exists($maintained_status{$filename})) {
885 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
886 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700887
Joe Perchescd28b112019-12-04 16:52:09 -0800888 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700889}
890
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700891sub is_SPDX_License_valid {
892 my ($license) = @_;
893
Joe Perches56294112018-08-21 21:58:04 -0700894 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700895
Joe Perches56294112018-08-21 21:58:04 -0700896 my $root_path = abs_path($root);
897 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700898 return 0 if ($status ne "");
899 return 1;
900}
901
Joe Perches34456862013-07-03 15:05:34 -0700902my $camelcase_seeded = 0;
903sub seed_camelcase_includes {
904 return if ($camelcase_seeded);
905
906 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700907 my $camelcase_cache = "";
908 my @include_files = ();
909
910 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700911
Richard Genoud3645e322014-02-10 14:25:32 -0800912 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700913 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -0700914 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700915 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700916 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700917 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700918 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700919 @include_files = split('\n', $files);
920 foreach my $file (@include_files) {
921 my $date = POSIX::strftime("%Y%m%d%H%M",
922 localtime((stat $file)[9]));
923 $last_mod_date = $date if ($last_mod_date < $date);
924 }
925 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700926 }
Joe Perchesc707a812013-07-08 16:00:43 -0700927
928 if ($camelcase_cache ne "" && -f $camelcase_cache) {
929 open(my $camelcase_file, '<', "$camelcase_cache")
930 or warn "$P: Can't read '$camelcase_cache' $!\n";
931 while (<$camelcase_file>) {
932 chomp;
933 $camelcase{$_} = 1;
934 }
935 close($camelcase_file);
936
937 return;
938 }
939
Richard Genoud3645e322014-02-10 14:25:32 -0800940 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700941 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700942 @include_files = split('\n', $files);
943 }
944
Joe Perches34456862013-07-03 15:05:34 -0700945 foreach my $file (@include_files) {
946 seed_camelcase_file($file);
947 }
Joe Perches351b2a12013-07-03 15:05:36 -0700948
Joe Perchesc707a812013-07-08 16:00:43 -0700949 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700950 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700951 open(my $camelcase_file, '>', "$camelcase_cache")
952 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700953 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
954 print $camelcase_file ("$_\n");
955 }
956 close($camelcase_file);
957 }
Joe Perches34456862013-07-03 15:05:34 -0700958}
959
Joe Perchesd311cd42014-08-06 16:10:57 -0700960sub git_commit_info {
961 my ($commit, $id, $desc) = @_;
962
963 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
964
Joe Perchesdbbf8692019-09-25 16:46:52 -0700965 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -0700966 $output =~ s/^\s*//gm;
967 my @lines = split("\n", $output);
968
Joe Perches0d7835f2015-02-13 14:38:35 -0800969 return ($id, $desc) if ($#lines < 0);
970
Sean Christopherson5a7f4452019-09-25 16:46:49 -0700971 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -0700972# Maybe one day convert this block of bash into something that returns
973# all matching commit ids, but it's very slow...
974#
975# echo "checking commits $1..."
976# git rev-list --remotes | grep -i "^$1" |
977# while read line ; do
978# git log --format='%H %s' -1 $line |
979# echo "commit $(cut -c 1-12,41-)"
980# done
981 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700982 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700983 } else {
984 $id = substr($lines[0], 0, 12);
985 $desc = substr($lines[0], 41);
986 }
987
988 return ($id, $desc);
989}
990
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700991$chk_signoff = 0 if ($file);
992
Andy Whitcroft00df3442007-06-08 13:47:06 -0700993my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800994my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700995my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700996my @fixed_inserted = ();
997my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700998my $fixlinenr = -1;
999
Du, Changbin4a593c32016-05-20 17:04:16 -07001000# If input is git commits, extract all commits from the commit expressions.
1001# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1002die "$P: No git repository found\n" if ($git && !-e ".git");
1003
1004if ($git) {
1005 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001006 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001007 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001008 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1009 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001010 } elsif ($commit_expr =~ m/\.\./) {
1011 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001012 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001013 $git_range = "-1 $commit_expr";
1014 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001015 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001016 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001017 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1018 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001019 my $sha1 = $1;
1020 my $subject = $2;
1021 unshift(@commits, $sha1);
1022 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001023 }
1024 }
1025 die "$P: no git commits after extraction!\n" if (@commits == 0);
1026 @ARGV = @commits;
1027}
1028
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001029my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001030$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001031for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001032 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -07001033 if ($git) {
1034 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1035 die "$P: $filename: git format-patch failed - $!\n";
1036 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001037 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001038 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001039 } elsif ($filename eq '-') {
1040 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001041 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001042 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001043 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001044 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001045 if ($filename eq '-') {
1046 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001047 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001048 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001049 } else {
1050 $vname = $filename;
1051 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001052 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001053 chomp;
1054 push(@rawlines, $_);
1055 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001056 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001057
1058 if ($#ARGV > 0 && $quiet == 0) {
1059 print '-' x length($vname) . "\n";
1060 print "$vname\n";
1061 print '-' x length($vname) . "\n";
1062 }
1063
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001064 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001065 $exit = 1;
1066 }
1067 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001068 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001069 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001070 @fixed_inserted = ();
1071 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001072 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001073 @modifierListFile = ();
1074 @typeListFile = ();
1075 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001076}
1077
Joe Perchesd8469f12015-06-25 15:03:00 -07001078if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001079 hash_show_words(\%use_type, "Used");
1080 hash_show_words(\%ignore_type, "Ignored");
1081
Joe Perches5b579802018-08-21 21:57:33 -07001082 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001083 print << "EOM"
1084
1085NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001086 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001087EOM
1088 }
1089 if ($exit) {
1090 print << "EOM"
1091
1092NOTE: If any of the errors are false positives, please report
1093 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1094EOM
1095 }
1096}
1097
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001098exit($exit);
1099
1100sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001101 my ($root) = @_;
1102
1103 my @tree_check = (
1104 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1105 "README", "Documentation", "arch", "include", "drivers",
1106 "fs", "init", "ipc", "kernel", "lib", "scripts",
1107 );
1108
1109 foreach my $check (@tree_check) {
1110 if (! -e $root . '/' . $check) {
1111 return 0;
1112 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001113 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001114 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001115}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001116
Joe Perches20112472011-07-25 17:13:23 -07001117sub parse_email {
1118 my ($formatted_email) = @_;
1119
1120 my $name = "";
1121 my $address = "";
1122 my $comment = "";
1123
1124 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1125 $name = $1;
1126 $address = $2;
1127 $comment = $3 if defined $3;
1128 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1129 $address = $1;
1130 $comment = $2 if defined $2;
1131 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1132 $address = $1;
1133 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001134 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001135 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001136 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001137 $name =~ s/^\"|\"$//g;
1138 # If there's a name left after stripping spaces and
1139 # leading quotes, and the address doesn't have both
1140 # leading and trailing angle brackets, the address
1141 # is invalid. ie:
1142 # "joe smith joe@smith.com" bad
1143 # "joe smith <joe@smith.com" bad
1144 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1145 $name = "";
1146 $address = "";
1147 $comment = "";
1148 }
1149 }
1150
Joe Perches3705ce52013-07-03 15:05:31 -07001151 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001152 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001153 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001154 $address =~ s/^\<|\>$//g;
1155
1156 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1157 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1158 $name = "\"$name\"";
1159 }
1160
1161 return ($name, $address, $comment);
1162}
1163
1164sub format_email {
1165 my ($name, $address) = @_;
1166
1167 my $formatted_email;
1168
Joe Perches3705ce52013-07-03 15:05:31 -07001169 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001170 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001171 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001172
1173 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1174 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1175 $name = "\"$name\"";
1176 }
1177
1178 if ("$name" eq "") {
1179 $formatted_email = "$address";
1180 } else {
1181 $formatted_email = "$name <$address>";
1182 }
1183
1184 return $formatted_email;
1185}
1186
Joe Perchesd311cd42014-08-06 16:10:57 -07001187sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001188 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001189
Joe Perchesbd474ca2014-08-06 16:11:10 -07001190 foreach my $path (split(/:/, $ENV{PATH})) {
1191 if (-e "$path/$bin") {
1192 return "$path/$bin";
1193 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001194 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001195
Joe Perchesbd474ca2014-08-06 16:11:10 -07001196 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001197}
1198
Joe Perches000d1cc12011-07-25 17:13:25 -07001199sub which_conf {
1200 my ($conf) = @_;
1201
1202 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1203 if (-e "$path/$conf") {
1204 return "$path/$conf";
1205 }
1206 }
1207
1208 return "";
1209}
1210
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001211sub expand_tabs {
1212 my ($str) = @_;
1213
1214 my $res = '';
1215 my $n = 0;
1216 for my $c (split(//, $str)) {
1217 if ($c eq "\t") {
1218 $res .= ' ';
1219 $n++;
1220 for (; ($n % 8) != 0; $n++) {
1221 $res .= ' ';
1222 }
1223 next;
1224 }
1225 $res .= $c;
1226 $n++;
1227 }
1228
1229 return $res;
1230}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001231sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001232 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001233 return $res;
1234}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001235
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001236sub line_stats {
1237 my ($line) = @_;
1238
1239 # Drop the diff line leader and expand tabs
1240 $line =~ s/^.//;
1241 $line = expand_tabs($line);
1242
1243 # Pick the indent from the front of the line.
1244 my ($white) = ($line =~ /^(\s*)/);
1245
1246 return (length($line), length($white));
1247}
1248
Andy Whitcroft773647a2008-03-28 14:15:58 -07001249my $sanitise_quote = '';
1250
1251sub sanitise_line_reset {
1252 my ($in_comment) = @_;
1253
1254 if ($in_comment) {
1255 $sanitise_quote = '*/';
1256 } else {
1257 $sanitise_quote = '';
1258 }
1259}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001260sub sanitise_line {
1261 my ($line) = @_;
1262
1263 my $res = '';
1264 my $l = '';
1265
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001266 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001267 my $off = 0;
1268 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001269
Andy Whitcroft773647a2008-03-28 14:15:58 -07001270 # Always copy over the diff marker.
1271 $res = substr($line, 0, 1);
1272
1273 for ($off = 1; $off < length($line); $off++) {
1274 $c = substr($line, $off, 1);
1275
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001276 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001277 # and end, all to $;.
1278 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1279 $sanitise_quote = '*/';
1280
1281 substr($res, $off, 2, "$;$;");
1282 $off++;
1283 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001284 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001285 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001286 $sanitise_quote = '';
1287 substr($res, $off, 2, "$;$;");
1288 $off++;
1289 next;
1290 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001291 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1292 $sanitise_quote = '//';
1293
1294 substr($res, $off, 2, $sanitise_quote);
1295 $off++;
1296 next;
1297 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001298
1299 # A \ in a string means ignore the next character.
1300 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1301 $c eq "\\") {
1302 substr($res, $off, 2, 'XX');
1303 $off++;
1304 next;
1305 }
1306 # Regular quotes.
1307 if ($c eq "'" || $c eq '"') {
1308 if ($sanitise_quote eq '') {
1309 $sanitise_quote = $c;
1310
1311 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001312 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001313 } elsif ($sanitise_quote eq $c) {
1314 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001315 }
1316 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001317
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001318 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001319 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1320 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001321 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1322 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001323 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1324 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001325 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001326 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001327 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001328 }
1329
Daniel Walker113f04a2009-09-21 17:04:35 -07001330 if ($sanitise_quote eq '//') {
1331 $sanitise_quote = '';
1332 }
1333
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001334 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001335 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001336 my $clean = 'X' x length($1);
1337 $res =~ s@\<.*\>@<$clean>@;
1338
1339 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001340 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001341 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001342 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001343 }
1344
Joe Perchesdadf6802016-08-02 14:04:33 -07001345 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1346 my $match = $1;
1347 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1348 }
1349
Andy Whitcroft00df3442007-06-08 13:47:06 -07001350 return $res;
1351}
1352
Joe Perchesa6962d72013-04-29 16:18:13 -07001353sub get_quoted_string {
1354 my ($line, $rawline) = @_;
1355
Joe Perches478b1792018-04-10 16:33:34 -07001356 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001357 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001358 return substr($rawline, $-[0], $+[0] - $-[0]);
1359}
1360
Andy Whitcroft8905a672007-11-28 16:21:06 -08001361sub ctx_statement_block {
1362 my ($linenr, $remain, $off) = @_;
1363 my $line = $linenr - 1;
1364 my $blk = '';
1365 my $soff = $off;
1366 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001367 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001368
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001369 my $loff = 0;
1370
Andy Whitcroft8905a672007-11-28 16:21:06 -08001371 my $type = '';
1372 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001373 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001374 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001375 my $c;
1376 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001377
1378 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001379 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001380 @stack = (['', 0]) if ($#stack == -1);
1381
Andy Whitcroft773647a2008-03-28 14:15:58 -07001382 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001383 # If we are about to drop off the end, pull in more
1384 # context.
1385 if ($off >= $len) {
1386 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001387 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001388 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001389 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001390 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001391 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001392 $len = length($blk);
1393 $line++;
1394 last;
1395 }
1396 # Bail if there is no further context.
1397 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001398 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001399 last;
1400 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001401 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1402 $level++;
1403 $type = '#';
1404 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001405 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001406 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001407 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001408 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001409
Andy Whitcroft773647a2008-03-28 14:15:58 -07001410 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001411
1412 # Handle nested #if/#else.
1413 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1414 push(@stack, [ $type, $level ]);
1415 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1416 ($type, $level) = @{$stack[$#stack - 1]};
1417 } elsif ($remainder =~ /^#\s*endif\b/) {
1418 ($type, $level) = @{pop(@stack)};
1419 }
1420
Andy Whitcroft8905a672007-11-28 16:21:06 -08001421 # Statement ends at the ';' or a close '}' at the
1422 # outermost level.
1423 if ($level == 0 && $c eq ';') {
1424 last;
1425 }
1426
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001427 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001428 if ($level == 0 && $coff_set == 0 &&
1429 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1430 $remainder =~ /^(else)(?:\s|{)/ &&
1431 $remainder !~ /^else\s+if\b/) {
1432 $coff = $off + length($1) - 1;
1433 $coff_set = 1;
1434 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1435 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001436 }
1437
Andy Whitcroft8905a672007-11-28 16:21:06 -08001438 if (($type eq '' || $type eq '(') && $c eq '(') {
1439 $level++;
1440 $type = '(';
1441 }
1442 if ($type eq '(' && $c eq ')') {
1443 $level--;
1444 $type = ($level != 0)? '(' : '';
1445
1446 if ($level == 0 && $coff < $soff) {
1447 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001448 $coff_set = 1;
1449 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001450 }
1451 }
1452 if (($type eq '' || $type eq '{') && $c eq '{') {
1453 $level++;
1454 $type = '{';
1455 }
1456 if ($type eq '{' && $c eq '}') {
1457 $level--;
1458 $type = ($level != 0)? '{' : '';
1459
1460 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001461 if (substr($blk, $off + 1, 1) eq ';') {
1462 $off++;
1463 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001464 last;
1465 }
1466 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001467 # Preprocessor commands end at the newline unless escaped.
1468 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1469 $level--;
1470 $type = '';
1471 $off++;
1472 last;
1473 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001474 $off++;
1475 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001476 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001477 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001478 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001479 $line++;
1480 $remain--;
1481 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001482
1483 my $statement = substr($blk, $soff, $off - $soff + 1);
1484 my $condition = substr($blk, $soff, $coff - $soff + 1);
1485
1486 #warn "STATEMENT<$statement>\n";
1487 #warn "CONDITION<$condition>\n";
1488
Andy Whitcroft773647a2008-03-28 14:15:58 -07001489 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001490
1491 return ($statement, $condition,
1492 $line, $remain + 1, $off - $loff + 1, $level);
1493}
1494
Andy Whitcroftcf655042008-03-04 14:28:20 -08001495sub statement_lines {
1496 my ($stmt) = @_;
1497
1498 # Strip the diff line prefixes and rip blank lines at start and end.
1499 $stmt =~ s/(^|\n)./$1/g;
1500 $stmt =~ s/^\s*//;
1501 $stmt =~ s/\s*$//;
1502
1503 my @stmt_lines = ($stmt =~ /\n/g);
1504
1505 return $#stmt_lines + 2;
1506}
1507
1508sub statement_rawlines {
1509 my ($stmt) = @_;
1510
1511 my @stmt_lines = ($stmt =~ /\n/g);
1512
1513 return $#stmt_lines + 2;
1514}
1515
1516sub statement_block_size {
1517 my ($stmt) = @_;
1518
1519 $stmt =~ s/(^|\n)./$1/g;
1520 $stmt =~ s/^\s*{//;
1521 $stmt =~ s/}\s*$//;
1522 $stmt =~ s/^\s*//;
1523 $stmt =~ s/\s*$//;
1524
1525 my @stmt_lines = ($stmt =~ /\n/g);
1526 my @stmt_statements = ($stmt =~ /;/g);
1527
1528 my $stmt_lines = $#stmt_lines + 2;
1529 my $stmt_statements = $#stmt_statements + 1;
1530
1531 if ($stmt_lines > $stmt_statements) {
1532 return $stmt_lines;
1533 } else {
1534 return $stmt_statements;
1535 }
1536}
1537
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001538sub ctx_statement_full {
1539 my ($linenr, $remain, $off) = @_;
1540 my ($statement, $condition, $level);
1541
1542 my (@chunks);
1543
Andy Whitcroftcf655042008-03-04 14:28:20 -08001544 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001545 ($statement, $condition, $linenr, $remain, $off, $level) =
1546 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001547 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001548 push(@chunks, [ $condition, $statement ]);
1549 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1550 return ($level, $linenr, @chunks);
1551 }
1552
1553 # Pull in the following conditional/block pairs and see if they
1554 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001555 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001556 ($statement, $condition, $linenr, $remain, $off, $level) =
1557 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001558 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001559 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001560 #print "C: push\n";
1561 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001562 }
1563
1564 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001565}
1566
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001567sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001568 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001569 my $line;
1570 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001571 my $blk = '';
1572 my @o;
1573 my @c;
1574 my @res = ();
1575
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001576 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001577 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001578 for ($line = $start; $remain > 0; $line++) {
1579 next if ($rawlines[$line] =~ /^-/);
1580 $remain--;
1581
1582 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001583
1584 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001585 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001586 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001587 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001588 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001589 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001590 $level = pop(@stack);
1591 }
1592
Andy Whitcroft01464f32010-10-26 14:23:19 -07001593 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001594 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1595 if ($off > 0) {
1596 $off--;
1597 next;
1598 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001599
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001600 if ($c eq $close && $level > 0) {
1601 $level--;
1602 last if ($level == 0);
1603 } elsif ($c eq $open) {
1604 $level++;
1605 }
1606 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001607
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001608 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001609 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001610 }
1611
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001612 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001613 }
1614
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001615 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001616}
1617sub ctx_block_outer {
1618 my ($linenr, $remain) = @_;
1619
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001620 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1621 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001622}
1623sub ctx_block {
1624 my ($linenr, $remain) = @_;
1625
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001626 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1627 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001628}
1629sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001630 my ($linenr, $remain, $off) = @_;
1631
1632 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1633 return @r;
1634}
1635sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001636 my ($linenr, $remain) = @_;
1637
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001638 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001639}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001640sub ctx_statement_level {
1641 my ($linenr, $remain, $off) = @_;
1642
1643 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1644}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001645
1646sub ctx_locate_comment {
1647 my ($first_line, $end_line) = @_;
1648
1649 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001650 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001651 return $current_comment if (defined $current_comment);
1652
1653 # Look through the context and try and figure out if there is a
1654 # comment.
1655 my $in_comment = 0;
1656 $current_comment = '';
1657 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001658 my $line = $rawlines[$linenr - 1];
1659 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001660 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1661 $in_comment = 1;
1662 }
1663 if ($line =~ m@/\*@) {
1664 $in_comment = 1;
1665 }
1666 if (!$in_comment && $current_comment ne '') {
1667 $current_comment = '';
1668 }
1669 $current_comment .= $line . "\n" if ($in_comment);
1670 if ($line =~ m@\*/@) {
1671 $in_comment = 0;
1672 }
1673 }
1674
1675 chomp($current_comment);
1676 return($current_comment);
1677}
1678sub ctx_has_comment {
1679 my ($first_line, $end_line) = @_;
1680 my $cmt = ctx_locate_comment($first_line, $end_line);
1681
Andy Whitcroft00df3442007-06-08 13:47:06 -07001682 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001683 ##print "CMMT: $cmt\n";
1684
1685 return ($cmt ne '');
1686}
1687
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001688sub raw_line {
1689 my ($linenr, $cnt) = @_;
1690
1691 my $offset = $linenr - 1;
1692 $cnt++;
1693
1694 my $line;
1695 while ($cnt) {
1696 $line = $rawlines[$offset++];
1697 next if (defined($line) && $line =~ /^-/);
1698 $cnt--;
1699 }
1700
1701 return $line;
1702}
1703
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001704sub get_stat_real {
1705 my ($linenr, $lc) = @_;
1706
1707 my $stat_real = raw_line($linenr, 0);
1708 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1709 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1710 }
1711
1712 return $stat_real;
1713}
1714
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001715sub get_stat_here {
1716 my ($linenr, $cnt, $here) = @_;
1717
1718 my $herectx = $here . "\n";
1719 for (my $n = 0; $n < $cnt; $n++) {
1720 $herectx .= raw_line($linenr, $n) . "\n";
1721 }
1722
1723 return $herectx;
1724}
1725
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001726sub cat_vet {
1727 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001728 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001729
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001730 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001731 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1732 $res .= $1;
1733 if ($2 ne '') {
1734 $coded = sprintf("^%c", unpack('C', $2) + 64);
1735 $res .= $coded;
1736 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001737 }
1738 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001739
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001740 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001741}
1742
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001743my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001744my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001745my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001746my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001747
1748sub annotate_reset {
1749 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001750 $av_pending = '_';
1751 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001752 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001753}
1754
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001755sub annotate_values {
1756 my ($stream, $type) = @_;
1757
1758 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001759 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001760 my $cur = $stream;
1761
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001762 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001763
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001764 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001765 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001766 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001767 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001768 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001769 print "WS($1)\n" if ($dbg_values > 1);
1770 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001771 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001772 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001773 }
1774
Florian Micklerc023e4732011-01-12 16:59:58 -08001775 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001776 print "CAST($1)\n" if ($dbg_values > 1);
1777 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001778 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001779
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001780 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001781 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001782 $type = 'T';
1783
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001784 } elsif ($cur =~ /^($Modifier)\s*/) {
1785 print "MODIFIER($1)\n" if ($dbg_values > 1);
1786 $type = 'T';
1787
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001788 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001789 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001790 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001791 push(@av_paren_type, $type);
1792 if ($2 ne '') {
1793 $av_pending = 'N';
1794 }
1795 $type = 'E';
1796
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001797 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001798 print "UNDEF($1)\n" if ($dbg_values > 1);
1799 $av_preprocessor = 1;
1800 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001801
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001802 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001803 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001804 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001805
1806 push(@av_paren_type, $type);
1807 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001808 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001809
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001810 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001811 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1812 $av_preprocessor = 1;
1813
1814 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1815
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001816 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001817
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001818 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001819 print "PRE_END($1)\n" if ($dbg_values > 1);
1820
1821 $av_preprocessor = 1;
1822
1823 # Assume all arms of the conditional end as this
1824 # one does, and continue as if the #endif was not here.
1825 pop(@av_paren_type);
1826 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001827 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001828
1829 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001830 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001831
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001832 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1833 print "ATTR($1)\n" if ($dbg_values > 1);
1834 $av_pending = $type;
1835 $type = 'N';
1836
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001837 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001838 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001839 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001840 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001841 }
1842 $type = 'N';
1843
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001844 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001845 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001846 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001847 $type = 'N';
1848
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001849 } elsif ($cur =~/^(case)/o) {
1850 print "CASE($1)\n" if ($dbg_values > 1);
1851 $av_pend_colon = 'C';
1852 $type = 'N';
1853
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001854 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001855 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001856 $type = 'N';
1857
1858 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001859 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001860 push(@av_paren_type, $av_pending);
1861 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001862 $type = 'N';
1863
1864 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001865 my $new_type = pop(@av_paren_type);
1866 if ($new_type ne '_') {
1867 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001868 print "PAREN('$1') -> $type\n"
1869 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001870 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001871 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001872 }
1873
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001874 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001875 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001876 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001877 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001878
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001879 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1880 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001881 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001882 } elsif ($type eq 'E') {
1883 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001884 }
1885 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1886 $type = 'V';
1887
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001888 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001889 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001890 $type = 'V';
1891
1892 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001893 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001894 $type = 'N';
1895
Andy Whitcroftcf655042008-03-04 14:28:20 -08001896 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001897 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001898 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001899 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001900
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001901 } elsif ($cur =~/^(,)/) {
1902 print "COMMA($1)\n" if ($dbg_values > 1);
1903 $type = 'C';
1904
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001905 } elsif ($cur =~ /^(\?)/o) {
1906 print "QUESTION($1)\n" if ($dbg_values > 1);
1907 $type = 'N';
1908
1909 } elsif ($cur =~ /^(:)/o) {
1910 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1911
1912 substr($var, length($res), 1, $av_pend_colon);
1913 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1914 $type = 'E';
1915 } else {
1916 $type = 'N';
1917 }
1918 $av_pend_colon = 'O';
1919
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001920 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001921 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001922 $type = 'N';
1923
Andy Whitcroft0d413862008-10-15 22:02:16 -07001924 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001925 my $variant;
1926
1927 print "OPV($1)\n" if ($dbg_values > 1);
1928 if ($type eq 'V') {
1929 $variant = 'B';
1930 } else {
1931 $variant = 'U';
1932 }
1933
1934 substr($var, length($res), 1, $variant);
1935 $type = 'N';
1936
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001937 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001938 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001939 if ($1 ne '++' && $1 ne '--') {
1940 $type = 'N';
1941 }
1942
1943 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001944 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001945 }
1946 if (defined $1) {
1947 $cur = substr($cur, length($1));
1948 $res .= $type x length($1);
1949 }
1950 }
1951
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001952 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001953}
1954
Andy Whitcroft8905a672007-11-28 16:21:06 -08001955sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001956 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001957 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001958 ^(?:
1959 $Modifier|
1960 $Storage|
1961 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001962 DEFINE_\S+
1963 )$|
1964 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001965 goto|
1966 return|
1967 case|
1968 else|
1969 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001970 do|
1971 \#|
1972 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001973 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001974 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001975 )}x;
1976 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1977 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001978 # Check for modifiers.
1979 $possible =~ s/\s*$Storage\s*//g;
1980 $possible =~ s/\s*$Sparse\s*//g;
1981 if ($possible =~ /^\s*$/) {
1982
1983 } elsif ($possible =~ /\s/) {
1984 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001985 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001986 if ($modifier !~ $notPermitted) {
1987 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001988 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001989 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001990 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001991
1992 } else {
1993 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001994 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001995 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001996 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001997 } else {
1998 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001999 }
2000}
2001
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002002my $prefix = '';
2003
Joe Perches000d1cc12011-07-25 17:13:25 -07002004sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002005 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002006
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002007 $type =~ tr/[a-z]/[A-Z]/;
2008
Joe Perchescbec18a2014-04-03 14:49:19 -07002009 return defined $use_type{$type} if (scalar keys %use_type > 0);
2010
2011 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002012}
2013
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002014sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002015 my ($level, $type, $msg) = @_;
2016
2017 if (!show_type($type) ||
2018 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002019 return 0;
2020 }
Joe Perches57230292015-06-25 15:03:03 -07002021 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002022 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002023 if ($level eq 'ERROR') {
2024 $output .= RED;
2025 } elsif ($level eq 'WARNING') {
2026 $output .= YELLOW;
2027 } else {
2028 $output .= GREEN;
2029 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002030 }
Joe Perches57230292015-06-25 15:03:03 -07002031 $output .= $prefix . $level . ':';
2032 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002033 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002034 $output .= "$type:";
2035 }
John Brooks737c0762017-07-10 15:52:24 -07002036 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002037 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002038
2039 if ($showfile) {
2040 my @lines = split("\n", $output, -1);
2041 splice(@lines, 1, 1);
2042 $output = join("\n", @lines);
2043 }
Joe Perches57230292015-06-25 15:03:03 -07002044 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002045
Joe Perches57230292015-06-25 15:03:03 -07002046 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002047
2048 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002049}
Joe Perchescbec18a2014-04-03 14:49:19 -07002050
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002051sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002052 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002053}
Joe Perches000d1cc12011-07-25 17:13:25 -07002054
Joe Perchesd752fcc2014-08-06 16:11:05 -07002055sub fixup_current_range {
2056 my ($lineRef, $offset, $length) = @_;
2057
2058 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2059 my $o = $1;
2060 my $l = $2;
2061 my $no = $o + $offset;
2062 my $nl = $l + $length;
2063 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2064 }
2065}
2066
2067sub fix_inserted_deleted_lines {
2068 my ($linesRef, $insertedRef, $deletedRef) = @_;
2069
2070 my $range_last_linenr = 0;
2071 my $delta_offset = 0;
2072
2073 my $old_linenr = 0;
2074 my $new_linenr = 0;
2075
2076 my $next_insert = 0;
2077 my $next_delete = 0;
2078
2079 my @lines = ();
2080
2081 my $inserted = @{$insertedRef}[$next_insert++];
2082 my $deleted = @{$deletedRef}[$next_delete++];
2083
2084 foreach my $old_line (@{$linesRef}) {
2085 my $save_line = 1;
2086 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002087 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002088 $delta_offset = 0;
2089 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2090 $range_last_linenr = $new_linenr;
2091 fixup_current_range(\$line, $delta_offset, 0);
2092 }
2093
2094 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2095 $deleted = @{$deletedRef}[$next_delete++];
2096 $save_line = 0;
2097 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2098 }
2099
2100 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2101 push(@lines, ${$inserted}{'LINE'});
2102 $inserted = @{$insertedRef}[$next_insert++];
2103 $new_linenr++;
2104 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2105 }
2106
2107 if ($save_line) {
2108 push(@lines, $line);
2109 $new_linenr++;
2110 }
2111
2112 $old_linenr++;
2113 }
2114
2115 return @lines;
2116}
2117
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002118sub fix_insert_line {
2119 my ($linenr, $line) = @_;
2120
2121 my $inserted = {
2122 LINENR => $linenr,
2123 LINE => $line,
2124 };
2125 push(@fixed_inserted, $inserted);
2126}
2127
2128sub fix_delete_line {
2129 my ($linenr, $line) = @_;
2130
2131 my $deleted = {
2132 LINENR => $linenr,
2133 LINE => $line,
2134 };
2135
2136 push(@fixed_deleted, $deleted);
2137}
2138
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002139sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002140 my ($type, $msg) = @_;
2141
2142 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002143 our $clean = 0;
2144 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002145 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002146 }
Joe Perches3705ce52013-07-03 15:05:31 -07002147 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002148}
2149sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002150 my ($type, $msg) = @_;
2151
2152 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002153 our $clean = 0;
2154 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002155 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002156 }
Joe Perches3705ce52013-07-03 15:05:31 -07002157 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002158}
2159sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002160 my ($type, $msg) = @_;
2161
2162 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002163 our $clean = 0;
2164 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002165 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002166 }
Joe Perches3705ce52013-07-03 15:05:31 -07002167 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002168}
2169
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002170sub check_absolute_file {
2171 my ($absolute, $herecurr) = @_;
2172 my $file = $absolute;
2173
2174 ##print "absolute<$absolute>\n";
2175
2176 # See if any suffix of this path is a path within the tree.
2177 while ($file =~ s@^[^/]*/@@) {
2178 if (-f "$root/$file") {
2179 ##print "file<$file>\n";
2180 last;
2181 }
2182 }
2183 if (! -f _) {
2184 return 0;
2185 }
2186
2187 # It is, so see if the prefix is acceptable.
2188 my $prefix = $absolute;
2189 substr($prefix, -length($file)) = '';
2190
2191 ##print "prefix<$prefix>\n";
2192 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002193 WARN("USE_RELATIVE_PATH",
2194 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002195 }
2196}
2197
Joe Perches3705ce52013-07-03 15:05:31 -07002198sub trim {
2199 my ($string) = @_;
2200
Joe Perchesb34c6482013-09-11 14:24:01 -07002201 $string =~ s/^\s+|\s+$//g;
2202
2203 return $string;
2204}
2205
2206sub ltrim {
2207 my ($string) = @_;
2208
2209 $string =~ s/^\s+//;
2210
2211 return $string;
2212}
2213
2214sub rtrim {
2215 my ($string) = @_;
2216
2217 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002218
2219 return $string;
2220}
2221
Joe Perches52ea8502013-11-12 15:10:09 -08002222sub string_find_replace {
2223 my ($string, $find, $replace) = @_;
2224
2225 $string =~ s/$find/$replace/g;
2226
2227 return $string;
2228}
2229
Joe Perches3705ce52013-07-03 15:05:31 -07002230sub tabify {
2231 my ($leading) = @_;
2232
2233 my $source_indent = 8;
2234 my $max_spaces_before_tab = $source_indent - 1;
2235 my $spaces_to_tab = " " x $source_indent;
2236
2237 #convert leading spaces to tabs
2238 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2239 #Remove spaces before a tab
2240 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2241
2242 return "$leading";
2243}
2244
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002245sub pos_last_openparen {
2246 my ($line) = @_;
2247
2248 my $pos = 0;
2249
2250 my $opens = $line =~ tr/\(/\(/;
2251 my $closes = $line =~ tr/\)/\)/;
2252
2253 my $last_openparen = 0;
2254
2255 if (($opens == 0) || ($closes >= $opens)) {
2256 return -1;
2257 }
2258
2259 my $len = length($line);
2260
2261 for ($pos = 0; $pos < $len; $pos++) {
2262 my $string = substr($line, $pos);
2263 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2264 $pos += length($1) - 1;
2265 } elsif (substr($line, $pos, 1) eq '(') {
2266 $last_openparen = $pos;
2267 } elsif (index($string, '(') == -1) {
2268 last;
2269 }
2270 }
2271
Joe Perches91cb5192014-04-03 14:49:32 -07002272 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002273}
2274
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002275sub process {
2276 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002277
2278 my $linenr=0;
2279 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002280 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002281 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002282 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002283
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002284 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002285 my $indent;
2286 my $previndent=0;
2287 my $stashindent=0;
2288
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002289 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002290 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002291 my $author = '';
2292 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002293 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002294 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002295 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002296 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002297 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002298 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002299 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002300 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002301 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002302 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002303 my $non_utf8_charset = 0;
2304
Joe Perches365dd4e2014-08-06 16:10:42 -07002305 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002306 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002307
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002308 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002309 our $cnt_lines = 0;
2310 our $cnt_error = 0;
2311 our $cnt_warn = 0;
2312 our $cnt_chk = 0;
2313
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002314 # Trace the real file/line as we go.
2315 my $realfile = '';
2316 my $realline = 0;
2317 my $realcnt = 0;
2318 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002319 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002320 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002321 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002322 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002323 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002324
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002325 my $prev_values = 'E';
2326
2327 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002328 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002329 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002330 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002331 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002332
Joe Perches7e51f192013-09-11 14:23:57 -07002333 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002334
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002335 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002336 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002337 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002338 my @setup_docs = ();
2339 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002340
Joe Perchesd8b07712013-11-12 15:10:06 -08002341 my $camelcase_file_seeded = 0;
2342
Rob Herring9f3a8992018-04-10 16:33:13 -07002343 my $checklicenseline = 1;
2344
Andy Whitcroft773647a2008-03-28 14:15:58 -07002345 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002346 my $line;
2347 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002348 $linenr++;
2349 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002350
Joe Perches3705ce52013-07-03 15:05:31 -07002351 push(@fixed, $rawline) if ($fix);
2352
Andy Whitcroft773647a2008-03-28 14:15:58 -07002353 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002354 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002355 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002356 $setup_docs = 1;
2357 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002358 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002359 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002360 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002361 $realline=$1-1;
2362 if (defined $2) {
2363 $realcnt=$3+1;
2364 } else {
2365 $realcnt=1+1;
2366 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002367 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002368
2369 # Guestimate if this is a continuing comment. Run
2370 # the context looking for a comment "edge". If this
2371 # edge is a close comment then we must be in a comment
2372 # at context start.
2373 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002374 my $cnt = $realcnt;
2375 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2376 next if (defined $rawlines[$ln - 1] &&
2377 $rawlines[$ln - 1] =~ /^-/);
2378 $cnt--;
2379 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002380 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002381 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2382 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2383 ($edge) = $1;
2384 last;
2385 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002386 }
2387 if (defined $edge && $edge eq '*/') {
2388 $in_comment = 1;
2389 }
2390
2391 # Guestimate if this is a continuing comment. If this
2392 # is the start of a diff block and this line starts
2393 # ' *' then it is very likely a comment.
2394 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002395 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002396 {
2397 $in_comment = 1;
2398 }
2399
2400 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2401 sanitise_line_reset($in_comment);
2402
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002403 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002404 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002405 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002406 $line = sanitise_line($rawline);
2407 }
2408 push(@lines, $line);
2409
2410 if ($realcnt > 1) {
2411 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2412 } else {
2413 $realcnt = 0;
2414 }
2415
2416 #print "==>$rawline\n";
2417 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002418
2419 if ($setup_docs && $line =~ /^\+/) {
2420 push(@setup_docs, $line);
2421 }
2422 }
2423
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002424 $prefix = '';
2425
Andy Whitcroft773647a2008-03-28 14:15:58 -07002426 $realcnt = 0;
2427 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002428 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002429 foreach my $line (@lines) {
2430 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002431 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002432 my $sline = $line; #copy of $line
2433 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002434
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002435 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002436
Joe Perches12c253a2018-06-07 17:10:58 -07002437# check if it's a mode change, rename or start of a patch
2438 if (!$in_commit_log &&
2439 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2440 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2441 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2442 $is_patch = 1;
2443 }
2444
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002445#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002446 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002447 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2448 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002449 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002450 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002451 $realline=$1-1;
2452 if (defined $2) {
2453 $realcnt=$3+1;
2454 } else {
2455 $realcnt=1+1;
2456 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002457 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002458 $prev_values = 'E';
2459
Andy Whitcroft773647a2008-03-28 14:15:58 -07002460 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002461 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002462 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002463 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002464 if ($context =~ /\b(\w+)\s*\(/) {
2465 $context_function = $1;
2466 } else {
2467 undef $context_function;
2468 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002469 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002470
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002471# track the line number as we move through the hunk, note that
2472# new versions of GNU diff omit the leading space on completely
2473# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002474 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002475 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002476 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002477
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002478 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002479 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002480
2481 # Track the previous line.
2482 ($prevline, $stashline) = ($stashline, $line);
2483 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002484 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2485
Andy Whitcroft773647a2008-03-28 14:15:58 -07002486 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002487
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002488 } elsif ($realcnt == 1) {
2489 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002490 }
2491
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002492 my $hunk_line = ($realcnt != 0);
2493
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002494 $here = "#$linenr: " if (!$file);
2495 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002496
Joe Perches2ac73b42014-06-04 16:12:05 -07002497 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002498 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002499 if ($line =~ /^diff --git.*?(\S+)$/) {
2500 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002501 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002502 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002503 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002504 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002505 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002506 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002507 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002508
2509 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002510 if (!$file && $tree && $p1_prefix ne '' &&
2511 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002512 WARN("PATCH_PREFIX",
2513 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002514 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002515
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002516 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002517 ERROR("MODIFIED_INCLUDE_ASM",
2518 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
Andy Whitcroft773647a2008-03-28 14:15:58 -07002519 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002520 $found_file = 1;
2521 }
2522
Joe Perches34d88152015-06-25 15:03:05 -07002523#make up the handle for any error we report on this line
2524 if ($showfile) {
2525 $prefix = "$realfile:$realline: "
2526 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002527 if ($file) {
2528 $prefix = "$filename:$realline: ";
2529 } else {
2530 $prefix = "$filename:$linenr: ";
2531 }
Joe Perches34d88152015-06-25 15:03:05 -07002532 }
2533
Joe Perches2ac73b42014-06-04 16:12:05 -07002534 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002535 if (is_maintained_obsolete($realfile)) {
2536 WARN("OBSOLETE",
2537 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2538 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002539 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002540 $check = 1;
2541 } else {
2542 $check = $check_orig;
2543 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002544 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002545
2546 if ($realfile !~ /^MAINTAINERS/) {
2547 my $last_binding_patch = $is_binding_patch;
2548
2549 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2550
2551 if (($last_binding_patch != -1) &&
2552 ($last_binding_patch ^ $is_binding_patch)) {
2553 WARN("DT_SPLIT_BINDING_PATCH",
2554 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2555 }
2556 }
2557
Andy Whitcroft773647a2008-03-28 14:15:58 -07002558 next;
2559 }
2560
Randy Dunlap389834b2007-06-08 13:47:03 -07002561 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002562
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002563 my $hereline = "$here\n$rawline\n";
2564 my $herecurr = "$here\n$rawline\n";
2565 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002566
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002567 $cnt_lines++ if ($realcnt != 0);
2568
Joe Perches490b2922018-08-21 21:58:01 -07002569# Verify the existence of a commit log if appropriate
2570# 2 is used because a $signature is counted in $commit_log_lines
2571 if ($in_commit_log) {
2572 if ($line !~ /^\s*$/) {
2573 $commit_log_lines++; #could be a $signature
2574 }
2575 } elsif ($has_commit_log && $commit_log_lines < 2) {
2576 WARN("COMMIT_MESSAGE",
2577 "Missing commit description - Add an appropriate one\n");
2578 $commit_log_lines = 2; #warn only once
2579 }
2580
Joe Perchese518e9a2015-06-25 15:03:27 -07002581# Check if the commit log has what seems like a diff which can confuse patch
2582 if ($in_commit_log && !$commit_log_has_diff &&
2583 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2584 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2585 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2586 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2587 ERROR("DIFF_IN_COMMIT_MSG",
2588 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2589 $commit_log_has_diff = 1;
2590 }
2591
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002592# Check for incorrect file permissions
2593 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2594 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002595 if ($realfile !~ m@scripts/@ &&
2596 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002597 ERROR("EXECUTE_PERMISSIONS",
2598 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002599 }
2600 }
2601
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002602# Check the patch for a From:
2603 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2604 $author = $1;
2605 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2606 $author =~ s/"//g;
2607 }
2608
Joe Perches20112472011-07-25 17:13:23 -07002609# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002610 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002611 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002612 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002613 if ($author ne '') {
2614 my $l = $line;
2615 $l =~ s/"//g;
2616 if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2617 $authorsignoff = 1;
2618 }
2619 }
Joe Perches20112472011-07-25 17:13:23 -07002620 }
2621
Joe Perchese0d975b2014-12-10 15:51:49 -08002622# Check if MAINTAINERS is being updated. If so, there's probably no need to
2623# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2624 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2625 $reported_maintainer_file = 1;
2626 }
2627
Joe Perches20112472011-07-25 17:13:23 -07002628# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002629 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002630 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002631 my $space_before = $1;
2632 my $sign_off = $2;
2633 my $space_after = $3;
2634 my $email = $4;
2635 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2636
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002637 if ($sign_off !~ /$signature_tags/) {
2638 WARN("BAD_SIGN_OFF",
2639 "Non-standard signature: $sign_off\n" . $herecurr);
2640 }
Joe Perches20112472011-07-25 17:13:23 -07002641 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002642 if (WARN("BAD_SIGN_OFF",
2643 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2644 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002645 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002646 "$ucfirst_sign_off $email";
2647 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002648 }
Joe Perches20112472011-07-25 17:13:23 -07002649 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002650 if (WARN("BAD_SIGN_OFF",
2651 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2652 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002653 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002654 "$ucfirst_sign_off $email";
2655 }
2656
Joe Perches20112472011-07-25 17:13:23 -07002657 }
2658 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002659 if (WARN("BAD_SIGN_OFF",
2660 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2661 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002662 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002663 "$ucfirst_sign_off $email";
2664 }
Joe Perches20112472011-07-25 17:13:23 -07002665 }
2666
2667 my ($email_name, $email_address, $comment) = parse_email($email);
2668 my $suggested_email = format_email(($email_name, $email_address));
2669 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002670 ERROR("BAD_SIGN_OFF",
2671 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002672 } else {
2673 my $dequoted = $suggested_email;
2674 $dequoted =~ s/^"//;
2675 $dequoted =~ s/" </ </;
2676 # Don't force email to have quotes
2677 # Allow just an angle bracketed address
2678 if ("$dequoted$comment" ne $email &&
2679 "<$email_address>$comment" ne $email &&
2680 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002681 WARN("BAD_SIGN_OFF",
2682 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002683 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002684 }
Joe Perches7e51f192013-09-11 14:23:57 -07002685
2686# Check for duplicate signatures
2687 my $sig_nospace = $line;
2688 $sig_nospace =~ s/\s//g;
2689 $sig_nospace = lc($sig_nospace);
2690 if (defined $signatures{$sig_nospace}) {
2691 WARN("BAD_SIGN_OFF",
2692 "Duplicate signature\n" . $herecurr);
2693 } else {
2694 $signatures{$sig_nospace} = 1;
2695 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002696
2697# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2698 if ($sign_off =~ /^co-developed-by:$/i) {
2699 if ($email eq $author) {
2700 WARN("BAD_SIGN_OFF",
2701 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2702 }
2703 if (!defined $lines[$linenr]) {
2704 WARN("BAD_SIGN_OFF",
2705 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2706 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2707 WARN("BAD_SIGN_OFF",
2708 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2709 } elsif ($1 ne $email) {
2710 WARN("BAD_SIGN_OFF",
2711 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2712 }
2713 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002714 }
2715
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002716# Check email subject for common tools that don't need to be mentioned
2717 if ($in_header_lines &&
2718 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2719 WARN("EMAIL_SUBJECT",
2720 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2721 }
2722
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002723# Check for unwanted Gerrit info
2724 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2725 ERROR("GERRIT_CHANGE_ID",
2726 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2727 }
2728
Joe Perches369c8dd2015-11-06 16:31:34 -08002729# Check if the commit log is in a possible stack dump
2730 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2731 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2732 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2733 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07002734 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2735 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2736 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2737 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08002738 $commit_log_possible_stack_dump = 1;
2739 }
2740
Joe Perches2a076f42015-04-16 12:44:28 -07002741# Check for line lengths > 75 in commit log, warn once
2742 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002743 length($line) > 75 &&
2744 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2745 # file delta changes
2746 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2747 # filename then :
2748 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2749 # A Fixes: or Link: line
2750 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002751 WARN("COMMIT_LOG_LONG_LINE",
2752 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2753 $commit_log_long_line = 1;
2754 }
2755
Joe Perchesbf4daf12015-09-09 15:37:50 -07002756# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002757 if ($in_commit_log && $commit_log_possible_stack_dump &&
2758 $line =~ /^\s*$/) {
2759 $commit_log_possible_stack_dump = 0;
2760 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002761
Joe Perches0d7835f2015-02-13 14:38:35 -08002762# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002763 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002764 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002765 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002766 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002767 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002768 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2769 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002770 my $init_char = "c";
2771 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002772 my $short = 1;
2773 my $long = 0;
2774 my $case = 1;
2775 my $space = 1;
2776 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002777 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002778 my $id = '0123456789ab';
2779 my $orig_desc = "commit description";
2780 my $description = "";
2781
Joe Perchesfe043ea2015-09-09 15:37:25 -07002782 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2783 $init_char = $1;
2784 $orig_commit = lc($2);
2785 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2786 $orig_commit = lc($1);
2787 }
2788
Joe Perches0d7835f2015-02-13 14:38:35 -08002789 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2790 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2791 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2792 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2793 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2794 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002795 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002796 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2797 defined $rawlines[$linenr] &&
2798 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2799 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002800 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002801 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2802 defined $rawlines[$linenr] &&
2803 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2804 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2805 $orig_desc = $1;
2806 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2807 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002808 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002809 }
2810
2811 ($id, $description) = git_commit_info($orig_commit,
2812 $id, $orig_desc);
2813
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002814 if (defined($id) &&
2815 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002816 ERROR("GIT_COMMIT_ID",
2817 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2818 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002819 }
2820
Joe Perches13f19372014-08-06 16:10:59 -07002821# Check for added, moved or deleted files
2822 if (!$reported_maintainer_file && !$in_commit_log &&
2823 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2824 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2825 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2826 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002827 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002828 $reported_maintainer_file = 1;
2829 WARN("FILE_PATH_CHANGES",
2830 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2831 }
2832
Rob Herringe400edb2019-09-12 15:18:20 +01002833# Check for adding new DT bindings not in schema format
2834 if (!$in_commit_log &&
2835 ($line =~ /^new file mode\s*\d+\s*$/) &&
2836 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2837 WARN("DT_SCHEMA_BINDING_PATCH",
2838 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2839 }
2840
Andy Whitcroft00df3442007-06-08 13:47:06 -07002841# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002842 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002843 ERROR("CORRUPTED_PATCH",
2844 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002845 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002846 }
2847
2848# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2849 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002850 $rawline !~ m/^$UTF8*$/) {
2851 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2852
2853 my $blank = copy_spacing($rawline);
2854 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2855 my $hereptr = "$hereline$ptr\n";
2856
Joe Perches34d99212011-07-25 17:13:26 -07002857 CHK("INVALID_UTF8",
2858 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002859 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002860
Joe Perches15662b32011-10-31 17:13:12 -07002861# Check if it's the start of a commit log
2862# (not a header line and we haven't seen the patch filename)
2863 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002864 !($rawline =~ /^\s+(?:\S|$)/ ||
2865 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002866 $in_header_lines = 0;
2867 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002868 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002869 }
2870
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002871# Check if there is UTF-8 in a commit log when a mail header has explicitly
2872# declined it, i.e defined some charset where it is missing.
2873 if ($in_header_lines &&
2874 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2875 $1 !~ /utf-8/i) {
2876 $non_utf8_charset = 1;
2877 }
2878
2879 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002880 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002881 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002882 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2883 }
2884
Joe Perchesd6430f72016-12-12 16:46:28 -08002885# Check for absolute kernel paths in commit message
2886 if ($tree && $in_commit_log) {
2887 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2888 my $file = $1;
2889
2890 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2891 check_absolute_file($1, $herecurr)) {
2892 #
2893 } else {
2894 check_absolute_file($file, $herecurr);
2895 }
2896 }
2897 }
2898
Kees Cook66b47b42014-10-13 15:51:57 -07002899# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002900 if (defined($misspellings) &&
2901 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002902 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002903 my $typo = $1;
2904 my $typo_fix = $spelling_fix{lc($typo)};
2905 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2906 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002907 my $msg_level = \&WARN;
2908 $msg_level = \&CHK if ($file);
2909 if (&{$msg_level}("TYPO_SPELLING",
2910 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002911 $fix) {
2912 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2913 }
2914 }
2915 }
2916
Matteo Crocea8dd86b2019-09-25 16:46:38 -07002917# check for invalid commit id
2918 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2919 my $id;
2920 my $description;
2921 ($id, $description) = git_commit_info($2, undef, undef);
2922 if (!defined($id)) {
2923 WARN("UNKNOWN_COMMIT_ID",
2924 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2925 }
2926 }
2927
Andy Whitcroft306708542008-10-15 22:02:28 -07002928# ignore non-hunk lines and lines being removed
2929 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002930
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002931#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002932 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002933 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002934 if (ERROR("DOS_LINE_ENDINGS",
2935 "DOS line endings\n" . $herevet) &&
2936 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002937 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002938 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002939 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2940 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002941 if (ERROR("TRAILING_WHITESPACE",
2942 "trailing whitespace\n" . $herevet) &&
2943 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002944 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002945 }
2946
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002947 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002948 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002949
Josh Triplett4783f892013-11-12 15:10:12 -08002950# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002951 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002952 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002953 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2954 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002955 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07002956 my $msg_level = \&ERROR;
2957 $msg_level = \&CHK if ($file);
2958 &{$msg_level}("FSF_MAILING_ADDRESS",
2959 "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
Josh Triplett4783f892013-11-12 15:10:12 -08002960 }
2961
Andi Kleen33549572010-05-24 14:33:29 -07002962# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002963# Only applies when adding the entry originally, after that we do not have
2964# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002965 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01002966 # 'choice' is usually the last thing on the line (though
2967 # Kconfig supports named choices), so use a word boundary
2968 # (\b) rather than a whitespace character (\s)
2969 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07002970 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002971 my $cnt = $realcnt;
2972 my $ln = $linenr + 1;
2973 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002974 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002975 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002976 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002977 $f = $lines[$ln - 1];
2978 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2979 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002980
2981 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002982 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002983
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01002984 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002985 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01002986 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
2987 if ($lines[$ln - 1] =~ "---help---") {
2988 WARN("CONFIG_DESCRIPTION",
2989 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
2990 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002991 $length = -1;
2992 }
2993
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002994 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002995 $f =~ s/#.*//;
2996 $f =~ s/^\s+//;
2997 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01002998
2999 # This only checks context lines in the patch
3000 # and so hopefully shouldn't trigger false
3001 # positives, even though some of these are
3002 # common words in help texts
3003 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3004 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003005 $is_end = 1;
3006 last;
3007 }
Andi Kleen33549572010-05-24 14:33:29 -07003008 $length++;
3009 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003010 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3011 WARN("CONFIG_DESCRIPTION",
3012 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3013 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003014 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003015 }
3016
Joe Perches628f91a2017-07-10 15:52:07 -07003017# check for MAINTAINERS entries that don't have the right form
3018 if ($realfile =~ /^MAINTAINERS$/ &&
3019 $rawline =~ /^\+[A-Z]:/ &&
3020 $rawline !~ /^\+[A-Z]:\t\S/) {
3021 if (WARN("MAINTAINERS_STYLE",
3022 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3023 $fix) {
3024 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3025 }
3026 }
3027
Christoph Jaeger327953e2015-02-13 14:38:29 -08003028# discourage the use of boolean for type definition attributes of Kconfig options
3029 if ($realfile =~ /Kconfig/ &&
3030 $line =~ /^\+\s*\bboolean\b/) {
3031 WARN("CONFIG_TYPE_BOOLEAN",
3032 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3033 }
3034
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003035 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3036 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3037 my $flag = $1;
3038 my $replacement = {
3039 'EXTRA_AFLAGS' => 'asflags-y',
3040 'EXTRA_CFLAGS' => 'ccflags-y',
3041 'EXTRA_CPPFLAGS' => 'cppflags-y',
3042 'EXTRA_LDFLAGS' => 'ldflags-y',
3043 };
3044
3045 WARN("DEPRECATED_VARIABLE",
3046 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3047 }
3048
Rob Herringbff5da42014-01-23 15:54:51 -08003049# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003050 if (defined $root &&
3051 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3052 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3053
Rob Herringbff5da42014-01-23 15:54:51 -08003054 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3055
Florian Vaussardcc933192014-04-03 14:49:27 -07003056 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003057 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003058
Rob Herringbff5da42014-01-23 15:54:51 -08003059 foreach my $compat (@compats) {
3060 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003061 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3062 my $compat3 = $compat;
3063 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3064 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003065 if ( $? >> 8 ) {
3066 WARN("UNDOCUMENTED_DT_STRING",
3067 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3068 }
3069
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003070 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3071 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003072 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003073 if ( $? >> 8 ) {
3074 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003075 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003076 }
3077 }
3078 }
3079
Rob Herring9f3a8992018-04-10 16:33:13 -07003080# check for using SPDX license tag at beginning of files
3081 if ($realline == $checklicenseline) {
3082 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3083 $checklicenseline = 2;
3084 } elsif ($rawline =~ /^\+/) {
3085 my $comment = "";
3086 if ($realfile =~ /\.(h|s|S)$/) {
3087 $comment = '/*';
3088 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3089 $comment = '//';
3090 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
3091 $comment = '#';
3092 } elsif ($realfile =~ /\.rst$/) {
3093 $comment = '..';
3094 }
3095
Joe Perchesfdf13692019-03-07 16:28:32 -08003096# check SPDX comment style for .[chsS] files
3097 if ($realfile =~ /\.[chsS]$/ &&
3098 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003099 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003100 WARN("SPDX_LICENSE_TAG",
3101 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3102 }
3103
Rob Herring9f3a8992018-04-10 16:33:13 -07003104 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003105 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3106 WARN("SPDX_LICENSE_TAG",
3107 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003108 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003109 my $spdx_license = $1;
3110 if (!is_SPDX_License_valid($spdx_license)) {
3111 WARN("SPDX_LICENSE_TAG",
3112 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3113 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003114 }
3115 }
3116 }
3117
Andy Whitcroft5368df202008-10-15 22:02:27 -07003118# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003119 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003120
Joe Perchesa8da38a2019-03-07 16:28:42 -08003121# check for using SPDX-License-Identifier on the wrong line number
3122 if ($realline != $checklicenseline &&
3123 $rawline =~ /\bSPDX-License-Identifier:/ &&
3124 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3125 WARN("SPDX_LICENSE_TAG",
3126 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3127 }
3128
Joe Perches47e0c882015-06-25 15:02:57 -07003129# line length limit (with some exclusions)
3130#
3131# There are a few types of lines that may extend beyond $max_line_length:
3132# logging functions like pr_info that end in a string
3133# lines with a single string
3134# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003135# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003136#
3137# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003138# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003139# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3140# LONG_LINE all other lines longer than $max_line_length
3141#
3142# if LONG_LINE is ignored, the other 2 types are also ignored
3143#
3144
Joe Perchesb4749e92015-07-17 16:24:01 -07003145 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003146 my $msg_type = "LONG_LINE";
3147
3148 # Check the allowed long line types first
3149
3150 # logging functions that end in a string that starts
3151 # before $max_line_length
3152 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3153 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3154 $msg_type = "";
3155
3156 # lines with only strings (w/ possible termination)
3157 # #defines with only strings
3158 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3159 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3160 $msg_type = "";
3161
Joe Perchescc147502017-11-17 15:28:44 -08003162 # More special cases
3163 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3164 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003165 $msg_type = "";
3166
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003167 # URL ($rawline is used in case the URL is in a comment)
3168 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3169 $msg_type = "";
3170
Joe Perches47e0c882015-06-25 15:02:57 -07003171 # Otherwise set the alternate message types
3172
3173 # a comment starts before $max_line_length
3174 } elsif ($line =~ /($;[\s$;]*)$/ &&
3175 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3176 $msg_type = "LONG_LINE_COMMENT"
3177
3178 # a quoted string starts before $max_line_length
3179 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3180 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3181 $msg_type = "LONG_LINE_STRING"
3182 }
3183
3184 if ($msg_type ne "" &&
3185 (show_type("LONG_LINE") || show_type($msg_type))) {
3186 WARN($msg_type,
3187 "line over $max_line_length characters\n" . $herecurr);
3188 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003189 }
3190
Andy Whitcroft8905a672007-11-28 16:21:06 -08003191# check for adding lines without a newline.
3192 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003193 WARN("MISSING_EOF_NEWLINE",
3194 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003195 }
3196
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003197# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003198 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003199
3200# at the beginning of a line any tabs must come first and anything
3201# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003202 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3203 $rawline =~ /^\+\s* \s*/) {
3204 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003205 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003206 if (ERROR("CODE_INDENT",
3207 "code indent should use tabs where possible\n" . $herevet) &&
3208 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003209 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003210 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003211 }
3212
Alberto Panizzo08e44362010-03-05 13:43:54 -08003213# check for space before tabs.
3214 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3215 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003216 if (WARN("SPACE_BEFORE_TAB",
3217 "please, no space before tabs\n" . $herevet) &&
3218 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003219 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003220 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003221 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003222 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003223 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003224 }
3225
Joe Perches6a487212018-04-10 16:34:04 -07003226# check for assignments on the start of a line
3227 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3228 CHK("ASSIGNMENT_CONTINUATIONS",
3229 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3230 }
3231
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003232# check for && or || at the start of a line
3233 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3234 CHK("LOGICAL_CONTINUATIONS",
3235 "Logical continuations should be on the previous line\n" . $hereprev);
3236 }
3237
Joe Perchesa91e8992016-05-20 17:04:05 -07003238# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003239 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003240 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003241 my $indent = length($1);
3242 if ($indent % 8) {
3243 if (WARN("TABSTOP",
3244 "Statements should start on a tabstop\n" . $herecurr) &&
3245 $fix) {
3246 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3247 }
3248 }
3249 }
3250
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003251# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003252 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003253 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003254 $prevline =~ /^\+(\t*)(.*)$/;
3255 my $oldindent = $1;
3256 my $rest = $2;
3257
3258 my $pos = pos_last_openparen($rest);
3259 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003260 $line =~ /^(\+| )([ \t]*)/;
3261 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003262
3263 my $goodtabindent = $oldindent .
3264 "\t" x ($pos / 8) .
3265 " " x ($pos % 8);
3266 my $goodspaceindent = $oldindent . " " x $pos;
3267
3268 if ($newindent ne $goodtabindent &&
3269 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003270
3271 if (CHK("PARENTHESIS_ALIGNMENT",
3272 "Alignment should match open parenthesis\n" . $hereprev) &&
3273 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003274 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003275 s/^\+[ \t]*/\+$goodtabindent/;
3276 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003277 }
3278 }
3279 }
3280
Joe Perches6ab3a972015-04-16 12:44:05 -07003281# check for space after cast like "(int) foo" or "(struct foo) bar"
3282# avoid checking a few false positives:
3283# "sizeof(<type>)" or "__alignof__(<type>)"
3284# function pointer declarations like "(*foo)(int) = bar;"
3285# structure definitions like "(struct foo) { 0 };"
3286# multiline macros that define functions
3287# known attributes or the __attribute__ keyword
3288 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3289 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003290 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003291 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003292 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003293 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003294 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003295 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003296 }
3297
Joe Perches86406b12015-09-09 15:37:41 -07003298# Block comment styles
3299# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003300 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003301 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003302 $rawline =~ /^\+[ \t]*\*/ &&
3303 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003304 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3305 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3306 }
3307
Joe Perches86406b12015-09-09 15:37:41 -07003308# Block comments use * on subsequent lines
3309 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3310 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003311 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003312 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003313 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003314 WARN("BLOCK_COMMENT_STYLE",
3315 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003316 }
3317
Joe Perches86406b12015-09-09 15:37:41 -07003318# Block comments use */ on trailing lines
3319 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003320 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3321 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3322 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003323 WARN("BLOCK_COMMENT_STYLE",
3324 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003325 }
3326
Joe Perches08eb9b82016-10-11 13:51:50 -07003327# Block comment * alignment
3328 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003329 $line =~ /^\+[ \t]*$;/ && #leading comment
3330 $rawline =~ /^\+[ \t]*\*/ && #leading *
3331 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003332 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003333 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3334 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003335 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003336 if (defined($1)) {
3337 $oldindent = expand_tabs($1);
3338 } else {
3339 $prevrawline =~ m@^\+(.*/?)\*@;
3340 $oldindent = expand_tabs($1);
3341 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003342 $rawline =~ m@^\+([ \t]*)\*@;
3343 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003344 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003345 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003346 WARN("BLOCK_COMMENT_STYLE",
3347 "Block comments should align the * on each line\n" . $hereprev);
3348 }
3349 }
3350
Joe Perches7f619192014-08-06 16:10:39 -07003351# check for missing blank lines after struct/union declarations
3352# with exceptions for various attributes and macros
3353 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3354 $line =~ /^\+/ &&
3355 !($line =~ /^\+\s*$/ ||
3356 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3357 $line =~ /^\+\s*MODULE_/i ||
3358 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3359 $line =~ /^\+[a-z_]*init/ ||
3360 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3361 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003362 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003363 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003364 if (CHK("LINE_SPACING",
3365 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3366 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003367 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003368 }
Joe Perches7f619192014-08-06 16:10:39 -07003369 }
3370
Joe Perches365dd4e2014-08-06 16:10:42 -07003371# check for multiple consecutive blank lines
3372 if ($prevline =~ /^[\+ ]\s*$/ &&
3373 $line =~ /^\+\s*$/ &&
3374 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003375 if (CHK("LINE_SPACING",
3376 "Please don't use multiple blank lines\n" . $hereprev) &&
3377 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003378 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003379 }
3380
Joe Perches365dd4e2014-08-06 16:10:42 -07003381 $last_blank_line = $linenr;
3382 }
3383
Joe Perches3b617e32014-04-03 14:49:28 -07003384# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003385 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3386 # actual declarations
3387 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003388 # function pointer declarations
3389 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003390 # foo bar; where foo is some local typedef or #define
3391 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3392 # known declaration macros
3393 $prevline =~ /^\+\s+$declaration_macros/) &&
3394 # for "else if" which can look like "$Ident $Ident"
3395 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3396 # other possible extensions of declaration lines
3397 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3398 # not starting a section or a macro "\" extended line
3399 $prevline =~ /(?:\{\s*|\\)$/) &&
3400 # looks like a declaration
3401 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003402 # function pointer declarations
3403 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003404 # foo bar; where foo is some local typedef or #define
3405 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3406 # known declaration macros
3407 $sline =~ /^\+\s+$declaration_macros/ ||
3408 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003409 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003410 # start or end of block or continuation of declaration
3411 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3412 # bitfield continuation
3413 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3414 # other possible extensions of declaration lines
3415 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3416 # indentation of previous and current line are the same
3417 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003418 if (WARN("LINE_SPACING",
3419 "Missing a blank line after declarations\n" . $hereprev) &&
3420 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003421 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003422 }
Joe Perches3b617e32014-04-03 14:49:28 -07003423 }
3424
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003425# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003426# Exceptions:
3427# 1) within comments
3428# 2) indented preprocessor commands
3429# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003430 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003431 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003432 if (WARN("LEADING_SPACE",
3433 "please, no spaces at the start of a line\n" . $herevet) &&
3434 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003435 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003436 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003437 }
3438
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003439# check we are in a valid C source file if not then ignore this hunk
3440 next if ($realfile !~ /\.(h|c)$/);
3441
Joe Perches5751a242017-11-17 15:28:52 -08003442# check for unusual line ending [ or (
3443 if ($line =~ /^\+.*([\[\(])\s*$/) {
3444 CHK("OPEN_ENDED_LINE",
3445 "Lines should not end with a '$1'\n" . $herecurr);
3446 }
3447
Joe Perches4dbed762017-05-08 15:55:39 -07003448# check if this appears to be the start function declaration, save the name
3449 if ($sline =~ /^\+\{\s*$/ &&
3450 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3451 $context_function = $1;
3452 }
3453
3454# check if this appears to be the end of function declaration
3455 if ($sline =~ /^\+\}\s*$/) {
3456 undef $context_function;
3457 }
3458
Joe Perches032a4c02014-08-06 16:10:29 -07003459# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003460# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003461# if the previous line is a break or return and is indented 1 tab more...
3462 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3463 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003464 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3465 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3466 defined $lines[$linenr] &&
3467 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003468 WARN("UNNECESSARY_ELSE",
3469 "else is not generally useful after a break or return\n" . $hereprev);
3470 }
3471 }
3472
Joe Perchesc00df192014-08-06 16:11:01 -07003473# check indentation of a line with a break;
3474# if the previous line is a goto or return and is indented the same # of tabs
3475 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3476 my $tabs = $1;
3477 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3478 WARN("UNNECESSARY_BREAK",
3479 "break is not useful after a goto or return\n" . $hereprev);
3480 }
3481 }
3482
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003483# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003484 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003485 WARN("CVS_KEYWORD",
3486 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003487 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003488
Joe Perches56e77d72013-02-21 16:44:14 -08003489# check for old HOTPLUG __dev<foo> section markings
3490 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3491 WARN("HOTPLUG_SECTION",
3492 "Using $1 is unnecessary\n" . $herecurr);
3493 }
3494
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003495# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003496 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3497 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003498#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003499 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003500 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003501 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003502 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003503 $stat =~ s/\n./\n /g;
3504 $cond =~ s/\n./\n /g;
3505
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003506#print "linenr<$linenr> <$stat>\n";
3507 # If this statement has no statement boundaries within
3508 # it there is no point in retrying a statement scan
3509 # until we hit end of it.
3510 my $frag = $stat; $frag =~ s/;+\s*$//;
3511 if ($frag !~ /(?:{|;)/) {
3512#print "skip<$line_nr_next>\n";
3513 $suppress_statement = $line_nr_next;
3514 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003515
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003516 # Find the real next line.
3517 $realline_next = $line_nr_next;
3518 if (defined $realline_next &&
3519 (!defined $lines[$realline_next - 1] ||
3520 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3521 $realline_next++;
3522 }
3523
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003524 my $s = $stat;
3525 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003526
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003527 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003528 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003529
3530 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003531 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003532
Andy Whitcroft463f2862009-09-21 17:04:34 -07003533 } elsif ($s =~ /^.\s*else\b/s) {
3534
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003535 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003536 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003537 my $type = $1;
3538 $type =~ s/\s+/ /g;
3539 possible($type, "A:" . $s);
3540
Andy Whitcroft8905a672007-11-28 16:21:06 -08003541 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003542 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003543 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003544 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003545
3546 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003547 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003548 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003549 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003550
3551 # Check for any sort of function declaration.
3552 # int foo(something bar, other baz);
3553 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003554 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003555 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003556
Andy Whitcroftcf655042008-03-04 14:28:20 -08003557 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003558 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003559 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003560
Andy Whitcroft8905a672007-11-28 16:21:06 -08003561 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003562 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003563
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003564 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003565 }
3566 }
3567 }
3568
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003569 }
3570
Andy Whitcroft653d4872007-06-23 17:16:34 -07003571#
3572# Checks which may be anchored in the context.
3573#
3574
3575# Check for switch () and associated case and default
3576# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003577 if ($line=~/\bswitch\s*\(.*\)/) {
3578 my $err = '';
3579 my $sep = '';
3580 my @ctx = ctx_block_outer($linenr, $realcnt);
3581 shift(@ctx);
3582 for my $ctx (@ctx) {
3583 my ($clen, $cindent) = line_stats($ctx);
3584 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3585 $indent != $cindent) {
3586 $err .= "$sep$ctx\n";
3587 $sep = '';
3588 } else {
3589 $sep = "[...]\n";
3590 }
3591 }
3592 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003593 ERROR("SWITCH_CASE_INDENT_LEVEL",
3594 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003595 }
3596 }
3597
3598# if/while/etc brace do not go on next line, unless defining a do while loop,
3599# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003600 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003601 my $pre_ctx = "$1$2";
3602
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003603 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003604
3605 if ($line =~ /^\+\t{6,}/) {
3606 WARN("DEEP_INDENTATION",
3607 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3608 }
3609
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003610 my $ctx_cnt = $realcnt - $#ctx - 1;
3611 my $ctx = join("\n", @ctx);
3612
Andy Whitcroft548596d2008-07-23 21:29:01 -07003613 my $ctx_ln = $linenr;
3614 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003615
Andy Whitcroft548596d2008-07-23 21:29:01 -07003616 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3617 defined $lines[$ctx_ln - 1] &&
3618 $lines[$ctx_ln - 1] =~ /^-/)) {
3619 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3620 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003621 $ctx_ln++;
3622 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003623
Andy Whitcroft53210162008-07-23 21:29:03 -07003624 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3625 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003626
Joe Perchesd752fcc2014-08-06 16:11:05 -07003627 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003628 ERROR("OPEN_BRACE",
3629 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003630 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003631 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003632 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3633 $ctx =~ /\)\s*\;\s*$/ &&
3634 defined $lines[$ctx_ln - 1])
3635 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003636 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3637 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003638 WARN("TRAILING_SEMICOLON",
3639 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003640 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003641 }
3642 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003643 }
3644
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003645# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003646 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003647 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3648 ctx_statement_block($linenr, $realcnt, 0)
3649 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003650 my ($s, $c) = ($stat, $cond);
3651
3652 substr($s, 0, length($c), '');
3653
Joe Perches9f5af482015-09-09 15:37:30 -07003654 # remove inline comments
3655 $s =~ s/$;/ /g;
3656 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003657
3658 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003659 my @newlines = ($c =~ /\n/gs);
3660 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003661
Joe Perches9f5af482015-09-09 15:37:30 -07003662 # Make sure we remove the line prefixes as we have
3663 # none on the first line, and are going to readd them
3664 # where necessary.
3665 $s =~ s/\n./\n/gs;
3666 while ($s =~ /\n\s+\\\n/) {
3667 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3668 }
3669
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003670 # We want to check the first line inside the block
3671 # starting at the end of the conditional, so remove:
3672 # 1) any blank line termination
3673 # 2) any opening brace { on end of the line
3674 # 3) any do (...) {
3675 my $continuation = 0;
3676 my $check = 0;
3677 $s =~ s/^.*\bdo\b//;
3678 $s =~ s/^\s*{//;
3679 if ($s =~ s/^\s*\\//) {
3680 $continuation = 1;
3681 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003682 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003683 $check = 1;
3684 $cond_lines++;
3685 }
3686
3687 # Also ignore a loop construct at the end of a
3688 # preprocessor statement.
3689 if (($prevline =~ /^.\s*#\s*define\s/ ||
3690 $prevline =~ /\\\s*$/) && $continuation == 0) {
3691 $check = 0;
3692 }
3693
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003694 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003695 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003696 while ($cond_ptr != $cond_lines) {
3697 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003698
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003699 # If we see an #else/#elif then the code
3700 # is not linear.
3701 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3702 $check = 0;
3703 }
3704
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003705 # Ignore:
3706 # 1) blank lines, they should be at 0,
3707 # 2) preprocessor lines, and
3708 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003709 if ($continuation ||
3710 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003711 $s =~ /^\s*#\s*?/ ||
3712 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003713 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003714 if ($s =~ s/^.*?\n//) {
3715 $cond_lines++;
3716 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003717 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003718 }
3719
3720 my (undef, $sindent) = line_stats("+" . $s);
3721 my $stat_real = raw_line($linenr, $cond_lines);
3722
3723 # Check if either of these lines are modified, else
3724 # this is not this patch's fault.
3725 if (!defined($stat_real) ||
3726 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3727 $check = 0;
3728 }
3729 if (defined($stat_real) && $cond_lines > 1) {
3730 $stat_real = "[...]\n$stat_real";
3731 }
3732
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003733 #print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003734
Joe Perches9f5af482015-09-09 15:37:30 -07003735 if ($check && $s ne '' &&
3736 (($sindent % 8) != 0 ||
3737 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003738 ($sindent == $indent &&
3739 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003740 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003741 WARN("SUSPECT_CODE_INDENT",
3742 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003743 }
3744 }
3745
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003746 # Track the 'values' across context and added lines.
3747 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003748 my ($curr_values, $curr_vars) =
3749 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003750 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003751 if ($dbg_values) {
3752 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003753 print "$linenr > .$outline\n";
3754 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003755 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003756 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003757 $prev_values = substr($curr_values, -1);
3758
Andy Whitcroft00df3442007-06-08 13:47:06 -07003759#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003760 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003761
Joe Perches11ca40a2016-12-12 16:46:31 -08003762# check for dereferences that span multiple lines
3763 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3764 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3765 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3766 my $ref = $1;
3767 $line =~ /^.\s*($Lval)/;
3768 $ref .= $1;
3769 $ref =~ s/\s//g;
3770 WARN("MULTILINE_DEREFERENCE",
3771 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3772 }
3773
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003774# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003775 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003776 my $type = $1;
3777 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003778 $var = "" if (!defined $var);
3779 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003780 my $sign = $1;
3781 my $pointer = $2;
3782
3783 $pointer = "" if (!defined $pointer);
3784
3785 if (WARN("UNSPECIFIED_INT",
3786 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3787 $fix) {
3788 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003789 my $comp_pointer = $pointer;
3790 $comp_pointer =~ s/\s//g;
3791 $decl .= $comp_pointer;
3792 $decl = rtrim($decl) if ($var eq "");
3793 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003794 }
3795 }
3796 }
3797
Andy Whitcroft653d4872007-06-23 17:16:34 -07003798# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003799 if ($dbg_type) {
3800 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003801 ERROR("TEST_TYPE",
3802 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003803 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003804 ERROR("TEST_NOT_TYPE",
3805 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003806 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003807 next;
3808 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003809# TEST: allow direct testing of the attribute matcher.
3810 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003811 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003812 ERROR("TEST_ATTR",
3813 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003814 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003815 ERROR("TEST_NOT_ATTR",
3816 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003817 }
3818 next;
3819 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003820
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003821# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003822 if ($line =~ /^.\s*{/ &&
3823 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003824 if (ERROR("OPEN_BRACE",
3825 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003826 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3827 fix_delete_line($fixlinenr - 1, $prevrawline);
3828 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003829 my $fixedline = $prevrawline;
3830 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003831 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003832 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003833 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003834 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003835 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003836 }
3837
Andy Whitcroft653d4872007-06-23 17:16:34 -07003838#
3839# Checks which are anchored on the added line.
3840#
3841
3842# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003843 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003844 my $path = $1;
3845 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003846 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003847 "malformed #include filename\n" . $herecurr);
3848 }
3849 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3850 ERROR("UAPI_INCLUDE",
3851 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003852 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003853 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003854
3855# no C99 // comments
3856 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003857 if (ERROR("C99_COMMENTS",
3858 "do not use C99 // comments\n" . $herecurr) &&
3859 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003860 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003861 if ($line =~ /\/\/(.*)$/) {
3862 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003863 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003864 }
3865 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003866 }
3867 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003868 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003869 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003870
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003871# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3872# the whole statement.
3873#print "APW <$lines[$realline_next - 1]>\n";
3874 if (defined $realline_next &&
3875 exists $lines[$realline_next - 1] &&
3876 !defined $suppress_export{$realline_next} &&
3877 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3878 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003879 # Handle definitions which produce identifiers with
3880 # a prefix:
3881 # XXX(foo);
3882 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003883 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003884 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003885 $name =~ /^${Ident}_$2/) {
3886#print "FOO C name<$name>\n";
3887 $suppress_export{$realline_next} = 1;
3888
3889 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003890 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003891 ^.DEFINE_$Ident\(\Q$name\E\)|
3892 ^.DECLARE_$Ident\(\Q$name\E\)|
3893 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003894 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3895 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003896 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003897#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3898 $suppress_export{$realline_next} = 2;
3899 } else {
3900 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003901 }
3902 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003903 if (!defined $suppress_export{$linenr} &&
3904 $prevline =~ /^.\s*$/ &&
3905 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3906 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3907#print "FOO B <$lines[$linenr - 1]>\n";
3908 $suppress_export{$linenr} = 2;
3909 }
3910 if (defined $suppress_export{$linenr} &&
3911 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003912 WARN("EXPORT_SYMBOL",
3913 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003914 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003915
Joe Eloff5150bda2010-08-09 17:21:00 -07003916# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003917 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003918 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003919 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003920 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003921 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003922 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003923 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003924# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003925 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003926 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003927 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003928 $herecurr) &&
3929 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003930 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003931 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003932 }
3933
Joe Perches18130872014-08-06 16:11:22 -07003934# check for misordered declarations of char/short/int/long with signed/unsigned
3935 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3936 my $tmp = trim($1);
3937 WARN("MISORDERED_TYPE",
3938 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3939 }
3940
Joe Perches809e0822018-08-21 21:58:12 -07003941# check for unnecessary <signed> int declarations of short/long/long long
3942 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3943 my $type = trim($1);
3944 next if ($type !~ /\bint\b/);
3945 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3946 my $new_type = $type;
3947 $new_type =~ s/\b\s*int\s*\b/ /;
3948 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3949 $new_type =~ s/^const\s+//;
3950 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3951 $new_type = "const $new_type" if ($type =~ /^const\b/);
3952 $new_type =~ s/\s+/ /g;
3953 $new_type = trim($new_type);
3954 if (WARN("UNNECESSARY_INT",
3955 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3956 $fix) {
3957 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3958 }
3959 }
3960
Joe Perchescb710ec2010-10-26 14:23:20 -07003961# check for static const char * arrays.
3962 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003963 WARN("STATIC_CONST_CHAR_ARRAY",
3964 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003965 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08003966 }
3967
3968# check for initialized const char arrays that should be static const
3969 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
3970 if (WARN("STATIC_CONST_CHAR_ARRAY",
3971 "const array should probably be static const\n" . $herecurr) &&
3972 $fix) {
3973 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
3974 }
3975 }
Joe Perchescb710ec2010-10-26 14:23:20 -07003976
3977# check for static char foo[] = "bar" declarations.
3978 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003979 WARN("STATIC_CONST_CHAR_ARRAY",
3980 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003981 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08003982 }
Joe Perchescb710ec2010-10-26 14:23:20 -07003983
Joe Perchesab7e23f2015-04-16 12:44:22 -07003984# check for const <foo> const where <foo> is not a pointer or array type
3985 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3986 my $found = $1;
3987 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3988 WARN("CONST_CONST",
3989 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3990 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3991 WARN("CONST_CONST",
3992 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3993 }
3994 }
3995
Joe Perches9b0fa602014-04-03 14:49:18 -07003996# check for non-global char *foo[] = {"bar", ...} declarations.
3997 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3998 WARN("STATIC_CONST_CHAR_ARRAY",
3999 "char * array declaration might be better as static const\n" .
4000 $herecurr);
4001 }
4002
Joe Perchesb598b672015-04-16 12:44:36 -07004003# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4004 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4005 my $array = $1;
4006 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4007 my $array_div = $1;
4008 if (WARN("ARRAY_SIZE",
4009 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4010 $fix) {
4011 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4012 }
4013 }
4014 }
4015
Joe Perchesb36190c2014-01-27 17:07:18 -08004016# check for function declarations without arguments like "int foo()"
4017 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
4018 if (ERROR("FUNCTION_WITHOUT_ARGS",
4019 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4020 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004021 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004022 }
4023 }
4024
Andy Whitcroft653d4872007-06-23 17:16:34 -07004025# check for new typedefs, only function parameters and sparse annotations
4026# make sense.
4027 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004028 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004029 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004030 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004031 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004032 WARN("NEW_TYPEDEFS",
4033 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004034 }
4035
4036# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004037 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004038 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4039 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004040 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004041
Andy Whitcroft65863862009-01-06 14:41:21 -08004042 # Should start with a space.
4043 $to =~ s/^(\S)/ $1/;
4044 # Should not end with a space.
4045 $to =~ s/\s+$//;
4046 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004047 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004048 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004049
Joe Perches3705ce52013-07-03 15:05:31 -07004050## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004051 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004052 if (ERROR("POINTER_LOCATION",
4053 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4054 $fix) {
4055 my $sub_from = $ident;
4056 my $sub_to = $ident;
4057 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004058 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004059 s@\Q$sub_from\E@$sub_to@;
4060 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004061 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004062 }
4063 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4064 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004065 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004066
Andy Whitcroft65863862009-01-06 14:41:21 -08004067 # Should start with a space.
4068 $to =~ s/^(\S)/ $1/;
4069 # Should not end with a space.
4070 $to =~ s/\s+$//;
4071 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004072 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004073 }
4074 # Modifiers should have spaces.
4075 $to =~ s/(\b$Modifier$)/$1 /;
4076
Joe Perches3705ce52013-07-03 15:05:31 -07004077## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004078 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004079 if (ERROR("POINTER_LOCATION",
4080 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4081 $fix) {
4082
4083 my $sub_from = $match;
4084 my $sub_to = $match;
4085 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004086 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004087 s@\Q$sub_from\E@$sub_to@;
4088 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004089 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004090 }
4091
Joe Perches9d3e3c72015-09-09 15:37:27 -07004092# avoid BUG() or BUG_ON()
4093 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004094 my $msg_level = \&WARN;
4095 $msg_level = \&CHK if ($file);
4096 &{$msg_level}("AVOID_BUG",
4097 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
Joe Perches9d3e3c72015-09-09 15:37:27 -07004098 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004099
Joe Perches9d3e3c72015-09-09 15:37:27 -07004100# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004101 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004102 WARN("LINUX_VERSION_CODE",
4103 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004104 }
4105
Joe Perches17441222011-06-15 15:08:17 -07004106# check for uses of printk_ratelimit
4107 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004108 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004109 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004110 }
4111
Joe Percheseeef5732017-11-17 15:28:41 -08004112# printk should use KERN_* levels
4113 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4114 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4115 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004116 }
4117
Joe Perches243f3802012-05-31 16:26:09 -07004118 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4119 my $orig = $1;
4120 my $level = lc($orig);
4121 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004122 my $level2 = $level;
4123 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004124 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004125 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07004126 }
4127
4128 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004129 if (WARN("PREFER_PR_LEVEL",
4130 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4131 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004132 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004133 s/\bpr_warning\b/pr_warn/;
4134 }
Joe Perches243f3802012-05-31 16:26:09 -07004135 }
4136
Joe Perchesdc139312013-02-21 16:44:13 -08004137 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4138 my $orig = $1;
4139 my $level = lc($orig);
4140 $level = "warn" if ($level eq "warning");
4141 $level = "dbg" if ($level eq "debug");
4142 WARN("PREFER_DEV_LEVEL",
4143 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4144 }
4145
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004146# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4147# number of false positives, but assembly files are not checked, so at
4148# least the arch entry code will not trigger this warning.
4149 if ($line =~ /\bENOSYS\b/) {
4150 WARN("ENOSYS",
4151 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4152 }
4153
Andy Whitcroft653d4872007-06-23 17:16:34 -07004154# function brace can't be on same line, except for #defines of do while,
4155# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004156 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004157 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4158 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4159 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004160 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004161 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004162 $fix) {
4163 fix_delete_line($fixlinenr, $rawline);
4164 my $fixed_line = $rawline;
4165 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4166 my $line1 = $1;
4167 my $line2 = $2;
4168 fix_insert_line($fixlinenr, ltrim($line1));
4169 fix_insert_line($fixlinenr, "\+{");
4170 if ($line2 !~ /^\s*$/) {
4171 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4172 }
4173 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004174 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004175
Andy Whitcroft8905a672007-11-28 16:21:06 -08004176# open braces for enum, union and struct go on the same line.
4177 if ($line =~ /^.\s*{/ &&
4178 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004179 if (ERROR("OPEN_BRACE",
4180 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4181 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4182 fix_delete_line($fixlinenr - 1, $prevrawline);
4183 fix_delete_line($fixlinenr, $rawline);
4184 my $fixedline = rtrim($prevrawline) . " {";
4185 fix_insert_line($fixlinenr, $fixedline);
4186 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004187 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004188 if ($fixedline !~ /^\+\s*$/) {
4189 fix_insert_line($fixlinenr, $fixedline);
4190 }
4191 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004192 }
4193
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004194# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004195 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4196 if (WARN("SPACING",
4197 "missing space after $1 definition\n" . $herecurr) &&
4198 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004199 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004200 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4201 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004202 }
4203
Joe Perches31070b52014-01-23 15:54:49 -08004204# Function pointer declarations
4205# check spacing between type, funcptr, and args
4206# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004207 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004208 my $declare = $1;
4209 my $pre_pointer_space = $2;
4210 my $post_pointer_space = $3;
4211 my $funcname = $4;
4212 my $post_funcname_space = $5;
4213 my $pre_args_space = $6;
4214
Joe Perches91f72e92014-04-03 14:49:12 -07004215# the $Declare variable will capture all spaces after the type
4216# so check it for a missing trailing missing space but pointer return types
4217# don't need a space so don't warn for those.
4218 my $post_declare_space = "";
4219 if ($declare =~ /(\s+)$/) {
4220 $post_declare_space = $1;
4221 $declare = rtrim($declare);
4222 }
4223 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004224 WARN("SPACING",
4225 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004226 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004227 }
4228
4229# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004230# This test is not currently implemented because these declarations are
4231# equivalent to
4232# int foo(int bar, ...)
4233# and this is form shouldn't/doesn't generate a checkpatch warning.
4234#
4235# elsif ($declare =~ /\s{2,}$/) {
4236# WARN("SPACING",
4237# "Multiple spaces after return type\n" . $herecurr);
4238# }
Joe Perches31070b52014-01-23 15:54:49 -08004239
4240# unnecessary space "type ( *funcptr)(args...)"
4241 if (defined $pre_pointer_space &&
4242 $pre_pointer_space =~ /^\s/) {
4243 WARN("SPACING",
4244 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4245 }
4246
4247# unnecessary space "type (* funcptr)(args...)"
4248 if (defined $post_pointer_space &&
4249 $post_pointer_space =~ /^\s/) {
4250 WARN("SPACING",
4251 "Unnecessary space before function pointer name\n" . $herecurr);
4252 }
4253
4254# unnecessary space "type (*funcptr )(args...)"
4255 if (defined $post_funcname_space &&
4256 $post_funcname_space =~ /^\s/) {
4257 WARN("SPACING",
4258 "Unnecessary space after function pointer name\n" . $herecurr);
4259 }
4260
4261# unnecessary space "type (*funcptr) (args...)"
4262 if (defined $pre_args_space &&
4263 $pre_args_space =~ /^\s/) {
4264 WARN("SPACING",
4265 "Unnecessary space before function pointer arguments\n" . $herecurr);
4266 }
4267
4268 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004269 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004270 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004271 }
4272 }
4273
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004274# check for spacing round square brackets; allowed:
4275# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004276# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4277# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004278 while ($line =~ /(.*?\s)\[/g) {
4279 my ($where, $prefix) = ($-[1], $1);
4280 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004281 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004282 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004283 if (ERROR("BRACKET_SPACE",
4284 "space prohibited before open square bracket '['\n" . $herecurr) &&
4285 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004286 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004287 s/^(\+.*?)\s+\[/$1\[/;
4288 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004289 }
4290 }
4291
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004292# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004293 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004294 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004295 my $ctx_before = substr($line, 0, $-[1]);
4296 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004297
4298 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004299 if ($name =~ /^(?:
4300 if|for|while|switch|return|case|
4301 volatile|__volatile__|
4302 __attribute__|format|__extension__|
4303 asm|__asm__)$/x)
4304 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004305 # cpp #define statements have non-optional spaces, ie
4306 # if there is a space between the name and the open
4307 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004308 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004309
4310 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004311 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004312
4313 # If this whole things ends with a type its most
4314 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004315 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004316
4317 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004318 if (WARN("SPACING",
4319 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4320 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004321 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004322 s/\b$name\s+\(/$name\(/;
4323 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004324 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004325 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004326
Andy Whitcroft653d4872007-06-23 17:16:34 -07004327# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004328 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004329 my $fixed_line = "";
4330 my $line_fixed = 0;
4331
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004332 my $ops = qr{
4333 <<=|>>=|<=|>=|==|!=|
4334 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4335 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004336 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004337 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004338 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004339 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004340
4341## print("element count: <" . $#elements . ">\n");
4342## foreach my $el (@elements) {
4343## print("el: <$el>\n");
4344## }
4345
4346 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004347 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004348
Joe Perches3705ce52013-07-03 15:05:31 -07004349 foreach my $el (@elements) {
4350 push(@fix_elements, substr($rawline, $off, length($el)));
4351 $off += length($el);
4352 }
4353
4354 $off = 0;
4355
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004356 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004357 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004358
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004359 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004360
4361 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4362
4363## print("n: <$n> good: <$good>\n");
4364
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004365 $off += length($elements[$n]);
4366
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004367 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004368 my $ca = substr($opline, 0, $off);
4369 my $cc = '';
4370 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4371 $cc = substr($opline, $off + length($elements[$n + 1]));
4372 }
4373 my $cb = "$ca$;$cc";
4374
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004375 my $a = '';
4376 $a = 'V' if ($elements[$n] ne '');
4377 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004378 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004379 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4380 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004381 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004382
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004383 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004384
4385 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004386 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004387 $c = 'V' if ($elements[$n + 2] ne '');
4388 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004389 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004390 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4391 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004392 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004393 } else {
4394 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004395 }
4396
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004397 my $ctx = "${a}x${c}";
4398
4399 my $at = "(ctx:$ctx)";
4400
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004401 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004402 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004403
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004404 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004405 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004406
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004407 # Get the full operator variant.
4408 my $opv = $op . substr($curr_vars, $off, 1);
4409
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004410 # Ignore operators passed as parameters.
4411 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004412 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004413
Andy Whitcroftcf655042008-03-04 14:28:20 -08004414# # Ignore comments
4415# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004416
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004417 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004418 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004419 if ($ctx !~ /.x[WEBC]/ &&
4420 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004421 if (ERROR("SPACING",
4422 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004423 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004424 $line_fixed = 1;
4425 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004426 }
4427
4428 # // is a comment
4429 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004430
Joe Perchesb00e4812014-04-03 14:49:33 -07004431 # : when part of a bitfield
4432 } elsif ($opv eq ':B') {
4433 # skip the bitfield test for now
4434
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004435 # No spaces for:
4436 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004437 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004438 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004439 if (ERROR("SPACING",
4440 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004441 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004442 if (defined $fix_elements[$n + 2]) {
4443 $fix_elements[$n + 2] =~ s/^\s+//;
4444 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004445 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004446 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004447 }
4448
Joe Perches23810972014-12-10 15:51:32 -08004449 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004450 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004451 my $rtrim_before = 0;
4452 my $space_after = 0;
4453 if ($ctx =~ /Wx./) {
4454 if (ERROR("SPACING",
4455 "space prohibited before that '$op' $at\n" . $hereptr)) {
4456 $line_fixed = 1;
4457 $rtrim_before = 1;
4458 }
4459 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004460 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004461 if (ERROR("SPACING",
4462 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004463 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004464 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004465 $space_after = 1;
4466 }
4467 }
4468 if ($rtrim_before || $space_after) {
4469 if ($rtrim_before) {
4470 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4471 } else {
4472 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4473 }
4474 if ($space_after) {
4475 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004476 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004477 }
4478
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004479 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004480 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004481 #warn "'*' is part of type\n";
4482
4483 # unary operators should have a space before and
4484 # none after. May be left adjacent to another
4485 # unary operator, or a cast
4486 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004487 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004488 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004489 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004490 if (ERROR("SPACING",
4491 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004492 if ($n != $last_after + 2) {
4493 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4494 $line_fixed = 1;
4495 }
Joe Perches3705ce52013-07-03 15:05:31 -07004496 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004497 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004498 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004499 # A unary '*' may be const
4500
4501 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004502 if (ERROR("SPACING",
4503 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004504 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004505 if (defined $fix_elements[$n + 2]) {
4506 $fix_elements[$n + 2] =~ s/^\s+//;
4507 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004508 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004509 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004510 }
4511
4512 # unary ++ and unary -- are allowed no space on one side.
4513 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004514 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004515 if (ERROR("SPACING",
4516 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004517 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004518 $line_fixed = 1;
4519 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004520 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004521 if ($ctx =~ /Wx[BE]/ ||
4522 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004523 if (ERROR("SPACING",
4524 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004525 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004526 $line_fixed = 1;
4527 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004528 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004529 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004530 if (ERROR("SPACING",
4531 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004532 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004533 if (defined $fix_elements[$n + 2]) {
4534 $fix_elements[$n + 2] =~ s/^\s+//;
4535 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004536 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004537 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004538 }
4539
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004540 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004541 } elsif ($op eq '<<' or $op eq '>>' or
4542 $op eq '&' or $op eq '^' or $op eq '|' or
4543 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004544 $op eq '*' or $op eq '/' or
4545 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004546 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004547 if ($check) {
4548 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4549 if (CHK("SPACING",
4550 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4551 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4552 $fix_elements[$n + 2] =~ s/^\s+//;
4553 $line_fixed = 1;
4554 }
4555 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4556 if (CHK("SPACING",
4557 "space preferred before that '$op' $at\n" . $hereptr)) {
4558 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4559 $line_fixed = 1;
4560 }
4561 }
4562 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004563 if (ERROR("SPACING",
4564 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004565 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4566 if (defined $fix_elements[$n + 2]) {
4567 $fix_elements[$n + 2] =~ s/^\s+//;
4568 }
Joe Perches3705ce52013-07-03 15:05:31 -07004569 $line_fixed = 1;
4570 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004571 }
4572
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004573 # A colon needs no spaces before when it is
4574 # terminating a case value or a label.
4575 } elsif ($opv eq ':C' || $opv eq ':L') {
4576 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004577 if (ERROR("SPACING",
4578 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004579 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004580 $line_fixed = 1;
4581 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004582 }
4583
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004584 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004585 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004586 my $ok = 0;
4587
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004588 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004589 if (($op eq '<' &&
4590 $cc =~ /^\S+\@\S+>/) ||
4591 ($op eq '>' &&
4592 $ca =~ /<\S+\@\S+$/))
4593 {
4594 $ok = 1;
4595 }
4596
Joe Perchese0df7e12015-04-16 12:44:53 -07004597 # for asm volatile statements
4598 # ignore a colon with another
4599 # colon immediately before or after
4600 if (($op eq ':') &&
4601 ($ca =~ /:$/ || $cc =~ /^:/)) {
4602 $ok = 1;
4603 }
4604
Joe Perches84731622013-11-12 15:10:05 -08004605 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004606 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004607 my $msg_level = \&ERROR;
4608 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004609
Jean Delvare0675a8f2017-09-08 16:16:07 -07004610 if (&{$msg_level}("SPACING",
4611 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004612 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4613 if (defined $fix_elements[$n + 2]) {
4614 $fix_elements[$n + 2] =~ s/^\s+//;
4615 }
Joe Perches3705ce52013-07-03 15:05:31 -07004616 $line_fixed = 1;
4617 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004618 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004619 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004620 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004621
4622## print("n: <$n> GOOD: <$good>\n");
4623
4624 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004625 }
Joe Perches3705ce52013-07-03 15:05:31 -07004626
4627 if (($#elements % 2) == 0) {
4628 $fixed_line = $fixed_line . $fix_elements[$#elements];
4629 }
4630
Joe Perches194f66f2014-08-06 16:11:03 -07004631 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4632 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004633 }
4634
4635
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004636 }
4637
Joe Perches786b6322013-07-03 15:05:32 -07004638# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004639 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004640 if (WARN("SPACING",
4641 "space prohibited before semicolon\n" . $herecurr) &&
4642 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004643 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004644 s/^(\+.*\S)\s+;/$1;/;
4645 }
4646 }
4647
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004648# check for multiple assignments
4649 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004650 CHK("MULTIPLE_ASSIGNMENTS",
4651 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004652 }
4653
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004654## # check for multiple declarations, allowing for a function declaration
4655## # continuation.
4656## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4657## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4658##
4659## # Remove any bracketed sections to ensure we do not
4660## # falsly report the parameters of functions.
4661## my $ln = $line;
4662## while ($ln =~ s/\([^\(\)]*\)//g) {
4663## }
4664## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004665## WARN("MULTIPLE_DECLARATION",
4666## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004667## }
4668## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004669
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004670#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004671 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004672 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004673 if (ERROR("SPACING",
4674 "space required before the open brace '{'\n" . $herecurr) &&
4675 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004676 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004677 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004678 }
4679
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004680## # check for blank lines before declarations
4681## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4682## $prevrawline =~ /^.\s*$/) {
4683## WARN("SPACING",
4684## "No blank lines before declarations\n" . $hereprev);
4685## }
4686##
4687
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004688# closing brace should have a space following it when it has anything
4689# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07004690 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004691 if (ERROR("SPACING",
4692 "space required after that close brace '}'\n" . $herecurr) &&
4693 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004694 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004695 s/}((?!(?:,|;|\)))\S)/} $1/;
4696 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004697 }
4698
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004699# check spacing on square brackets
4700 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004701 if (ERROR("SPACING",
4702 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4703 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004704 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004705 s/\[\s+/\[/;
4706 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004707 }
4708 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004709 if (ERROR("SPACING",
4710 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4711 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004712 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004713 s/\s+\]/\]/;
4714 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004715 }
4716
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004717# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004718 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4719 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004720 if (ERROR("SPACING",
4721 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4722 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004723 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004724 s/\(\s+/\(/;
4725 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004726 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004727 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004728 $line !~ /for\s*\(.*;\s+\)/ &&
4729 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004730 if (ERROR("SPACING",
4731 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4732 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004733 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004734 s/\s+\)/\)/;
4735 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004736 }
4737
Joe Perchese2826fd2014-08-06 16:10:48 -07004738# check unnecessary parentheses around addressof/dereference single $Lvals
4739# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4740
4741 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004742 my $var = $1;
4743 if (CHK("UNNECESSARY_PARENTHESES",
4744 "Unnecessary parentheses around $var\n" . $herecurr) &&
4745 $fix) {
4746 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4747 }
4748 }
4749
4750# check for unnecessary parentheses around function pointer uses
4751# ie: (foo->bar)(); should be foo->bar();
4752# but not "if (foo->bar) (" to avoid some false positives
4753 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4754 my $var = $2;
4755 if (CHK("UNNECESSARY_PARENTHESES",
4756 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4757 $fix) {
4758 my $var2 = deparenthesize($var);
4759 $var2 =~ s/\s//g;
4760 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4761 }
4762 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004763
Joe Perches63b7c732017-09-08 16:16:01 -07004764# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004765# when !drivers/staging or command-line uses --strict
4766 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004767 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004768 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4769 my $if_stat = $1;
4770 my $test = substr($2, 1, -1);
4771 my $herectx;
4772 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4773 my $match = $1;
4774 # avoid parentheses around potential macro args
4775 next if ($match =~ /^\s*\w+\s*$/);
4776 if (!defined($herectx)) {
4777 $herectx = $here . "\n";
4778 my $cnt = statement_rawlines($if_stat);
4779 for (my $n = 0; $n < $cnt; $n++) {
4780 my $rl = raw_line($linenr, $n);
4781 $herectx .= $rl . "\n";
4782 last if $rl =~ /^[ \+].*\{/;
4783 }
4784 }
4785 CHK("UNNECESSARY_PARENTHESES",
4786 "Unnecessary parentheses around '$match'\n" . $herectx);
4787 }
4788 }
4789
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004790#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004791 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004792 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004793 if (WARN("INDENTED_LABEL",
4794 "labels should not be indented\n" . $herecurr) &&
4795 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004796 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004797 s/^(.)\s+/$1/;
4798 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004799 }
4800
Joe Perches5b9553a2014-04-03 14:49:21 -07004801# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004802 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004803 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004804 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004805 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4806 my $value = $1;
4807 $value = deparenthesize($value);
4808 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4809 ERROR("RETURN_PARENTHESES",
4810 "return is not a function, parentheses are not required\n" . $herecurr);
4811 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004812 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004813 ERROR("SPACING",
4814 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004815 }
4816 }
Joe Perches507e5142013-11-12 15:10:13 -08004817
Joe Perchesb43ae212014-06-23 13:22:07 -07004818# unnecessary return in a void function
4819# at end-of-function, with the previous line a single leading tab, then return;
4820# and the line before that not a goto label target like "out:"
4821 if ($sline =~ /^[ \+]}\s*$/ &&
4822 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4823 $linenr >= 3 &&
4824 $lines[$linenr - 3] =~ /^[ +]/ &&
4825 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004826 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004827 "void function return statements are not generally useful\n" . $hereprev);
4828 }
Joe Perches9819cf22014-06-04 16:12:09 -07004829
Joe Perches189248d2014-01-23 15:54:47 -08004830# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004831 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004832 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4833 my $openparens = $1;
4834 my $count = $openparens =~ tr@\(@\(@;
4835 my $msg = "";
4836 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4837 my $comp = $4; #Not $1 because of $LvalOrFunc
4838 $msg = " - maybe == should be = ?" if ($comp eq "==");
4839 WARN("UNNECESSARY_PARENTHESES",
4840 "Unnecessary parentheses$msg\n" . $herecurr);
4841 }
4842 }
4843
Joe Perchesc5595fa2015-09-09 15:37:58 -07004844# comparisons with a constant or upper case identifier on the left
4845# avoid cases like "foo + BAR < baz"
4846# only fix matches surrounded by parentheses to avoid incorrect
4847# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004848 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004849 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4850 my $lead = $1;
4851 my $const = $2;
4852 my $comp = $3;
4853 my $to = $4;
4854 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004855 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004856 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4857 WARN("CONSTANT_COMPARISON",
4858 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4859 $fix) {
4860 if ($comp eq "<") {
4861 $newcomp = ">";
4862 } elsif ($comp eq "<=") {
4863 $newcomp = ">=";
4864 } elsif ($comp eq ">") {
4865 $newcomp = "<";
4866 } elsif ($comp eq ">=") {
4867 $newcomp = "<=";
4868 }
4869 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4870 }
4871 }
4872
Joe Perchesf34e4a42015-04-16 12:44:19 -07004873# Return of what appears to be an errno should normally be negative
4874 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004875 my $name = $1;
4876 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004877 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004878 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004879 }
4880 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004881
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004882# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004883 if ($line =~ /\b(if|while|for|switch)\(/) {
4884 if (ERROR("SPACING",
4885 "space required before the open parenthesis '('\n" . $herecurr) &&
4886 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004887 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004888 s/\b(if|while|for|switch)\(/$1 \(/;
4889 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004890 }
4891
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004892# Check for illegal assignment in if conditional -- and check for trailing
4893# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004894 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004895 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4896 ctx_statement_block($linenr, $realcnt, 0)
4897 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004898 my ($stat_next) = ctx_statement_block($line_nr_next,
4899 $remain_next, $off_next);
4900 $stat_next =~ s/\n./\n /g;
4901 ##print "stat<$stat> stat_next<$stat_next>\n";
4902
4903 if ($stat_next =~ /^\s*while\b/) {
4904 # If the statement carries leading newlines,
4905 # then count those as offsets.
4906 my ($whitespace) =
4907 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4908 my $offset =
4909 statement_rawlines($whitespace) - 1;
4910
4911 $suppress_whiletrailers{$line_nr_next +
4912 $offset} = 1;
4913 }
4914 }
4915 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004916 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004917 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004918 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004919
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004920 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004921 ERROR("ASSIGN_IN_IF",
4922 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004923 }
4924
4925 # Find out what is on the end of the line after the
4926 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004927 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004928 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004929 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004930 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4931 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004932 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004933 # Find out how long the conditional actually is.
4934 my @newlines = ($c =~ /\n/gs);
4935 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004936 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004937
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004938 $stat_real = raw_line($linenr, $cond_lines)
4939 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004940 if (defined($stat_real) && $cond_lines > 1) {
4941 $stat_real = "[...]\n$stat_real";
4942 }
4943
Joe Perches000d1cc12011-07-25 17:13:25 -07004944 ERROR("TRAILING_STATEMENTS",
4945 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004946 }
4947 }
4948
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004949# Check for bitwise tests written as boolean
4950 if ($line =~ /
4951 (?:
4952 (?:\[|\(|\&\&|\|\|)
4953 \s*0[xX][0-9]+\s*
4954 (?:\&\&|\|\|)
4955 |
4956 (?:\&\&|\|\|)
4957 \s*0[xX][0-9]+\s*
4958 (?:\&\&|\|\||\)|\])
4959 )/x)
4960 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004961 WARN("HEXADECIMAL_BOOLEAN_TEST",
4962 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004963 }
4964
Andy Whitcroft8905a672007-11-28 16:21:06 -08004965# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004966 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4967 my $s = $1;
4968 $s =~ s/$;//g; # Remove any comments
4969 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004970 ERROR("TRAILING_STATEMENTS",
4971 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004972 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004973 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004974# if should not continue a brace
4975 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004976 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004977 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004978 $herecurr);
4979 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004980# case and default should not have general statements after them
4981 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4982 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004983 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004984 \s*return\s+
4985 )/xg)
4986 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004987 ERROR("TRAILING_STATEMENTS",
4988 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004989 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004990
4991 # Check for }<nl>else {, these must be at the same
4992 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004993 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4994 $previndent == $indent) {
4995 if (ERROR("ELSE_AFTER_BRACE",
4996 "else should follow close brace '}'\n" . $hereprev) &&
4997 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4998 fix_delete_line($fixlinenr - 1, $prevrawline);
4999 fix_delete_line($fixlinenr, $rawline);
5000 my $fixedline = $prevrawline;
5001 $fixedline =~ s/}\s*$//;
5002 if ($fixedline !~ /^\+\s*$/) {
5003 fix_insert_line($fixlinenr, $fixedline);
5004 }
5005 $fixedline = $rawline;
5006 $fixedline =~ s/^(.\s*)else/$1} else/;
5007 fix_insert_line($fixlinenr, $fixedline);
5008 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005009 }
5010
Joe Perches8b8856f2014-08-06 16:11:14 -07005011 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5012 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005013 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5014
5015 # Find out what is on the end of the line after the
5016 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005017 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005018 $s =~ s/\n.*//g;
5019
5020 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005021 if (ERROR("WHILE_AFTER_BRACE",
5022 "while should follow close brace '}'\n" . $hereprev) &&
5023 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5024 fix_delete_line($fixlinenr - 1, $prevrawline);
5025 fix_delete_line($fixlinenr, $rawline);
5026 my $fixedline = $prevrawline;
5027 my $trailing = $rawline;
5028 $trailing =~ s/^\+//;
5029 $trailing = trim($trailing);
5030 $fixedline =~ s/}\s*$/} $trailing/;
5031 fix_insert_line($fixlinenr, $fixedline);
5032 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005033 }
5034 }
5035
Joe Perches95e2c602013-07-03 15:05:20 -07005036#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005037 while ($line =~ m{($Constant|$Lval)}g) {
5038 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005039
Joe Perches95e2c602013-07-03 15:05:20 -07005040#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005041 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005042 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005043#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005044 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005045#Ignore SI style variants like nS, mV and dB
5046#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5047 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005048#Ignore some three character SI units explicitly, like MiB and KHz
5049 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005050 while ($var =~ m{($Ident)}g) {
5051 my $word = $1;
5052 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005053 if ($check) {
5054 seed_camelcase_includes();
5055 if (!$file && !$camelcase_file_seeded) {
5056 seed_camelcase_file($realfile);
5057 $camelcase_file_seeded = 1;
5058 }
5059 }
Joe Perches7e781f62013-09-11 14:23:55 -07005060 if (!defined $camelcase{$word}) {
5061 $camelcase{$word} = 1;
5062 CHK("CAMELCASE",
5063 "Avoid CamelCase: <$word>\n" . $herecurr);
5064 }
Joe Perches34456862013-07-03 15:05:34 -07005065 }
Joe Perches323c1262012-12-17 16:02:07 -08005066 }
5067 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005068
5069#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005070 if ($line =~ /\#\s*define.*\\\s+$/) {
5071 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5072 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5073 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005074 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005075 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005076 }
5077
Fabian Frederick0e212e02015-04-16 12:44:25 -07005078# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5079# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005080 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005081 my $file = "$1.h";
5082 my $checkfile = "include/linux/$file";
5083 if (-f "$root/$checkfile" &&
5084 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005085 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005086 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005087 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5088 if ($asminclude > 0) {
5089 if ($realfile =~ m{^arch/}) {
5090 CHK("ARCH_INCLUDE_LINUX",
5091 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5092 } else {
5093 WARN("INCLUDE_LINUX",
5094 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5095 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005096 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005097 }
5098 }
5099
Andy Whitcroft653d4872007-06-23 17:16:34 -07005100# multi-statement macros should be enclosed in a do while loop, grab the
5101# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005102# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07005103 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5104 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005105 my $ln = $linenr;
5106 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005107 my ($off, $dstat, $dcond, $rest);
5108 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005109 my $has_flow_statement = 0;
5110 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005111 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005112 ctx_statement_block($linenr, $realcnt, 0);
5113 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005114 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005115 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005116
Joe Perches08a28432014-10-13 15:51:55 -07005117 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005118 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005119
Joe Perchesf59b64b2016-10-11 13:52:08 -07005120 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5121 my $define_args = $1;
5122 my $define_stmt = $dstat;
5123 my @def_args = ();
5124
5125 if (defined $define_args && $define_args ne "") {
5126 $define_args = substr($define_args, 1, length($define_args) - 2);
5127 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005128 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005129 @def_args = split(",", $define_args);
5130 }
5131
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005132 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005133 $dstat =~ s/\\\n.//g;
5134 $dstat =~ s/^\s*//s;
5135 $dstat =~ s/\s*$//s;
5136
5137 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005138 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5139 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005140 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005141 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005142 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005143
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005144 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07005145 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5146 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005147 {
5148 }
5149
Joe Perches42e15292016-03-15 14:58:01 -07005150 # Make asm volatile uses seem like a generic function
5151 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5152
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005153 my $exceptions = qr{
5154 $Declare|
5155 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005156 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005157 DECLARE_PER_CPU|
5158 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005159 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005160 union|
5161 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005162 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005163 ^\"|\"$|
5164 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005165 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005166 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005167
5168 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005169 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005170 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005171
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005172 if ($dstat ne '' &&
5173 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5174 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005175 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005176 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005177 $dstat !~ /$exceptions/ &&
5178 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005179 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005180 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005181 $dstat !~ /^for\s*$Constant$/ && # for (...)
5182 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5183 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005184 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005185 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005186 {
Joe Perchese7955562017-05-08 15:55:48 -07005187 if ($dstat =~ /^\s*if\b/) {
5188 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5189 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5190 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005191 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5192 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5193 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005194 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005195 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005196 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005197
5198 }
Joe Perches52076492016-10-11 13:52:14 -07005199
5200 # Make $define_stmt single line, comment-free, etc
5201 my @stmt_array = split('\n', $define_stmt);
5202 my $first = 1;
5203 $define_stmt = "";
5204 foreach my $l (@stmt_array) {
5205 $l =~ s/\\$//;
5206 if ($first) {
5207 $define_stmt = $l;
5208 $first = 0;
5209 } elsif ($l =~ /^[\+ ]/) {
5210 $define_stmt .= substr($l, 1);
5211 }
5212 }
5213 $define_stmt =~ s/$;//g;
5214 $define_stmt =~ s/\s+/ /g;
5215 $define_stmt = trim($define_stmt);
5216
Joe Perchesf59b64b2016-10-11 13:52:08 -07005217# check if any macro arguments are reused (ignore '...' and 'type')
5218 foreach my $arg (@def_args) {
5219 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005220 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005221 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005222 $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
Joe Perches7fe528a22017-07-10 15:52:27 -07005223 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5224 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005225 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005226 if ($use_cnt > 1) {
5227 CHK("MACRO_ARG_REUSE",
5228 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005229 }
5230# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005231 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005232 ((defined($1) && $1 ne ',') ||
5233 (defined($2) && $2 ne ','))) {
5234 CHK("MACRO_ARG_PRECEDENCE",
5235 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005236 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005237 }
Joe Perches5023d342012-12-17 16:01:47 -08005238
Joe Perches08a28432014-10-13 15:51:55 -07005239# check for macros with flow control, but without ## concatenation
5240# ## concatenation is commonly a macro that defines a function so ignore those
5241 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005242 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005243 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005244
Joe Perches08a28432014-10-13 15:51:55 -07005245 WARN("MACRO_WITH_FLOW_CONTROL",
5246 "Macros with flow control statements should be avoided\n" . "$herectx");
5247 }
5248
Joe Perches481eb482012-12-17 16:01:56 -08005249# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005250
5251 } else {
5252 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005253 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5254 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005255 $line =~ /^\+.*\\$/) {
5256 WARN("LINE_CONTINUATIONS",
5257 "Avoid unnecessary line continuations\n" . $herecurr);
5258 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005259 }
5260
Joe Perchesb13edf72012-07-30 14:41:24 -07005261# do {} while (0) macro tests:
5262# single-statement macros do not need to be enclosed in do while (0) loop,
5263# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005264 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005265 $realfile !~ m@/vmlinux.lds.h$@ &&
5266 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5267 my $ln = $linenr;
5268 my $cnt = $realcnt;
5269 my ($off, $dstat, $dcond, $rest);
5270 my $ctx = '';
5271 ($dstat, $dcond, $ln, $cnt, $off) =
5272 ctx_statement_block($linenr, $realcnt, 0);
5273 $ctx = $dstat;
5274
5275 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005276 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005277
5278 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5279 my $stmts = $2;
5280 my $semis = $3;
5281
5282 $ctx =~ s/\n*$//;
5283 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005284 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005285
Joe Perchesac8e97f2012-08-21 16:15:53 -07005286 if (($stmts =~ tr/;/;/) == 1 &&
5287 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005288 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5289 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5290 }
5291 if (defined $semis && $semis ne "") {
5292 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5293 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5294 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005295 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5296 $ctx =~ s/\n*$//;
5297 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005298 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005299
5300 WARN("TRAILING_SEMICOLON",
5301 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005302 }
5303 }
5304
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005305# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005306 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5307 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005308 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005309 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005310 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5311 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005312 my @allowed = ();
5313 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005314 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005315 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005316 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005317 for my $chunk (@chunks) {
5318 my ($cond, $block) = @{$chunk};
5319
Andy Whitcroft773647a2008-03-28 14:15:58 -07005320 # If the condition carries leading newlines, then count those as offsets.
5321 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5322 my $offset = statement_rawlines($whitespace) - 1;
5323
Joe Perchesaad4f612012-03-23 15:02:19 -07005324 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005325 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5326
5327 # We have looked at and allowed this specific line.
5328 $suppress_ifbraces{$ln + $offset} = 1;
5329
5330 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005331 $ln += statement_rawlines($block) - 1;
5332
Andy Whitcroft773647a2008-03-28 14:15:58 -07005333 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005334
5335 $seen++ if ($block =~ /^\s*{/);
5336
Joe Perchesaad4f612012-03-23 15:02:19 -07005337 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005338 if (statement_lines($cond) > 1) {
5339 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005340 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005341 }
5342 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005343 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005344 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005345 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005346 if (statement_block_size($block) > 1) {
5347 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005348 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005349 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005350 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005351 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005352 if ($seen) {
5353 my $sum_allowed = 0;
5354 foreach (@allowed) {
5355 $sum_allowed += $_;
5356 }
5357 if ($sum_allowed == 0) {
5358 WARN("BRACES",
5359 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5360 } elsif ($sum_allowed != $allow &&
5361 $seen != $allow) {
5362 CHK("BRACES",
5363 "braces {} should be used on all arms of this statement\n" . $herectx);
5364 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005365 }
5366 }
5367 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005368 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005369 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005370 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005371
Andy Whitcroftcf655042008-03-04 14:28:20 -08005372 # Check the pre-context.
5373 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5374 #print "APW: ALLOWED: pre<$1>\n";
5375 $allowed = 1;
5376 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005377
5378 my ($level, $endln, @chunks) =
5379 ctx_statement_full($linenr, $realcnt, $-[0]);
5380
Andy Whitcroftcf655042008-03-04 14:28:20 -08005381 # Check the condition.
5382 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005383 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005384 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005385 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005386 }
5387 if (statement_lines($cond) > 1) {
5388 #print "APW: ALLOWED: cond<$cond>\n";
5389 $allowed = 1;
5390 }
5391 if ($block =~/\b(?:if|for|while)\b/) {
5392 #print "APW: ALLOWED: block<$block>\n";
5393 $allowed = 1;
5394 }
5395 if (statement_block_size($block) > 1) {
5396 #print "APW: ALLOWED: lines block<$block>\n";
5397 $allowed = 1;
5398 }
5399 # Check the post-context.
5400 if (defined $chunks[1]) {
5401 my ($cond, $block) = @{$chunks[1]};
5402 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005403 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005404 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005405 if ($block =~ /^\s*\{/) {
5406 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5407 $allowed = 1;
5408 }
5409 }
5410 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005411 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005412 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005413
Joe Perches000d1cc12011-07-25 17:13:25 -07005414 WARN("BRACES",
5415 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005416 }
5417 }
5418
Joe Perchese4c5bab2017-02-24 15:01:41 -08005419# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005420 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5421 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005422 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5423 }
5424
Joe Perches0979ae62012-12-17 16:01:59 -08005425# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005426 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005427 if (CHK("BRACES",
5428 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5429 $fix && $prevrawline =~ /^\+/) {
5430 fix_delete_line($fixlinenr - 1, $prevrawline);
5431 }
Joe Perches0979ae62012-12-17 16:01:59 -08005432 }
Joe Perches77b9a532013-07-03 15:05:29 -07005433 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005434 if (CHK("BRACES",
5435 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5436 $fix) {
5437 fix_delete_line($fixlinenr, $rawline);
5438 }
Joe Perches0979ae62012-12-17 16:01:59 -08005439 }
5440
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005441# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005442 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5443 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005444 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005445 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005446 }
5447
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005448# Check for user-visible strings broken across lines, which breaks the ability
5449# to grep for the string. Make exceptions when the previous string ends in a
5450# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5451# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005452 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005453 $prevline =~ /"\s*$/ &&
5454 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5455 if (WARN("SPLIT_STRING",
5456 "quoted string split across lines\n" . $hereprev) &&
5457 $fix &&
5458 $prevrawline =~ /^\+.*"\s*$/ &&
5459 $last_coalesced_string_linenr != $linenr - 1) {
5460 my $extracted_string = get_quoted_string($line, $rawline);
5461 my $comma_close = "";
5462 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5463 $comma_close = $1;
5464 }
5465
5466 fix_delete_line($fixlinenr - 1, $prevrawline);
5467 fix_delete_line($fixlinenr, $rawline);
5468 my $fixedline = $prevrawline;
5469 $fixedline =~ s/"\s*$//;
5470 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5471 fix_insert_line($fixlinenr - 1, $fixedline);
5472 $fixedline = $rawline;
5473 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5474 if ($fixedline !~ /\+\s*$/) {
5475 fix_insert_line($fixlinenr, $fixedline);
5476 }
5477 $last_coalesced_string_linenr = $linenr;
5478 }
5479 }
5480
5481# check for missing a space in a string concatenation
5482 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5483 WARN('MISSING_SPACE',
5484 "break quoted strings at a space character\n" . $hereprev);
5485 }
5486
Joe Perchese4b7d302017-05-08 15:55:51 -07005487# check for an embedded function name in a string when the function is known
5488# This does not work very well for -f --file checking as it depends on patch
5489# context providing the function name or a single line form for in-file
5490# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005491 if ($line =~ /^\+.*$String/ &&
5492 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005493 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5494 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005495 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005496 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005497 }
5498
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005499# check for spaces before a quoted newline
5500 if ($rawline =~ /^.*\".*\s\\n/) {
5501 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5502 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5503 $fix) {
5504 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5505 }
5506
5507 }
5508
Joe Perchesf17dba42014-10-13 15:51:51 -07005509# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005510 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5511 if (CHK("CONCATENATED_STRING",
5512 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5513 $fix) {
5514 while ($line =~ /($String)/g) {
5515 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5516 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5517 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5518 }
5519 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005520 }
5521
Joe Perches90ad30e2014-12-10 15:51:59 -08005522# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005523 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005524 if (WARN("STRING_FRAGMENTS",
5525 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5526 $fix) {
5527 while ($line =~ /($String)(?=\s*")/g) {
5528 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5529 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5530 }
5531 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005532 }
5533
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005534# check for non-standard and hex prefixed decimal printf formats
5535 my $show_L = 1; #don't show the same defect twice
5536 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005537 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005538 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005539 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005540 # check for %L
5541 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005542 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005543 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5544 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005545 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005546 # check for %Z
5547 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5548 WARN("PRINTF_Z",
5549 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5550 $show_Z = 0;
5551 }
5552 # check for 0x<decimal>
5553 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5554 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005555 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5556 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005557 }
5558
5559# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005560 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005561 WARN("LINE_CONTINUATIONS",
5562 "Avoid line continuations in quoted strings\n" . $herecurr);
5563 }
5564
Andy Whitcroft00df3442007-06-08 13:47:06 -07005565# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005566 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005567 WARN("IF_0",
5568 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5569 }
5570
5571# warn about #if 1
5572 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5573 WARN("IF_1",
5574 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005575 }
5576
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005577# check for needless "if (<foo>) fn(<foo>)" uses
5578 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005579 my $tested = quotemeta($1);
5580 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5581 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5582 my $func = $1;
5583 if (WARN('NEEDLESS_IF',
5584 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5585 $fix) {
5586 my $do_fix = 1;
5587 my $leading_tabs = "";
5588 my $new_leading_tabs = "";
5589 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5590 $leading_tabs = $1;
5591 } else {
5592 $do_fix = 0;
5593 }
5594 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5595 $new_leading_tabs = $1;
5596 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5597 $do_fix = 0;
5598 }
5599 } else {
5600 $do_fix = 0;
5601 }
5602 if ($do_fix) {
5603 fix_delete_line($fixlinenr - 1, $prevrawline);
5604 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5605 }
5606 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005607 }
5608 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005609
Joe Perchesebfdc402014-08-06 16:10:27 -07005610# check for unnecessary "Out of Memory" messages
5611 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5612 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5613 (defined $1 || defined $3) &&
5614 $linenr > 3) {
5615 my $testval = $2;
5616 my $testline = $lines[$linenr - 3];
5617
5618 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5619# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5620
Joe Perchese29a70f2019-03-07 16:28:35 -08005621 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5622 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005623 WARN("OOM_MESSAGE",
5624 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5625 }
5626 }
5627
Joe Perchesf78d98f2014-10-13 15:52:01 -07005628# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005629 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005630 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5631 my $level = $1;
5632 if (WARN("UNNECESSARY_KERN_LEVEL",
5633 "Possible unnecessary $level\n" . $herecurr) &&
5634 $fix) {
5635 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5636 }
5637 }
5638
Joe Perches45c55e92017-02-24 15:01:31 -08005639# check for logging continuations
5640 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5641 WARN("LOGGING_CONTINUATION",
5642 "Avoid logging continuation uses where feasible\n" . $herecurr);
5643 }
5644
Joe Perchesabb08a52014-12-10 15:51:46 -08005645# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005646 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005647 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5648 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5649 WARN("MASK_THEN_SHIFT",
5650 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5651 }
5652
Joe Perchesb75ac612014-12-10 15:52:02 -08005653# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005654 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005655 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5656 my $val = $1;
5657 my $equal = "!";
5658 $equal = "" if ($4 eq "!=");
5659 if (CHK("COMPARISON_TO_NULL",
5660 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5661 $fix) {
5662 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5663 }
5664 }
5665 }
5666
Joe Perches8716de32013-09-11 14:24:05 -07005667# check for bad placement of section $InitAttribute (e.g.: __initdata)
5668 if ($line =~ /(\b$InitAttribute\b)/) {
5669 my $attr = $1;
5670 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5671 my $ptr = $1;
5672 my $var = $2;
5673 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5674 ERROR("MISPLACED_INIT",
5675 "$attr should be placed after $var\n" . $herecurr)) ||
5676 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5677 WARN("MISPLACED_INIT",
5678 "$attr should be placed after $var\n" . $herecurr))) &&
5679 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005680 $fixed[$fixlinenr] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
Joe Perches8716de32013-09-11 14:24:05 -07005681 }
5682 }
5683 }
5684
Joe Perchese970b8842013-11-12 15:10:10 -08005685# check for $InitAttributeData (ie: __initdata) with const
5686 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5687 my $attr = $1;
5688 $attr =~ /($InitAttributePrefix)(.*)/;
5689 my $attr_prefix = $1;
5690 my $attr_type = $2;
5691 if (ERROR("INIT_ATTRIBUTE",
5692 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5693 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005694 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005695 s/$InitAttributeData/${attr_prefix}initconst/;
5696 }
5697 }
5698
5699# check for $InitAttributeConst (ie: __initconst) without const
5700 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5701 my $attr = $1;
5702 if (ERROR("INIT_ATTRIBUTE",
5703 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5704 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005705 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005706 /(^\+\s*(?:static\s+))/;
5707 $lead = rtrim($1);
5708 $lead = "$lead " if ($lead !~ /^\+$/);
5709 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005710 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005711 }
5712 }
5713
Joe Perchesc17893c2015-04-16 12:44:42 -07005714# check for __read_mostly with const non-pointer (should just be const)
5715 if ($line =~ /\b__read_mostly\b/ &&
5716 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5717 if (ERROR("CONST_READ_MOSTLY",
5718 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5719 $fix) {
5720 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5721 }
5722 }
5723
Joe Perchesfbdb8132014-04-03 14:49:14 -07005724# don't use __constant_<foo> functions outside of include/uapi/
5725 if ($realfile !~ m@^include/uapi/@ &&
5726 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5727 my $constant_func = $1;
5728 my $func = $constant_func;
5729 $func =~ s/^__constant_//;
5730 if (WARN("CONSTANT_CONVERSION",
5731 "$constant_func should be $func\n" . $herecurr) &&
5732 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005733 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005734 }
5735 }
5736
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005737# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005738 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005739 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005740 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005741 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005742 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005743 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07005744 }
5745 if ($delay > 2000) {
5746 WARN("LONG_UDELAY",
5747 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005748 }
5749 }
5750
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005751# warn about unexpectedly long msleep's
5752 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5753 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005754 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005755 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005756 }
5757 }
5758
Joe Perches36ec1932013-07-03 15:05:25 -07005759# check for comparisons of jiffies
5760 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5761 WARN("JIFFIES_COMPARISON",
5762 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5763 }
5764
Joe Perches9d7a34a2013-07-03 15:05:26 -07005765# check for comparisons of get_jiffies_64()
5766 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5767 WARN("JIFFIES_COMPARISON",
5768 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5769 }
5770
Andy Whitcroft00df3442007-06-08 13:47:06 -07005771# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005772# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005773# print "#ifdef in C files should be avoided\n";
5774# print "$herecurr";
5775# $clean = 0;
5776# }
5777
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005778# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005779 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005780 if (ERROR("SPACING",
5781 "exactly one space required after that #$1\n" . $herecurr) &&
5782 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005783 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005784 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5785 }
5786
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005787 }
5788
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005789# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005790 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5791 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005792 my $which = $1;
5793 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005794 CHK("UNCOMMENTED_DEFINITION",
5795 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005796 }
5797 }
5798# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005799
5800 my $barriers = qr{
5801 mb|
5802 rmb|
5803 wmb|
5804 read_barrier_depends
5805 }x;
5806 my $barrier_stems = qr{
5807 mb__before_atomic|
5808 mb__after_atomic|
5809 store_release|
5810 load_acquire|
5811 store_mb|
5812 (?:$barriers)
5813 }x;
5814 my $all_barriers = qr{
5815 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005816 smp_(?:$barrier_stems)|
5817 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005818 }x;
5819
5820 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005821 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005822 WARN("MEMORY_BARRIER",
5823 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005824 }
5825 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005826
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005827 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5828
5829 if ($realfile !~ m@^include/asm-generic/@ &&
5830 $realfile !~ m@/barrier\.h$@ &&
5831 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5832 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5833 WARN("MEMORY_BARRIER",
5834 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5835 }
5836
Joe Perchescb426e92015-06-25 15:02:46 -07005837# check for waitqueue_active without a comment.
5838 if ($line =~ /\bwaitqueue_active\s*\(/) {
5839 if (!ctx_has_comment($first_line, $linenr)) {
5840 WARN("WAITQUEUE_ACTIVE",
5841 "waitqueue_active without comment\n" . $herecurr);
5842 }
5843 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005844
Paul E. McKenney91db2592017-11-27 09:37:35 -08005845# check for smp_read_barrier_depends and read_barrier_depends
5846 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5847 WARN("READ_BARRIER_DEPENDS",
5848 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5849 }
5850
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005851# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005852 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005853 CHK("ARCH_DEFINES",
5854 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005855 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005856
Joe Perches596ed452017-07-12 14:37:02 -07005857# check that the storage class is not after a type
5858 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005859 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005860 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5861 }
5862# Check that the storage class is at the beginning of a declaration
5863 if ($line =~ /\b$Storage\b/ &&
5864 $line !~ /^.\s*$Storage/ &&
5865 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5866 $1 !~ /[\,\)]\s*$/) {
5867 WARN("STORAGE_CLASS",
5868 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005869 }
5870
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005871# check the location of the inline attribute, that it is between
5872# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005873 if ($line =~ /\b$Type\s+$Inline\b/ ||
5874 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005875 ERROR("INLINE_LOCATION",
5876 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005877 }
5878
Andy Whitcroft8905a672007-11-28 16:21:06 -08005879# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005880 if ($realfile !~ m@\binclude/uapi/@ &&
5881 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005882 if (WARN("INLINE",
5883 "plain inline is preferred over $1\n" . $herecurr) &&
5884 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005885 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005886
5887 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005888 }
5889
Joe Perches3d130fd2011-01-12 17:00:00 -08005890# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005891 if ($realfile !~ m@\binclude/uapi/@ &&
5892 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005893 WARN("PREFER_PACKED",
5894 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005895 }
5896
Joe Perches39b7e282011-07-25 17:13:24 -07005897# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005898 if ($realfile !~ m@\binclude/uapi/@ &&
5899 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005900 WARN("PREFER_ALIGNED",
5901 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005902 }
5903
Joe Perches462811d2019-09-25 16:46:44 -07005904# Check for __attribute__ section, prefer __section
5905 if ($realfile !~ m@\binclude/uapi/@ &&
5906 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
5907 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
5908 my $new = substr($old, 1, -1);
5909 if (WARN("PREFER_SECTION",
5910 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
5911 $fix) {
5912 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
5913 }
5914 }
5915
Joe Perches5f14d3b2012-01-10 15:09:52 -08005916# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005917 if ($realfile !~ m@\binclude/uapi/@ &&
5918 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005919 if (WARN("PREFER_PRINTF",
5920 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5921 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005922 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005923
5924 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005925 }
5926
Joe Perches6061d942012-03-23 15:02:16 -07005927# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005928 if ($realfile !~ m@\binclude/uapi/@ &&
5929 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005930 if (WARN("PREFER_SCANF",
5931 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5932 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005933 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005934 }
Joe Perches6061d942012-03-23 15:02:16 -07005935 }
5936
Joe Perches619a9082014-12-10 15:51:35 -08005937# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07005938 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08005939 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5940 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5941 $line =~ /\b__weak\b/)) {
5942 ERROR("WEAK_DECLARATION",
5943 "Using weak declarations can have unintended link defects\n" . $herecurr);
5944 }
5945
Tomas Winklerfd39f902016-12-12 16:46:34 -08005946# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005947 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005948 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005949 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5950 my $type = $1;
5951 if ($type =~ /\b($typeC99Typedefs)\b/) {
5952 $type = $1;
5953 my $kernel_type = 'u';
5954 $kernel_type = 's' if ($type =~ /^_*[si]/);
5955 $type =~ /(\d+)/;
5956 $kernel_type .= $1;
5957 if (CHK("PREFER_KERNEL_TYPES",
5958 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5959 $fix) {
5960 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5961 }
5962 }
5963 }
5964
Joe Perches938224b2016-01-20 14:59:15 -08005965# check for cast of C90 native int or longer types constants
5966 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5967 my $cast = $1;
5968 my $const = $2;
5969 if (WARN("TYPECAST_INT_CONSTANT",
5970 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5971 $fix) {
5972 my $suffix = "";
5973 my $newconst = $const;
5974 $newconst =~ s/${Int_type}$//;
5975 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5976 if ($cast =~ /\blong\s+long\b/) {
5977 $suffix .= 'LL';
5978 } elsif ($cast =~ /\blong\b/) {
5979 $suffix .= 'L';
5980 }
5981 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5982 }
5983 }
5984
Joe Perches8f53a9b2010-03-05 13:43:48 -08005985# check for sizeof(&)
5986 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005987 WARN("SIZEOF_ADDRESS",
5988 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005989 }
5990
Joe Perches66c80b62012-07-30 14:41:22 -07005991# check for sizeof without parenthesis
5992 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005993 if (WARN("SIZEOF_PARENTHESIS",
5994 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5995 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005996 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005997 }
Joe Perches66c80b62012-07-30 14:41:22 -07005998 }
5999
Joe Perches88982fe2012-12-17 16:02:00 -08006000# check for struct spinlock declarations
6001 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6002 WARN("USE_SPINLOCK_T",
6003 "struct spinlock should be spinlock_t\n" . $herecurr);
6004 }
6005
Joe Perchesa6962d72013-04-29 16:18:13 -07006006# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006007 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006008 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006009 $fmt =~ s/%%//g;
6010 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006011 if (WARN("PREFER_SEQ_PUTS",
6012 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6013 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006014 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006015 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006016 }
6017 }
6018
Joe Perches478b1792018-04-10 16:33:34 -07006019# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006020 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006021 defined $stat &&
6022 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6023 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006024 my $stat_real;
6025
Joe Perches0b523762017-05-08 15:55:36 -07006026 my $lc = $stat =~ tr@\n@@;
6027 $lc = $lc + $linenr;
6028 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006029 my $specifier;
6030 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006031 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006032 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006033 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6034 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006035
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006036 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006037 $specifier = $1;
6038 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006039 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006040 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006041 ($extension eq "f" &&
6042 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006043 $bad_specifier = $specifier;
6044 last;
6045 }
6046 if ($extension eq "x" && !defined($stat_real)) {
6047 if (!defined($stat_real)) {
6048 $stat_real = get_stat_real($linenr, $lc);
6049 }
6050 WARN("VSPRINTF_SPECIFIER_PX",
6051 "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");
6052 }
6053 }
6054 if ($bad_specifier ne "") {
6055 my $stat_real = get_stat_real($linenr, $lc);
6056 my $ext_type = "Invalid";
6057 my $use = "";
6058 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006059 $use = " - use %pS instead";
6060 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6061 }
6062
6063 WARN("VSPRINTF_POINTER_EXTENSION",
6064 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6065 }
Joe Perches0b523762017-05-08 15:55:36 -07006066 }
6067 }
6068
Andy Whitcroft554e1652012-01-10 15:09:57 -08006069# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006070 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006071 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006072 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006073
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006074 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006075 my $ms_val = $7;
6076 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006077
Andy Whitcroft554e1652012-01-10 15:09:57 -08006078 if ($ms_size =~ /^(0x|)0$/i) {
6079 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006080 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006081 } elsif ($ms_size =~ /^(0x|)1$/i) {
6082 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006083 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6084 }
6085 }
6086
Joe Perches98a9bba2014-01-23 15:54:52 -08006087# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006088# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006089# defined $stat &&
6090# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6091# if (WARN("PREFER_ETHER_ADDR_COPY",
6092# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6093# $fix) {
6094# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6095# }
6096# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006097
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006098# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006099# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006100# defined $stat &&
6101# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6102# WARN("PREFER_ETHER_ADDR_EQUAL",
6103# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6104# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006105
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006106# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6107# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006108# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006109# defined $stat &&
6110# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6111#
6112# my $ms_val = $7;
6113#
6114# if ($ms_val =~ /^(?:0x|)0+$/i) {
6115# if (WARN("PREFER_ETH_ZERO_ADDR",
6116# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6117# $fix) {
6118# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6119# }
6120# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6121# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6122# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6123# $fix) {
6124# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6125# }
6126# }
6127# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006128
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006129# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006130 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006131 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006132 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006133 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006134 my $call = $1;
6135 my $cast1 = deparenthesize($2);
6136 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006137 my $cast2 = deparenthesize($7);
6138 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006139 my $cast;
6140
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006141 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006142 $cast = "$cast1 or $cast2";
6143 } elsif ($cast1 ne "") {
6144 $cast = $cast1;
6145 } else {
6146 $cast = $cast2;
6147 }
6148 WARN("MINMAX",
6149 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006150 }
6151 }
6152
Joe Perches4a273192012-07-30 14:41:20 -07006153# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006154 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006155 defined $stat &&
6156 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6157 my $min = $1;
6158 my $max = $7;
6159 if ($min eq $max) {
6160 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006161 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Joe Perches4a273192012-07-30 14:41:20 -07006162 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6163 $min > $max) {
6164 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006165 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Joe Perches4a273192012-07-30 14:41:20 -07006166 }
6167 }
6168
Joe Perches823b7942013-11-12 15:10:15 -08006169# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006170 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006171 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006172 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006173 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6174 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6175 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6176 my $lc = $stat =~ tr@\n@@;
6177 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006178 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006179 WARN("NAKED_SSCANF",
6180 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6181 }
6182
Joe Perchesafc819a2014-06-04 16:12:08 -07006183# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006184 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006185 defined $stat &&
6186 $line =~ /\bsscanf\b/) {
6187 my $lc = $stat =~ tr@\n@@;
6188 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006189 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006190 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6191 my $format = $6;
6192 my $count = $format =~ tr@%@%@;
6193 if ($count == 1 &&
6194 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6195 WARN("SSCANF_TO_KSTRTO",
6196 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6197 }
6198 }
6199 }
6200
Joe Perches70dc8a42013-09-11 14:23:58 -07006201# check for new externs in .h files.
6202 if ($realfile =~ /\.h$/ &&
6203 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006204 if (CHK("AVOID_EXTERNS",
6205 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006206 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006207 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006208 }
6209 }
6210
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006211# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006212 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006213 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006214 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006215 my $function_name = $1;
6216 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006217
6218 my $s = $stat;
6219 if (defined $cond) {
6220 substr($s, 0, length($cond), '');
6221 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006222 if ($s =~ /^\s*;/ &&
6223 $function_name ne 'uninitialized_var')
6224 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006225 WARN("AVOID_EXTERNS",
6226 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006227 }
6228
6229 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006230 WARN("FUNCTION_ARGUMENTS",
6231 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006232 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006233
6234 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6235 $stat =~ /^.\s*extern\s+/)
6236 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006237 WARN("AVOID_EXTERNS",
6238 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006239 }
6240
Joe Perchesa0ad7592017-07-10 15:52:19 -07006241# check for function declarations that have arguments without identifier names
6242 if (defined $stat &&
Miles Chen25bdda22017-11-17 15:28:34 -08006243 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Joe Perchesca0d8922016-10-11 13:52:16 -07006244 $1 ne "void") {
6245 my $args = trim($1);
6246 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6247 my $arg = trim($1);
6248 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6249 WARN("FUNCTION_ARGUMENTS",
6250 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6251 }
6252 }
6253 }
6254
Joe Perchesa0ad7592017-07-10 15:52:19 -07006255# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006256 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006257 defined $stat &&
6258 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6259 $context_function = $1;
6260
6261# check for multiline function definition with misplaced open brace
6262 my $ok = 0;
6263 my $cnt = statement_rawlines($stat);
6264 my $herectx = $here . "\n";
6265 for (my $n = 0; $n < $cnt; $n++) {
6266 my $rl = raw_line($linenr, $n);
6267 $herectx .= $rl . "\n";
6268 $ok = 1 if ($rl =~ /^[ \+]\{/);
6269 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6270 last if $rl =~ /^[ \+].*\{/;
6271 }
6272 if (!$ok) {
6273 ERROR("OPEN_BRACE",
6274 "open brace '{' following function definitions go on the next line\n" . $herectx);
6275 }
6276 }
6277
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006278# checks for new __setup's
6279 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6280 my $name = $1;
6281
6282 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006283 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006284 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006285 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006286 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006287
Joe Perchese29a70f2019-03-07 16:28:35 -08006288# check for pointless casting of alloc functions
6289 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006290 WARN("UNNECESSARY_CASTS",
6291 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006292 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006293
Joe Perchesa640d252013-07-03 15:05:21 -07006294# alloc style
6295# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006296 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006297 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
Joe Perchesa640d252013-07-03 15:05:21 -07006298 CHK("ALLOC_SIZEOF_STRUCT",
6299 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6300 }
6301
Joe Perches60a55362014-06-04 16:12:07 -07006302# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006303 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006304 defined $stat &&
6305 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
Joe Perches60a55362014-06-04 16:12:07 -07006306 my $oldfunc = $3;
6307 my $a1 = $4;
6308 my $a2 = $10;
6309 my $newfunc = "kmalloc_array";
6310 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006311 my $r1 = $a1;
6312 my $r2 = $a2;
6313 if ($a1 =~ /^sizeof\s*\S/) {
6314 $r1 = $a2;
6315 $r2 = $a1;
6316 }
6317 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6318 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006319 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006320 my $herectx = get_stat_here($linenr, $cnt, $here);
6321
Joe Perches60a55362014-06-04 16:12:07 -07006322 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006323 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6324 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006325 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006326 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
Joe Perches60a55362014-06-04 16:12:07 -07006327 }
6328 }
6329 }
6330
Joe Perches972fdea2013-04-29 16:18:12 -07006331# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006332 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006333 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6334 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006335 WARN("KREALLOC_ARG_REUSE",
6336 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6337 }
6338
Joe Perches5ce59ae2013-02-21 16:44:18 -08006339# check for alloc argument mismatch
6340 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6341 WARN("ALLOC_ARRAY_ARGS",
6342 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6343 }
6344
Joe Perchescaf2a542011-01-12 16:59:56 -08006345# check for multiple semicolons
6346 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006347 if (WARN("ONE_SEMICOLON",
6348 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6349 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006350 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006351 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006352 }
6353
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006354# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6355 if ($realfile !~ m@^include/uapi/@ &&
6356 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006357 my $ull = "";
6358 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6359 if (CHK("BIT_MACRO",
6360 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6361 $fix) {
6362 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6363 }
6364 }
6365
Joe Perches2d632742016-05-20 17:04:00 -07006366# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6367 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6368 my $config = $1;
6369 if (WARN("PREFER_IS_ENABLED",
6370 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6371 $fix) {
6372 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6373 }
6374 }
6375
Joe Perchese81f2392014-08-06 16:11:25 -07006376# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006377 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6378 my $has_break = 0;
6379 my $has_statement = 0;
6380 my $count = 0;
6381 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006382 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006383 $prevline--;
6384 my $rline = $rawlines[$prevline - 1];
6385 my $fline = $lines[$prevline - 1];
6386 last if ($fline =~ /^\@\@/);
6387 next if ($fline =~ /^\-/);
6388 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6389 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6390 next if ($fline =~ /^.[\s$;]*$/);
6391 $has_statement = 1;
6392 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006393 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006394 }
6395 if (!$has_break && $has_statement) {
6396 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006397 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006398 }
6399 }
6400
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006401# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006402 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006403 defined $stat &&
6404 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006405 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006406 my $herectx = get_stat_here($linenr, $cnt, $here);
6407
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006408 WARN("DEFAULT_NO_BREAK",
6409 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006410 }
6411
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006412# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006413 if ($line =~ /\b__FUNCTION__\b/) {
6414 if (WARN("USE_FUNC",
6415 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6416 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006417 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006418 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006419 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006420
Joe Perches62ec8182015-02-13 14:38:18 -08006421# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6422 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6423 ERROR("DATE_TIME",
6424 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6425 }
6426
Joe Perches2c924882012-03-23 15:02:20 -07006427# check for use of yield()
6428 if ($line =~ /\byield\s*\(\s*\)/) {
6429 WARN("YIELD",
6430 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6431 }
6432
Joe Perches179f8f42013-07-03 15:05:30 -07006433# check for comparisons against true and false
6434 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6435 my $lead = $1;
6436 my $arg = $2;
6437 my $test = $3;
6438 my $otype = $4;
6439 my $trail = $5;
6440 my $op = "!";
6441
6442 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6443
6444 my $type = lc($otype);
6445 if ($type =~ /^(?:true|false)$/) {
6446 if (("$test" eq "==" && "$type" eq "true") ||
6447 ("$test" eq "!=" && "$type" eq "false")) {
6448 $op = "";
6449 }
6450
6451 CHK("BOOL_COMPARISON",
6452 "Using comparison to $otype is error prone\n" . $herecurr);
6453
6454## maybe suggesting a correct construct would better
6455## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6456
6457 }
6458 }
6459
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006460# check for semaphores initialized locked
6461 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006462 WARN("CONSIDER_COMPLETION",
6463 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006464 }
Joe Perches6712d852012-03-23 15:02:20 -07006465
Joe Perches67d0a072011-10-31 17:13:10 -07006466# recommend kstrto* over simple_strto* and strict_strto*
6467 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006468 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006469 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006470 }
Joe Perches6712d852012-03-23 15:02:20 -07006471
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006472# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006473 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006474 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006475 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07006476 }
Joe Perches6712d852012-03-23 15:02:20 -07006477
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006478# check for spin_is_locked(), suggest lockdep instead
6479 if ($line =~ /\bspin_is_locked\(/) {
6480 WARN("USE_LOCKDEP",
6481 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6482 }
6483
Joe Perches9189c7e2018-09-07 15:26:18 -07006484# check for deprecated apis
6485 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6486 my $deprecated_api = $1;
6487 my $new_api = $deprecated_apis{$deprecated_api};
6488 WARN("DEPRECATED_API",
6489 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6490 }
6491
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006492# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006493# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006494 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006495 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006496 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006497 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006498 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006499
6500# use of NR_CPUS is usually wrong
6501# ignore definitions of NR_CPUS and usage to define arrays as likely right
6502 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006503 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6504 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006505 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6506 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6507 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006508 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006509 WARN("NR_CPUS",
6510 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006511 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006512
Joe Perches52ea8502013-11-12 15:10:09 -08006513# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6514 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6515 ERROR("DEFINE_ARCH_HAS",
6516 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6517 }
6518
Joe Perchesacd93622015-02-13 14:38:38 -08006519# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006520 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006521 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6522 WARN("LIKELY_MISUSE",
6523 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6524 }
6525
Denis Efremovde3f1862019-09-25 16:49:25 -07006526# nested likely/unlikely calls
6527 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6528 WARN("LIKELY_MISUSE",
6529 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6530 }
6531
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006532# whine mightly about in_atomic
6533 if ($line =~ /\bin_atomic\s*\(/) {
6534 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006535 ERROR("IN_ATOMIC",
6536 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006537 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006538 WARN("IN_ATOMIC",
6539 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006540 }
6541 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006542
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006543# check for mutex_trylock_recursive usage
6544 if ($line =~ /mutex_trylock_recursive/) {
6545 ERROR("LOCKING",
6546 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6547 }
6548
Peter Zijlstra1704f472010-03-19 01:37:42 +01006549# check for lockdep_set_novalidate_class
6550 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6551 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6552 if ($realfile !~ m@^kernel/lockdep@ &&
6553 $realfile !~ m@^include/linux/lockdep@ &&
6554 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006555 ERROR("LOCKDEP",
6556 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006557 }
6558 }
Dave Jones88f88312011-01-12 16:59:59 -08006559
Joe Perchesb392c642015-04-16 12:44:16 -07006560 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6561 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006562 WARN("EXPORTED_WORLD_WRITABLE",
6563 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006564 }
Joe Perches24358802014-04-03 14:49:13 -07006565
Joe Perches00180462018-02-06 15:38:55 -08006566# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6567# and whether or not function naming is typical and if
6568# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006569 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006570 defined $stat &&
6571 $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*\)/) {
6572 my $var = $1;
6573 my $perms = $2;
6574 my $show = $3;
6575 my $store = $4;
6576 my $octal_perms = perms_to_octal($perms);
6577 if ($show =~ /^${var}_show$/ &&
6578 $store =~ /^${var}_store$/ &&
6579 $octal_perms eq "0644") {
6580 if (WARN("DEVICE_ATTR_RW",
6581 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6582 $fix) {
6583 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6584 }
6585 } elsif ($show =~ /^${var}_show$/ &&
6586 $store =~ /^NULL$/ &&
6587 $octal_perms eq "0444") {
6588 if (WARN("DEVICE_ATTR_RO",
6589 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6590 $fix) {
6591 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6592 }
6593 } elsif ($show =~ /^NULL$/ &&
6594 $store =~ /^${var}_store$/ &&
6595 $octal_perms eq "0200") {
6596 if (WARN("DEVICE_ATTR_WO",
6597 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6598 $fix) {
6599 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6600 }
6601 } elsif ($octal_perms eq "0644" ||
6602 $octal_perms eq "0444" ||
6603 $octal_perms eq "0200") {
6604 my $newshow = "$show";
6605 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6606 my $newstore = $store;
6607 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6608 my $rename = "";
6609 if ($show ne $newshow) {
6610 $rename .= " '$show' to '$newshow'";
6611 }
6612 if ($store ne $newstore) {
6613 $rename .= " '$store' to '$newstore'";
6614 }
6615 WARN("DEVICE_ATTR_FUNCTIONS",
6616 "Consider renaming function(s)$rename\n" . $herecurr);
6617 } else {
6618 WARN("DEVICE_ATTR_PERMS",
6619 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6620 }
6621 }
6622
Joe Perches515a2352014-04-03 14:49:24 -07006623# Mode permission misuses where it seems decimal should be octal
6624# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006625# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6626# specific definition of not visible in sysfs.
6627# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6628# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006629 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006630 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006631 $line =~ /$mode_perms_search/) {
6632 foreach my $entry (@mode_permission_funcs) {
6633 my $func = $entry->[0];
6634 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006635
Joe Perches459cf0a2016-10-11 13:52:19 -07006636 my $lc = $stat =~ tr@\n@@;
6637 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006638 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006639
Joe Perches515a2352014-04-03 14:49:24 -07006640 my $skip_args = "";
6641 if ($arg_pos > 1) {
6642 $arg_pos--;
6643 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6644 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006645 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006646 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006647 my $val = $1;
6648 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006649 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6650 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6651 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006652 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006653 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006654 }
6655 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006656 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006657 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006658 }
Joe Perches24358802014-04-03 14:49:13 -07006659 }
6660 }
6661 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006662
Joe Perches459cf0a2016-10-11 13:52:19 -07006663# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006664 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006665 my $oval = $1;
6666 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006667 if (WARN("SYMBOLIC_PERMS",
6668 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6669 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006670 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006671 }
6672 }
6673
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006674# validate content of MODULE_LICENSE against list from include/linux/module.h
6675 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6676 my $extracted_string = get_quoted_string($line, $rawline);
6677 my $valid_licenses = qr{
6678 GPL|
6679 GPL\ v2|
6680 GPL\ and\ additional\ rights|
6681 Dual\ BSD/GPL|
6682 Dual\ MIT/GPL|
6683 Dual\ MPL/GPL|
6684 Proprietary
6685 }x;
6686 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6687 WARN("MODULE_LICENSE",
6688 "unknown module license " . $extracted_string . "\n" . $herecurr);
6689 }
6690 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07006691
6692# check for sysctl duplicate constants
6693 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6694 WARN("DUPLICATED_SYSCTL_CONST",
6695 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6696 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006697 }
6698
6699 # If we have no input at all, then there is nothing to report on
6700 # so just keep quiet.
6701 if ($#rawlines == -1) {
6702 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006703 }
6704
Andy Whitcroft8905a672007-11-28 16:21:06 -08006705 # In mailback mode only produce a report in the negative, for
6706 # things that appear to be patches.
6707 if ($mailback && ($clean == 1 || !$is_patch)) {
6708 exit(0);
6709 }
6710
6711 # This is not a patch, and we are are in 'no-patch' mode so
6712 # just keep quiet.
6713 if (!$chk_patch && !$is_patch) {
6714 exit(0);
6715 }
6716
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006717 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006718 ERROR("NOT_UNIFIED_DIFF",
6719 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006720 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006721 if ($is_patch && $has_commit_log && $chk_signoff) {
6722 if ($signoff == 0) {
6723 ERROR("MISSING_SIGN_OFF",
6724 "Missing Signed-off-by: line(s)\n");
6725 } elsif (!$authorsignoff) {
6726 WARN("NO_AUTHOR_SIGN_OFF",
6727 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6728 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006729 }
6730
Andy Whitcroft8905a672007-11-28 16:21:06 -08006731 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006732 if ($summary && !($clean == 1 && $quiet == 1)) {
6733 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006734 print "total: $cnt_error errors, $cnt_warn warnings, " .
6735 (($check)? "$cnt_chk checks, " : "") .
6736 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006737 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006738
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006739 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006740 # If there were any defects found and not already fixing them
6741 if (!$clean and !$fix) {
6742 print << "EOM"
6743
6744NOTE: For some of the reported defects, checkpatch may be able to
6745 mechanically convert to the typical style using --fix or --fix-inplace.
6746EOM
6747 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006748 # If there were whitespace errors which cleanpatch can fix
6749 # then suggest that.
6750 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006751 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006752 print << "EOM"
6753
6754NOTE: Whitespace errors detected.
6755 You may wish to use scripts/cleanpatch or scripts/cleanfile
6756EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006757 }
6758 }
6759
Joe Perchesd752fcc2014-08-06 16:11:05 -07006760 if ($clean == 0 && $fix &&
6761 ("@rawlines" ne "@fixed" ||
6762 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006763 my $newfile = $filename;
6764 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006765 my $linecount = 0;
6766 my $f;
6767
Joe Perchesd752fcc2014-08-06 16:11:05 -07006768 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6769
Joe Perches3705ce52013-07-03 15:05:31 -07006770 open($f, '>', $newfile)
6771 or die "$P: Can't open $newfile for write\n";
6772 foreach my $fixed_line (@fixed) {
6773 $linecount++;
6774 if ($file) {
6775 if ($linecount > 3) {
6776 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006777 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006778 }
6779 } else {
6780 print $f $fixed_line . "\n";
6781 }
6782 }
6783 close($f);
6784
6785 if (!$quiet) {
6786 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006787
Joe Perches3705ce52013-07-03 15:05:31 -07006788Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6789
6790Do _NOT_ trust the results written to this file.
6791Do _NOT_ submit these changes without inspecting them for correctness.
6792
6793This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6794No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006795EOM
6796 }
6797 }
6798
Joe Perchesd8469f12015-06-25 15:03:00 -07006799 if ($quiet == 0) {
6800 print "\n";
6801 if ($clean == 1) {
6802 print "$vname has no obvious style problems and is ready for submission.\n";
6803 } else {
6804 print "$vname has style problems, please review.\n";
6805 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006806 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006807 return $clean;
6808}