blob: f49524e148516af2d591ace5c6ed1d5a81089c5b [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 Whitcroft015830b2010-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 Whitcroft0a920b52007-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 Whitcroft0a920b52007-06-01 00:46:48 -070017
David Keitel407fdcb2016-03-23 20:52:44 -070018use constant BEFORE_SHORTTEXT => 0;
Steve Muckle4a15af92012-03-05 10:12:04 -080019use constant IN_SHORTTEXT_BLANKLINE => 1;
20use constant IN_SHORTTEXT => 2;
21use constant AFTER_SHORTTEXT => 3;
22use constant CHECK_NEXT_SHORTTEXT => 4;
David Keitel407fdcb2016-03-23 20:52:44 -070023use constant SHORTTEXT_LIMIT => 75;
24
Andy Whitcroft0a920b52007-06-01 00:46:48 -070025my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080026my $D = dirname(abs_path($P));
Andy Whitcroft0a920b52007-06-01 00:46:48 -070027
Joe Perches000d1cc12011-07-25 17:13:25 -070028my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070029
30use Getopt::Long qw(:config no_auto_abbrev);
31
32my $quiet = 0;
33my $tree = 1;
34my $chk_signoff = 1;
35my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070036my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070037my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080038my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070039my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070040my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070041my $git = 0;
Joe Perches0dea9f12016-05-20 17:04:19 -070042my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070043my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070044my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080045my $summary = 1;
46my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080047my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070048my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070049my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070050my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080051my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070052my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080053my %debug;
Joe Perches34456862013-07-03 15:05:34 -070054my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070055my %use_type = ();
56my @use = ();
57my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070058my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070059my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070060my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080061my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070062my $ignore_perl_version = 0;
63my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070064my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070065my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070066my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070067my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070068my $conststructsfile = "$D/const_structs.checkpatch";
Jerome Forissier75ad8c52017-05-08 15:56:00 -070069my $typedefsfile = "";
John Brooks737c0762017-07-10 15:52:24 -070070my $color = "auto";
Joe Perchesdadf6802016-08-02 14:04:33 -070071my $allow_c99_comments = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070072
73sub help {
74 my ($exitcode) = @_;
75
76 print << "EOM";
77Usage: $P [OPTION]... [FILE]...
78Version: $V
79
80Options:
81 -q, --quiet quiet
82 --no-tree run without a kernel tree
83 --no-signoff do not check for 'Signed-off-by' line
84 --patch treat FILE as patchfile (default)
85 --emacs emacs compile window format
86 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070087 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070088 -g, --git treat FILE as a single commit or git revision range
89 single git commit with:
90 <rev>
91 <rev>^
92 <rev>~n
93 multiple git commits with:
94 <rev1>..<rev2>
95 <rev1>...<rev2>
96 <rev>-<count>
97 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070098 -f, --file treat FILE as regular source file
99 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -0700100 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -0700101 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -0700102 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -0700103 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -0800104 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -0700105 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -0700106 --root=PATH PATH to the kernel tree root
107 --no-summary suppress the per-file summary
108 --mailback only produce a report in case of warnings/errors
109 --summary-file include the filename in summary
110 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
111 'values', 'possible', 'type', and 'attr' (default
112 is all off)
113 --test-only=WORD report only warnings/errors containing WORD
114 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700115 --fix EXPERIMENTAL - may create horrible results
116 If correctable single-line errors exist, create
117 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
118 with potential errors corrected to the preferred
119 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800120 --fix-inplace EXPERIMENTAL - may create horrible results
121 Is the same as --fix, but overwrites the input
122 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700123 --ignore-perl-version override checking of perl version. expect
124 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700125 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700126 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700127 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700128 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700129 --color[=WHEN] Use colors 'always', 'never', or only when output
130 is a terminal ('auto'). Default is 'auto'.
Hannes Eder77f5b102009-09-21 17:04:37 -0700131 -h, --help, --version display this help and exit
132
133When FILE is - read standard input.
134EOM
135
136 exit($exitcode);
137}
138
Joe Perches3beb42e2016-05-20 17:04:14 -0700139sub uniq {
140 my %seen;
141 return grep { !$seen{$_}++ } @_;
142}
143
144sub list_types {
145 my ($exitcode) = @_;
146
147 my $count = 0;
148
149 local $/ = undef;
150
151 open(my $script, '<', abs_path($P)) or
152 die "$P: Can't read '$P' $!\n";
153
154 my $text = <$script>;
155 close($script);
156
157 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700158 # Also catch when type or level is passed through a variable
159 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700160 push (@types, $_);
161 }
162 @types = sort(uniq(@types));
163 print("#\tMessage type\n\n");
164 foreach my $type (@types) {
165 print(++$count . "\t" . $type . "\n");
166 }
167
168 exit($exitcode);
169}
170
Joe Perches000d1cc12011-07-25 17:13:25 -0700171my $conf = which_conf($configuration_file);
172if (-f $conf) {
173 my @conf_args;
174 open(my $conffile, '<', "$conf")
175 or warn "$P: Can't find a readable $configuration_file file $!\n";
176
177 while (<$conffile>) {
178 my $line = $_;
179
180 $line =~ s/\s*\n?$//g;
181 $line =~ s/^\s*//g;
182 $line =~ s/\s+/ /g;
183
184 next if ($line =~ m/^\s*#/);
185 next if ($line =~ m/^\s*$/);
186
187 my @words = split(" ", $line);
188 foreach my $word (@words) {
189 last if ($word =~ m/^#/);
190 push (@conf_args, $word);
191 }
192 }
193 close($conffile);
194 unshift(@ARGV, @conf_args) if @conf_args;
195}
196
John Brooks737c0762017-07-10 15:52:24 -0700197# Perl's Getopt::Long allows options to take optional arguments after a space.
198# Prevent --color by itself from consuming other arguments
199foreach (@ARGV) {
200 if ($_ eq "--color" || $_ eq "-color") {
201 $_ = "--color=$color";
202 }
203}
204
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700205GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700206 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700207 'tree!' => \$tree,
208 'signoff!' => \$chk_signoff,
209 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700210 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800211 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700212 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700213 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700214 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700215 'subjective!' => \$check,
216 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700217 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700218 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700219 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700220 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800221 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700222 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700223 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800224 'summary!' => \$summary,
225 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800226 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700227 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800228 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700229 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800230 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700231 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700232 'codespell!' => \$codespell,
233 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700234 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700235 'color=s' => \$color,
236 'no-color' => \$color, #keep old behaviors of -nocolor
237 'nocolor' => \$color, #keep old behaviors of -nocolor
Hannes Eder77f5b102009-09-21 17:04:37 -0700238 'h|help' => \$help,
239 'version' => \$help
240) or help(1);
241
242help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700243
Joe Perches3beb42e2016-05-20 17:04:14 -0700244list_types(0) if ($list_types);
245
Joe Perches9624b8d2014-01-23 15:54:44 -0800246$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700247$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800248
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700249my $exit = 0;
250
Joe Perches5b579802018-08-21 21:57:33 -0700251my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700252if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700253 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700254 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700255 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700256}
257
Allen Hubbe45107ff2016-08-02 14:04:47 -0700258#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700259if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700260 push(@ARGV, '-');
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700261}
262
John Brooks737c0762017-07-10 15:52:24 -0700263if ($color =~ /^[01]$/) {
264 $color = !$color;
265} elsif ($color =~ /^always$/i) {
266 $color = 1;
267} elsif ($color =~ /^never$/i) {
268 $color = 0;
269} elsif ($color =~ /^auto$/i) {
270 $color = (-t STDOUT);
271} else {
272 die "Invalid color mode: $color\n";
273}
274
Joe Perches91bfe482013-09-11 14:23:59 -0700275sub hash_save_array_words {
276 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700277
Joe Perches91bfe482013-09-11 14:23:59 -0700278 my @array = split(/,/, join(',', @$arrayRef));
279 foreach my $word (@array) {
280 $word =~ s/\s*\n?$//g;
281 $word =~ s/^\s*//g;
282 $word =~ s/\s+/ /g;
283 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700284
Joe Perches91bfe482013-09-11 14:23:59 -0700285 next if ($word =~ m/^\s*#/);
286 next if ($word =~ m/^\s*$/);
287
288 $hashRef->{$word}++;
289 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700290}
291
Joe Perches91bfe482013-09-11 14:23:59 -0700292sub hash_show_words {
293 my ($hashRef, $prefix) = @_;
294
Joe Perches3c816e42015-06-25 15:03:29 -0700295 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700296 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700297 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700298 print " $word";
299 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700300 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700301 }
302}
303
304hash_save_array_words(\%ignore_type, \@ignore);
305hash_save_array_words(\%use_type, \@use);
306
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800307my $dbg_values = 0;
308my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700309my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700310my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800311for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800312 ## no critic
313 eval "\${dbg_$key} = '$debug{$key}';";
314 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800315}
316
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700317my $rpt_cleaners = 0;
318
Andy Whitcroft8905a672007-11-28 16:21:06 -0800319if ($terse) {
320 $emacs = 1;
321 $quiet++;
322}
323
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700324if ($tree) {
325 if (defined $root) {
326 if (!top_of_kernel_tree($root)) {
327 die "$P: $root: --root does not point at a valid tree\n";
328 }
329 } else {
330 if (top_of_kernel_tree('.')) {
331 $root = '.';
332 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
333 top_of_kernel_tree($1)) {
334 $root = $1;
335 }
336 }
337
338 if (!defined $root) {
339 print "Must be run from the top-level dir. of a kernel tree\n";
340 exit(2);
341 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700342}
343
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700344my $emitted_corrupt = 0;
345
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700346our $Ident = qr{
347 [A-Za-z_][A-Za-z\d_]*
348 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
349 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700350our $Storage = qr{extern|static|asmlinkage};
351our $Sparse = qr{
352 __user|
353 __kernel|
354 __force|
355 __iomem|
356 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800357 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700358 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700359 __refconst|
360 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800361 __rcu|
362 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700363 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800364our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
365our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
366our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
367our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
368our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700369
Wolfram Sang52131292010-03-05 13:43:51 -0800370# Notes to $Attribute:
371# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700372our $Attribute = qr{
373 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700374 __percpu|
375 __nocast|
376 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200377 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700378 __packed__|
379 __packed2__|
380 __naked|
381 __maybe_unused|
382 __always_unused|
383 __noreturn|
384 __used|
385 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800386 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700387 __noclone|
388 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700389 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700390 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700391 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700392 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700393 ____cacheline_aligned|
394 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800395 ____cacheline_internodealigned_in_smp|
396 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700397 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700398our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700399our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700400our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
401our $Lval = qr{$Ident(?:$Member)*};
402
Joe Perches95e2c602013-07-03 15:05:20 -0700403our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
404our $Binary = qr{(?i)0b[01]+$Int_type?};
405our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
406our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700407our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800408our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800409our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
410our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
411our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800412our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700413our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800414our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700415our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700416our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700417our $Operators = qr{
418 <=|>=|==|!=|
419 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700420 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700421 }x;
422
Joe Perches91cb5192014-04-03 14:49:32 -0700423our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
424
Joe Perchesab7e23f2015-04-16 12:44:22 -0700425our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800426our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700427our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700428our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800429our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700430our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800431our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700432our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800433
Joe Perches15662b32011-10-31 17:13:12 -0700434our $NON_ASCII_UTF8 = qr{
435 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700436 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
437 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
438 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
439 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
440 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
441 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
442}x;
443
Joe Perches15662b32011-10-31 17:13:12 -0700444our $UTF8 = qr{
445 [\x09\x0A\x0D\x20-\x7E] # ASCII
446 | $NON_ASCII_UTF8
447}x;
448
Joe Perchese6176fa2015-06-25 15:02:49 -0700449our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800450our $typeOtherOSTypedefs = qr{(?x:
451 u_(?:char|short|int|long) | # bsd
452 u(?:nchar|short|int|long) # sysv
453)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700454our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700455 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700456 atomic_t
457)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700458our $typeTypedefs = qr{(?x:
459 $typeC99Typedefs\b|
460 $typeOtherOSTypedefs\b|
461 $typeKernelTypedefs\b
462)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700463
Joe Perches6d32f7a2015-11-06 16:31:37 -0800464our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
465
Joe Perches691e6692010-03-05 13:43:51 -0800466our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800467 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700468 (?:[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 -0800469 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700470 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700471 panic|
Joe Perches06668722013-11-12 15:10:07 -0800472 MODULE_[A-Z_]+|
473 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800474)};
475
Joe Perches20112472011-07-25 17:13:23 -0700476our $signature_tags = qr{(?xi:
477 Signed-off-by:|
Jorge Ramirez-Ortiz9a496642019-01-03 15:29:12 -0800478 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700479 Acked-by:|
480 Tested-by:|
481 Reviewed-by:|
482 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700483 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700484 To:|
485 Cc:
486)};
487
Joe Perches18130872014-08-06 16:11:22 -0700488our @typeListMisordered = (
489 qr{char\s+(?:un)?signed},
490 qr{int\s+(?:(?:un)?signed\s+)?short\s},
491 qr{int\s+short(?:\s+(?:un)?signed)},
492 qr{short\s+int(?:\s+(?:un)?signed)},
493 qr{(?:un)?signed\s+int\s+short},
494 qr{short\s+(?:un)?signed},
495 qr{long\s+int\s+(?:un)?signed},
496 qr{int\s+long\s+(?:un)?signed},
497 qr{long\s+(?:un)?signed\s+int},
498 qr{int\s+(?:un)?signed\s+long},
499 qr{int\s+(?:un)?signed},
500 qr{int\s+long\s+long\s+(?:un)?signed},
501 qr{long\s+long\s+int\s+(?:un)?signed},
502 qr{long\s+long\s+(?:un)?signed\s+int},
503 qr{long\s+long\s+(?:un)?signed},
504 qr{long\s+(?:un)?signed},
505);
506
Andy Whitcroft8905a672007-11-28 16:21:06 -0800507our @typeList = (
508 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700509 qr{(?:(?:un)?signed\s+)?char},
510 qr{(?:(?:un)?signed\s+)?short\s+int},
511 qr{(?:(?:un)?signed\s+)?short},
512 qr{(?:(?:un)?signed\s+)?int},
513 qr{(?:(?:un)?signed\s+)?long\s+int},
514 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
515 qr{(?:(?:un)?signed\s+)?long\s+long},
516 qr{(?:(?:un)?signed\s+)?long},
517 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800518 qr{float},
519 qr{double},
520 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800521 qr{struct\s+$Ident},
522 qr{union\s+$Ident},
523 qr{enum\s+$Ident},
524 qr{${Ident}_t},
525 qr{${Ident}_handler},
526 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700527 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800528);
Joe Perches938224b2016-01-20 14:59:15 -0800529
530our $C90_int_types = qr{(?x:
531 long\s+long\s+int\s+(?:un)?signed|
532 long\s+long\s+(?:un)?signed\s+int|
533 long\s+long\s+(?:un)?signed|
534 (?:(?:un)?signed\s+)?long\s+long\s+int|
535 (?:(?:un)?signed\s+)?long\s+long|
536 int\s+long\s+long\s+(?:un)?signed|
537 int\s+(?:(?:un)?signed\s+)?long\s+long|
538
539 long\s+int\s+(?:un)?signed|
540 long\s+(?:un)?signed\s+int|
541 long\s+(?:un)?signed|
542 (?:(?:un)?signed\s+)?long\s+int|
543 (?:(?:un)?signed\s+)?long|
544 int\s+long\s+(?:un)?signed|
545 int\s+(?:(?:un)?signed\s+)?long|
546
547 int\s+(?:un)?signed|
548 (?:(?:un)?signed\s+)?int
549)};
550
Alex Dowad485ff232015-06-25 15:02:52 -0700551our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700552our @typeListWithAttr = (
553 @typeList,
554 qr{struct\s+$InitAttribute\s+$Ident},
555 qr{union\s+$InitAttribute\s+$Ident},
556);
557
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700558our @modifierList = (
559 qr{fastcall},
560);
Alex Dowad485ff232015-06-25 15:02:52 -0700561our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800562
Joe Perches24358802014-04-03 14:49:13 -0700563our @mode_permission_funcs = (
564 ["module_param", 3],
565 ["module_param_(?:array|named|string)", 4],
566 ["module_param_array_named", 5],
567 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
568 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700569 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
570 ["IIO_DEV_ATTR_[A-Z_]+", 1],
571 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
572 ["SENSOR_TEMPLATE(?:_2|)", 3],
573 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700574);
575
Joe Perches515a2352014-04-03 14:49:24 -0700576#Create a search pattern for all these functions to speed up a loop below
577our $mode_perms_search = "";
578foreach my $entry (@mode_permission_funcs) {
579 $mode_perms_search .= '|' if ($mode_perms_search ne "");
580 $mode_perms_search .= $entry->[0];
581}
Joe Perches00180462018-02-06 15:38:55 -0800582$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700583
Joe Perchesb392c642015-04-16 12:44:16 -0700584our $mode_perms_world_writable = qr{
585 S_IWUGO |
586 S_IWOTH |
587 S_IRWXUGO |
588 S_IALLUGO |
589 0[0-7][0-7][2367]
590}x;
591
Joe Perchesf90774e2016-10-11 13:51:47 -0700592our %mode_permission_string_types = (
593 "S_IRWXU" => 0700,
594 "S_IRUSR" => 0400,
595 "S_IWUSR" => 0200,
596 "S_IXUSR" => 0100,
597 "S_IRWXG" => 0070,
598 "S_IRGRP" => 0040,
599 "S_IWGRP" => 0020,
600 "S_IXGRP" => 0010,
601 "S_IRWXO" => 0007,
602 "S_IROTH" => 0004,
603 "S_IWOTH" => 0002,
604 "S_IXOTH" => 0001,
605 "S_IRWXUGO" => 0777,
606 "S_IRUGO" => 0444,
607 "S_IWUGO" => 0222,
608 "S_IXUGO" => 0111,
609);
610
611#Create a search pattern for all these strings to speed up a loop below
612our $mode_perms_string_search = "";
613foreach my $entry (keys %mode_permission_string_types) {
614 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
615 $mode_perms_string_search .= $entry;
616}
Joe Perches00180462018-02-06 15:38:55 -0800617our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
618our $multi_mode_perms_string_search = qr{
619 ${single_mode_perms_string_search}
620 (?:\s*\|\s*${single_mode_perms_string_search})*
621}x;
622
623sub perms_to_octal {
624 my ($string) = @_;
625
626 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
627
628 my $val = "";
629 my $oval = "";
630 my $to = 0;
631 my $curpos = 0;
632 my $lastpos = 0;
633 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
634 $curpos = pos($string);
635 my $match = $2;
636 my $omatch = $1;
637 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
638 $lastpos = $curpos;
639 $to |= $mode_permission_string_types{$match};
640 $val .= '\s*\|\s*' if ($val ne "");
641 $val .= $match;
642 $oval .= $omatch;
643 }
644 $oval =~ s/^\s*\|\s*//;
645 $oval =~ s/\s*\|\s*$//;
646 return sprintf("%04o", $to);
647}
Joe Perchesf90774e2016-10-11 13:51:47 -0700648
Wolfram Sang7840a942010-08-09 17:20:57 -0700649our $allowed_asm_includes = qr{(?x:
650 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700651 memory|
652 time|
653 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700654)};
655# memory.h: ARM has a custom one
656
Kees Cook66b47b42014-10-13 15:51:57 -0700657# Load common spelling mistakes and build regular expression list.
658my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700659my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700660
Joe Perches36061e32014-12-10 15:51:43 -0800661if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800662 while (<$spelling>) {
663 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700664
Joe Perches36061e32014-12-10 15:51:43 -0800665 $line =~ s/\s*\n?$//g;
666 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700667
Joe Perches36061e32014-12-10 15:51:43 -0800668 next if ($line =~ m/^\s*#/);
669 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700670
Joe Perches36061e32014-12-10 15:51:43 -0800671 my ($suspect, $fix) = split(/\|\|/, $line);
672
Joe Perches36061e32014-12-10 15:51:43 -0800673 $spelling_fix{$suspect} = $fix;
674 }
675 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800676} else {
677 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700678}
Kees Cook66b47b42014-10-13 15:51:57 -0700679
Joe Perchesebfd7d62015-04-16 12:44:14 -0700680if ($codespell) {
681 if (open(my $spelling, '<', $codespellfile)) {
682 while (<$spelling>) {
683 my $line = $_;
684
685 $line =~ s/\s*\n?$//g;
686 $line =~ s/^\s*//g;
687
688 next if ($line =~ m/^\s*#/);
689 next if ($line =~ m/^\s*$/);
690 next if ($line =~ m/, disabled/i);
691
692 $line =~ s/,.*$//;
693
694 my ($suspect, $fix) = split(/->/, $line);
695
696 $spelling_fix{$suspect} = $fix;
697 }
698 close($spelling);
699 } else {
700 warn "No codespell typos will be found - file '$codespellfile': $!\n";
701 }
702}
703
704$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
705
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700706sub read_words {
707 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700708
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700709 if (open(my $words, '<', $file)) {
710 while (<$words>) {
711 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700712
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700713 $line =~ s/\s*\n?$//g;
714 $line =~ s/^\s*//g;
715
716 next if ($line =~ m/^\s*#/);
717 next if ($line =~ m/^\s*$/);
718 if ($line =~ /\s/) {
719 print("$file: '$line' invalid - ignored\n");
720 next;
721 }
722
723 $$wordsRef .= '|' if ($$wordsRef ne "");
724 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700725 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700726 close($file);
727 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700728 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700729
730 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700731}
732
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700733my $const_structs = "";
734read_words(\$const_structs, $conststructsfile)
735 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
736
737my $typeOtherTypedefs = "";
738if (length($typedefsfile)) {
739 read_words(\$typeOtherTypedefs, $typedefsfile)
740 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
741}
742$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
743
Andy Whitcroft8905a672007-11-28 16:21:06 -0800744sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700745 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
746 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700747 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700748 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700749 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700750 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700751 (?:$typeTypedefs\b)|
752 (?:${all}\b)
753 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800754 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700755 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800756 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800757 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700758 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700759 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800760 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700761 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800762 }x;
Joe Perches18130872014-08-06 16:11:22 -0700763 $NonptrTypeMisordered = qr{
764 (?:$Modifier\s+|const\s+)*
765 (?:
766 (?:${Misordered}\b)
767 )
768 (?:\s+$Modifier|\s+const)*
769 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700770 $NonptrTypeWithAttr = qr{
771 (?:$Modifier\s+|const\s+)*
772 (?:
773 (?:typeof|__typeof__)\s*\([^\)]*\)|
774 (?:$typeTypedefs\b)|
775 (?:${allWithAttr}\b)
776 )
777 (?:\s+$Modifier|\s+const)*
778 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800779 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700780 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700781 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700782 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800783 }x;
Joe Perches18130872014-08-06 16:11:22 -0700784 $TypeMisordered = qr{
785 $NonptrTypeMisordered
786 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
787 (?:\s+$Inline|\s+$Modifier)*
788 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700789 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700790 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800791}
792build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700793
Joe Perches7d2367a2011-07-25 17:13:22 -0700794our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700795
796# Using $balanced_parens, $LvalOrFunc, or $FuncArg
797# requires at least perl version v5.10.0
798# Any use must be runtime checked with $^V
799
800our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700801our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800802our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700803
Joe Perchesf8422302014-08-06 16:11:31 -0700804our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700805 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700806 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700807 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
808 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700809)};
810
Joe Perches7d2367a2011-07-25 17:13:22 -0700811sub deparenthesize {
812 my ($string) = @_;
813 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700814
815 while ($string =~ /^\s*\(.*\)\s*$/) {
816 $string =~ s@^\s*\(\s*@@;
817 $string =~ s@\s*\)\s*$@@;
818 }
819
Joe Perches7d2367a2011-07-25 17:13:22 -0700820 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700821
Joe Perches7d2367a2011-07-25 17:13:22 -0700822 return $string;
823}
824
Joe Perches34456862013-07-03 15:05:34 -0700825sub seed_camelcase_file {
826 my ($file) = @_;
827
828 return if (!(-f $file));
829
830 local $/;
831
832 open(my $include_file, '<', "$file")
833 or warn "$P: Can't read '$file' $!\n";
834 my $text = <$include_file>;
835 close($include_file);
836
837 my @lines = split('\n', $text);
838
839 foreach my $line (@lines) {
840 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
841 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
842 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800843 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
844 $camelcase{$1} = 1;
845 } 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 -0700846 $camelcase{$1} = 1;
847 }
848 }
849}
850
Joe Perches85b0ee12016-10-11 13:51:44 -0700851sub is_maintained_obsolete {
852 my ($filename) = @_;
853
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800854 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700855
Joe Perches0616efa2016-10-11 13:52:02 -0700856 my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
Joe Perches85b0ee12016-10-11 13:51:44 -0700857
858 return $status =~ /obsolete/i;
859}
860
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700861sub is_SPDX_License_valid {
862 my ($license) = @_;
863
Joe Perches56294112018-08-21 21:58:04 -0700864 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700865
Joe Perches56294112018-08-21 21:58:04 -0700866 my $root_path = abs_path($root);
867 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700868 return 0 if ($status ne "");
869 return 1;
870}
871
Joe Perches34456862013-07-03 15:05:34 -0700872my $camelcase_seeded = 0;
873sub seed_camelcase_includes {
874 return if ($camelcase_seeded);
875
876 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700877 my $camelcase_cache = "";
878 my @include_files = ();
879
880 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700881
Richard Genoud3645e322014-02-10 14:25:32 -0800882 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700883 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
884 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700885 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700886 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700887 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700888 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700889 @include_files = split('\n', $files);
890 foreach my $file (@include_files) {
891 my $date = POSIX::strftime("%Y%m%d%H%M",
892 localtime((stat $file)[9]));
893 $last_mod_date = $date if ($last_mod_date < $date);
894 }
895 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700896 }
Joe Perchesc707a812013-07-08 16:00:43 -0700897
898 if ($camelcase_cache ne "" && -f $camelcase_cache) {
899 open(my $camelcase_file, '<', "$camelcase_cache")
900 or warn "$P: Can't read '$camelcase_cache' $!\n";
901 while (<$camelcase_file>) {
902 chomp;
903 $camelcase{$_} = 1;
904 }
905 close($camelcase_file);
906
907 return;
908 }
909
Richard Genoud3645e322014-02-10 14:25:32 -0800910 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700911 $files = `git ls-files "include/*.h"`;
912 @include_files = split('\n', $files);
913 }
914
Joe Perches34456862013-07-03 15:05:34 -0700915 foreach my $file (@include_files) {
916 seed_camelcase_file($file);
917 }
Joe Perches351b2a12013-07-03 15:05:36 -0700918
Joe Perchesc707a812013-07-08 16:00:43 -0700919 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700920 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700921 open(my $camelcase_file, '>', "$camelcase_cache")
922 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700923 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
924 print $camelcase_file ("$_\n");
925 }
926 close($camelcase_file);
927 }
Joe Perches34456862013-07-03 15:05:34 -0700928}
929
Joe Perchesd311cd42014-08-06 16:10:57 -0700930sub git_commit_info {
931 my ($commit, $id, $desc) = @_;
932
933 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
934
935 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
936 $output =~ s/^\s*//gm;
937 my @lines = split("\n", $output);
938
Joe Perches0d7835f2015-02-13 14:38:35 -0800939 return ($id, $desc) if ($#lines < 0);
940
Joe Perchesd311cd42014-08-06 16:10:57 -0700941 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
942# Maybe one day convert this block of bash into something that returns
943# all matching commit ids, but it's very slow...
944#
945# echo "checking commits $1..."
946# git rev-list --remotes | grep -i "^$1" |
947# while read line ; do
948# git log --format='%H %s' -1 $line |
949# echo "commit $(cut -c 1-12,41-)"
950# done
951 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700952 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700953 } else {
954 $id = substr($lines[0], 0, 12);
955 $desc = substr($lines[0], 41);
956 }
957
958 return ($id, $desc);
959}
960
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700961$chk_signoff = 0 if ($file);
962
Andy Whitcroft00df3442007-06-08 13:47:06 -0700963my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800964my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700965my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700966my @fixed_inserted = ();
967my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700968my $fixlinenr = -1;
969
Du, Changbin4a593c32016-05-20 17:04:16 -0700970# If input is git commits, extract all commits from the commit expressions.
971# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
972die "$P: No git repository found\n" if ($git && !-e ".git");
973
974if ($git) {
975 my @commits = ();
Joe Perches0dea9f12016-05-20 17:04:19 -0700976 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700977 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700978 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
979 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700980 } elsif ($commit_expr =~ m/\.\./) {
981 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700982 } else {
Joe Perches0dea9f12016-05-20 17:04:19 -0700983 $git_range = "-1 $commit_expr";
984 }
985 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
986 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700987 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
988 next if (!defined($1) || !defined($2));
Joe Perches0dea9f12016-05-20 17:04:19 -0700989 my $sha1 = $1;
990 my $subject = $2;
991 unshift(@commits, $sha1);
992 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700993 }
994 }
995 die "$P: no git commits after extraction!\n" if (@commits == 0);
996 @ARGV = @commits;
997}
998
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800999my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001000for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001001 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -07001002 if ($git) {
1003 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1004 die "$P: $filename: git format-patch failed - $!\n";
1005 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001006 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001007 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001008 } elsif ($filename eq '-') {
1009 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001010 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001011 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001012 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001013 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001014 if ($filename eq '-') {
1015 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001016 } elsif ($git) {
Joe Perches0dea9f12016-05-20 17:04:19 -07001017 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001018 } else {
1019 $vname = $filename;
1020 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001021 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001022 chomp;
1023 push(@rawlines, $_);
1024 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001025 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001026
1027 if ($#ARGV > 0 && $quiet == 0) {
1028 print '-' x length($vname) . "\n";
1029 print "$vname\n";
1030 print '-' x length($vname) . "\n";
1031 }
1032
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001033 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001034 $exit = 1;
1035 }
1036 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001037 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001038 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001039 @fixed_inserted = ();
1040 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001041 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001042 @modifierListFile = ();
1043 @typeListFile = ();
1044 build_types();
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001045}
1046
Joe Perchesd8469f12015-06-25 15:03:00 -07001047if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001048 hash_show_words(\%use_type, "Used");
1049 hash_show_words(\%ignore_type, "Ignored");
1050
Joe Perches5b579802018-08-21 21:57:33 -07001051 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001052 print << "EOM"
1053
1054NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001055 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001056EOM
1057 }
1058 if ($exit) {
1059 print << "EOM"
1060
1061NOTE: If any of the errors are false positives, please report
1062 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1063EOM
1064 }
1065}
1066
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001067exit($exit);
1068
1069sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001070 my ($root) = @_;
1071
1072 my @tree_check = (
1073 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1074 "README", "Documentation", "arch", "include", "drivers",
1075 "fs", "init", "ipc", "kernel", "lib", "scripts",
1076 );
1077
1078 foreach my $check (@tree_check) {
1079 if (! -e $root . '/' . $check) {
1080 return 0;
1081 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001082 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001083 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001084}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001085
Joe Perches20112472011-07-25 17:13:23 -07001086sub parse_email {
1087 my ($formatted_email) = @_;
1088
1089 my $name = "";
1090 my $address = "";
1091 my $comment = "";
1092
1093 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1094 $name = $1;
1095 $address = $2;
1096 $comment = $3 if defined $3;
1097 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1098 $address = $1;
1099 $comment = $2 if defined $2;
1100 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1101 $address = $1;
1102 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001103 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001104 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001105 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001106 $name =~ s/^\"|\"$//g;
1107 # If there's a name left after stripping spaces and
1108 # leading quotes, and the address doesn't have both
1109 # leading and trailing angle brackets, the address
1110 # is invalid. ie:
1111 # "joe smith joe@smith.com" bad
1112 # "joe smith <joe@smith.com" bad
1113 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1114 $name = "";
1115 $address = "";
1116 $comment = "";
1117 }
1118 }
1119
Joe Perches3705ce52013-07-03 15:05:31 -07001120 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001121 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001122 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001123 $address =~ s/^\<|\>$//g;
1124
1125 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1126 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1127 $name = "\"$name\"";
1128 }
1129
1130 return ($name, $address, $comment);
1131}
1132
1133sub format_email {
1134 my ($name, $address) = @_;
1135
1136 my $formatted_email;
1137
Joe Perches3705ce52013-07-03 15:05:31 -07001138 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001139 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001140 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001141
1142 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1143 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1144 $name = "\"$name\"";
1145 }
1146
1147 if ("$name" eq "") {
1148 $formatted_email = "$address";
1149 } else {
1150 $formatted_email = "$name <$address>";
1151 }
1152
1153 return $formatted_email;
1154}
1155
Joe Perchesd311cd42014-08-06 16:10:57 -07001156sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001157 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001158
Joe Perchesbd474ca2014-08-06 16:11:10 -07001159 foreach my $path (split(/:/, $ENV{PATH})) {
1160 if (-e "$path/$bin") {
1161 return "$path/$bin";
1162 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001163 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001164
Joe Perchesbd474ca2014-08-06 16:11:10 -07001165 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001166}
1167
Joe Perches000d1cc12011-07-25 17:13:25 -07001168sub which_conf {
1169 my ($conf) = @_;
1170
1171 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1172 if (-e "$path/$conf") {
1173 return "$path/$conf";
1174 }
1175 }
1176
1177 return "";
1178}
1179
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001180sub expand_tabs {
1181 my ($str) = @_;
1182
1183 my $res = '';
1184 my $n = 0;
1185 for my $c (split(//, $str)) {
1186 if ($c eq "\t") {
1187 $res .= ' ';
1188 $n++;
1189 for (; ($n % 8) != 0; $n++) {
1190 $res .= ' ';
1191 }
1192 next;
1193 }
1194 $res .= $c;
1195 $n++;
1196 }
1197
1198 return $res;
1199}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001200sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001201 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001202 return $res;
1203}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001204
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001205sub line_stats {
1206 my ($line) = @_;
1207
1208 # Drop the diff line leader and expand tabs
1209 $line =~ s/^.//;
1210 $line = expand_tabs($line);
1211
1212 # Pick the indent from the front of the line.
1213 my ($white) = ($line =~ /^(\s*)/);
1214
1215 return (length($line), length($white));
1216}
1217
Andy Whitcroft773647a2008-03-28 14:15:58 -07001218my $sanitise_quote = '';
1219
1220sub sanitise_line_reset {
1221 my ($in_comment) = @_;
1222
1223 if ($in_comment) {
1224 $sanitise_quote = '*/';
1225 } else {
1226 $sanitise_quote = '';
1227 }
1228}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001229sub sanitise_line {
1230 my ($line) = @_;
1231
1232 my $res = '';
1233 my $l = '';
1234
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001235 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001236 my $off = 0;
1237 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001238
Andy Whitcroft773647a2008-03-28 14:15:58 -07001239 # Always copy over the diff marker.
1240 $res = substr($line, 0, 1);
1241
1242 for ($off = 1; $off < length($line); $off++) {
1243 $c = substr($line, $off, 1);
1244
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001245 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001246 # and end, all to $;.
1247 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1248 $sanitise_quote = '*/';
1249
1250 substr($res, $off, 2, "$;$;");
1251 $off++;
1252 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001253 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001254 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001255 $sanitise_quote = '';
1256 substr($res, $off, 2, "$;$;");
1257 $off++;
1258 next;
1259 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001260 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1261 $sanitise_quote = '//';
1262
1263 substr($res, $off, 2, $sanitise_quote);
1264 $off++;
1265 next;
1266 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001267
1268 # A \ in a string means ignore the next character.
1269 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1270 $c eq "\\") {
1271 substr($res, $off, 2, 'XX');
1272 $off++;
1273 next;
1274 }
1275 # Regular quotes.
1276 if ($c eq "'" || $c eq '"') {
1277 if ($sanitise_quote eq '') {
1278 $sanitise_quote = $c;
1279
1280 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001281 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001282 } elsif ($sanitise_quote eq $c) {
1283 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001284 }
1285 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001286
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001287 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001288 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1289 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001290 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1291 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001292 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1293 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001294 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001295 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001296 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001297 }
1298
Daniel Walker113f04a2009-09-21 17:04:35 -07001299 if ($sanitise_quote eq '//') {
1300 $sanitise_quote = '';
1301 }
1302
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001303 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001304 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001305 my $clean = 'X' x length($1);
1306 $res =~ s@\<.*\>@<$clean>@;
1307
1308 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001309 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001310 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001311 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001312 }
1313
Joe Perchesdadf6802016-08-02 14:04:33 -07001314 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1315 my $match = $1;
1316 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1317 }
1318
Andy Whitcroft00df3442007-06-08 13:47:06 -07001319 return $res;
1320}
1321
Joe Perchesa6962d72013-04-29 16:18:13 -07001322sub get_quoted_string {
1323 my ($line, $rawline) = @_;
1324
Joe Perches478b1792018-04-10 16:33:34 -07001325 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001326 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001327 return substr($rawline, $-[0], $+[0] - $-[0]);
1328}
1329
Andy Whitcroft8905a672007-11-28 16:21:06 -08001330sub ctx_statement_block {
1331 my ($linenr, $remain, $off) = @_;
1332 my $line = $linenr - 1;
1333 my $blk = '';
1334 my $soff = $off;
1335 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001336 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001337
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001338 my $loff = 0;
1339
Andy Whitcroft8905a672007-11-28 16:21:06 -08001340 my $type = '';
1341 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001342 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001343 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001344 my $c;
1345 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001346
1347 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001348 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001349 @stack = (['', 0]) if ($#stack == -1);
1350
Andy Whitcroft773647a2008-03-28 14:15:58 -07001351 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001352 # If we are about to drop off the end, pull in more
1353 # context.
1354 if ($off >= $len) {
1355 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001356 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001357 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001358 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001359 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001360 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001361 $len = length($blk);
1362 $line++;
1363 last;
1364 }
1365 # Bail if there is no further context.
1366 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001367 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001368 last;
1369 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001370 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1371 $level++;
1372 $type = '#';
1373 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001374 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001375 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001376 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001377 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001378
Andy Whitcroft773647a2008-03-28 14:15:58 -07001379 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001380
1381 # Handle nested #if/#else.
1382 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1383 push(@stack, [ $type, $level ]);
1384 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1385 ($type, $level) = @{$stack[$#stack - 1]};
1386 } elsif ($remainder =~ /^#\s*endif\b/) {
1387 ($type, $level) = @{pop(@stack)};
1388 }
1389
Andy Whitcroft8905a672007-11-28 16:21:06 -08001390 # Statement ends at the ';' or a close '}' at the
1391 # outermost level.
1392 if ($level == 0 && $c eq ';') {
1393 last;
1394 }
1395
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001396 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001397 if ($level == 0 && $coff_set == 0 &&
1398 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1399 $remainder =~ /^(else)(?:\s|{)/ &&
1400 $remainder !~ /^else\s+if\b/) {
1401 $coff = $off + length($1) - 1;
1402 $coff_set = 1;
1403 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1404 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001405 }
1406
Andy Whitcroft8905a672007-11-28 16:21:06 -08001407 if (($type eq '' || $type eq '(') && $c eq '(') {
1408 $level++;
1409 $type = '(';
1410 }
1411 if ($type eq '(' && $c eq ')') {
1412 $level--;
1413 $type = ($level != 0)? '(' : '';
1414
1415 if ($level == 0 && $coff < $soff) {
1416 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001417 $coff_set = 1;
1418 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001419 }
1420 }
1421 if (($type eq '' || $type eq '{') && $c eq '{') {
1422 $level++;
1423 $type = '{';
1424 }
1425 if ($type eq '{' && $c eq '}') {
1426 $level--;
1427 $type = ($level != 0)? '{' : '';
1428
1429 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001430 if (substr($blk, $off + 1, 1) eq ';') {
1431 $off++;
1432 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001433 last;
1434 }
1435 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001436 # Preprocessor commands end at the newline unless escaped.
1437 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1438 $level--;
1439 $type = '';
1440 $off++;
1441 last;
1442 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001443 $off++;
1444 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001445 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001446 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001447 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001448 $line++;
1449 $remain--;
1450 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001451
1452 my $statement = substr($blk, $soff, $off - $soff + 1);
1453 my $condition = substr($blk, $soff, $coff - $soff + 1);
1454
1455 #warn "STATEMENT<$statement>\n";
1456 #warn "CONDITION<$condition>\n";
1457
Andy Whitcroft773647a2008-03-28 14:15:58 -07001458 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001459
1460 return ($statement, $condition,
1461 $line, $remain + 1, $off - $loff + 1, $level);
1462}
1463
Andy Whitcroftcf655042008-03-04 14:28:20 -08001464sub statement_lines {
1465 my ($stmt) = @_;
1466
1467 # Strip the diff line prefixes and rip blank lines at start and end.
1468 $stmt =~ s/(^|\n)./$1/g;
1469 $stmt =~ s/^\s*//;
1470 $stmt =~ s/\s*$//;
1471
1472 my @stmt_lines = ($stmt =~ /\n/g);
1473
1474 return $#stmt_lines + 2;
1475}
1476
1477sub statement_rawlines {
1478 my ($stmt) = @_;
1479
1480 my @stmt_lines = ($stmt =~ /\n/g);
1481
1482 return $#stmt_lines + 2;
1483}
1484
1485sub statement_block_size {
1486 my ($stmt) = @_;
1487
1488 $stmt =~ s/(^|\n)./$1/g;
1489 $stmt =~ s/^\s*{//;
1490 $stmt =~ s/}\s*$//;
1491 $stmt =~ s/^\s*//;
1492 $stmt =~ s/\s*$//;
1493
1494 my @stmt_lines = ($stmt =~ /\n/g);
1495 my @stmt_statements = ($stmt =~ /;/g);
1496
1497 my $stmt_lines = $#stmt_lines + 2;
1498 my $stmt_statements = $#stmt_statements + 1;
1499
1500 if ($stmt_lines > $stmt_statements) {
1501 return $stmt_lines;
1502 } else {
1503 return $stmt_statements;
1504 }
1505}
1506
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001507sub ctx_statement_full {
1508 my ($linenr, $remain, $off) = @_;
1509 my ($statement, $condition, $level);
1510
1511 my (@chunks);
1512
Andy Whitcroftcf655042008-03-04 14:28:20 -08001513 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001514 ($statement, $condition, $linenr, $remain, $off, $level) =
1515 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001516 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001517 push(@chunks, [ $condition, $statement ]);
1518 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1519 return ($level, $linenr, @chunks);
1520 }
1521
1522 # Pull in the following conditional/block pairs and see if they
1523 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001524 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001525 ($statement, $condition, $linenr, $remain, $off, $level) =
1526 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001527 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001528 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001529 #print "C: push\n";
1530 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001531 }
1532
1533 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001534}
1535
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001536sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001537 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001538 my $line;
1539 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001540 my $blk = '';
1541 my @o;
1542 my @c;
1543 my @res = ();
1544
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001545 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001546 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001547 for ($line = $start; $remain > 0; $line++) {
1548 next if ($rawlines[$line] =~ /^-/);
1549 $remain--;
1550
1551 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001552
1553 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001554 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001555 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001556 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001557 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001558 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001559 $level = pop(@stack);
1560 }
1561
Andy Whitcroft01464f32010-10-26 14:23:19 -07001562 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001563 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1564 if ($off > 0) {
1565 $off--;
1566 next;
1567 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001568
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001569 if ($c eq $close && $level > 0) {
1570 $level--;
1571 last if ($level == 0);
1572 } elsif ($c eq $open) {
1573 $level++;
1574 }
1575 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001576
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001577 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001578 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001579 }
1580
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001581 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001582 }
1583
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001584 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001585}
1586sub ctx_block_outer {
1587 my ($linenr, $remain) = @_;
1588
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001589 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1590 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001591}
1592sub ctx_block {
1593 my ($linenr, $remain) = @_;
1594
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001595 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1596 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001597}
1598sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001599 my ($linenr, $remain, $off) = @_;
1600
1601 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1602 return @r;
1603}
1604sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001605 my ($linenr, $remain) = @_;
1606
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001607 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001608}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001609sub ctx_statement_level {
1610 my ($linenr, $remain, $off) = @_;
1611
1612 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1613}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001614
1615sub ctx_locate_comment {
1616 my ($first_line, $end_line) = @_;
1617
1618 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001619 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001620 return $current_comment if (defined $current_comment);
1621
1622 # Look through the context and try and figure out if there is a
1623 # comment.
1624 my $in_comment = 0;
1625 $current_comment = '';
1626 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001627 my $line = $rawlines[$linenr - 1];
1628 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001629 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1630 $in_comment = 1;
1631 }
1632 if ($line =~ m@/\*@) {
1633 $in_comment = 1;
1634 }
1635 if (!$in_comment && $current_comment ne '') {
1636 $current_comment = '';
1637 }
1638 $current_comment .= $line . "\n" if ($in_comment);
1639 if ($line =~ m@\*/@) {
1640 $in_comment = 0;
1641 }
1642 }
1643
1644 chomp($current_comment);
1645 return($current_comment);
1646}
1647sub ctx_has_comment {
1648 my ($first_line, $end_line) = @_;
1649 my $cmt = ctx_locate_comment($first_line, $end_line);
1650
Andy Whitcroft00df3442007-06-08 13:47:06 -07001651 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001652 ##print "CMMT: $cmt\n";
1653
1654 return ($cmt ne '');
1655}
1656
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001657sub raw_line {
1658 my ($linenr, $cnt) = @_;
1659
1660 my $offset = $linenr - 1;
1661 $cnt++;
1662
1663 my $line;
1664 while ($cnt) {
1665 $line = $rawlines[$offset++];
1666 next if (defined($line) && $line =~ /^-/);
1667 $cnt--;
1668 }
1669
1670 return $line;
1671}
1672
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001673sub get_stat_real {
1674 my ($linenr, $lc) = @_;
1675
1676 my $stat_real = raw_line($linenr, 0);
1677 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1678 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1679 }
1680
1681 return $stat_real;
1682}
1683
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001684sub get_stat_here {
1685 my ($linenr, $cnt, $here) = @_;
1686
1687 my $herectx = $here . "\n";
1688 for (my $n = 0; $n < $cnt; $n++) {
1689 $herectx .= raw_line($linenr, $n) . "\n";
1690 }
1691
1692 return $herectx;
1693}
1694
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001695sub cat_vet {
1696 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001697 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001698
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001699 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001700 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1701 $res .= $1;
1702 if ($2 ne '') {
1703 $coded = sprintf("^%c", unpack('C', $2) + 64);
1704 $res .= $coded;
1705 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001706 }
1707 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001708
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001709 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001710}
1711
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001712my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001713my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001714my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001715my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001716
1717sub annotate_reset {
1718 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001719 $av_pending = '_';
1720 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001721 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001722}
1723
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001724sub annotate_values {
1725 my ($stream, $type) = @_;
1726
1727 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001728 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001729 my $cur = $stream;
1730
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001731 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001732
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001733 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001734 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001735 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001736 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001737 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001738 print "WS($1)\n" if ($dbg_values > 1);
1739 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001740 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001741 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001742 }
1743
Florian Micklerc023e4732011-01-12 16:59:58 -08001744 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001745 print "CAST($1)\n" if ($dbg_values > 1);
1746 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001747 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001748
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001749 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001750 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001751 $type = 'T';
1752
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001753 } elsif ($cur =~ /^($Modifier)\s*/) {
1754 print "MODIFIER($1)\n" if ($dbg_values > 1);
1755 $type = 'T';
1756
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001757 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001758 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001759 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001760 push(@av_paren_type, $type);
1761 if ($2 ne '') {
1762 $av_pending = 'N';
1763 }
1764 $type = 'E';
1765
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001766 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001767 print "UNDEF($1)\n" if ($dbg_values > 1);
1768 $av_preprocessor = 1;
1769 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001770
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001771 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001772 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001773 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001774
1775 push(@av_paren_type, $type);
1776 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001777 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001778
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001779 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001780 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1781 $av_preprocessor = 1;
1782
1783 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1784
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001785 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001786
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001787 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001788 print "PRE_END($1)\n" if ($dbg_values > 1);
1789
1790 $av_preprocessor = 1;
1791
1792 # Assume all arms of the conditional end as this
1793 # one does, and continue as if the #endif was not here.
1794 pop(@av_paren_type);
1795 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001796 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001797
1798 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001799 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001800
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001801 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1802 print "ATTR($1)\n" if ($dbg_values > 1);
1803 $av_pending = $type;
1804 $type = 'N';
1805
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001806 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001807 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001808 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001809 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001810 }
1811 $type = 'N';
1812
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001813 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001814 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001815 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001816 $type = 'N';
1817
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001818 } elsif ($cur =~/^(case)/o) {
1819 print "CASE($1)\n" if ($dbg_values > 1);
1820 $av_pend_colon = 'C';
1821 $type = 'N';
1822
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001823 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001824 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001825 $type = 'N';
1826
1827 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001828 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001829 push(@av_paren_type, $av_pending);
1830 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001831 $type = 'N';
1832
1833 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001834 my $new_type = pop(@av_paren_type);
1835 if ($new_type ne '_') {
1836 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001837 print "PAREN('$1') -> $type\n"
1838 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001839 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001840 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001841 }
1842
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001843 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001844 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001845 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001846 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001847
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001848 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1849 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001850 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001851 } elsif ($type eq 'E') {
1852 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001853 }
1854 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1855 $type = 'V';
1856
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001857 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001858 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001859 $type = 'V';
1860
1861 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001862 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001863 $type = 'N';
1864
Andy Whitcroftcf655042008-03-04 14:28:20 -08001865 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001866 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001867 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001868 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001869
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001870 } elsif ($cur =~/^(,)/) {
1871 print "COMMA($1)\n" if ($dbg_values > 1);
1872 $type = 'C';
1873
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001874 } elsif ($cur =~ /^(\?)/o) {
1875 print "QUESTION($1)\n" if ($dbg_values > 1);
1876 $type = 'N';
1877
1878 } elsif ($cur =~ /^(:)/o) {
1879 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1880
1881 substr($var, length($res), 1, $av_pend_colon);
1882 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1883 $type = 'E';
1884 } else {
1885 $type = 'N';
1886 }
1887 $av_pend_colon = 'O';
1888
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001889 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001890 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001891 $type = 'N';
1892
Andy Whitcroft0d413862008-10-15 22:02:16 -07001893 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001894 my $variant;
1895
1896 print "OPV($1)\n" if ($dbg_values > 1);
1897 if ($type eq 'V') {
1898 $variant = 'B';
1899 } else {
1900 $variant = 'U';
1901 }
1902
1903 substr($var, length($res), 1, $variant);
1904 $type = 'N';
1905
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001906 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001907 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001908 if ($1 ne '++' && $1 ne '--') {
1909 $type = 'N';
1910 }
1911
1912 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001913 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001914 }
1915 if (defined $1) {
1916 $cur = substr($cur, length($1));
1917 $res .= $type x length($1);
1918 }
1919 }
1920
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001921 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001922}
1923
Andy Whitcroft8905a672007-11-28 16:21:06 -08001924sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001925 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001926 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001927 ^(?:
1928 $Modifier|
1929 $Storage|
1930 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001931 DEFINE_\S+
1932 )$|
1933 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001934 goto|
1935 return|
1936 case|
1937 else|
1938 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001939 do|
1940 \#|
1941 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001942 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001943 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001944 )}x;
1945 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1946 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001947 # Check for modifiers.
1948 $possible =~ s/\s*$Storage\s*//g;
1949 $possible =~ s/\s*$Sparse\s*//g;
1950 if ($possible =~ /^\s*$/) {
1951
1952 } elsif ($possible =~ /\s/) {
1953 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001954 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001955 if ($modifier !~ $notPermitted) {
1956 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001957 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001958 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001959 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001960
1961 } else {
1962 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001963 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001964 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001965 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001966 } else {
1967 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001968 }
1969}
1970
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001971my $prefix = '';
1972
Joe Perches000d1cc12011-07-25 17:13:25 -07001973sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001974 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001975
Alexey Dobriyan522b8372017-02-27 14:30:05 -08001976 $type =~ tr/[a-z]/[A-Z]/;
1977
Joe Perchescbec18a2014-04-03 14:49:19 -07001978 return defined $use_type{$type} if (scalar keys %use_type > 0);
1979
1980 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001981}
1982
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001983sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001984 my ($level, $type, $msg) = @_;
1985
1986 if (!show_type($type) ||
1987 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001988 return 0;
1989 }
Joe Perches57230292015-06-25 15:03:03 -07001990 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07001991 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07001992 if ($level eq 'ERROR') {
1993 $output .= RED;
1994 } elsif ($level eq 'WARNING') {
1995 $output .= YELLOW;
1996 } else {
1997 $output .= GREEN;
1998 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001999 }
Joe Perches57230292015-06-25 15:03:03 -07002000 $output .= $prefix . $level . ':';
2001 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002002 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002003 $output .= "$type:";
2004 }
John Brooks737c0762017-07-10 15:52:24 -07002005 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002006 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002007
2008 if ($showfile) {
2009 my @lines = split("\n", $output, -1);
2010 splice(@lines, 1, 1);
2011 $output = join("\n", @lines);
2012 }
Joe Perches57230292015-06-25 15:03:03 -07002013 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002014
Joe Perches57230292015-06-25 15:03:03 -07002015 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002016
2017 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002018}
Joe Perchescbec18a2014-04-03 14:49:19 -07002019
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002020sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002021 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002022}
Joe Perches000d1cc12011-07-25 17:13:25 -07002023
Joe Perchesd752fcc2014-08-06 16:11:05 -07002024sub fixup_current_range {
2025 my ($lineRef, $offset, $length) = @_;
2026
2027 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2028 my $o = $1;
2029 my $l = $2;
2030 my $no = $o + $offset;
2031 my $nl = $l + $length;
2032 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2033 }
2034}
2035
2036sub fix_inserted_deleted_lines {
2037 my ($linesRef, $insertedRef, $deletedRef) = @_;
2038
2039 my $range_last_linenr = 0;
2040 my $delta_offset = 0;
2041
2042 my $old_linenr = 0;
2043 my $new_linenr = 0;
2044
2045 my $next_insert = 0;
2046 my $next_delete = 0;
2047
2048 my @lines = ();
2049
2050 my $inserted = @{$insertedRef}[$next_insert++];
2051 my $deleted = @{$deletedRef}[$next_delete++];
2052
2053 foreach my $old_line (@{$linesRef}) {
2054 my $save_line = 1;
2055 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002056 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002057 $delta_offset = 0;
2058 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2059 $range_last_linenr = $new_linenr;
2060 fixup_current_range(\$line, $delta_offset, 0);
2061 }
2062
2063 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2064 $deleted = @{$deletedRef}[$next_delete++];
2065 $save_line = 0;
2066 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2067 }
2068
2069 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2070 push(@lines, ${$inserted}{'LINE'});
2071 $inserted = @{$insertedRef}[$next_insert++];
2072 $new_linenr++;
2073 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2074 }
2075
2076 if ($save_line) {
2077 push(@lines, $line);
2078 $new_linenr++;
2079 }
2080
2081 $old_linenr++;
2082 }
2083
2084 return @lines;
2085}
2086
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002087sub fix_insert_line {
2088 my ($linenr, $line) = @_;
2089
2090 my $inserted = {
2091 LINENR => $linenr,
2092 LINE => $line,
2093 };
2094 push(@fixed_inserted, $inserted);
2095}
2096
2097sub fix_delete_line {
2098 my ($linenr, $line) = @_;
2099
2100 my $deleted = {
2101 LINENR => $linenr,
2102 LINE => $line,
2103 };
2104
2105 push(@fixed_deleted, $deleted);
2106}
2107
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002108sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002109 my ($type, $msg) = @_;
2110
2111 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002112 our $clean = 0;
2113 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002114 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002115 }
Joe Perches3705ce52013-07-03 15:05:31 -07002116 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002117}
2118sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002119 my ($type, $msg) = @_;
2120
2121 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002122 our $clean = 0;
2123 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002124 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002125 }
Joe Perches3705ce52013-07-03 15:05:31 -07002126 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002127}
2128sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002129 my ($type, $msg) = @_;
2130
2131 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002132 our $clean = 0;
2133 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002134 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002135 }
Joe Perches3705ce52013-07-03 15:05:31 -07002136 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002137}
2138
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002139sub check_absolute_file {
2140 my ($absolute, $herecurr) = @_;
2141 my $file = $absolute;
2142
2143 ##print "absolute<$absolute>\n";
2144
2145 # See if any suffix of this path is a path within the tree.
2146 while ($file =~ s@^[^/]*/@@) {
2147 if (-f "$root/$file") {
2148 ##print "file<$file>\n";
2149 last;
2150 }
2151 }
2152 if (! -f _) {
2153 return 0;
2154 }
2155
2156 # It is, so see if the prefix is acceptable.
2157 my $prefix = $absolute;
2158 substr($prefix, -length($file)) = '';
2159
2160 ##print "prefix<$prefix>\n";
2161 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002162 WARN("USE_RELATIVE_PATH",
2163 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002164 }
2165}
2166
Joe Perches3705ce52013-07-03 15:05:31 -07002167sub trim {
2168 my ($string) = @_;
2169
Joe Perchesb34c6482013-09-11 14:24:01 -07002170 $string =~ s/^\s+|\s+$//g;
2171
2172 return $string;
2173}
2174
2175sub ltrim {
2176 my ($string) = @_;
2177
2178 $string =~ s/^\s+//;
2179
2180 return $string;
2181}
2182
2183sub rtrim {
2184 my ($string) = @_;
2185
2186 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002187
2188 return $string;
2189}
2190
Joe Perches52ea8502013-11-12 15:10:09 -08002191sub string_find_replace {
2192 my ($string, $find, $replace) = @_;
2193
2194 $string =~ s/$find/$replace/g;
2195
2196 return $string;
2197}
2198
Joe Perches3705ce52013-07-03 15:05:31 -07002199sub tabify {
2200 my ($leading) = @_;
2201
2202 my $source_indent = 8;
2203 my $max_spaces_before_tab = $source_indent - 1;
2204 my $spaces_to_tab = " " x $source_indent;
2205
2206 #convert leading spaces to tabs
2207 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2208 #Remove spaces before a tab
2209 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2210
2211 return "$leading";
2212}
2213
Gregory Bean2d5df1b2011-07-06 15:36:58 -07002214sub cleanup_continuation_headers {
2215 # Collapse any header-continuation lines into a single line so they
2216 # can be parsed meaningfully, as the parser only has one line
2217 # of context to work with.
2218 my $again;
2219 do {
2220 $again = 0;
2221 foreach my $n (0 .. scalar(@rawlines) - 2) {
2222 if ($rawlines[$n]=~/^\s*$/) {
2223 # A blank line means there's no more chance
2224 # of finding headers. Shortcut to done.
2225 return;
2226 }
2227 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
2228 $rawlines[$n+1]=~/^\s+/) {
2229 # Continuation header. Collapse it.
2230 my $line = splice @rawlines, $n+1, 1;
2231 $line=~s/^\s+/ /;
2232 $rawlines[$n] .= $line;
2233 # We've 'destabilized' the list, so restart.
2234 $again = 1;
2235 last;
2236 }
2237 }
2238 } while ($again);
2239}
2240
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002241sub pos_last_openparen {
2242 my ($line) = @_;
2243
2244 my $pos = 0;
2245
2246 my $opens = $line =~ tr/\(/\(/;
2247 my $closes = $line =~ tr/\)/\)/;
2248
2249 my $last_openparen = 0;
2250
2251 if (($opens == 0) || ($closes >= $opens)) {
2252 return -1;
2253 }
2254
2255 my $len = length($line);
2256
2257 for ($pos = 0; $pos < $len; $pos++) {
2258 my $string = substr($line, $pos);
2259 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2260 $pos += length($1) - 1;
2261 } elsif (substr($line, $pos, 1) eq '(') {
2262 $last_openparen = $pos;
2263 } elsif (index($string, '(') == -1) {
2264 last;
2265 }
2266 }
2267
Joe Perches91cb5192014-04-03 14:49:32 -07002268 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002269}
2270
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002271sub process {
2272 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002273
2274 my $linenr=0;
2275 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002276 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002277 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002278 my $stashrawline="";
David Keitel6ceb5dd2016-03-23 20:57:21 -07002279 my $subjectline="";
2280 my $sublinenr="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002281
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002282 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002283 my $indent;
2284 my $previndent=0;
2285 my $stashindent=0;
2286
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002287 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002288 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002289 my $author = '';
2290 my $authorsignoff = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002291 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002292 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002293 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002294 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002295 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002296 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002297 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002298 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002299 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002300 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002301 my $non_utf8_charset = 0;
2302
Joe Perches365dd4e2014-08-06 16:10:42 -07002303 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002304 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002305
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002306 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002307 our $cnt_lines = 0;
2308 our $cnt_error = 0;
2309 our $cnt_warn = 0;
2310 our $cnt_chk = 0;
2311
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002312 # Trace the real file/line as we go.
2313 my $realfile = '';
2314 my $realline = 0;
2315 my $realcnt = 0;
2316 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002317 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002318 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002319 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002320 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002321 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002322
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002323 my $prev_values = 'E';
2324
2325 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002326 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002327 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002328 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002329 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002330
Joe Perches7e51f192013-09-11 14:23:57 -07002331 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002332
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002333 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002334 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002335 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002336 my @setup_docs = ();
2337 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002338
Joe Perchesd8b07712013-11-12 15:10:06 -08002339 my $camelcase_file_seeded = 0;
David Keitel407fdcb2016-03-23 20:52:44 -07002340 my $shorttext = BEFORE_SHORTTEXT;
2341 my $shorttext_exspc = 0;
Steve Muckle4a15af92012-03-05 10:12:04 -08002342 my $commit_text_present = 0;
Joe Perchesd8b07712013-11-12 15:10:06 -08002343
Rob Herring9f3a8992018-04-10 16:33:13 -07002344 my $checklicenseline = 1;
2345
Andy Whitcroft773647a2008-03-28 14:15:58 -07002346 sanitise_line_reset();
Gregory Bean2d5df1b2011-07-06 15:36:58 -07002347 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002348 my $line;
Gregory Bean2d5df1b2011-07-06 15:36:58 -07002349
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002350 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002351 $linenr++;
2352 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002353
Joe Perches3705ce52013-07-03 15:05:31 -07002354 push(@fixed, $rawline) if ($fix);
2355
Andy Whitcroft773647a2008-03-28 14:15:58 -07002356 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002357 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002358 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002359 $setup_docs = 1;
2360 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002361 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002362 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002363 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002364 $realline=$1-1;
2365 if (defined $2) {
2366 $realcnt=$3+1;
2367 } else {
2368 $realcnt=1+1;
2369 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002370 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002371
2372 # Guestimate if this is a continuing comment. Run
2373 # the context looking for a comment "edge". If this
2374 # edge is a close comment then we must be in a comment
2375 # at context start.
2376 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002377 my $cnt = $realcnt;
2378 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2379 next if (defined $rawlines[$ln - 1] &&
2380 $rawlines[$ln - 1] =~ /^-/);
2381 $cnt--;
2382 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002383 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002384 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2385 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2386 ($edge) = $1;
2387 last;
2388 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002389 }
2390 if (defined $edge && $edge eq '*/') {
2391 $in_comment = 1;
2392 }
2393
2394 # Guestimate if this is a continuing comment. If this
2395 # is the start of a diff block and this line starts
2396 # ' *' then it is very likely a comment.
2397 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002398 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002399 {
2400 $in_comment = 1;
2401 }
2402
2403 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2404 sanitise_line_reset($in_comment);
2405
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002406 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002407 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002408 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002409 $line = sanitise_line($rawline);
2410 }
2411 push(@lines, $line);
2412
2413 if ($realcnt > 1) {
2414 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2415 } else {
2416 $realcnt = 0;
2417 }
2418
2419 #print "==>$rawline\n";
2420 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002421
2422 if ($setup_docs && $line =~ /^\+/) {
2423 push(@setup_docs, $line);
2424 }
2425 }
2426
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002427 $prefix = '';
2428
Andy Whitcroft773647a2008-03-28 14:15:58 -07002429 $realcnt = 0;
2430 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002431 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002432 foreach my $line (@lines) {
2433 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002434 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002435 my $sline = $line; #copy of $line
2436 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002437
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002438 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002439
Joe Perches12c253a2018-06-07 17:10:58 -07002440# check if it's a mode change, rename or start of a patch
2441 if (!$in_commit_log &&
2442 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2443 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2444 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2445 $is_patch = 1;
2446 }
2447
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002448#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002449 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002450 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2451 my $context = $4;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002452 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002453 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002454 $realline=$1-1;
2455 if (defined $2) {
2456 $realcnt=$3+1;
2457 } else {
2458 $realcnt=1+1;
2459 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002460 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002461 $prev_values = 'E';
2462
Andy Whitcroft773647a2008-03-28 14:15:58 -07002463 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002464 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002465 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002466 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002467 if ($context =~ /\b(\w+)\s*\(/) {
2468 $context_function = $1;
2469 } else {
2470 undef $context_function;
2471 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002472 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002473
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002474# track the line number as we move through the hunk, note that
2475# new versions of GNU diff omit the leading space on completely
2476# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002477 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002478 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002479 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002480
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002481 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002482 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002483
2484 # Track the previous line.
2485 ($prevline, $stashline) = ($stashline, $line);
2486 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002487 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2488
Andy Whitcroft773647a2008-03-28 14:15:58 -07002489 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002490
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002491 } elsif ($realcnt == 1) {
2492 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002493 }
2494
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002495 my $hunk_line = ($realcnt != 0);
2496
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002497 $here = "#$linenr: " if (!$file);
2498 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002499
Joe Perches2ac73b42014-06-04 16:12:05 -07002500 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002501 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002502 if ($line =~ /^diff --git.*?(\S+)$/) {
2503 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002504 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002505 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002506 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002507 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002508 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002509 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002510 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002511
2512 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002513 if (!$file && $tree && $p1_prefix ne '' &&
2514 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002515 WARN("PATCH_PREFIX",
2516 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002517 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002518
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002519 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002520 ERROR("MODIFIED_INCLUDE_ASM",
2521 "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 -07002522 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002523 $found_file = 1;
2524 }
2525
Joe Perches34d88152015-06-25 15:03:05 -07002526#make up the handle for any error we report on this line
2527 if ($showfile) {
2528 $prefix = "$realfile:$realline: "
2529 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002530 if ($file) {
2531 $prefix = "$filename:$realline: ";
2532 } else {
2533 $prefix = "$filename:$linenr: ";
2534 }
Joe Perches34d88152015-06-25 15:03:05 -07002535 }
2536
Joe Perches2ac73b42014-06-04 16:12:05 -07002537 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002538 if (is_maintained_obsolete($realfile)) {
2539 WARN("OBSOLETE",
2540 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2541 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002542 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002543 $check = 1;
2544 } else {
2545 $check = $check_orig;
2546 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002547 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002548
2549 if ($realfile !~ /^MAINTAINERS/) {
2550 my $last_binding_patch = $is_binding_patch;
2551
2552 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2553
2554 if (($last_binding_patch != -1) &&
2555 ($last_binding_patch ^ $is_binding_patch)) {
2556 WARN("DT_SPLIT_BINDING_PATCH",
2557 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2558 }
2559 }
2560
Andy Whitcroft773647a2008-03-28 14:15:58 -07002561 next;
2562 }
Randy Dunlap389834b2007-06-08 13:47:03 -07002563 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002564
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002565 my $hereline = "$here\n$rawline\n";
2566 my $herecurr = "$here\n$rawline\n";
2567 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002568
David Keitel407fdcb2016-03-23 20:52:44 -07002569 if ($shorttext != AFTER_SHORTTEXT) {
Steve Muckle4a15af92012-03-05 10:12:04 -08002570 if ($shorttext == IN_SHORTTEXT_BLANKLINE && $line=~/\S/) {
2571 # the subject line was just processed,
2572 # a blank line must be next
2573 WARN("NONBLANK_AFTER_SUMMARY",
2574 "non-blank line after summary line\n" . $herecurr);
2575 $shorttext = IN_SHORTTEXT;
2576 # this non-blank line may or may not be commit text -
2577 # a warning has been generated so assume it is commit
2578 # text and move on
2579 $commit_text_present = 1;
2580 # fall through and treat this line as IN_SHORTTEXT
2581 }
David Keitel407fdcb2016-03-23 20:52:44 -07002582 if ($shorttext == IN_SHORTTEXT) {
2583 if ($line=~/^---/ || $line=~/^diff.*/) {
Steve Muckle4a15af92012-03-05 10:12:04 -08002584 if ($commit_text_present == 0) {
2585 WARN("NO_COMMIT_TEXT",
2586 "please add commit text explaining " .
2587 "*why* the change is needed\n" .
2588 $herecurr);
2589 }
David Keitel407fdcb2016-03-23 20:52:44 -07002590 $shorttext = AFTER_SHORTTEXT;
Steve Muckle65f19dc2012-03-09 11:15:24 -08002591 } elsif ($line=~/^\s*change-id:/i ||
2592 $line=~/^\s*signed-off-by:/i ||
2593 $line=~/^\s*crs-fixed:/i ||
2594 $line=~/^\s*acked-by:/i) {
Steve Muckle4a15af92012-03-05 10:12:04 -08002595 # this is a tag, there must be commit
2596 # text by now
2597 if ($commit_text_present == 0) {
2598 WARN("NO_COMMIT_TEXT",
2599 "please add commit text explaining " .
2600 "*why* the change is needed\n" .
2601 $herecurr);
2602 # prevent duplicate warnings
2603 $commit_text_present = 1;
2604 }
2605 } elsif ($line=~/\S/) {
2606 $commit_text_present = 1;
David Keitel407fdcb2016-03-23 20:52:44 -07002607 }
Steve Muckle4a15af92012-03-05 10:12:04 -08002608 } elsif ($shorttext == IN_SHORTTEXT_BLANKLINE) {
2609 # case of non-blank line in this state handled above
2610 $shorttext = IN_SHORTTEXT;
David Keitel6ceb5dd2016-03-23 20:57:21 -07002611 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
Gregory Bean960f0b12011-03-03 13:46:41 -08002612# The Subject line doesn't have to be the last header in the patch.
2613# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
2614# Per RFC5322, continuation lines must be folded, so any left-justified
2615# text which looks like a header is definitely a header.
2616 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
2617 $shorttext = IN_SHORTTEXT;
Steve Muckle4a15af92012-03-05 10:12:04 -08002618 # Check for Subject line followed by a blank line.
Gregory Bean960f0b12011-03-03 13:46:41 -08002619 if (length($line) != 0) {
2620 WARN("NONBLANK_AFTER_SUMMARY",
2621 "non-blank line after " .
2622 "summary line\n" .
2623 $sublinenr . $here .
2624 "\n" . $subjectline .
2625 "\n" . $line . "\n");
Steve Muckle4a15af92012-03-05 10:12:04 -08002626 # this non-blank line may or may not
2627 # be commit text - a warning has been
2628 # generated so assume it is commit
2629 # text and move on
2630 $commit_text_present = 1;
Gregory Bean960f0b12011-03-03 13:46:41 -08002631 }
David Keitel6ceb5dd2016-03-23 20:57:21 -07002632 }
Steve Muckle4a15af92012-03-05 10:12:04 -08002633 # The next two cases are BEFORE_SHORTTEXT.
David Keitel6ceb5dd2016-03-23 20:57:21 -07002634 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
Steve Muckle4a15af92012-03-05 10:12:04 -08002635 # This is the subject line. Go to
2636 # CHECK_NEXT_SHORTTEXT to wait for the commit
2637 # text to show up.
David Keitel6ceb5dd2016-03-23 20:57:21 -07002638 $shorttext = CHECK_NEXT_SHORTTEXT;
2639 $subjectline = $line;
2640 $sublinenr = "#$linenr & ";
2641# Check for Subject line less than line limit
Matt Wagantall06760662012-08-18 11:21:00 -07002642 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel407fdcb2016-03-23 20:52:44 -07002643 WARN("LONG_SUMMARY_LINE",
2644 "summary line over " .
2645 SHORTTEXT_LIMIT .
2646 " characters\n" . $herecurr);
2647 }
2648 } elsif ($line=~/^ (.*)/) {
Steve Muckle4a15af92012-03-05 10:12:04 -08002649 # Indented format, this must be the summary
2650 # line (i.e. git show). There will be no more
2651 # headers so we are now in the shorttext.
2652 $shorttext = IN_SHORTTEXT_BLANKLINE;
David Keitel407fdcb2016-03-23 20:52:44 -07002653 $shorttext_exspc = 4;
Matt Wagantall06760662012-08-18 11:21:00 -07002654 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel407fdcb2016-03-23 20:52:44 -07002655 WARN("LONG_SUMMARY_LINE",
2656 "summary line over " .
2657 SHORTTEXT_LIMIT .
2658 " characters\n" . $herecurr);
2659 }
2660 }
2661 }
2662
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002663 $cnt_lines++ if ($realcnt != 0);
2664
Joe Perches490b2922018-08-21 21:58:01 -07002665# Verify the existence of a commit log if appropriate
2666# 2 is used because a $signature is counted in $commit_log_lines
2667 if ($in_commit_log) {
2668 if ($line !~ /^\s*$/) {
2669 $commit_log_lines++; #could be a $signature
2670 }
2671 } elsif ($has_commit_log && $commit_log_lines < 2) {
2672 WARN("COMMIT_MESSAGE",
2673 "Missing commit description - Add an appropriate one\n");
2674 $commit_log_lines = 2; #warn only once
2675 }
2676
Joe Perchese518e9a2015-06-25 15:03:27 -07002677# Check if the commit log has what seems like a diff which can confuse patch
2678 if ($in_commit_log && !$commit_log_has_diff &&
2679 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2680 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2681 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2682 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2683 ERROR("DIFF_IN_COMMIT_MSG",
2684 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2685 $commit_log_has_diff = 1;
2686 }
2687
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002688# Check for incorrect file permissions
2689 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2690 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002691 if ($realfile !~ m@scripts/@ &&
2692 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002693 ERROR("EXECUTE_PERMISSIONS",
2694 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002695 }
2696 }
2697
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002698# Check the patch for a From:
2699 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2700 $author = $1;
2701 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2702 $author =~ s/"//g;
2703 }
2704
Joe Perches20112472011-07-25 17:13:23 -07002705# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002706 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002707 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002708 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002709 if ($author ne '') {
2710 my $l = $line;
2711 $l =~ s/"//g;
2712 if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2713 $authorsignoff = 1;
2714 }
2715 }
Joe Perches20112472011-07-25 17:13:23 -07002716 }
2717
Joe Perchese0d975b2014-12-10 15:51:49 -08002718# Check if MAINTAINERS is being updated. If so, there's probably no need to
2719# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2720 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2721 $reported_maintainer_file = 1;
2722 }
2723
Joe Perches20112472011-07-25 17:13:23 -07002724# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002725 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002726 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002727 my $space_before = $1;
2728 my $sign_off = $2;
2729 my $space_after = $3;
2730 my $email = $4;
2731 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2732
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002733 if ($sign_off !~ /$signature_tags/) {
2734 WARN("BAD_SIGN_OFF",
2735 "Non-standard signature: $sign_off\n" . $herecurr);
2736 }
Joe Perches20112472011-07-25 17:13:23 -07002737 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002738 if (WARN("BAD_SIGN_OFF",
2739 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2740 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002741 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002742 "$ucfirst_sign_off $email";
2743 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002744 }
Joe Perches20112472011-07-25 17:13:23 -07002745 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002746 if (WARN("BAD_SIGN_OFF",
2747 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2748 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002749 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002750 "$ucfirst_sign_off $email";
2751 }
2752
Joe Perches20112472011-07-25 17:13:23 -07002753 }
2754 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002755 if (WARN("BAD_SIGN_OFF",
2756 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2757 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002758 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002759 "$ucfirst_sign_off $email";
2760 }
Joe Perches20112472011-07-25 17:13:23 -07002761 }
2762
2763 my ($email_name, $email_address, $comment) = parse_email($email);
2764 my $suggested_email = format_email(($email_name, $email_address));
2765 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002766 ERROR("BAD_SIGN_OFF",
2767 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002768 } else {
2769 my $dequoted = $suggested_email;
2770 $dequoted =~ s/^"//;
2771 $dequoted =~ s/" </ </;
2772 # Don't force email to have quotes
2773 # Allow just an angle bracketed address
2774 if ("$dequoted$comment" ne $email &&
2775 "<$email_address>$comment" ne $email &&
2776 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002777 WARN("BAD_SIGN_OFF",
2778 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002779 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002780 }
Joe Perches7e51f192013-09-11 14:23:57 -07002781
2782# Check for duplicate signatures
2783 my $sig_nospace = $line;
2784 $sig_nospace =~ s/\s//g;
2785 $sig_nospace = lc($sig_nospace);
2786 if (defined $signatures{$sig_nospace}) {
Jordan Crouseaa744e12018-08-08 13:01:36 -06002787 WARN("DUPLICATE_SIGN_OFF",
Joe Perches7e51f192013-09-11 14:23:57 -07002788 "Duplicate signature\n" . $herecurr);
2789 } else {
2790 $signatures{$sig_nospace} = 1;
2791 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002792 }
2793
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002794# Check email subject for common tools that don't need to be mentioned
2795 if ($in_header_lines &&
2796 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2797 WARN("EMAIL_SUBJECT",
2798 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2799 }
2800
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002801# Check for unwanted Gerrit info
2802 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2803 ERROR("GERRIT_CHANGE_ID",
2804 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2805 }
2806
Joe Perches369c8dd2015-11-06 16:31:34 -08002807# Check if the commit log is in a possible stack dump
2808 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2809 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2810 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2811 # timestamp
2812 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2813 # stack dump address
2814 $commit_log_possible_stack_dump = 1;
2815 }
2816
Joe Perches2a076f42015-04-16 12:44:28 -07002817# Check for line lengths > 75 in commit log, warn once
2818 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002819 length($line) > 75 &&
Neeraj Upadhyayc2e2f6f2019-11-04 15:28:51 +05302820 !($line =~ /^\s*[a-zA-Z0-9_\/\.\-\,]+\s+\|\s+\d+/ ||
Joe Perches369c8dd2015-11-06 16:31:34 -08002821 # file delta changes
2822 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2823 # filename then :
2824 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2825 # A Fixes: or Link: line
2826 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002827 WARN("COMMIT_LOG_LONG_LINE",
2828 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2829 $commit_log_long_line = 1;
2830 }
2831
Joe Perchesbf4daf12015-09-09 15:37:50 -07002832# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002833 if ($in_commit_log && $commit_log_possible_stack_dump &&
2834 $line =~ /^\s*$/) {
2835 $commit_log_possible_stack_dump = 0;
2836 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002837
Joe Perches0d7835f2015-02-13 14:38:35 -08002838# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002839 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002840 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002841 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002842 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002843 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002844 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2845 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002846 my $init_char = "c";
2847 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002848 my $short = 1;
2849 my $long = 0;
2850 my $case = 1;
2851 my $space = 1;
2852 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002853 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002854 my $id = '0123456789ab';
2855 my $orig_desc = "commit description";
2856 my $description = "";
2857
Joe Perchesfe043ea2015-09-09 15:37:25 -07002858 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2859 $init_char = $1;
2860 $orig_commit = lc($2);
2861 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2862 $orig_commit = lc($1);
2863 }
2864
Joe Perches0d7835f2015-02-13 14:38:35 -08002865 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2866 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2867 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2868 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2869 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2870 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002871 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002872 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2873 defined $rawlines[$linenr] &&
2874 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2875 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002876 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002877 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2878 defined $rawlines[$linenr] &&
2879 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2880 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2881 $orig_desc = $1;
2882 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2883 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002884 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002885 }
2886
2887 ($id, $description) = git_commit_info($orig_commit,
2888 $id, $orig_desc);
2889
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002890 if (defined($id) &&
2891 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002892 ERROR("GIT_COMMIT_ID",
2893 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2894 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002895 }
2896
Joe Perches13f19372014-08-06 16:10:59 -07002897# Check for added, moved or deleted files
2898 if (!$reported_maintainer_file && !$in_commit_log &&
2899 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2900 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2901 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2902 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002903 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002904 $reported_maintainer_file = 1;
2905 WARN("FILE_PATH_CHANGES",
2906 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2907 }
2908
Andy Whitcroft00df3442007-06-08 13:47:06 -07002909# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002910 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002911 ERROR("CORRUPTED_PATCH",
2912 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002913 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002914 }
2915
2916# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2917 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002918 $rawline !~ m/^$UTF8*$/) {
2919 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2920
2921 my $blank = copy_spacing($rawline);
2922 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2923 my $hereptr = "$hereline$ptr\n";
2924
Joe Perches34d99212011-07-25 17:13:26 -07002925 CHK("INVALID_UTF8",
2926 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002927 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002928
Joe Perches15662b32011-10-31 17:13:12 -07002929# Check if it's the start of a commit log
2930# (not a header line and we haven't seen the patch filename)
2931 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002932 !($rawline =~ /^\s+(?:\S|$)/ ||
2933 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002934 $in_header_lines = 0;
2935 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002936 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002937 }
2938
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002939# Check if there is UTF-8 in a commit log when a mail header has explicitly
2940# declined it, i.e defined some charset where it is missing.
2941 if ($in_header_lines &&
2942 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2943 $1 !~ /utf-8/i) {
2944 $non_utf8_charset = 1;
2945 }
2946
2947 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002948 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002949 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002950 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2951 }
2952
Joe Perchesd6430f72016-12-12 16:46:28 -08002953# Check for absolute kernel paths in commit message
2954 if ($tree && $in_commit_log) {
2955 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2956 my $file = $1;
2957
2958 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2959 check_absolute_file($1, $herecurr)) {
2960 #
2961 } else {
2962 check_absolute_file($file, $herecurr);
2963 }
2964 }
2965 }
2966
Kees Cook66b47b42014-10-13 15:51:57 -07002967# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002968 if (defined($misspellings) &&
2969 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002970 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002971 my $typo = $1;
2972 my $typo_fix = $spelling_fix{lc($typo)};
2973 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2974 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002975 my $msg_level = \&WARN;
2976 $msg_level = \&CHK if ($file);
2977 if (&{$msg_level}("TYPO_SPELLING",
2978 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002979 $fix) {
2980 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2981 }
2982 }
2983 }
2984
Andy Whitcroft306708542008-10-15 22:02:28 -07002985# ignore non-hunk lines and lines being removed
2986 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002987
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002988#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002989 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002990 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002991 if (ERROR("DOS_LINE_ENDINGS",
2992 "DOS line endings\n" . $herevet) &&
2993 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002994 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002995 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002996 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2997 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002998 if (ERROR("TRAILING_WHITESPACE",
2999 "trailing whitespace\n" . $herevet) &&
3000 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003001 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07003002 }
3003
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003004 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003005 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07003006
Josh Triplett4783f892013-11-12 15:10:12 -08003007# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08003008 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08003009 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08003010 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3011 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08003012 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07003013 my $msg_level = \&ERROR;
3014 $msg_level = \&CHK if ($file);
3015 &{$msg_level}("FSF_MAILING_ADDRESS",
3016 "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 -08003017 }
3018
Andi Kleen33549572010-05-24 14:33:29 -07003019# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003020# Only applies when adding the entry originally, after that we do not have
3021# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07003022 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01003023 # 'choice' is usually the last thing on the line (though
3024 # Kconfig supports named choices), so use a word boundary
3025 # (\b) rather than a whitespace character (\s)
3026 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003027 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003028 my $cnt = $realcnt;
3029 my $ln = $linenr + 1;
3030 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003031 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003032 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003033 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003034 $f = $lines[$ln - 1];
3035 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3036 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003037
3038 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003039 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003040
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003041 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003042 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01003043 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
3044 if ($lines[$ln - 1] =~ "---help---") {
3045 WARN("CONFIG_DESCRIPTION",
3046 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
3047 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003048 $length = -1;
3049 }
3050
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003051 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003052 $f =~ s/#.*//;
3053 $f =~ s/^\s+//;
3054 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003055
3056 # This only checks context lines in the patch
3057 # and so hopefully shouldn't trigger false
3058 # positives, even though some of these are
3059 # common words in help texts
3060 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3061 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003062 $is_end = 1;
3063 last;
3064 }
Andi Kleen33549572010-05-24 14:33:29 -07003065 $length++;
3066 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003067 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3068 WARN("CONFIG_DESCRIPTION",
3069 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3070 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003071 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003072 }
3073
Joe Perches628f91a2017-07-10 15:52:07 -07003074# check for MAINTAINERS entries that don't have the right form
3075 if ($realfile =~ /^MAINTAINERS$/ &&
3076 $rawline =~ /^\+[A-Z]:/ &&
3077 $rawline !~ /^\+[A-Z]:\t\S/) {
3078 if (WARN("MAINTAINERS_STYLE",
3079 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3080 $fix) {
3081 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3082 }
3083 }
3084
Christoph Jaeger327953e2015-02-13 14:38:29 -08003085# discourage the use of boolean for type definition attributes of Kconfig options
3086 if ($realfile =~ /Kconfig/ &&
3087 $line =~ /^\+\s*\bboolean\b/) {
3088 WARN("CONFIG_TYPE_BOOLEAN",
3089 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3090 }
3091
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003092 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3093 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3094 my $flag = $1;
3095 my $replacement = {
3096 'EXTRA_AFLAGS' => 'asflags-y',
3097 'EXTRA_CFLAGS' => 'ccflags-y',
3098 'EXTRA_CPPFLAGS' => 'cppflags-y',
3099 'EXTRA_LDFLAGS' => 'ldflags-y',
3100 };
3101
3102 WARN("DEPRECATED_VARIABLE",
3103 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3104 }
3105
Rob Herringbff5da42014-01-23 15:54:51 -08003106# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003107 if (defined $root &&
3108 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3109 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3110
Rob Herringbff5da42014-01-23 15:54:51 -08003111 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3112
Florian Vaussardcc933192014-04-03 14:49:27 -07003113 my $dt_path = $root . "/Documentation/devicetree/bindings/";
3114 my $vp_file = $dt_path . "vendor-prefixes.txt";
3115
Rob Herringbff5da42014-01-23 15:54:51 -08003116 foreach my $compat (@compats) {
3117 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003118 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3119 my $compat3 = $compat;
3120 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
Murali Nalajalab2d6b7a2019-08-29 13:44:29 -07003121 `grep -ERq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003122 if ( $? >> 8 ) {
3123 WARN("UNDOCUMENTED_DT_STRING",
3124 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3125 }
3126
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003127 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3128 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07003129 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003130 if ( $? >> 8 ) {
3131 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003132 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003133 }
3134 }
3135 }
3136
Rob Herring9f3a8992018-04-10 16:33:13 -07003137# check for using SPDX license tag at beginning of files
3138 if ($realline == $checklicenseline) {
3139 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3140 $checklicenseline = 2;
3141 } elsif ($rawline =~ /^\+/) {
3142 my $comment = "";
3143 if ($realfile =~ /\.(h|s|S)$/) {
3144 $comment = '/*';
3145 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3146 $comment = '//';
3147 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
3148 $comment = '#';
3149 } elsif ($realfile =~ /\.rst$/) {
3150 $comment = '..';
3151 }
3152
3153 if ($comment !~ /^$/ &&
3154 $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003155 WARN("SPDX_LICENSE_TAG",
3156 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
3157 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
3158 my $spdx_license = $1;
3159 if (!is_SPDX_License_valid($spdx_license)) {
3160 WARN("SPDX_LICENSE_TAG",
3161 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3162 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003163 }
3164 }
3165 }
3166
Andy Whitcroft5368df22008-10-15 22:02:27 -07003167# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003168 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07003169
Joe Perches47e0c882015-06-25 15:02:57 -07003170# line length limit (with some exclusions)
3171#
3172# There are a few types of lines that may extend beyond $max_line_length:
3173# logging functions like pr_info that end in a string
3174# lines with a single string
3175# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003176# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003177#
3178# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003179# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003180# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3181# LONG_LINE all other lines longer than $max_line_length
3182#
3183# if LONG_LINE is ignored, the other 2 types are also ignored
3184#
3185
Joe Perchesb4749e92015-07-17 16:24:01 -07003186 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003187 my $msg_type = "LONG_LINE";
3188
3189 # Check the allowed long line types first
3190
3191 # logging functions that end in a string that starts
3192 # before $max_line_length
3193 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3194 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3195 $msg_type = "";
3196
3197 # lines with only strings (w/ possible termination)
3198 # #defines with only strings
3199 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3200 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3201 $msg_type = "";
3202
Joe Perchescc147502017-11-17 15:28:44 -08003203 # More special cases
3204 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3205 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003206 $msg_type = "";
3207
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003208 # URL ($rawline is used in case the URL is in a comment)
3209 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3210 $msg_type = "";
3211
Bryan Huntsman2073c4c12018-02-12 17:26:10 -08003212 # Long copyright statements are another special case
3213 } elsif ($rawline =~ /^\+.\*.*copyright.*\(c\).*$/i) {
3214 $msg_type = "";
3215
Joe Perches47e0c882015-06-25 15:02:57 -07003216 # Otherwise set the alternate message types
3217
3218 # a comment starts before $max_line_length
3219 } elsif ($line =~ /($;[\s$;]*)$/ &&
3220 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3221 $msg_type = "LONG_LINE_COMMENT"
3222
3223 # a quoted string starts before $max_line_length
3224 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3225 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3226 $msg_type = "LONG_LINE_STRING"
3227 }
3228
3229 if ($msg_type ne "" &&
3230 (show_type("LONG_LINE") || show_type($msg_type))) {
3231 WARN($msg_type,
3232 "line over $max_line_length characters\n" . $herecurr);
3233 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003234 }
3235
Andy Whitcroft8905a672007-11-28 16:21:06 -08003236# check for adding lines without a newline.
3237 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003238 WARN("MISSING_EOF_NEWLINE",
3239 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003240 }
3241
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003242# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003243 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003244
3245# at the beginning of a line any tabs must come first and anything
3246# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003247 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3248 $rawline =~ /^\+\s* \s*/) {
3249 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003250 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003251 if (ERROR("CODE_INDENT",
3252 "code indent should use tabs where possible\n" . $herevet) &&
3253 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003254 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003255 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003256 }
3257
Alberto Panizzo08e44362010-03-05 13:43:54 -08003258# check for space before tabs.
3259 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3260 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003261 if (WARN("SPACE_BEFORE_TAB",
3262 "please, no space before tabs\n" . $herevet) &&
3263 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003264 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003265 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003266 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003267 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003268 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003269 }
3270
Joe Perches6a487212018-04-10 16:34:04 -07003271# check for assignments on the start of a line
3272 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3273 CHK("ASSIGNMENT_CONTINUATIONS",
3274 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3275 }
3276
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003277# check for && or || at the start of a line
3278 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3279 CHK("LOGICAL_CONTINUATIONS",
3280 "Logical continuations should be on the previous line\n" . $hereprev);
3281 }
3282
Joe Perchesa91e8992016-05-20 17:04:05 -07003283# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003284 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003285 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003286 my $indent = length($1);
3287 if ($indent % 8) {
3288 if (WARN("TABSTOP",
3289 "Statements should start on a tabstop\n" . $herecurr) &&
3290 $fix) {
3291 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3292 }
3293 }
3294 }
3295
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003296# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003297 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003298 $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 -07003299 $prevline =~ /^\+(\t*)(.*)$/;
3300 my $oldindent = $1;
3301 my $rest = $2;
3302
3303 my $pos = pos_last_openparen($rest);
3304 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003305 $line =~ /^(\+| )([ \t]*)/;
3306 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003307
3308 my $goodtabindent = $oldindent .
3309 "\t" x ($pos / 8) .
3310 " " x ($pos % 8);
3311 my $goodspaceindent = $oldindent . " " x $pos;
3312
3313 if ($newindent ne $goodtabindent &&
3314 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003315
3316 if (CHK("PARENTHESIS_ALIGNMENT",
3317 "Alignment should match open parenthesis\n" . $hereprev) &&
3318 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003319 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003320 s/^\+[ \t]*/\+$goodtabindent/;
3321 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003322 }
3323 }
3324 }
3325
Joe Perches6ab3a972015-04-16 12:44:05 -07003326# check for space after cast like "(int) foo" or "(struct foo) bar"
3327# avoid checking a few false positives:
3328# "sizeof(<type>)" or "__alignof__(<type>)"
3329# function pointer declarations like "(*foo)(int) = bar;"
3330# structure definitions like "(struct foo) { 0 };"
3331# multiline macros that define functions
3332# known attributes or the __attribute__ keyword
3333 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3334 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003335 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003336 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003337 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003338 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003339 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003340 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003341 }
3342
Joe Perches86406b12015-09-09 15:37:41 -07003343# Block comment styles
3344# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003345 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003346 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003347 $rawline =~ /^\+[ \t]*\*/ &&
3348 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003349 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3350 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3351 }
3352
Joe Perches86406b12015-09-09 15:37:41 -07003353# Block comments use * on subsequent lines
3354 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3355 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003356 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003357 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003358 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003359 WARN("BLOCK_COMMENT_STYLE",
3360 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003361 }
3362
Joe Perches86406b12015-09-09 15:37:41 -07003363# Block comments use */ on trailing lines
3364 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003365 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3366 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3367 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003368 WARN("BLOCK_COMMENT_STYLE",
3369 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003370 }
3371
Joe Perches08eb9b82016-10-11 13:51:50 -07003372# Block comment * alignment
3373 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003374 $line =~ /^\+[ \t]*$;/ && #leading comment
3375 $rawline =~ /^\+[ \t]*\*/ && #leading *
3376 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003377 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003378 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3379 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003380 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003381 if (defined($1)) {
3382 $oldindent = expand_tabs($1);
3383 } else {
3384 $prevrawline =~ m@^\+(.*/?)\*@;
3385 $oldindent = expand_tabs($1);
3386 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003387 $rawline =~ m@^\+([ \t]*)\*@;
3388 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003389 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003390 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003391 WARN("BLOCK_COMMENT_STYLE",
3392 "Block comments should align the * on each line\n" . $hereprev);
3393 }
3394 }
3395
Joe Perches7f619192014-08-06 16:10:39 -07003396# check for missing blank lines after struct/union declarations
3397# with exceptions for various attributes and macros
3398 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3399 $line =~ /^\+/ &&
3400 !($line =~ /^\+\s*$/ ||
3401 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3402 $line =~ /^\+\s*MODULE_/i ||
3403 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3404 $line =~ /^\+[a-z_]*init/ ||
3405 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3406 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003407 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003408 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003409 if (CHK("LINE_SPACING",
3410 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3411 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003412 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003413 }
Joe Perches7f619192014-08-06 16:10:39 -07003414 }
3415
Joe Perches365dd4e2014-08-06 16:10:42 -07003416# check for multiple consecutive blank lines
3417 if ($prevline =~ /^[\+ ]\s*$/ &&
3418 $line =~ /^\+\s*$/ &&
3419 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003420 if (CHK("LINE_SPACING",
3421 "Please don't use multiple blank lines\n" . $hereprev) &&
3422 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003423 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003424 }
3425
Joe Perches365dd4e2014-08-06 16:10:42 -07003426 $last_blank_line = $linenr;
3427 }
3428
Joe Perches3b617e32014-04-03 14:49:28 -07003429# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003430 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3431 # actual declarations
3432 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003433 # function pointer declarations
3434 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003435 # foo bar; where foo is some local typedef or #define
3436 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3437 # known declaration macros
3438 $prevline =~ /^\+\s+$declaration_macros/) &&
3439 # for "else if" which can look like "$Ident $Ident"
3440 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3441 # other possible extensions of declaration lines
3442 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3443 # not starting a section or a macro "\" extended line
3444 $prevline =~ /(?:\{\s*|\\)$/) &&
3445 # looks like a declaration
3446 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003447 # function pointer declarations
3448 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003449 # foo bar; where foo is some local typedef or #define
3450 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3451 # known declaration macros
3452 $sline =~ /^\+\s+$declaration_macros/ ||
3453 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003454 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003455 # start or end of block or continuation of declaration
3456 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3457 # bitfield continuation
3458 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3459 # other possible extensions of declaration lines
3460 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3461 # indentation of previous and current line are the same
3462 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003463 if (WARN("LINE_SPACING",
3464 "Missing a blank line after declarations\n" . $hereprev) &&
3465 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003466 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003467 }
Joe Perches3b617e32014-04-03 14:49:28 -07003468 }
3469
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003470# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003471# Exceptions:
3472# 1) within comments
3473# 2) indented preprocessor commands
3474# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003475 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003476 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003477 if (WARN("LEADING_SPACE",
3478 "please, no spaces at the start of a line\n" . $herevet) &&
3479 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003480 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003481 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003482 }
3483
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003484# check we are in a valid C source file if not then ignore this hunk
3485 next if ($realfile !~ /\.(h|c)$/);
3486
Joe Perches5751a242017-11-17 15:28:52 -08003487# check for unusual line ending [ or (
3488 if ($line =~ /^\+.*([\[\(])\s*$/) {
3489 CHK("OPEN_ENDED_LINE",
3490 "Lines should not end with a '$1'\n" . $herecurr);
3491 }
3492
Joe Perches4dbed762017-05-08 15:55:39 -07003493# check if this appears to be the start function declaration, save the name
3494 if ($sline =~ /^\+\{\s*$/ &&
3495 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3496 $context_function = $1;
3497 }
3498
3499# check if this appears to be the end of function declaration
3500 if ($sline =~ /^\+\}\s*$/) {
3501 undef $context_function;
3502 }
3503
Joe Perches032a4c02014-08-06 16:10:29 -07003504# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003505# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003506# if the previous line is a break or return and is indented 1 tab more...
3507 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3508 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003509 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3510 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3511 defined $lines[$linenr] &&
3512 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003513 WARN("UNNECESSARY_ELSE",
3514 "else is not generally useful after a break or return\n" . $hereprev);
3515 }
3516 }
3517
Joe Perchesc00df192014-08-06 16:11:01 -07003518# check indentation of a line with a break;
3519# if the previous line is a goto or return and is indented the same # of tabs
3520 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3521 my $tabs = $1;
3522 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3523 WARN("UNNECESSARY_BREAK",
3524 "break is not useful after a goto or return\n" . $hereprev);
3525 }
3526 }
3527
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003528# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003529 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003530 WARN("CVS_KEYWORD",
3531 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003532 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003533
Joe Perches56e77d72013-02-21 16:44:14 -08003534# check for old HOTPLUG __dev<foo> section markings
3535 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3536 WARN("HOTPLUG_SECTION",
3537 "Using $1 is unnecessary\n" . $herecurr);
3538 }
3539
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003540# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003541 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3542 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003543#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003544 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003545 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003546 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003547 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003548 $stat =~ s/\n./\n /g;
3549 $cond =~ s/\n./\n /g;
3550
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003551#print "linenr<$linenr> <$stat>\n";
3552 # If this statement has no statement boundaries within
3553 # it there is no point in retrying a statement scan
3554 # until we hit end of it.
3555 my $frag = $stat; $frag =~ s/;+\s*$//;
3556 if ($frag !~ /(?:{|;)/) {
3557#print "skip<$line_nr_next>\n";
3558 $suppress_statement = $line_nr_next;
3559 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003560
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003561 # Find the real next line.
3562 $realline_next = $line_nr_next;
3563 if (defined $realline_next &&
3564 (!defined $lines[$realline_next - 1] ||
3565 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3566 $realline_next++;
3567 }
3568
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003569 my $s = $stat;
3570 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003571
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003572 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003573 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003574
3575 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003576 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003577
Andy Whitcroft463f2862009-09-21 17:04:34 -07003578 } elsif ($s =~ /^.\s*else\b/s) {
3579
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003580 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003581 } 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 -07003582 my $type = $1;
3583 $type =~ s/\s+/ /g;
3584 possible($type, "A:" . $s);
3585
Andy Whitcroft8905a672007-11-28 16:21:06 -08003586 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003587 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003588 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003589 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003590
3591 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003592 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003593 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003594 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003595
3596 # Check for any sort of function declaration.
3597 # int foo(something bar, other baz);
3598 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003599 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 -08003600 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003601
Andy Whitcroftcf655042008-03-04 14:28:20 -08003602 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003603 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003604 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003605
Andy Whitcroft8905a672007-11-28 16:21:06 -08003606 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003607 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003608
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003609 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003610 }
3611 }
3612 }
3613
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003614 }
3615
Andy Whitcroft653d4872007-06-23 17:16:34 -07003616#
3617# Checks which may be anchored in the context.
3618#
3619
3620# Check for switch () and associated case and default
3621# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003622 if ($line=~/\bswitch\s*\(.*\)/) {
3623 my $err = '';
3624 my $sep = '';
3625 my @ctx = ctx_block_outer($linenr, $realcnt);
3626 shift(@ctx);
3627 for my $ctx (@ctx) {
3628 my ($clen, $cindent) = line_stats($ctx);
3629 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3630 $indent != $cindent) {
3631 $err .= "$sep$ctx\n";
3632 $sep = '';
3633 } else {
3634 $sep = "[...]\n";
3635 }
3636 }
3637 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003638 ERROR("SWITCH_CASE_INDENT_LEVEL",
3639 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003640 }
3641 }
3642
3643# if/while/etc brace do not go on next line, unless defining a do while loop,
3644# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003645 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 -07003646 my $pre_ctx = "$1$2";
3647
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003648 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003649
3650 if ($line =~ /^\+\t{6,}/) {
3651 WARN("DEEP_INDENTATION",
3652 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3653 }
3654
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003655 my $ctx_cnt = $realcnt - $#ctx - 1;
3656 my $ctx = join("\n", @ctx);
3657
Andy Whitcroft548596d2008-07-23 21:29:01 -07003658 my $ctx_ln = $linenr;
3659 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003660
Andy Whitcroft548596d2008-07-23 21:29:01 -07003661 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3662 defined $lines[$ctx_ln - 1] &&
3663 $lines[$ctx_ln - 1] =~ /^-/)) {
3664 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3665 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003666 $ctx_ln++;
3667 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003668
Andy Whitcroft53210162008-07-23 21:29:03 -07003669 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3670 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003671
Joe Perchesd752fcc2014-08-06 16:11:05 -07003672 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003673 ERROR("OPEN_BRACE",
3674 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003675 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003676 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003677 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3678 $ctx =~ /\)\s*\;\s*$/ &&
3679 defined $lines[$ctx_ln - 1])
3680 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003681 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3682 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003683 WARN("TRAILING_SEMICOLON",
3684 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003685 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003686 }
3687 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003688 }
3689
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003690# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003691 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 -08003692 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3693 ctx_statement_block($linenr, $realcnt, 0)
3694 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003695 my ($s, $c) = ($stat, $cond);
3696
3697 substr($s, 0, length($c), '');
3698
Joe Perches9f5af482015-09-09 15:37:30 -07003699 # remove inline comments
3700 $s =~ s/$;/ /g;
3701 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003702
3703 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003704 my @newlines = ($c =~ /\n/gs);
3705 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003706
Joe Perches9f5af482015-09-09 15:37:30 -07003707 # Make sure we remove the line prefixes as we have
3708 # none on the first line, and are going to readd them
3709 # where necessary.
3710 $s =~ s/\n./\n/gs;
3711 while ($s =~ /\n\s+\\\n/) {
3712 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3713 }
3714
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003715 # We want to check the first line inside the block
3716 # starting at the end of the conditional, so remove:
3717 # 1) any blank line termination
3718 # 2) any opening brace { on end of the line
3719 # 3) any do (...) {
3720 my $continuation = 0;
3721 my $check = 0;
3722 $s =~ s/^.*\bdo\b//;
3723 $s =~ s/^\s*{//;
3724 if ($s =~ s/^\s*\\//) {
3725 $continuation = 1;
3726 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003727 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003728 $check = 1;
3729 $cond_lines++;
3730 }
3731
3732 # Also ignore a loop construct at the end of a
3733 # preprocessor statement.
3734 if (($prevline =~ /^.\s*#\s*define\s/ ||
3735 $prevline =~ /\\\s*$/) && $continuation == 0) {
3736 $check = 0;
3737 }
3738
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003739 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003740 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003741 while ($cond_ptr != $cond_lines) {
3742 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003743
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003744 # If we see an #else/#elif then the code
3745 # is not linear.
3746 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3747 $check = 0;
3748 }
3749
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003750 # Ignore:
3751 # 1) blank lines, they should be at 0,
3752 # 2) preprocessor lines, and
3753 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003754 if ($continuation ||
3755 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003756 $s =~ /^\s*#\s*?/ ||
3757 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003758 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003759 if ($s =~ s/^.*?\n//) {
3760 $cond_lines++;
3761 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003762 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003763 }
3764
3765 my (undef, $sindent) = line_stats("+" . $s);
3766 my $stat_real = raw_line($linenr, $cond_lines);
3767
3768 # Check if either of these lines are modified, else
3769 # this is not this patch's fault.
3770 if (!defined($stat_real) ||
3771 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3772 $check = 0;
3773 }
3774 if (defined($stat_real) && $cond_lines > 1) {
3775 $stat_real = "[...]\n$stat_real";
3776 }
3777
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003778 #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 -07003779
Joe Perches9f5af482015-09-09 15:37:30 -07003780 if ($check && $s ne '' &&
3781 (($sindent % 8) != 0 ||
3782 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003783 ($sindent == $indent &&
3784 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003785 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003786 WARN("SUSPECT_CODE_INDENT",
3787 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003788 }
3789 }
3790
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003791 # Track the 'values' across context and added lines.
3792 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003793 my ($curr_values, $curr_vars) =
3794 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003795 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003796 if ($dbg_values) {
3797 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003798 print "$linenr > .$outline\n";
3799 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003800 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003801 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003802 $prev_values = substr($curr_values, -1);
3803
Andy Whitcroft00df3442007-06-08 13:47:06 -07003804#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003805 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003806
Joe Perches11ca40a2016-12-12 16:46:31 -08003807# check for dereferences that span multiple lines
3808 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3809 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3810 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3811 my $ref = $1;
3812 $line =~ /^.\s*($Lval)/;
3813 $ref .= $1;
3814 $ref =~ s/\s//g;
3815 WARN("MULTILINE_DEREFERENCE",
3816 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3817 }
3818
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003819# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003820 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 -07003821 my $type = $1;
3822 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003823 $var = "" if (!defined $var);
3824 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003825 my $sign = $1;
3826 my $pointer = $2;
3827
3828 $pointer = "" if (!defined $pointer);
3829
3830 if (WARN("UNSPECIFIED_INT",
3831 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3832 $fix) {
3833 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003834 my $comp_pointer = $pointer;
3835 $comp_pointer =~ s/\s//g;
3836 $decl .= $comp_pointer;
3837 $decl = rtrim($decl) if ($var eq "");
3838 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003839 }
3840 }
3841 }
3842
Andy Whitcroft653d4872007-06-23 17:16:34 -07003843# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003844 if ($dbg_type) {
3845 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003846 ERROR("TEST_TYPE",
3847 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003848 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003849 ERROR("TEST_NOT_TYPE",
3850 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003851 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003852 next;
3853 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003854# TEST: allow direct testing of the attribute matcher.
3855 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003856 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003857 ERROR("TEST_ATTR",
3858 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003859 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003860 ERROR("TEST_NOT_ATTR",
3861 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003862 }
3863 next;
3864 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003865
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003866# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003867 if ($line =~ /^.\s*{/ &&
3868 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003869 if (ERROR("OPEN_BRACE",
3870 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003871 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3872 fix_delete_line($fixlinenr - 1, $prevrawline);
3873 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003874 my $fixedline = $prevrawline;
3875 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003876 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003877 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003878 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003879 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003880 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003881 }
3882
Andy Whitcroft653d4872007-06-23 17:16:34 -07003883#
3884# Checks which are anchored on the added line.
3885#
3886
3887# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003888 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003889 my $path = $1;
3890 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003891 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003892 "malformed #include filename\n" . $herecurr);
3893 }
3894 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3895 ERROR("UAPI_INCLUDE",
3896 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003897 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003898 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003899
3900# no C99 // comments
3901 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003902 if (ERROR("C99_COMMENTS",
3903 "do not use C99 // comments\n" . $herecurr) &&
3904 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003905 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003906 if ($line =~ /\/\/(.*)$/) {
3907 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003908 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003909 }
3910 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003911 }
3912 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003913 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003914 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003915
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003916# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3917# the whole statement.
3918#print "APW <$lines[$realline_next - 1]>\n";
3919 if (defined $realline_next &&
3920 exists $lines[$realline_next - 1] &&
3921 !defined $suppress_export{$realline_next} &&
3922 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3923 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003924 # Handle definitions which produce identifiers with
3925 # a prefix:
3926 # XXX(foo);
3927 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003928 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003929 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003930 $name =~ /^${Ident}_$2/) {
3931#print "FOO C name<$name>\n";
3932 $suppress_export{$realline_next} = 1;
3933
3934 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003935 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003936 ^.DEFINE_$Ident\(\Q$name\E\)|
3937 ^.DECLARE_$Ident\(\Q$name\E\)|
3938 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003939 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3940 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003941 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003942#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3943 $suppress_export{$realline_next} = 2;
3944 } else {
3945 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003946 }
3947 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003948 if (!defined $suppress_export{$linenr} &&
3949 $prevline =~ /^.\s*$/ &&
3950 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3951 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3952#print "FOO B <$lines[$linenr - 1]>\n";
3953 $suppress_export{$linenr} = 2;
3954 }
3955 if (defined $suppress_export{$linenr} &&
3956 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003957 WARN("EXPORT_SYMBOL",
3958 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003959 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003960
Joe Eloff5150bda2010-08-09 17:21:00 -07003961# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003962 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003963 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003964 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003965 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003966 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003967 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003968 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003969# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003970 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003971 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003972 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003973 $herecurr) &&
3974 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003975 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003976 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003977 }
3978
Joe Perches18130872014-08-06 16:11:22 -07003979# check for misordered declarations of char/short/int/long with signed/unsigned
3980 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3981 my $tmp = trim($1);
3982 WARN("MISORDERED_TYPE",
3983 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3984 }
3985
Joe Perches809e0822018-08-21 21:58:12 -07003986# check for unnecessary <signed> int declarations of short/long/long long
3987 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3988 my $type = trim($1);
3989 next if ($type !~ /\bint\b/);
3990 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3991 my $new_type = $type;
3992 $new_type =~ s/\b\s*int\s*\b/ /;
3993 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3994 $new_type =~ s/^const\s+//;
3995 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3996 $new_type = "const $new_type" if ($type =~ /^const\b/);
3997 $new_type =~ s/\s+/ /g;
3998 $new_type = trim($new_type);
3999 if (WARN("UNNECESSARY_INT",
4000 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4001 $fix) {
4002 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4003 }
4004 }
4005
Joe Perchescb710ec2010-10-26 14:23:20 -07004006# check for static const char * arrays.
4007 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004008 WARN("STATIC_CONST_CHAR_ARRAY",
4009 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004010 $herecurr);
4011 }
4012
4013# check for static char foo[] = "bar" declarations.
4014 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004015 WARN("STATIC_CONST_CHAR_ARRAY",
4016 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004017 $herecurr);
4018 }
4019
Joe Perchesab7e23f2015-04-16 12:44:22 -07004020# check for const <foo> const where <foo> is not a pointer or array type
4021 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4022 my $found = $1;
4023 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4024 WARN("CONST_CONST",
4025 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4026 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4027 WARN("CONST_CONST",
4028 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4029 }
4030 }
4031
Joe Perches9b0fa602014-04-03 14:49:18 -07004032# check for non-global char *foo[] = {"bar", ...} declarations.
4033 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4034 WARN("STATIC_CONST_CHAR_ARRAY",
4035 "char * array declaration might be better as static const\n" .
4036 $herecurr);
4037 }
4038
Joe Perchesb598b672015-04-16 12:44:36 -07004039# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4040 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4041 my $array = $1;
4042 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4043 my $array_div = $1;
4044 if (WARN("ARRAY_SIZE",
4045 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4046 $fix) {
4047 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4048 }
4049 }
4050 }
4051
Joe Perchesb36190c2014-01-27 17:07:18 -08004052# check for function declarations without arguments like "int foo()"
4053 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
4054 if (ERROR("FUNCTION_WITHOUT_ARGS",
4055 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4056 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004057 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004058 }
4059 }
4060
Andy Whitcroft653d4872007-06-23 17:16:34 -07004061# check for new typedefs, only function parameters and sparse annotations
4062# make sense.
4063 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004064 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004065 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004066 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004067 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004068 WARN("NEW_TYPEDEFS",
4069 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004070 }
4071
4072# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004073 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004074 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4075 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004076 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004077
Andy Whitcroft65863862009-01-06 14:41:21 -08004078 # Should start with a space.
4079 $to =~ s/^(\S)/ $1/;
4080 # Should not end with a space.
4081 $to =~ s/\s+$//;
4082 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004083 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004084 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004085
Joe Perches3705ce52013-07-03 15:05:31 -07004086## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004087 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004088 if (ERROR("POINTER_LOCATION",
4089 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4090 $fix) {
4091 my $sub_from = $ident;
4092 my $sub_to = $ident;
4093 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004094 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004095 s@\Q$sub_from\E@$sub_to@;
4096 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004097 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004098 }
4099 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4100 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004101 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004102
Andy Whitcroft65863862009-01-06 14:41:21 -08004103 # Should start with a space.
4104 $to =~ s/^(\S)/ $1/;
4105 # Should not end with a space.
4106 $to =~ s/\s+$//;
4107 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004108 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004109 }
4110 # Modifiers should have spaces.
4111 $to =~ s/(\b$Modifier$)/$1 /;
4112
Joe Perches3705ce52013-07-03 15:05:31 -07004113## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004114 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004115 if (ERROR("POINTER_LOCATION",
4116 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4117 $fix) {
4118
4119 my $sub_from = $match;
4120 my $sub_to = $match;
4121 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004122 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004123 s@\Q$sub_from\E@$sub_to@;
4124 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004125 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004126 }
4127
Joe Perches9d3e3c72015-09-09 15:37:27 -07004128# avoid BUG() or BUG_ON()
4129 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004130 my $msg_level = \&WARN;
4131 $msg_level = \&CHK if ($file);
4132 &{$msg_level}("AVOID_BUG",
4133 "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 -07004134 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004135
Joe Perches9d3e3c72015-09-09 15:37:27 -07004136# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004137 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004138 WARN("LINUX_VERSION_CODE",
4139 "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 -08004140 }
4141
Joe Perches17441222011-06-15 15:08:17 -07004142# check for uses of printk_ratelimit
4143 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004144 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004145 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004146 }
4147
Joe Percheseeef5732017-11-17 15:28:41 -08004148# printk should use KERN_* levels
4149 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4150 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4151 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004152 }
4153
Joe Perches243f3802012-05-31 16:26:09 -07004154 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4155 my $orig = $1;
4156 my $level = lc($orig);
4157 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004158 my $level2 = $level;
4159 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004160 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004161 "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 -07004162 }
4163
4164 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004165 if (WARN("PREFER_PR_LEVEL",
4166 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4167 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004168 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004169 s/\bpr_warning\b/pr_warn/;
4170 }
Joe Perches243f3802012-05-31 16:26:09 -07004171 }
4172
Joe Perchesdc139312013-02-21 16:44:13 -08004173 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4174 my $orig = $1;
4175 my $level = lc($orig);
4176 $level = "warn" if ($level eq "warning");
4177 $level = "dbg" if ($level eq "debug");
4178 WARN("PREFER_DEV_LEVEL",
4179 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4180 }
4181
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004182# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4183# number of false positives, but assembly files are not checked, so at
4184# least the arch entry code will not trigger this warning.
4185 if ($line =~ /\bENOSYS\b/) {
4186 WARN("ENOSYS",
4187 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4188 }
4189
Andy Whitcroft653d4872007-06-23 17:16:34 -07004190# function brace can't be on same line, except for #defines of do while,
4191# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004192 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004193 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4194 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4195 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004196 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004197 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004198 $fix) {
4199 fix_delete_line($fixlinenr, $rawline);
4200 my $fixed_line = $rawline;
4201 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4202 my $line1 = $1;
4203 my $line2 = $2;
4204 fix_insert_line($fixlinenr, ltrim($line1));
4205 fix_insert_line($fixlinenr, "\+{");
4206 if ($line2 !~ /^\s*$/) {
4207 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4208 }
4209 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004210 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004211
Andy Whitcroft8905a672007-11-28 16:21:06 -08004212# open braces for enum, union and struct go on the same line.
4213 if ($line =~ /^.\s*{/ &&
4214 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004215 if (ERROR("OPEN_BRACE",
4216 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4217 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4218 fix_delete_line($fixlinenr - 1, $prevrawline);
4219 fix_delete_line($fixlinenr, $rawline);
4220 my $fixedline = rtrim($prevrawline) . " {";
4221 fix_insert_line($fixlinenr, $fixedline);
4222 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004223 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004224 if ($fixedline !~ /^\+\s*$/) {
4225 fix_insert_line($fixlinenr, $fixedline);
4226 }
4227 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004228 }
4229
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004230# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004231 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4232 if (WARN("SPACING",
4233 "missing space after $1 definition\n" . $herecurr) &&
4234 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004235 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004236 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4237 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004238 }
4239
Joe Perches31070b52014-01-23 15:54:49 -08004240# Function pointer declarations
4241# check spacing between type, funcptr, and args
4242# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004243 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004244 my $declare = $1;
4245 my $pre_pointer_space = $2;
4246 my $post_pointer_space = $3;
4247 my $funcname = $4;
4248 my $post_funcname_space = $5;
4249 my $pre_args_space = $6;
4250
Joe Perches91f72e92014-04-03 14:49:12 -07004251# the $Declare variable will capture all spaces after the type
4252# so check it for a missing trailing missing space but pointer return types
4253# don't need a space so don't warn for those.
4254 my $post_declare_space = "";
4255 if ($declare =~ /(\s+)$/) {
4256 $post_declare_space = $1;
4257 $declare = rtrim($declare);
4258 }
4259 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004260 WARN("SPACING",
4261 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004262 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004263 }
4264
4265# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004266# This test is not currently implemented because these declarations are
4267# equivalent to
4268# int foo(int bar, ...)
4269# and this is form shouldn't/doesn't generate a checkpatch warning.
4270#
4271# elsif ($declare =~ /\s{2,}$/) {
4272# WARN("SPACING",
4273# "Multiple spaces after return type\n" . $herecurr);
4274# }
Joe Perches31070b52014-01-23 15:54:49 -08004275
4276# unnecessary space "type ( *funcptr)(args...)"
4277 if (defined $pre_pointer_space &&
4278 $pre_pointer_space =~ /^\s/) {
4279 WARN("SPACING",
4280 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4281 }
4282
4283# unnecessary space "type (* funcptr)(args...)"
4284 if (defined $post_pointer_space &&
4285 $post_pointer_space =~ /^\s/) {
4286 WARN("SPACING",
4287 "Unnecessary space before function pointer name\n" . $herecurr);
4288 }
4289
4290# unnecessary space "type (*funcptr )(args...)"
4291 if (defined $post_funcname_space &&
4292 $post_funcname_space =~ /^\s/) {
4293 WARN("SPACING",
4294 "Unnecessary space after function pointer name\n" . $herecurr);
4295 }
4296
4297# unnecessary space "type (*funcptr) (args...)"
4298 if (defined $pre_args_space &&
4299 $pre_args_space =~ /^\s/) {
4300 WARN("SPACING",
4301 "Unnecessary space before function pointer arguments\n" . $herecurr);
4302 }
4303
4304 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004305 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004306 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004307 }
4308 }
4309
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004310# check for spacing round square brackets; allowed:
4311# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004312# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4313# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004314 while ($line =~ /(.*?\s)\[/g) {
4315 my ($where, $prefix) = ($-[1], $1);
4316 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004317 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004318 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004319 if (ERROR("BRACKET_SPACE",
4320 "space prohibited before open square bracket '['\n" . $herecurr) &&
4321 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004322 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004323 s/^(\+.*?)\s+\[/$1\[/;
4324 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004325 }
4326 }
4327
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004328# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004329 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004330 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004331 my $ctx_before = substr($line, 0, $-[1]);
4332 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004333
4334 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004335 if ($name =~ /^(?:
4336 if|for|while|switch|return|case|
4337 volatile|__volatile__|
4338 __attribute__|format|__extension__|
4339 asm|__asm__)$/x)
4340 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004341 # cpp #define statements have non-optional spaces, ie
4342 # if there is a space between the name and the open
4343 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004344 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004345
4346 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004347 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004348
4349 # If this whole things ends with a type its most
4350 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004351 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004352
4353 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004354 if (WARN("SPACING",
4355 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4356 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004357 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004358 s/\b$name\s+\(/$name\(/;
4359 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004360 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004361 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004362
Andy Whitcroft653d4872007-06-23 17:16:34 -07004363# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004364 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004365 my $fixed_line = "";
4366 my $line_fixed = 0;
4367
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004368 my $ops = qr{
4369 <<=|>>=|<=|>=|==|!=|
4370 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4371 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004372 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004373 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004374 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004375 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004376
4377## print("element count: <" . $#elements . ">\n");
4378## foreach my $el (@elements) {
4379## print("el: <$el>\n");
4380## }
4381
4382 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004383 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004384
Joe Perches3705ce52013-07-03 15:05:31 -07004385 foreach my $el (@elements) {
4386 push(@fix_elements, substr($rawline, $off, length($el)));
4387 $off += length($el);
4388 }
4389
4390 $off = 0;
4391
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004392 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004393 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004394
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004395 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004396
4397 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4398
4399## print("n: <$n> good: <$good>\n");
4400
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004401 $off += length($elements[$n]);
4402
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004403 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004404 my $ca = substr($opline, 0, $off);
4405 my $cc = '';
4406 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4407 $cc = substr($opline, $off + length($elements[$n + 1]));
4408 }
4409 my $cb = "$ca$;$cc";
4410
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004411 my $a = '';
4412 $a = 'V' if ($elements[$n] ne '');
4413 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004414 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004415 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4416 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004417 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004418
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004419 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004420
4421 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004422 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004423 $c = 'V' if ($elements[$n + 2] ne '');
4424 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004425 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004426 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4427 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004428 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004429 } else {
4430 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004431 }
4432
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004433 my $ctx = "${a}x${c}";
4434
4435 my $at = "(ctx:$ctx)";
4436
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004437 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004438 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004439
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004440 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004441 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004442
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004443 # Get the full operator variant.
4444 my $opv = $op . substr($curr_vars, $off, 1);
4445
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004446 # Ignore operators passed as parameters.
4447 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004448 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004449
Andy Whitcroftcf655042008-03-04 14:28:20 -08004450# # Ignore comments
4451# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004452
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004453 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004454 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004455 if ($ctx !~ /.x[WEBC]/ &&
4456 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004457 if (ERROR("SPACING",
4458 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004459 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004460 $line_fixed = 1;
4461 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004462 }
4463
4464 # // is a comment
4465 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004466
Joe Perchesb00e4812014-04-03 14:49:33 -07004467 # : when part of a bitfield
4468 } elsif ($opv eq ':B') {
4469 # skip the bitfield test for now
4470
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004471 # No spaces for:
4472 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004473 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004474 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004475 if (ERROR("SPACING",
4476 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004477 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004478 if (defined $fix_elements[$n + 2]) {
4479 $fix_elements[$n + 2] =~ s/^\s+//;
4480 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004481 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004482 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004483 }
4484
Joe Perches23810972014-12-10 15:51:32 -08004485 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004486 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004487 my $rtrim_before = 0;
4488 my $space_after = 0;
4489 if ($ctx =~ /Wx./) {
4490 if (ERROR("SPACING",
4491 "space prohibited before that '$op' $at\n" . $hereptr)) {
4492 $line_fixed = 1;
4493 $rtrim_before = 1;
4494 }
4495 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004496 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004497 if (ERROR("SPACING",
4498 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004499 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004500 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004501 $space_after = 1;
4502 }
4503 }
4504 if ($rtrim_before || $space_after) {
4505 if ($rtrim_before) {
4506 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4507 } else {
4508 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4509 }
4510 if ($space_after) {
4511 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004512 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004513 }
4514
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004515 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004516 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004517 #warn "'*' is part of type\n";
4518
4519 # unary operators should have a space before and
4520 # none after. May be left adjacent to another
4521 # unary operator, or a cast
4522 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004523 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004524 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004525 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004526 if (ERROR("SPACING",
4527 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004528 if ($n != $last_after + 2) {
4529 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4530 $line_fixed = 1;
4531 }
Joe Perches3705ce52013-07-03 15:05:31 -07004532 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004533 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004534 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004535 # A unary '*' may be const
4536
4537 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004538 if (ERROR("SPACING",
4539 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004540 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004541 if (defined $fix_elements[$n + 2]) {
4542 $fix_elements[$n + 2] =~ s/^\s+//;
4543 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004544 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004545 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004546 }
4547
4548 # unary ++ and unary -- are allowed no space on one side.
4549 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004550 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004551 if (ERROR("SPACING",
4552 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004553 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004554 $line_fixed = 1;
4555 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004556 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004557 if ($ctx =~ /Wx[BE]/ ||
4558 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004559 if (ERROR("SPACING",
4560 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004561 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004562 $line_fixed = 1;
4563 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004564 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004565 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004566 if (ERROR("SPACING",
4567 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004568 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004569 if (defined $fix_elements[$n + 2]) {
4570 $fix_elements[$n + 2] =~ s/^\s+//;
4571 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004572 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004573 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004574 }
4575
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004576 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004577 } elsif ($op eq '<<' or $op eq '>>' or
4578 $op eq '&' or $op eq '^' or $op eq '|' or
4579 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004580 $op eq '*' or $op eq '/' or
4581 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004582 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004583 if ($check) {
4584 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4585 if (CHK("SPACING",
4586 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4587 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4588 $fix_elements[$n + 2] =~ s/^\s+//;
4589 $line_fixed = 1;
4590 }
4591 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4592 if (CHK("SPACING",
4593 "space preferred before that '$op' $at\n" . $hereptr)) {
4594 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4595 $line_fixed = 1;
4596 }
4597 }
4598 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004599 if (ERROR("SPACING",
4600 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004601 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4602 if (defined $fix_elements[$n + 2]) {
4603 $fix_elements[$n + 2] =~ s/^\s+//;
4604 }
Joe Perches3705ce52013-07-03 15:05:31 -07004605 $line_fixed = 1;
4606 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004607 }
4608
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004609 # A colon needs no spaces before when it is
4610 # terminating a case value or a label.
4611 } elsif ($opv eq ':C' || $opv eq ':L') {
4612 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004613 if (ERROR("SPACING",
4614 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004615 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004616 $line_fixed = 1;
4617 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004618 }
4619
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004620 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004621 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004622 my $ok = 0;
4623
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004624 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004625 if (($op eq '<' &&
4626 $cc =~ /^\S+\@\S+>/) ||
4627 ($op eq '>' &&
4628 $ca =~ /<\S+\@\S+$/))
4629 {
4630 $ok = 1;
4631 }
4632
Joe Perchese0df7e12015-04-16 12:44:53 -07004633 # for asm volatile statements
4634 # ignore a colon with another
4635 # colon immediately before or after
4636 if (($op eq ':') &&
4637 ($ca =~ /:$/ || $cc =~ /^:/)) {
4638 $ok = 1;
4639 }
4640
Joe Perches84731622013-11-12 15:10:05 -08004641 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004642 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004643 my $msg_level = \&ERROR;
4644 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004645
Jean Delvare0675a8f2017-09-08 16:16:07 -07004646 if (&{$msg_level}("SPACING",
4647 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004648 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4649 if (defined $fix_elements[$n + 2]) {
4650 $fix_elements[$n + 2] =~ s/^\s+//;
4651 }
Joe Perches3705ce52013-07-03 15:05:31 -07004652 $line_fixed = 1;
4653 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004654 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004655 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004656 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004657
4658## print("n: <$n> GOOD: <$good>\n");
4659
4660 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004661 }
Joe Perches3705ce52013-07-03 15:05:31 -07004662
4663 if (($#elements % 2) == 0) {
4664 $fixed_line = $fixed_line . $fix_elements[$#elements];
4665 }
4666
Joe Perches194f66f2014-08-06 16:11:03 -07004667 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4668 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004669 }
4670
4671
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004672 }
4673
Joe Perches786b6322013-07-03 15:05:32 -07004674# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004675 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004676 if (WARN("SPACING",
4677 "space prohibited before semicolon\n" . $herecurr) &&
4678 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004679 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004680 s/^(\+.*\S)\s+;/$1;/;
4681 }
4682 }
4683
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004684# check for multiple assignments
4685 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004686 CHK("MULTIPLE_ASSIGNMENTS",
4687 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004688 }
4689
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004690## # check for multiple declarations, allowing for a function declaration
4691## # continuation.
4692## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4693## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4694##
4695## # Remove any bracketed sections to ensure we do not
4696## # falsly report the parameters of functions.
4697## my $ln = $line;
4698## while ($ln =~ s/\([^\(\)]*\)//g) {
4699## }
4700## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004701## WARN("MULTIPLE_DECLARATION",
4702## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004703## }
4704## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004705
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004706#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004707 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004708 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004709 if (ERROR("SPACING",
4710 "space required before the open brace '{'\n" . $herecurr) &&
4711 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004712 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004713 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004714 }
4715
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004716## # check for blank lines before declarations
4717## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4718## $prevrawline =~ /^.\s*$/) {
4719## WARN("SPACING",
4720## "No blank lines before declarations\n" . $hereprev);
4721## }
4722##
4723
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004724# closing brace should have a space following it when it has anything
4725# on the line
4726 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004727 if (ERROR("SPACING",
4728 "space required after that close brace '}'\n" . $herecurr) &&
4729 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004730 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004731 s/}((?!(?:,|;|\)))\S)/} $1/;
4732 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004733 }
4734
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004735# check spacing on square brackets
4736 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004737 if (ERROR("SPACING",
4738 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4739 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004740 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004741 s/\[\s+/\[/;
4742 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004743 }
4744 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004745 if (ERROR("SPACING",
4746 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4747 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004748 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004749 s/\s+\]/\]/;
4750 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004751 }
4752
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004753# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004754 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Matt Wagantallb0cc4672010-06-03 12:28:18 -07004755 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004756 if (ERROR("SPACING",
4757 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4758 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004759 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004760 s/\(\s+/\(/;
4761 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004762 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004763 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004764 $line !~ /for\s*\(.*;\s+\)/ &&
4765 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004766 if (ERROR("SPACING",
4767 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4768 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004769 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004770 s/\s+\)/\)/;
4771 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004772 }
4773
Joe Perchese2826fd2014-08-06 16:10:48 -07004774# check unnecessary parentheses around addressof/dereference single $Lvals
4775# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4776
4777 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004778 my $var = $1;
4779 if (CHK("UNNECESSARY_PARENTHESES",
4780 "Unnecessary parentheses around $var\n" . $herecurr) &&
4781 $fix) {
4782 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4783 }
4784 }
4785
4786# check for unnecessary parentheses around function pointer uses
4787# ie: (foo->bar)(); should be foo->bar();
4788# but not "if (foo->bar) (" to avoid some false positives
4789 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4790 my $var = $2;
4791 if (CHK("UNNECESSARY_PARENTHESES",
4792 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4793 $fix) {
4794 my $var2 = deparenthesize($var);
4795 $var2 =~ s/\s//g;
4796 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4797 }
4798 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004799
Joe Perches63b7c732017-09-08 16:16:01 -07004800# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004801# when !drivers/staging or command-line uses --strict
4802 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004803 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004804 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4805 my $if_stat = $1;
4806 my $test = substr($2, 1, -1);
4807 my $herectx;
4808 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4809 my $match = $1;
4810 # avoid parentheses around potential macro args
4811 next if ($match =~ /^\s*\w+\s*$/);
4812 if (!defined($herectx)) {
4813 $herectx = $here . "\n";
4814 my $cnt = statement_rawlines($if_stat);
4815 for (my $n = 0; $n < $cnt; $n++) {
4816 my $rl = raw_line($linenr, $n);
4817 $herectx .= $rl . "\n";
4818 last if $rl =~ /^[ \+].*\{/;
4819 }
4820 }
4821 CHK("UNNECESSARY_PARENTHESES",
4822 "Unnecessary parentheses around '$match'\n" . $herectx);
4823 }
4824 }
4825
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004826#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004827 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004828 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004829 if (WARN("INDENTED_LABEL",
4830 "labels should not be indented\n" . $herecurr) &&
4831 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004832 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004833 s/^(.)\s+/$1/;
4834 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004835 }
4836
Joe Perches5b9553a2014-04-03 14:49:21 -07004837# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004838 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004839 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004840 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004841 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4842 my $value = $1;
4843 $value = deparenthesize($value);
4844 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4845 ERROR("RETURN_PARENTHESES",
4846 "return is not a function, parentheses are not required\n" . $herecurr);
4847 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004848 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004849 ERROR("SPACING",
4850 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004851 }
4852 }
Joe Perches507e5142013-11-12 15:10:13 -08004853
Joe Perchesb43ae212014-06-23 13:22:07 -07004854# unnecessary return in a void function
4855# at end-of-function, with the previous line a single leading tab, then return;
4856# and the line before that not a goto label target like "out:"
4857 if ($sline =~ /^[ \+]}\s*$/ &&
4858 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4859 $linenr >= 3 &&
4860 $lines[$linenr - 3] =~ /^[ +]/ &&
4861 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004862 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004863 "void function return statements are not generally useful\n" . $hereprev);
4864 }
Joe Perches9819cf22014-06-04 16:12:09 -07004865
Joe Perches189248d2014-01-23 15:54:47 -08004866# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004867 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004868 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4869 my $openparens = $1;
4870 my $count = $openparens =~ tr@\(@\(@;
4871 my $msg = "";
4872 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4873 my $comp = $4; #Not $1 because of $LvalOrFunc
4874 $msg = " - maybe == should be = ?" if ($comp eq "==");
4875 WARN("UNNECESSARY_PARENTHESES",
4876 "Unnecessary parentheses$msg\n" . $herecurr);
4877 }
4878 }
4879
Joe Perchesc5595fa2015-09-09 15:37:58 -07004880# comparisons with a constant or upper case identifier on the left
4881# avoid cases like "foo + BAR < baz"
4882# only fix matches surrounded by parentheses to avoid incorrect
4883# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004884 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004885 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4886 my $lead = $1;
4887 my $const = $2;
4888 my $comp = $3;
4889 my $to = $4;
4890 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004891 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004892 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4893 WARN("CONSTANT_COMPARISON",
4894 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4895 $fix) {
4896 if ($comp eq "<") {
4897 $newcomp = ">";
4898 } elsif ($comp eq "<=") {
4899 $newcomp = ">=";
4900 } elsif ($comp eq ">") {
4901 $newcomp = "<";
4902 } elsif ($comp eq ">=") {
4903 $newcomp = "<=";
4904 }
4905 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4906 }
4907 }
4908
Joe Perchesf34e4a42015-04-16 12:44:19 -07004909# Return of what appears to be an errno should normally be negative
4910 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004911 my $name = $1;
4912 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004913 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004914 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004915 }
4916 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004917
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004918# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004919 if ($line =~ /\b(if|while|for|switch)\(/) {
4920 if (ERROR("SPACING",
4921 "space required before the open parenthesis '('\n" . $herecurr) &&
4922 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004923 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004924 s/\b(if|while|for|switch)\(/$1 \(/;
4925 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004926 }
4927
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004928# Check for illegal assignment in if conditional -- and check for trailing
4929# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004930 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004931 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4932 ctx_statement_block($linenr, $realcnt, 0)
4933 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004934 my ($stat_next) = ctx_statement_block($line_nr_next,
4935 $remain_next, $off_next);
4936 $stat_next =~ s/\n./\n /g;
4937 ##print "stat<$stat> stat_next<$stat_next>\n";
4938
4939 if ($stat_next =~ /^\s*while\b/) {
4940 # If the statement carries leading newlines,
4941 # then count those as offsets.
4942 my ($whitespace) =
4943 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4944 my $offset =
4945 statement_rawlines($whitespace) - 1;
4946
4947 $suppress_whiletrailers{$line_nr_next +
4948 $offset} = 1;
4949 }
4950 }
4951 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004952 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004953 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004954 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004955
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004956 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004957 ERROR("ASSIGN_IN_IF",
4958 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004959 }
4960
4961 # Find out what is on the end of the line after the
4962 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004963 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004964 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004965 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004966 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4967 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004968 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004969 # Find out how long the conditional actually is.
4970 my @newlines = ($c =~ /\n/gs);
4971 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004972 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004973
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004974 $stat_real = raw_line($linenr, $cond_lines)
4975 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004976 if (defined($stat_real) && $cond_lines > 1) {
4977 $stat_real = "[...]\n$stat_real";
4978 }
4979
Joe Perches000d1cc12011-07-25 17:13:25 -07004980 ERROR("TRAILING_STATEMENTS",
4981 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004982 }
4983 }
4984
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004985# Check for bitwise tests written as boolean
4986 if ($line =~ /
4987 (?:
4988 (?:\[|\(|\&\&|\|\|)
4989 \s*0[xX][0-9]+\s*
4990 (?:\&\&|\|\|)
4991 |
4992 (?:\&\&|\|\|)
4993 \s*0[xX][0-9]+\s*
4994 (?:\&\&|\|\||\)|\])
4995 )/x)
4996 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004997 WARN("HEXADECIMAL_BOOLEAN_TEST",
4998 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004999 }
5000
Andy Whitcroft8905a672007-11-28 16:21:06 -08005001# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005002 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5003 my $s = $1;
5004 $s =~ s/$;//g; # Remove any comments
5005 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005006 ERROR("TRAILING_STATEMENTS",
5007 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005008 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005009 }
Andy Whitcroft39667782009-01-15 13:51:06 -08005010# if should not continue a brace
5011 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005012 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005013 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005014 $herecurr);
5015 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005016# case and default should not have general statements after them
5017 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5018 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005019 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005020 \s*return\s+
5021 )/xg)
5022 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005023 ERROR("TRAILING_STATEMENTS",
5024 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005025 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005026
5027 # Check for }<nl>else {, these must be at the same
5028 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005029 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5030 $previndent == $indent) {
5031 if (ERROR("ELSE_AFTER_BRACE",
5032 "else should follow close brace '}'\n" . $hereprev) &&
5033 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5034 fix_delete_line($fixlinenr - 1, $prevrawline);
5035 fix_delete_line($fixlinenr, $rawline);
5036 my $fixedline = $prevrawline;
5037 $fixedline =~ s/}\s*$//;
5038 if ($fixedline !~ /^\+\s*$/) {
5039 fix_insert_line($fixlinenr, $fixedline);
5040 }
5041 $fixedline = $rawline;
5042 $fixedline =~ s/^(.\s*)else/$1} else/;
5043 fix_insert_line($fixlinenr, $fixedline);
5044 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005045 }
5046
Joe Perches8b8856f2014-08-06 16:11:14 -07005047 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5048 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005049 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5050
5051 # Find out what is on the end of the line after the
5052 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005053 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005054 $s =~ s/\n.*//g;
5055
5056 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005057 if (ERROR("WHILE_AFTER_BRACE",
5058 "while should follow close brace '}'\n" . $hereprev) &&
5059 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5060 fix_delete_line($fixlinenr - 1, $prevrawline);
5061 fix_delete_line($fixlinenr, $rawline);
5062 my $fixedline = $prevrawline;
5063 my $trailing = $rawline;
5064 $trailing =~ s/^\+//;
5065 $trailing = trim($trailing);
5066 $fixedline =~ s/}\s*$/} $trailing/;
5067 fix_insert_line($fixlinenr, $fixedline);
5068 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005069 }
5070 }
5071
Joe Perches95e2c602013-07-03 15:05:20 -07005072#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005073 while ($line =~ m{($Constant|$Lval)}g) {
5074 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005075
5076#gcc binary extension
5077 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005078 if (WARN("GCC_BINARY_CONSTANT",
5079 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
5080 $fix) {
5081 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07005082 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07005083 s/\b$var\b/$hexval/;
5084 }
Joe Perches95e2c602013-07-03 15:05:20 -07005085 }
5086
5087#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005088 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005089 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005090#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005091 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005092#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08005093 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
5094#Ignore some three character SI units explicitly, like MiB and KHz
5095 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005096 while ($var =~ m{($Ident)}g) {
5097 my $word = $1;
5098 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005099 if ($check) {
5100 seed_camelcase_includes();
5101 if (!$file && !$camelcase_file_seeded) {
5102 seed_camelcase_file($realfile);
5103 $camelcase_file_seeded = 1;
5104 }
5105 }
Joe Perches7e781f62013-09-11 14:23:55 -07005106 if (!defined $camelcase{$word}) {
5107 $camelcase{$word} = 1;
5108 CHK("CAMELCASE",
5109 "Avoid CamelCase: <$word>\n" . $herecurr);
5110 }
Joe Perches34456862013-07-03 15:05:34 -07005111 }
Joe Perches323c1262012-12-17 16:02:07 -08005112 }
5113 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005114
5115#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005116 if ($line =~ /\#\s*define.*\\\s+$/) {
5117 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5118 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5119 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005120 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005121 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005122 }
5123
Fabian Frederick0e212e02015-04-16 12:44:25 -07005124# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5125# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005126 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005127 my $file = "$1.h";
5128 my $checkfile = "include/linux/$file";
5129 if (-f "$root/$checkfile" &&
5130 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005131 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005132 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005133 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5134 if ($asminclude > 0) {
5135 if ($realfile =~ m{^arch/}) {
5136 CHK("ARCH_INCLUDE_LINUX",
5137 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5138 } else {
5139 WARN("INCLUDE_LINUX",
5140 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5141 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005142 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005143 }
5144 }
5145
Andy Whitcroft653d4872007-06-23 17:16:34 -07005146# multi-statement macros should be enclosed in a do while loop, grab the
5147# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005148# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005149 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5150 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005151 my $ln = $linenr;
Gregory Beanc77f1b02011-05-11 09:11:12 -07005152 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005153 my ($off, $dstat, $dcond, $rest);
5154 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005155 my $has_flow_statement = 0;
5156 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005157 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005158 ctx_statement_block($linenr, $realcnt, 0);
5159 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005160 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005161 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005162
Joe Perches08a28432014-10-13 15:51:55 -07005163 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005164 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005165
Joe Perchesf59b64b2016-10-11 13:52:08 -07005166 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5167 my $define_args = $1;
5168 my $define_stmt = $dstat;
5169 my @def_args = ();
5170
5171 if (defined $define_args && $define_args ne "") {
5172 $define_args = substr($define_args, 1, length($define_args) - 2);
5173 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005174 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005175 @def_args = split(",", $define_args);
5176 }
5177
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005178 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005179 $dstat =~ s/\\\n.//g;
5180 $dstat =~ s/^\s*//s;
5181 $dstat =~ s/\s*$//s;
5182
5183 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005184 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5185 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005186 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005187 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005188 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005189
Gregory Beanc77f1b02011-05-11 09:11:12 -07005190 # Extremely long macros may fall off the end of the
5191 # available context without closing. Give a dangling
5192 # backslash the benefit of the doubt and allow it
5193 # to gobble any hanging open-parens.
5194 $dstat =~ s/\(.+\\$/1/;
5195
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005196 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07005197 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5198 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005199 {
5200 }
5201
Joe Perches42e15292016-03-15 14:58:01 -07005202 # Make asm volatile uses seem like a generic function
5203 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5204
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005205 my $exceptions = qr{
5206 $Declare|
5207 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005208 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005209 DECLARE_PER_CPU|
5210 DEFINE_PER_CPU|
Matt Wagantallb0cc4672010-06-03 12:28:18 -07005211 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005212 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005213 union|
5214 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005215 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005216 ^\"|\"$|
5217 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005218 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005219 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005220
5221 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005222 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005223 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005224
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005225 if ($dstat ne '' &&
5226 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5227 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005228 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005229 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005230 $dstat !~ /$exceptions/ &&
5231 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005232 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005233 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005234 $dstat !~ /^for\s*$Constant$/ && # for (...)
5235 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5236 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005237 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005238 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005239 {
Joe Perchese7955562017-05-08 15:55:48 -07005240 if ($dstat =~ /^\s*if\b/) {
5241 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5242 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5243 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005244 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5245 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5246 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005247 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005248 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005249 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005250
5251 }
Joe Perches52076492016-10-11 13:52:14 -07005252
5253 # Make $define_stmt single line, comment-free, etc
5254 my @stmt_array = split('\n', $define_stmt);
5255 my $first = 1;
5256 $define_stmt = "";
5257 foreach my $l (@stmt_array) {
5258 $l =~ s/\\$//;
5259 if ($first) {
5260 $define_stmt = $l;
5261 $first = 0;
5262 } elsif ($l =~ /^[\+ ]/) {
5263 $define_stmt .= substr($l, 1);
5264 }
5265 }
5266 $define_stmt =~ s/$;//g;
5267 $define_stmt =~ s/\s+/ /g;
5268 $define_stmt = trim($define_stmt);
5269
Joe Perchesf59b64b2016-10-11 13:52:08 -07005270# check if any macro arguments are reused (ignore '...' and 'type')
5271 foreach my $arg (@def_args) {
5272 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005273 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005274 my $tmp_stmt = $define_stmt;
5275 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
5276 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5277 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005278 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005279 if ($use_cnt > 1) {
5280 CHK("MACRO_ARG_REUSE",
5281 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005282 }
5283# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005284 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005285 ((defined($1) && $1 ne ',') ||
5286 (defined($2) && $2 ne ','))) {
5287 CHK("MACRO_ARG_PRECEDENCE",
5288 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005289 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005290 }
Joe Perches5023d342012-12-17 16:01:47 -08005291
Joe Perches08a28432014-10-13 15:51:55 -07005292# check for macros with flow control, but without ## concatenation
5293# ## concatenation is commonly a macro that defines a function so ignore those
5294 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005295 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005296 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005297
Joe Perches08a28432014-10-13 15:51:55 -07005298 WARN("MACRO_WITH_FLOW_CONTROL",
5299 "Macros with flow control statements should be avoided\n" . "$herectx");
5300 }
5301
Joe Perches481eb482012-12-17 16:01:56 -08005302# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005303
5304 } else {
5305 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005306 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5307 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005308 $line =~ /^\+.*\\$/) {
5309 WARN("LINE_CONTINUATIONS",
5310 "Avoid unnecessary line continuations\n" . $herecurr);
5311 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005312 }
5313
Joe Perchesb13edf72012-07-30 14:41:24 -07005314# do {} while (0) macro tests:
5315# single-statement macros do not need to be enclosed in do while (0) loop,
5316# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005317 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005318 $realfile !~ m@/vmlinux.lds.h$@ &&
5319 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5320 my $ln = $linenr;
5321 my $cnt = $realcnt;
5322 my ($off, $dstat, $dcond, $rest);
5323 my $ctx = '';
5324 ($dstat, $dcond, $ln, $cnt, $off) =
5325 ctx_statement_block($linenr, $realcnt, 0);
5326 $ctx = $dstat;
5327
5328 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005329 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005330
5331 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5332 my $stmts = $2;
5333 my $semis = $3;
5334
5335 $ctx =~ s/\n*$//;
5336 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005337 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005338
Joe Perchesac8e97f2012-08-21 16:15:53 -07005339 if (($stmts =~ tr/;/;/) == 1 &&
5340 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005341 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5342 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5343 }
5344 if (defined $semis && $semis ne "") {
5345 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5346 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5347 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005348 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5349 $ctx =~ s/\n*$//;
5350 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005351 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005352
5353 WARN("TRAILING_SEMICOLON",
5354 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005355 }
5356 }
5357
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005358# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005359 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5360 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005361 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005362 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005363 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5364 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005365 my @allowed = ();
5366 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005367 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005368 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005369 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005370 for my $chunk (@chunks) {
5371 my ($cond, $block) = @{$chunk};
5372
Andy Whitcroft773647a2008-03-28 14:15:58 -07005373 # If the condition carries leading newlines, then count those as offsets.
5374 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5375 my $offset = statement_rawlines($whitespace) - 1;
5376
Joe Perchesaad4f612012-03-23 15:02:19 -07005377 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005378 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5379
5380 # We have looked at and allowed this specific line.
5381 $suppress_ifbraces{$ln + $offset} = 1;
5382
5383 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005384 $ln += statement_rawlines($block) - 1;
5385
Andy Whitcroft773647a2008-03-28 14:15:58 -07005386 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005387
5388 $seen++ if ($block =~ /^\s*{/);
5389
Joe Perchesaad4f612012-03-23 15:02:19 -07005390 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005391 if (statement_lines($cond) > 1) {
5392 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005393 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005394 }
5395 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005396 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005397 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005398 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005399 if (statement_block_size($block) > 1) {
5400 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005401 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005402 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005403 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005404 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005405 if ($seen) {
5406 my $sum_allowed = 0;
5407 foreach (@allowed) {
5408 $sum_allowed += $_;
5409 }
5410 if ($sum_allowed == 0) {
5411 WARN("BRACES",
5412 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5413 } elsif ($sum_allowed != $allow &&
5414 $seen != $allow) {
5415 CHK("BRACES",
5416 "braces {} should be used on all arms of this statement\n" . $herectx);
5417 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005418 }
5419 }
5420 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005421 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005422 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005423 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005424
Andy Whitcroftcf655042008-03-04 14:28:20 -08005425 # Check the pre-context.
5426 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5427 #print "APW: ALLOWED: pre<$1>\n";
5428 $allowed = 1;
5429 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005430
5431 my ($level, $endln, @chunks) =
5432 ctx_statement_full($linenr, $realcnt, $-[0]);
5433
Andy Whitcroftcf655042008-03-04 14:28:20 -08005434 # Check the condition.
5435 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005436 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005437 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005438 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005439 }
5440 if (statement_lines($cond) > 1) {
5441 #print "APW: ALLOWED: cond<$cond>\n";
5442 $allowed = 1;
5443 }
5444 if ($block =~/\b(?:if|for|while)\b/) {
5445 #print "APW: ALLOWED: block<$block>\n";
5446 $allowed = 1;
5447 }
5448 if (statement_block_size($block) > 1) {
5449 #print "APW: ALLOWED: lines block<$block>\n";
5450 $allowed = 1;
5451 }
5452 # Check the post-context.
5453 if (defined $chunks[1]) {
5454 my ($cond, $block) = @{$chunks[1]};
5455 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005456 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005457 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005458 if ($block =~ /^\s*\{/) {
5459 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5460 $allowed = 1;
5461 }
5462 }
5463 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005464 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005465 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005466
Joe Perches000d1cc12011-07-25 17:13:25 -07005467 WARN("BRACES",
5468 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005469 }
5470 }
5471
Joe Perchese4c5bab2017-02-24 15:01:41 -08005472# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005473 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5474 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005475 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5476 }
5477
Joe Perches0979ae62012-12-17 16:01:59 -08005478# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005479 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005480 if (CHK("BRACES",
5481 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5482 $fix && $prevrawline =~ /^\+/) {
5483 fix_delete_line($fixlinenr - 1, $prevrawline);
5484 }
Joe Perches0979ae62012-12-17 16:01:59 -08005485 }
Joe Perches77b9a532013-07-03 15:05:29 -07005486 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005487 if (CHK("BRACES",
5488 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5489 $fix) {
5490 fix_delete_line($fixlinenr, $rawline);
5491 }
Joe Perches0979ae62012-12-17 16:01:59 -08005492 }
5493
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005494# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005495 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5496 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005497 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005498 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005499 }
5500
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005501# Check for user-visible strings broken across lines, which breaks the ability
5502# to grep for the string. Make exceptions when the previous string ends in a
5503# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5504# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005505 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005506 $prevline =~ /"\s*$/ &&
5507 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5508 if (WARN("SPLIT_STRING",
5509 "quoted string split across lines\n" . $hereprev) &&
5510 $fix &&
5511 $prevrawline =~ /^\+.*"\s*$/ &&
5512 $last_coalesced_string_linenr != $linenr - 1) {
5513 my $extracted_string = get_quoted_string($line, $rawline);
5514 my $comma_close = "";
5515 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5516 $comma_close = $1;
5517 }
5518
5519 fix_delete_line($fixlinenr - 1, $prevrawline);
5520 fix_delete_line($fixlinenr, $rawline);
5521 my $fixedline = $prevrawline;
5522 $fixedline =~ s/"\s*$//;
5523 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5524 fix_insert_line($fixlinenr - 1, $fixedline);
5525 $fixedline = $rawline;
5526 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5527 if ($fixedline !~ /\+\s*$/) {
5528 fix_insert_line($fixlinenr, $fixedline);
5529 }
5530 $last_coalesced_string_linenr = $linenr;
5531 }
5532 }
5533
5534# check for missing a space in a string concatenation
5535 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5536 WARN('MISSING_SPACE',
5537 "break quoted strings at a space character\n" . $hereprev);
5538 }
5539
Joe Perchese4b7d302017-05-08 15:55:51 -07005540# check for an embedded function name in a string when the function is known
5541# This does not work very well for -f --file checking as it depends on patch
5542# context providing the function name or a single line form for in-file
5543# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005544 if ($line =~ /^\+.*$String/ &&
5545 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005546 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5547 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005548 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005549 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005550 }
5551
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005552# check for spaces before a quoted newline
5553 if ($rawline =~ /^.*\".*\s\\n/) {
5554 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5555 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5556 $fix) {
5557 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5558 }
5559
5560 }
5561
Joe Perchesf17dba42014-10-13 15:51:51 -07005562# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005563 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5564 if (CHK("CONCATENATED_STRING",
5565 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5566 $fix) {
5567 while ($line =~ /($String)/g) {
5568 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5569 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5570 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5571 }
5572 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005573 }
5574
Joe Perches90ad30e2014-12-10 15:51:59 -08005575# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005576 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005577 if (WARN("STRING_FRAGMENTS",
5578 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5579 $fix) {
5580 while ($line =~ /($String)(?=\s*")/g) {
5581 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5582 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5583 }
5584 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005585 }
5586
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005587# check for non-standard and hex prefixed decimal printf formats
5588 my $show_L = 1; #don't show the same defect twice
5589 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005590 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005591 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005592 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005593 # check for %L
5594 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005595 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005596 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5597 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005598 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005599 # check for %Z
5600 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5601 WARN("PRINTF_Z",
5602 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5603 $show_Z = 0;
5604 }
5605 # check for 0x<decimal>
5606 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5607 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005608 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5609 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005610 }
5611
5612# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005613 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005614 WARN("LINE_CONTINUATIONS",
5615 "Avoid line continuations in quoted strings\n" . $herecurr);
5616 }
5617
Gregory Bean6b7fd1b2011-03-17 14:18:20 -07005618# sys_open/read/write/close are not allowed in the kernel
5619 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
5620 ERROR("FILE_OPS",
5621 "$1 is inappropriate in kernel code.\n" .
5622 $herecurr);
5623 }
5624
Gregory Beanca9aed12011-06-07 08:06:45 -07005625# filp_open is a backdoor for sys_open
5626 if ($line =~ /\b(filp_open)\b/) {
5627 ERROR("FILE_OPS",
5628 "$1 is inappropriate in kernel code.\n" .
5629 $herecurr);
5630 }
5631
Gregory Bean08d3bb42011-05-02 13:50:35 -07005632# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
5633 if ($line =~ /\b((?:read|write)[bwl])\b/) {
5634 ERROR("NON_RELAXED_IO",
5635 "Use of $1 is deprecated: use $1_relaxed\n\t" .
5636 "with appropriate memory barriers instead.\n" .
5637 $herecurr);
5638 }
5639
5640# likewise, in/out[bwl] should be __raw_read/write[bwl]...
5641 if ($line =~ /\b((in|out)([bwl]))\b/) {
5642 my ($all, $pref, $suf) = ($1, $2, $3);
5643 $pref =~ s/in/read/;
5644 $pref =~ s/out/write/;
5645 ERROR("NON_RELAXED_IO",
5646 "Use of $all is deprecated: use " .
5647 "__raw_$pref$suf\n\t" .
5648 "with appropriate memory barriers instead.\n" .
5649 $herecurr);
5650 }
5651
Gregory Bean9a4f09a2011-06-15 09:32:38 -07005652# dsb is too ARMish, and should usually be mb.
Sarangdhar Joshi5accaae2015-10-15 14:21:47 -07005653 if ($line =~ /[^-_>*\.]\bdsb\b[^-_\.;]/) {
Gregory Bean9a4f09a2011-06-15 09:32:38 -07005654 WARN("ARM_BARRIER",
5655 "Use of dsb is discouranged: prefer mb.\n" .
5656 $herecurr);
5657 }
5658
Gregory Bean58794002011-05-10 12:34:09 -07005659# unbounded string functions are overflow risks
5660 my %str_fns = (
5661 "sprintf" => "snprintf",
5662 "strcpy" => "strlcpy",
5663 "strncpy" => "strlcpy",
5664 "strcat" => "strlcat",
5665 "strncat" => "strlcat",
5666 "vsprintf" => "vsnprintf",
5667 "strchr" => "strnchr",
5668 "strstr" => "strnstr",
5669 );
5670 foreach my $k (keys %str_fns) {
5671 if ($line =~ /\b$k\b/) {
5672 ERROR("UNBOUNDED_STRING_FNS",
5673 "Use of $k is deprecated: " .
5674 "use $str_fns{$k} instead.\n" .
5675 $herecurr);
5676 }
5677 }
5678
Andy Whitcroft00df3442007-06-08 13:47:06 -07005679# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005680 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Abhijeet Dharmapurikare63245d2009-10-01 12:01:44 -07005681 WARN("IF_0",
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005682 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
Abhijeet Dharmapurikare63245d2009-10-01 12:01:44 -07005683 }
5684
5685# warn about #if 1
5686 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5687 WARN("IF_1",
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005688 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005689 }
5690
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005691# check for needless "if (<foo>) fn(<foo>)" uses
5692 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005693 my $tested = quotemeta($1);
5694 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5695 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5696 my $func = $1;
5697 if (WARN('NEEDLESS_IF',
5698 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5699 $fix) {
5700 my $do_fix = 1;
5701 my $leading_tabs = "";
5702 my $new_leading_tabs = "";
5703 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5704 $leading_tabs = $1;
5705 } else {
5706 $do_fix = 0;
5707 }
5708 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5709 $new_leading_tabs = $1;
5710 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5711 $do_fix = 0;
5712 }
5713 } else {
5714 $do_fix = 0;
5715 }
5716 if ($do_fix) {
5717 fix_delete_line($fixlinenr - 1, $prevrawline);
5718 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5719 }
5720 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005721 }
5722 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005723
Joe Perchesebfdc402014-08-06 16:10:27 -07005724# check for unnecessary "Out of Memory" messages
5725 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5726 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5727 (defined $1 || defined $3) &&
5728 $linenr > 3) {
5729 my $testval = $2;
5730 my $testline = $lines[$linenr - 3];
5731
5732 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5733# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5734
Joe Perchesfb0d0e02017-07-10 15:52:04 -07005735 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|kmemdup|(?:dev_)?alloc_skb)/) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005736 WARN("OOM_MESSAGE",
5737 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5738 }
5739 }
5740
Joe Perchesf78d98f2014-10-13 15:52:01 -07005741# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005742 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005743 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5744 my $level = $1;
5745 if (WARN("UNNECESSARY_KERN_LEVEL",
5746 "Possible unnecessary $level\n" . $herecurr) &&
5747 $fix) {
5748 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5749 }
5750 }
5751
Joe Perches45c55e92017-02-24 15:01:31 -08005752# check for logging continuations
5753 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5754 WARN("LOGGING_CONTINUATION",
5755 "Avoid logging continuation uses where feasible\n" . $herecurr);
5756 }
5757
Joe Perchesabb08a52014-12-10 15:51:46 -08005758# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005759 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005760 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5761 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5762 WARN("MASK_THEN_SHIFT",
5763 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5764 }
5765
Joe Perchesb75ac612014-12-10 15:52:02 -08005766# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005767 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005768 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5769 my $val = $1;
5770 my $equal = "!";
5771 $equal = "" if ($4 eq "!=");
5772 if (CHK("COMPARISON_TO_NULL",
5773 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5774 $fix) {
5775 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5776 }
5777 }
5778 }
5779
Joe Perches8716de32013-09-11 14:24:05 -07005780# check for bad placement of section $InitAttribute (e.g.: __initdata)
5781 if ($line =~ /(\b$InitAttribute\b)/) {
5782 my $attr = $1;
5783 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5784 my $ptr = $1;
5785 my $var = $2;
5786 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5787 ERROR("MISPLACED_INIT",
5788 "$attr should be placed after $var\n" . $herecurr)) ||
5789 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5790 WARN("MISPLACED_INIT",
5791 "$attr should be placed after $var\n" . $herecurr))) &&
5792 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005793 $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 -07005794 }
5795 }
5796 }
5797
Joe Perchese970b8842013-11-12 15:10:10 -08005798# check for $InitAttributeData (ie: __initdata) with const
5799 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5800 my $attr = $1;
5801 $attr =~ /($InitAttributePrefix)(.*)/;
5802 my $attr_prefix = $1;
5803 my $attr_type = $2;
5804 if (ERROR("INIT_ATTRIBUTE",
5805 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5806 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005807 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005808 s/$InitAttributeData/${attr_prefix}initconst/;
5809 }
5810 }
5811
5812# check for $InitAttributeConst (ie: __initconst) without const
5813 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5814 my $attr = $1;
5815 if (ERROR("INIT_ATTRIBUTE",
5816 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5817 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005818 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005819 /(^\+\s*(?:static\s+))/;
5820 $lead = rtrim($1);
5821 $lead = "$lead " if ($lead !~ /^\+$/);
5822 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005823 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005824 }
5825 }
5826
Joe Perchesc17893c2015-04-16 12:44:42 -07005827# check for __read_mostly with const non-pointer (should just be const)
5828 if ($line =~ /\b__read_mostly\b/ &&
5829 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5830 if (ERROR("CONST_READ_MOSTLY",
5831 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5832 $fix) {
5833 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5834 }
5835 }
5836
Joe Perchesfbdb8132014-04-03 14:49:14 -07005837# don't use __constant_<foo> functions outside of include/uapi/
5838 if ($realfile !~ m@^include/uapi/@ &&
5839 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5840 my $constant_func = $1;
5841 my $func = $constant_func;
5842 $func =~ s/^__constant_//;
5843 if (WARN("CONSTANT_CONVERSION",
5844 "$constant_func should be $func\n" . $herecurr) &&
5845 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005846 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005847 }
5848 }
5849
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005850# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005851 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005852 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005853 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005854 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005855 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005856 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5857 }
5858 if ($delay > 2000) {
5859 WARN("LONG_UDELAY",
5860 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005861 }
5862 }
5863
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005864# warn about unexpectedly long msleep's
5865 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5866 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005867 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005868 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005869 }
5870 }
5871
Joe Perches36ec1932013-07-03 15:05:25 -07005872# check for comparisons of jiffies
5873 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5874 WARN("JIFFIES_COMPARISON",
5875 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5876 }
5877
Joe Perches9d7a34a2013-07-03 15:05:26 -07005878# check for comparisons of get_jiffies_64()
5879 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5880 WARN("JIFFIES_COMPARISON",
5881 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5882 }
5883
Israel Schlesinger5d376962010-07-21 13:34:18 -07005884# check the patch for use of mdelay
5885 if ($line =~ /\bmdelay\s*\(/) {
5886 WARN("MDELAY",
5887 "use of mdelay() found: msleep() is the preferred API.\n" . $herecurr );
5888 }
5889
Andy Whitcroft00df3442007-06-08 13:47:06 -07005890# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005891# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005892# print "#ifdef in C files should be avoided\n";
5893# print "$herecurr";
5894# $clean = 0;
5895# }
5896
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005897# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005898 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005899 if (ERROR("SPACING",
5900 "exactly one space required after that #$1\n" . $herecurr) &&
5901 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005902 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005903 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5904 }
5905
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005906 }
5907
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005908# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005909 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5910 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005911 my $which = $1;
5912 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005913 CHK("UNCOMMENTED_DEFINITION",
5914 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005915 }
5916 }
5917# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005918
5919 my $barriers = qr{
5920 mb|
5921 rmb|
5922 wmb|
5923 read_barrier_depends
5924 }x;
5925 my $barrier_stems = qr{
5926 mb__before_atomic|
5927 mb__after_atomic|
5928 store_release|
5929 load_acquire|
5930 store_mb|
5931 (?:$barriers)
5932 }x;
5933 my $all_barriers = qr{
5934 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005935 smp_(?:$barrier_stems)|
5936 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005937 }x;
5938
5939 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005940 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005941 WARN("MEMORY_BARRIER",
5942 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005943 }
5944 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005945
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005946 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5947
5948 if ($realfile !~ m@^include/asm-generic/@ &&
5949 $realfile !~ m@/barrier\.h$@ &&
5950 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5951 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5952 WARN("MEMORY_BARRIER",
5953 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5954 }
5955
Joe Perchescb426e92015-06-25 15:02:46 -07005956# check for waitqueue_active without a comment.
5957 if ($line =~ /\bwaitqueue_active\s*\(/) {
5958 if (!ctx_has_comment($first_line, $linenr)) {
5959 WARN("WAITQUEUE_ACTIVE",
5960 "waitqueue_active without comment\n" . $herecurr);
5961 }
5962 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005963
Paul E. McKenney91db2592017-11-27 09:37:35 -08005964# check for smp_read_barrier_depends and read_barrier_depends
5965 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5966 WARN("READ_BARRIER_DEPENDS",
5967 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5968 }
5969
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005970# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005971 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005972 CHK("ARCH_DEFINES",
5973 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005974 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005975
Joe Perches596ed452017-07-12 14:37:02 -07005976# check that the storage class is not after a type
5977 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005978 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005979 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5980 }
5981# Check that the storage class is at the beginning of a declaration
5982 if ($line =~ /\b$Storage\b/ &&
5983 $line !~ /^.\s*$Storage/ &&
5984 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5985 $1 !~ /[\,\)]\s*$/) {
5986 WARN("STORAGE_CLASS",
5987 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005988 }
5989
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005990# check the location of the inline attribute, that it is between
5991# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005992 if ($line =~ /\b$Type\s+$Inline\b/ ||
5993 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005994 ERROR("INLINE_LOCATION",
5995 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005996 }
5997
Andy Whitcroft8905a672007-11-28 16:21:06 -08005998# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005999 if ($realfile !~ m@\binclude/uapi/@ &&
6000 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006001 if (WARN("INLINE",
6002 "plain inline is preferred over $1\n" . $herecurr) &&
6003 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006004 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006005
6006 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006007 }
6008
Joe Perches3d130fd2011-01-12 17:00:00 -08006009# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08006010 if ($realfile !~ m@\binclude/uapi/@ &&
6011 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006012 WARN("PREFER_PACKED",
6013 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08006014 }
6015
Joe Perches39b7e282011-07-25 17:13:24 -07006016# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08006017 if ($realfile !~ m@\binclude/uapi/@ &&
6018 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006019 WARN("PREFER_ALIGNED",
6020 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07006021 }
6022
Joe Perches5f14d3b2012-01-10 15:09:52 -08006023# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08006024 if ($realfile !~ m@\binclude/uapi/@ &&
6025 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006026 if (WARN("PREFER_PRINTF",
6027 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6028 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006029 $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 -07006030
6031 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08006032 }
6033
Joe Perches6061d942012-03-23 15:02:16 -07006034# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08006035 if ($realfile !~ m@\binclude/uapi/@ &&
6036 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006037 if (WARN("PREFER_SCANF",
6038 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6039 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006040 $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 -07006041 }
Joe Perches6061d942012-03-23 15:02:16 -07006042 }
6043
Joe Perches619a9082014-12-10 15:51:35 -08006044# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07006045 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08006046 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6047 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6048 $line =~ /\b__weak\b/)) {
6049 ERROR("WEAK_DECLARATION",
6050 "Using weak declarations can have unintended link defects\n" . $herecurr);
6051 }
6052
Tomas Winklerfd39f902016-12-12 16:46:34 -08006053# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07006054 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08006055 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07006056 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6057 my $type = $1;
6058 if ($type =~ /\b($typeC99Typedefs)\b/) {
6059 $type = $1;
6060 my $kernel_type = 'u';
6061 $kernel_type = 's' if ($type =~ /^_*[si]/);
6062 $type =~ /(\d+)/;
6063 $kernel_type .= $1;
6064 if (CHK("PREFER_KERNEL_TYPES",
6065 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6066 $fix) {
6067 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6068 }
6069 }
6070 }
6071
Joe Perches938224b2016-01-20 14:59:15 -08006072# check for cast of C90 native int or longer types constants
6073 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6074 my $cast = $1;
6075 my $const = $2;
6076 if (WARN("TYPECAST_INT_CONSTANT",
6077 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6078 $fix) {
6079 my $suffix = "";
6080 my $newconst = $const;
6081 $newconst =~ s/${Int_type}$//;
6082 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6083 if ($cast =~ /\blong\s+long\b/) {
6084 $suffix .= 'LL';
6085 } elsif ($cast =~ /\blong\b/) {
6086 $suffix .= 'L';
6087 }
6088 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6089 }
6090 }
6091
Joe Perches8f53a9b2010-03-05 13:43:48 -08006092# check for sizeof(&)
6093 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006094 WARN("SIZEOF_ADDRESS",
6095 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006096 }
6097
Joe Perches66c80b62012-07-30 14:41:22 -07006098# check for sizeof without parenthesis
6099 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006100 if (WARN("SIZEOF_PARENTHESIS",
6101 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6102 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006103 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006104 }
Joe Perches66c80b62012-07-30 14:41:22 -07006105 }
6106
Joe Perches88982fe2012-12-17 16:02:00 -08006107# check for struct spinlock declarations
6108 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6109 WARN("USE_SPINLOCK_T",
6110 "struct spinlock should be spinlock_t\n" . $herecurr);
6111 }
6112
Joe Perchesa6962d72013-04-29 16:18:13 -07006113# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006114 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006115 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006116 $fmt =~ s/%%//g;
6117 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006118 if (WARN("PREFER_SEQ_PUTS",
6119 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6120 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006121 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006122 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006123 }
6124 }
6125
Joe Perches478b1792018-04-10 16:33:34 -07006126# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006127 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006128 defined $stat &&
6129 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6130 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006131 my $stat_real;
6132
Joe Perches0b523762017-05-08 15:55:36 -07006133 my $lc = $stat =~ tr@\n@@;
6134 $lc = $lc + $linenr;
6135 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006136 my $specifier;
6137 my $extension;
6138 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006139 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6140 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006141
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006142 while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
6143 $specifier = $1;
6144 $extension = $2;
6145 if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
6146 $bad_specifier = $specifier;
6147 last;
6148 }
6149 if ($extension eq "x" && !defined($stat_real)) {
6150 if (!defined($stat_real)) {
6151 $stat_real = get_stat_real($linenr, $lc);
6152 }
6153 WARN("VSPRINTF_SPECIFIER_PX",
6154 "Using vsprintf specifier '\%px' potentially exposes the kernel memory layout, if you don't really need the address please consider using '\%p'.\n" . "$here\n$stat_real\n");
6155 }
6156 }
6157 if ($bad_specifier ne "") {
6158 my $stat_real = get_stat_real($linenr, $lc);
6159 my $ext_type = "Invalid";
6160 my $use = "";
6161 if ($bad_specifier =~ /p[Ff]/) {
6162 $ext_type = "Deprecated";
6163 $use = " - use %pS instead";
6164 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6165 }
6166
6167 WARN("VSPRINTF_POINTER_EXTENSION",
6168 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6169 }
Joe Perches0b523762017-05-08 15:55:36 -07006170 }
6171 }
6172
Andy Whitcroft554e1652012-01-10 15:09:57 -08006173# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006174 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006175 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006176 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006177
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006178 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006179 my $ms_val = $7;
6180 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006181
Andy Whitcroft554e1652012-01-10 15:09:57 -08006182 if ($ms_size =~ /^(0x|)0$/i) {
6183 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006184 "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 -08006185 } elsif ($ms_size =~ /^(0x|)1$/i) {
6186 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006187 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6188 }
6189 }
6190
Joe Perches98a9bba2014-01-23 15:54:52 -08006191# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006192# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006193# defined $stat &&
6194# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6195# if (WARN("PREFER_ETHER_ADDR_COPY",
6196# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6197# $fix) {
6198# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6199# }
6200# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006201
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006202# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006203# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006204# defined $stat &&
6205# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6206# WARN("PREFER_ETHER_ADDR_EQUAL",
6207# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6208# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006209
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006210# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6211# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006212# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006213# defined $stat &&
6214# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6215#
6216# my $ms_val = $7;
6217#
6218# if ($ms_val =~ /^(?:0x|)0+$/i) {
6219# if (WARN("PREFER_ETH_ZERO_ADDR",
6220# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6221# $fix) {
6222# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6223# }
6224# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6225# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6226# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6227# $fix) {
6228# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6229# }
6230# }
6231# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006232
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006233# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006234 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006235 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006236 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006237 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006238 my $call = $1;
6239 my $cast1 = deparenthesize($2);
6240 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006241 my $cast2 = deparenthesize($7);
6242 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006243 my $cast;
6244
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006245 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006246 $cast = "$cast1 or $cast2";
6247 } elsif ($cast1 ne "") {
6248 $cast = $cast1;
6249 } else {
6250 $cast = $cast2;
6251 }
6252 WARN("MINMAX",
6253 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006254 }
6255 }
6256
Joe Perches4a273192012-07-30 14:41:20 -07006257# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006258 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006259 defined $stat &&
6260 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6261 my $min = $1;
6262 my $max = $7;
6263 if ($min eq $max) {
6264 WARN("USLEEP_RANGE",
6265 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6266 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6267 $min > $max) {
6268 WARN("USLEEP_RANGE",
6269 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6270 }
6271 }
6272
Joe Perches823b7942013-11-12 15:10:15 -08006273# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006274 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006275 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006276 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006277 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6278 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6279 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6280 my $lc = $stat =~ tr@\n@@;
6281 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006282 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006283 WARN("NAKED_SSCANF",
6284 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6285 }
6286
Joe Perchesafc819a2014-06-04 16:12:08 -07006287# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006288 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006289 defined $stat &&
6290 $line =~ /\bsscanf\b/) {
6291 my $lc = $stat =~ tr@\n@@;
6292 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006293 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006294 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6295 my $format = $6;
6296 my $count = $format =~ tr@%@%@;
6297 if ($count == 1 &&
6298 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6299 WARN("SSCANF_TO_KSTRTO",
6300 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6301 }
6302 }
6303 }
6304
Joe Perches70dc8a42013-09-11 14:23:58 -07006305# check for new externs in .h files.
6306 if ($realfile =~ /\.h$/ &&
6307 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006308 if (CHK("AVOID_EXTERNS",
6309 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006310 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006311 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006312 }
6313 }
6314
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006315# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006316 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006317 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006318 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006319 my $function_name = $1;
6320 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006321
6322 my $s = $stat;
6323 if (defined $cond) {
6324 substr($s, 0, length($cond), '');
6325 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006326 if ($s =~ /^\s*;/ &&
6327 $function_name ne 'uninitialized_var')
6328 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006329 WARN("AVOID_EXTERNS",
6330 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006331 }
6332
6333 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006334 WARN("FUNCTION_ARGUMENTS",
6335 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006336 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006337
6338 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6339 $stat =~ /^.\s*extern\s+/)
6340 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006341 WARN("AVOID_EXTERNS",
6342 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006343 }
6344
Joe Perchesa0ad7592017-07-10 15:52:19 -07006345# check for function declarations that have arguments without identifier names
6346 if (defined $stat &&
Miles Chen25bdda22017-11-17 15:28:34 -08006347 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Joe Perchesca0d8922016-10-11 13:52:16 -07006348 $1 ne "void") {
6349 my $args = trim($1);
6350 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6351 my $arg = trim($1);
6352 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6353 WARN("FUNCTION_ARGUMENTS",
6354 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6355 }
6356 }
6357 }
6358
Joe Perchesa0ad7592017-07-10 15:52:19 -07006359# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006360 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006361 defined $stat &&
6362 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6363 $context_function = $1;
6364
6365# check for multiline function definition with misplaced open brace
6366 my $ok = 0;
6367 my $cnt = statement_rawlines($stat);
6368 my $herectx = $here . "\n";
6369 for (my $n = 0; $n < $cnt; $n++) {
6370 my $rl = raw_line($linenr, $n);
6371 $herectx .= $rl . "\n";
6372 $ok = 1 if ($rl =~ /^[ \+]\{/);
6373 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6374 last if $rl =~ /^[ \+].*\{/;
6375 }
6376 if (!$ok) {
6377 ERROR("OPEN_BRACE",
6378 "open brace '{' following function definitions go on the next line\n" . $herectx);
6379 }
6380 }
6381
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006382# checks for new __setup's
6383 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6384 my $name = $1;
6385
6386 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006387 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006388 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006389 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006390 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006391
6392# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006393 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006394 WARN("UNNECESSARY_CASTS",
6395 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006396 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006397
Joe Perchesa640d252013-07-03 15:05:21 -07006398# alloc style
6399# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006400 if ($perl_version_ok &&
Joe Perchesa640d252013-07-03 15:05:21 -07006401 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6402 CHK("ALLOC_SIZEOF_STRUCT",
6403 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6404 }
6405
Joe Perches60a55362014-06-04 16:12:07 -07006406# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006407 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006408 defined $stat &&
6409 $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 -07006410 my $oldfunc = $3;
6411 my $a1 = $4;
6412 my $a2 = $10;
6413 my $newfunc = "kmalloc_array";
6414 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006415 my $r1 = $a1;
6416 my $r2 = $a2;
6417 if ($a1 =~ /^sizeof\s*\S/) {
6418 $r1 = $a2;
6419 $r2 = $a1;
6420 }
6421 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6422 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006423 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006424 my $herectx = get_stat_here($linenr, $cnt, $here);
6425
Joe Perches60a55362014-06-04 16:12:07 -07006426 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006427 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6428 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006429 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006430 $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 -07006431 }
6432 }
6433 }
6434
Joe Perches972fdea2013-04-29 16:18:12 -07006435# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006436 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006437 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6438 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006439 WARN("KREALLOC_ARG_REUSE",
6440 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6441 }
6442
Joe Perches5ce59ae2013-02-21 16:44:18 -08006443# check for alloc argument mismatch
6444 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6445 WARN("ALLOC_ARRAY_ARGS",
6446 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6447 }
6448
Joe Perchescaf2a542011-01-12 16:59:56 -08006449# check for multiple semicolons
6450 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006451 if (WARN("ONE_SEMICOLON",
6452 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6453 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006454 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006455 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006456 }
6457
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006458# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6459 if ($realfile !~ m@^include/uapi/@ &&
6460 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006461 my $ull = "";
6462 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6463 if (CHK("BIT_MACRO",
6464 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6465 $fix) {
6466 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6467 }
6468 }
6469
Joe Perches2d632742016-05-20 17:04:00 -07006470# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6471 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6472 my $config = $1;
6473 if (WARN("PREFER_IS_ENABLED",
6474 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6475 $fix) {
6476 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6477 }
6478 }
6479
Joe Perchese81f2392014-08-06 16:11:25 -07006480# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006481 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6482 my $has_break = 0;
6483 my $has_statement = 0;
6484 my $count = 0;
6485 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006486 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006487 $prevline--;
6488 my $rline = $rawlines[$prevline - 1];
6489 my $fline = $lines[$prevline - 1];
6490 last if ($fline =~ /^\@\@/);
6491 next if ($fline =~ /^\-/);
6492 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6493 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6494 next if ($fline =~ /^.[\s$;]*$/);
6495 $has_statement = 1;
6496 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006497 $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 -08006498 }
6499 if (!$has_break && $has_statement) {
6500 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006501 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006502 }
6503 }
6504
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006505# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006506 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006507 defined $stat &&
6508 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006509 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006510 my $herectx = get_stat_here($linenr, $cnt, $here);
6511
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006512 WARN("DEFAULT_NO_BREAK",
6513 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006514 }
6515
Patrick Pannuto9fe4b952010-07-23 13:34:18 -07006516# check for return codes on error paths
6517 if ($line =~ /\breturn\s+-\d+/) {
6518 ERROR("NO_ERROR_CODE",
Patrick Pannutob1122c72010-08-04 14:30:59 -07006519 "illegal return value, please use an error code\n" . $herecurr);
Patrick Pannuto9fe4b952010-07-23 13:34:18 -07006520 }
6521
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006522# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006523 if ($line =~ /\b__FUNCTION__\b/) {
6524 if (WARN("USE_FUNC",
6525 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6526 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006527 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006528 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006529 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006530
Joe Perches62ec8182015-02-13 14:38:18 -08006531# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6532 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6533 ERROR("DATE_TIME",
6534 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6535 }
6536
Joe Perches2c924882012-03-23 15:02:20 -07006537# check for use of yield()
6538 if ($line =~ /\byield\s*\(\s*\)/) {
6539 WARN("YIELD",
6540 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6541 }
6542
Joe Perches179f8f42013-07-03 15:05:30 -07006543# check for comparisons against true and false
6544 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6545 my $lead = $1;
6546 my $arg = $2;
6547 my $test = $3;
6548 my $otype = $4;
6549 my $trail = $5;
6550 my $op = "!";
6551
6552 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6553
6554 my $type = lc($otype);
6555 if ($type =~ /^(?:true|false)$/) {
6556 if (("$test" eq "==" && "$type" eq "true") ||
6557 ("$test" eq "!=" && "$type" eq "false")) {
6558 $op = "";
6559 }
6560
6561 CHK("BOOL_COMPARISON",
6562 "Using comparison to $otype is error prone\n" . $herecurr);
6563
6564## maybe suggesting a correct construct would better
6565## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6566
6567 }
6568 }
6569
Joe Perches5d430902018-04-10 16:34:25 -07006570# check for bool bitfields
6571 if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6572 WARN("BOOL_BITFIELD",
6573 "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6574 }
6575
Joe Perchesd7295932018-08-21 21:57:26 -07006576# check for bool use in .h files
6577 if ($realfile =~ /\.h$/ &&
6578 $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6579 CHK("BOOL_MEMBER",
6580 "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6581 }
6582
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006583# check for semaphores initialized locked
6584 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006585 WARN("CONSIDER_COMPLETION",
6586 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006587 }
Joe Perches6712d852012-03-23 15:02:20 -07006588
Joe Perches67d0a072011-10-31 17:13:10 -07006589# recommend kstrto* over simple_strto* and strict_strto*
6590 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006591 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006592 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006593 }
Joe Perches6712d852012-03-23 15:02:20 -07006594
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006595# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006596 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006597 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006598 "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 -07006599 }
Joe Perches6712d852012-03-23 15:02:20 -07006600
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006601# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006602# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006603 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006604 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006605 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006606 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006607 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006608
6609# use of NR_CPUS is usually wrong
6610# ignore definitions of NR_CPUS and usage to define arrays as likely right
6611 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006612 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6613 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006614 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6615 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6616 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006617 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006618 WARN("NR_CPUS",
6619 "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 -07006620 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006621
Joe Perches52ea8502013-11-12 15:10:09 -08006622# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6623 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6624 ERROR("DEFINE_ARCH_HAS",
6625 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6626 }
6627
Joe Perchesacd93622015-02-13 14:38:38 -08006628# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006629 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006630 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6631 WARN("LIKELY_MISUSE",
6632 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6633 }
6634
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006635# whine mightly about in_atomic
6636 if ($line =~ /\bin_atomic\s*\(/) {
6637 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006638 ERROR("IN_ATOMIC",
6639 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006640 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006641 WARN("IN_ATOMIC",
6642 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006643 }
6644 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006645
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006646# check for mutex_trylock_recursive usage
6647 if ($line =~ /mutex_trylock_recursive/) {
6648 ERROR("LOCKING",
6649 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6650 }
6651
Peter Zijlstra1704f472010-03-19 01:37:42 +01006652# check for lockdep_set_novalidate_class
6653 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6654 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6655 if ($realfile !~ m@^kernel/lockdep@ &&
6656 $realfile !~ m@^include/linux/lockdep@ &&
6657 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006658 ERROR("LOCKDEP",
6659 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006660 }
6661 }
Dave Jones88f88312011-01-12 16:59:59 -08006662
Joe Perchesb392c642015-04-16 12:44:16 -07006663 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6664 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006665 WARN("EXPORTED_WORLD_WRITABLE",
6666 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006667 }
Joe Perches24358802014-04-03 14:49:13 -07006668
Joe Perches00180462018-02-06 15:38:55 -08006669# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6670# and whether or not function naming is typical and if
6671# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006672 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006673 defined $stat &&
6674 $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*\)/) {
6675 my $var = $1;
6676 my $perms = $2;
6677 my $show = $3;
6678 my $store = $4;
6679 my $octal_perms = perms_to_octal($perms);
6680 if ($show =~ /^${var}_show$/ &&
6681 $store =~ /^${var}_store$/ &&
6682 $octal_perms eq "0644") {
6683 if (WARN("DEVICE_ATTR_RW",
6684 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6685 $fix) {
6686 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6687 }
6688 } elsif ($show =~ /^${var}_show$/ &&
6689 $store =~ /^NULL$/ &&
6690 $octal_perms eq "0444") {
6691 if (WARN("DEVICE_ATTR_RO",
6692 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6693 $fix) {
6694 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6695 }
6696 } elsif ($show =~ /^NULL$/ &&
6697 $store =~ /^${var}_store$/ &&
6698 $octal_perms eq "0200") {
6699 if (WARN("DEVICE_ATTR_WO",
6700 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6701 $fix) {
6702 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6703 }
6704 } elsif ($octal_perms eq "0644" ||
6705 $octal_perms eq "0444" ||
6706 $octal_perms eq "0200") {
6707 my $newshow = "$show";
6708 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6709 my $newstore = $store;
6710 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6711 my $rename = "";
6712 if ($show ne $newshow) {
6713 $rename .= " '$show' to '$newshow'";
6714 }
6715 if ($store ne $newstore) {
6716 $rename .= " '$store' to '$newstore'";
6717 }
6718 WARN("DEVICE_ATTR_FUNCTIONS",
6719 "Consider renaming function(s)$rename\n" . $herecurr);
6720 } else {
6721 WARN("DEVICE_ATTR_PERMS",
6722 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6723 }
6724 }
6725
Joe Perches515a2352014-04-03 14:49:24 -07006726# Mode permission misuses where it seems decimal should be octal
6727# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006728# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6729# specific definition of not visible in sysfs.
6730# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6731# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006732 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006733 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006734 $line =~ /$mode_perms_search/) {
6735 foreach my $entry (@mode_permission_funcs) {
6736 my $func = $entry->[0];
6737 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006738
Joe Perches459cf0a2016-10-11 13:52:19 -07006739 my $lc = $stat =~ tr@\n@@;
6740 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006741 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006742
Joe Perches515a2352014-04-03 14:49:24 -07006743 my $skip_args = "";
6744 if ($arg_pos > 1) {
6745 $arg_pos--;
6746 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6747 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006748 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006749 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006750 my $val = $1;
6751 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006752 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6753 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6754 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006755 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006756 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006757 }
6758 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006759 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006760 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006761 }
Joe Perches24358802014-04-03 14:49:13 -07006762 }
6763 }
6764 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006765
Joe Perches459cf0a2016-10-11 13:52:19 -07006766# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006767 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006768 my $oval = $1;
6769 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006770 if (WARN("SYMBOLIC_PERMS",
6771 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6772 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006773 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006774 }
6775 }
6776
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006777# validate content of MODULE_LICENSE against list from include/linux/module.h
6778 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6779 my $extracted_string = get_quoted_string($line, $rawline);
6780 my $valid_licenses = qr{
6781 GPL|
6782 GPL\ v2|
6783 GPL\ and\ additional\ rights|
6784 Dual\ BSD/GPL|
6785 Dual\ MIT/GPL|
6786 Dual\ MPL/GPL|
6787 Proprietary
6788 }x;
6789 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6790 WARN("MODULE_LICENSE",
6791 "unknown module license " . $extracted_string . "\n" . $herecurr);
6792 }
6793 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006794 }
6795
6796 # If we have no input at all, then there is nothing to report on
6797 # so just keep quiet.
6798 if ($#rawlines == -1) {
6799 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006800 }
6801
Andy Whitcroft8905a672007-11-28 16:21:06 -08006802 # In mailback mode only produce a report in the negative, for
6803 # things that appear to be patches.
6804 if ($mailback && ($clean == 1 || !$is_patch)) {
6805 exit(0);
6806 }
6807
6808 # This is not a patch, and we are are in 'no-patch' mode so
6809 # just keep quiet.
6810 if (!$chk_patch && !$is_patch) {
6811 exit(0);
6812 }
6813
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006814 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006815 ERROR("NOT_UNIFIED_DIFF",
6816 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006817 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006818 if ($is_patch && $has_commit_log && $chk_signoff) {
6819 if ($signoff == 0) {
6820 ERROR("MISSING_SIGN_OFF",
6821 "Missing Signed-off-by: line(s)\n");
6822 } elsif (!$authorsignoff) {
6823 WARN("NO_AUTHOR_SIGN_OFF",
6824 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6825 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006826 }
6827
Andy Whitcroft8905a672007-11-28 16:21:06 -08006828 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006829 if ($summary && !($clean == 1 && $quiet == 1)) {
6830 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006831 print "total: $cnt_error errors, $cnt_warn warnings, " .
6832 (($check)? "$cnt_chk checks, " : "") .
6833 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006834 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006835
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006836 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006837 # If there were any defects found and not already fixing them
6838 if (!$clean and !$fix) {
6839 print << "EOM"
6840
6841NOTE: For some of the reported defects, checkpatch may be able to
6842 mechanically convert to the typical style using --fix or --fix-inplace.
6843EOM
6844 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006845 # If there were whitespace errors which cleanpatch can fix
6846 # then suggest that.
6847 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006848 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006849 print << "EOM"
6850
6851NOTE: Whitespace errors detected.
6852 You may wish to use scripts/cleanpatch or scripts/cleanfile
6853EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006854 }
6855 }
6856
Joe Perchesd752fcc2014-08-06 16:11:05 -07006857 if ($clean == 0 && $fix &&
6858 ("@rawlines" ne "@fixed" ||
6859 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006860 my $newfile = $filename;
6861 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006862 my $linecount = 0;
6863 my $f;
6864
Joe Perchesd752fcc2014-08-06 16:11:05 -07006865 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6866
Joe Perches3705ce52013-07-03 15:05:31 -07006867 open($f, '>', $newfile)
6868 or die "$P: Can't open $newfile for write\n";
6869 foreach my $fixed_line (@fixed) {
6870 $linecount++;
6871 if ($file) {
6872 if ($linecount > 3) {
6873 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006874 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006875 }
6876 } else {
6877 print $f $fixed_line . "\n";
6878 }
6879 }
6880 close($f);
6881
6882 if (!$quiet) {
6883 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006884
Joe Perches3705ce52013-07-03 15:05:31 -07006885Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6886
6887Do _NOT_ trust the results written to this file.
6888Do _NOT_ submit these changes without inspecting them for correctness.
6889
6890This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6891No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006892EOM
6893 }
6894 }
6895
Joe Perchesd8469f12015-06-25 15:03:00 -07006896 if ($quiet == 0) {
6897 print "\n";
6898 if ($clean == 1) {
6899 print "$vname has no obvious style problems and is ready for submission.\n";
6900 } else {
6901 print "$vname has style problems, please review.\n";
6902 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006903 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006904 return $clean;
6905}