blob: dfd265e036f3b7dabe329bf81812b2567a46549e [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Joe Perches882ea1d2018-06-07 17:04:33 -07002# SPDX-License-Identifier: GPL-2.0
3#
Dave Jonesdbf004d2010-01-12 16:59:52 -05004# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07005# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08006# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07007# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Joe Perches882ea1d2018-06-07 17:04:33 -07008# (c) 2010-2018 Joe Perches <joe@perches.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07009
10use strict;
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020011use warnings;
Joe Perchesc707a812013-07-08 16:00:43 -070012use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080013use File::Basename;
14use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070015use Term::ANSIColor qw(:constants);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070016
17my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080018my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070019
Joe Perches000d1cc12011-07-25 17:13:25 -070020my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070021
22use Getopt::Long qw(:config no_auto_abbrev);
23
24my $quiet = 0;
25my $tree = 1;
26my $chk_signoff = 1;
27my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070028my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070029my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080030my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070031my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070032my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070033my $git = 0;
Joe Perches0dea9f1e2016-05-20 17:04:19 -070034my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070035my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070036my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080037my $summary = 1;
38my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080039my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070040my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070041my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070042my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080043my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070044my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080045my %debug;
Joe Perches34456862013-07-03 15:05:34 -070046my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070047my %use_type = ();
48my @use = ();
49my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070050my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070051my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070052my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080053my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070054my $ignore_perl_version = 0;
55my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070056my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070057my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070058my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070059my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070060my $conststructsfile = "$D/const_structs.checkpatch";
Jerome Forissier75ad8c52017-05-08 15:56:00 -070061my $typedefsfile = "";
John Brooks737c0762017-07-10 15:52:24 -070062my $color = "auto";
Joe Perchesdadf6802016-08-02 14:04:33 -070063my $allow_c99_comments = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070064
65sub help {
66 my ($exitcode) = @_;
67
68 print << "EOM";
69Usage: $P [OPTION]... [FILE]...
70Version: $V
71
72Options:
73 -q, --quiet quiet
74 --no-tree run without a kernel tree
75 --no-signoff do not check for 'Signed-off-by' line
76 --patch treat FILE as patchfile (default)
77 --emacs emacs compile window format
78 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070079 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070080 -g, --git treat FILE as a single commit or git revision range
81 single git commit with:
82 <rev>
83 <rev>^
84 <rev>~n
85 multiple git commits with:
86 <rev1>..<rev2>
87 <rev1>...<rev2>
88 <rev>-<count>
89 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070090 -f, --file treat FILE as regular source file
91 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070092 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070093 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070094 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070095 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080096 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070097 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -070098 --root=PATH PATH to the kernel tree root
99 --no-summary suppress the per-file summary
100 --mailback only produce a report in case of warnings/errors
101 --summary-file include the filename in summary
102 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
103 'values', 'possible', 'type', and 'attr' (default
104 is all off)
105 --test-only=WORD report only warnings/errors containing WORD
106 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700107 --fix EXPERIMENTAL - may create horrible results
108 If correctable single-line errors exist, create
109 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
110 with potential errors corrected to the preferred
111 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800112 --fix-inplace EXPERIMENTAL - may create horrible results
113 Is the same as --fix, but overwrites the input
114 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700115 --ignore-perl-version override checking of perl version. expect
116 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700117 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700118 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700119 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700120 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700121 --color[=WHEN] Use colors 'always', 'never', or only when output
122 is a terminal ('auto'). Default is 'auto'.
Hannes Eder77f5b102009-09-21 17:04:37 -0700123 -h, --help, --version display this help and exit
124
125When FILE is - read standard input.
126EOM
127
128 exit($exitcode);
129}
130
Joe Perches3beb42e2016-05-20 17:04:14 -0700131sub uniq {
132 my %seen;
133 return grep { !$seen{$_}++ } @_;
134}
135
136sub list_types {
137 my ($exitcode) = @_;
138
139 my $count = 0;
140
141 local $/ = undef;
142
143 open(my $script, '<', abs_path($P)) or
144 die "$P: Can't read '$P' $!\n";
145
146 my $text = <$script>;
147 close($script);
148
149 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700150 # Also catch when type or level is passed through a variable
151 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700152 push (@types, $_);
153 }
154 @types = sort(uniq(@types));
155 print("#\tMessage type\n\n");
156 foreach my $type (@types) {
157 print(++$count . "\t" . $type . "\n");
158 }
159
160 exit($exitcode);
161}
162
Joe Perches000d1cc12011-07-25 17:13:25 -0700163my $conf = which_conf($configuration_file);
164if (-f $conf) {
165 my @conf_args;
166 open(my $conffile, '<', "$conf")
167 or warn "$P: Can't find a readable $configuration_file file $!\n";
168
169 while (<$conffile>) {
170 my $line = $_;
171
172 $line =~ s/\s*\n?$//g;
173 $line =~ s/^\s*//g;
174 $line =~ s/\s+/ /g;
175
176 next if ($line =~ m/^\s*#/);
177 next if ($line =~ m/^\s*$/);
178
179 my @words = split(" ", $line);
180 foreach my $word (@words) {
181 last if ($word =~ m/^#/);
182 push (@conf_args, $word);
183 }
184 }
185 close($conffile);
186 unshift(@ARGV, @conf_args) if @conf_args;
187}
188
John Brooks737c0762017-07-10 15:52:24 -0700189# Perl's Getopt::Long allows options to take optional arguments after a space.
190# Prevent --color by itself from consuming other arguments
191foreach (@ARGV) {
192 if ($_ eq "--color" || $_ eq "-color") {
193 $_ = "--color=$color";
194 }
195}
196
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700197GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700198 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700199 'tree!' => \$tree,
200 'signoff!' => \$chk_signoff,
201 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700202 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800203 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700204 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700205 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700206 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700207 'subjective!' => \$check,
208 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700209 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700210 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700211 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700212 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800213 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700214 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700215 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800216 'summary!' => \$summary,
217 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800218 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700219 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800220 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700221 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800222 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700223 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700224 'codespell!' => \$codespell,
225 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700226 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700227 'color=s' => \$color,
228 'no-color' => \$color, #keep old behaviors of -nocolor
229 'nocolor' => \$color, #keep old behaviors of -nocolor
Hannes Eder77f5b102009-09-21 17:04:37 -0700230 'h|help' => \$help,
231 'version' => \$help
232) or help(1);
233
234help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700235
Joe Perches3beb42e2016-05-20 17:04:14 -0700236list_types(0) if ($list_types);
237
Joe Perches9624b8d2014-01-23 15:54:44 -0800238$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700239$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800240
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700241my $exit = 0;
242
Dave Hansend62a2012013-09-11 14:23:56 -0700243if ($^V && $^V lt $minimum_perl_version) {
244 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
245 if (!$ignore_perl_version) {
246 exit(1);
247 }
248}
249
Allen Hubbe45107ff2016-08-02 14:04:47 -0700250#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700251if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700252 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700253}
254
John Brooks737c0762017-07-10 15:52:24 -0700255if ($color =~ /^[01]$/) {
256 $color = !$color;
257} elsif ($color =~ /^always$/i) {
258 $color = 1;
259} elsif ($color =~ /^never$/i) {
260 $color = 0;
261} elsif ($color =~ /^auto$/i) {
262 $color = (-t STDOUT);
263} else {
264 die "Invalid color mode: $color\n";
265}
266
Joe Perches91bfe482013-09-11 14:23:59 -0700267sub hash_save_array_words {
268 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700269
Joe Perches91bfe482013-09-11 14:23:59 -0700270 my @array = split(/,/, join(',', @$arrayRef));
271 foreach my $word (@array) {
272 $word =~ s/\s*\n?$//g;
273 $word =~ s/^\s*//g;
274 $word =~ s/\s+/ /g;
275 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700276
Joe Perches91bfe482013-09-11 14:23:59 -0700277 next if ($word =~ m/^\s*#/);
278 next if ($word =~ m/^\s*$/);
279
280 $hashRef->{$word}++;
281 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700282}
283
Joe Perches91bfe482013-09-11 14:23:59 -0700284sub hash_show_words {
285 my ($hashRef, $prefix) = @_;
286
Joe Perches3c816e42015-06-25 15:03:29 -0700287 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700288 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700289 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700290 print " $word";
291 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700292 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700293 }
294}
295
296hash_save_array_words(\%ignore_type, \@ignore);
297hash_save_array_words(\%use_type, \@use);
298
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800299my $dbg_values = 0;
300my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700301my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700302my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800303for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800304 ## no critic
305 eval "\${dbg_$key} = '$debug{$key}';";
306 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800307}
308
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700309my $rpt_cleaners = 0;
310
Andy Whitcroft8905a672007-11-28 16:21:06 -0800311if ($terse) {
312 $emacs = 1;
313 $quiet++;
314}
315
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700316if ($tree) {
317 if (defined $root) {
318 if (!top_of_kernel_tree($root)) {
319 die "$P: $root: --root does not point at a valid tree\n";
320 }
321 } else {
322 if (top_of_kernel_tree('.')) {
323 $root = '.';
324 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
325 top_of_kernel_tree($1)) {
326 $root = $1;
327 }
328 }
329
330 if (!defined $root) {
331 print "Must be run from the top-level dir. of a kernel tree\n";
332 exit(2);
333 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700334}
335
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700336my $emitted_corrupt = 0;
337
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700338our $Ident = qr{
339 [A-Za-z_][A-Za-z\d_]*
340 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
341 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700342our $Storage = qr{extern|static|asmlinkage};
343our $Sparse = qr{
344 __user|
345 __kernel|
346 __force|
347 __iomem|
348 __must_check|
349 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800350 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700351 __ref|
Boqun Fengad315452015-12-29 12:18:46 +0800352 __rcu|
353 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700354 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800355our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
356our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
357our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
358our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
359our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700360
Wolfram Sang52131292010-03-05 13:43:51 -0800361# Notes to $Attribute:
362# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700363our $Attribute = qr{
364 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700365 __percpu|
366 __nocast|
367 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200368 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700369 __packed__|
370 __packed2__|
371 __naked|
372 __maybe_unused|
373 __always_unused|
374 __noreturn|
375 __used|
376 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800377 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700378 __noclone|
379 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700380 __read_mostly|
381 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700382 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700383 ____cacheline_aligned|
384 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800385 ____cacheline_internodealigned_in_smp|
386 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700387 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700388our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700389our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700390our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
391our $Lval = qr{$Ident(?:$Member)*};
392
Joe Perches95e2c602013-07-03 15:05:20 -0700393our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
394our $Binary = qr{(?i)0b[01]+$Int_type?};
395our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
396our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700397our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800398our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800399our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
400our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
401our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800402our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700403our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800404our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700405our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700406our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700407our $Operators = qr{
408 <=|>=|==|!=|
409 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700410 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700411 }x;
412
Joe Perches91cb5192014-04-03 14:49:32 -0700413our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
414
Joe Perchesab7e23f2015-04-16 12:44:22 -0700415our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800416our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700417our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700418our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800419our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700420our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800421our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700422our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800423
Joe Perches15662b32011-10-31 17:13:12 -0700424our $NON_ASCII_UTF8 = qr{
425 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700426 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
427 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
428 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
429 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
430 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
431 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
432}x;
433
Joe Perches15662b32011-10-31 17:13:12 -0700434our $UTF8 = qr{
435 [\x09\x0A\x0D\x20-\x7E] # ASCII
436 | $NON_ASCII_UTF8
437}x;
438
Joe Perchese6176fa2015-06-25 15:02:49 -0700439our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800440our $typeOtherOSTypedefs = qr{(?x:
441 u_(?:char|short|int|long) | # bsd
442 u(?:nchar|short|int|long) # sysv
443)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700444our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700445 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700446 atomic_t
447)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700448our $typeTypedefs = qr{(?x:
449 $typeC99Typedefs\b|
450 $typeOtherOSTypedefs\b|
451 $typeKernelTypedefs\b
452)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700453
Joe Perches6d32f7a2015-11-06 16:31:37 -0800454our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
455
Joe Perches691e6692010-03-05 13:43:51 -0800456our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800457 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700458 (?:[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 -0800459 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700460 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700461 panic|
Joe Perches06668722013-11-12 15:10:07 -0800462 MODULE_[A-Z_]+|
463 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800464)};
465
Joe Perches20112472011-07-25 17:13:23 -0700466our $signature_tags = qr{(?xi:
467 Signed-off-by:|
468 Acked-by:|
469 Tested-by:|
470 Reviewed-by:|
471 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700472 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700473 To:|
474 Cc:
475)};
476
Joe Perches18130872014-08-06 16:11:22 -0700477our @typeListMisordered = (
478 qr{char\s+(?:un)?signed},
479 qr{int\s+(?:(?:un)?signed\s+)?short\s},
480 qr{int\s+short(?:\s+(?:un)?signed)},
481 qr{short\s+int(?:\s+(?:un)?signed)},
482 qr{(?:un)?signed\s+int\s+short},
483 qr{short\s+(?:un)?signed},
484 qr{long\s+int\s+(?:un)?signed},
485 qr{int\s+long\s+(?:un)?signed},
486 qr{long\s+(?:un)?signed\s+int},
487 qr{int\s+(?:un)?signed\s+long},
488 qr{int\s+(?:un)?signed},
489 qr{int\s+long\s+long\s+(?:un)?signed},
490 qr{long\s+long\s+int\s+(?:un)?signed},
491 qr{long\s+long\s+(?:un)?signed\s+int},
492 qr{long\s+long\s+(?:un)?signed},
493 qr{long\s+(?:un)?signed},
494);
495
Andy Whitcroft8905a672007-11-28 16:21:06 -0800496our @typeList = (
497 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700498 qr{(?:(?:un)?signed\s+)?char},
499 qr{(?:(?:un)?signed\s+)?short\s+int},
500 qr{(?:(?:un)?signed\s+)?short},
501 qr{(?:(?:un)?signed\s+)?int},
502 qr{(?:(?:un)?signed\s+)?long\s+int},
503 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
504 qr{(?:(?:un)?signed\s+)?long\s+long},
505 qr{(?:(?:un)?signed\s+)?long},
506 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800507 qr{float},
508 qr{double},
509 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800510 qr{struct\s+$Ident},
511 qr{union\s+$Ident},
512 qr{enum\s+$Ident},
513 qr{${Ident}_t},
514 qr{${Ident}_handler},
515 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700516 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800517);
Joe Perches938224b2016-01-20 14:59:15 -0800518
519our $C90_int_types = qr{(?x:
520 long\s+long\s+int\s+(?:un)?signed|
521 long\s+long\s+(?:un)?signed\s+int|
522 long\s+long\s+(?:un)?signed|
523 (?:(?:un)?signed\s+)?long\s+long\s+int|
524 (?:(?:un)?signed\s+)?long\s+long|
525 int\s+long\s+long\s+(?:un)?signed|
526 int\s+(?:(?:un)?signed\s+)?long\s+long|
527
528 long\s+int\s+(?:un)?signed|
529 long\s+(?:un)?signed\s+int|
530 long\s+(?:un)?signed|
531 (?:(?:un)?signed\s+)?long\s+int|
532 (?:(?:un)?signed\s+)?long|
533 int\s+long\s+(?:un)?signed|
534 int\s+(?:(?:un)?signed\s+)?long|
535
536 int\s+(?:un)?signed|
537 (?:(?:un)?signed\s+)?int
538)};
539
Alex Dowad485ff232015-06-25 15:02:52 -0700540our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700541our @typeListWithAttr = (
542 @typeList,
543 qr{struct\s+$InitAttribute\s+$Ident},
544 qr{union\s+$InitAttribute\s+$Ident},
545);
546
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700547our @modifierList = (
548 qr{fastcall},
549);
Alex Dowad485ff232015-06-25 15:02:52 -0700550our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800551
Joe Perches24358802014-04-03 14:49:13 -0700552our @mode_permission_funcs = (
553 ["module_param", 3],
554 ["module_param_(?:array|named|string)", 4],
555 ["module_param_array_named", 5],
556 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
557 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700558 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
559 ["IIO_DEV_ATTR_[A-Z_]+", 1],
560 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
561 ["SENSOR_TEMPLATE(?:_2|)", 3],
562 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700563);
564
Joe Perches515a2352014-04-03 14:49:24 -0700565#Create a search pattern for all these functions to speed up a loop below
566our $mode_perms_search = "";
567foreach my $entry (@mode_permission_funcs) {
568 $mode_perms_search .= '|' if ($mode_perms_search ne "");
569 $mode_perms_search .= $entry->[0];
570}
Joe Perches00180462018-02-06 15:38:55 -0800571$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700572
Joe Perchesb392c642015-04-16 12:44:16 -0700573our $mode_perms_world_writable = qr{
574 S_IWUGO |
575 S_IWOTH |
576 S_IRWXUGO |
577 S_IALLUGO |
578 0[0-7][0-7][2367]
579}x;
580
Joe Perchesf90774e2016-10-11 13:51:47 -0700581our %mode_permission_string_types = (
582 "S_IRWXU" => 0700,
583 "S_IRUSR" => 0400,
584 "S_IWUSR" => 0200,
585 "S_IXUSR" => 0100,
586 "S_IRWXG" => 0070,
587 "S_IRGRP" => 0040,
588 "S_IWGRP" => 0020,
589 "S_IXGRP" => 0010,
590 "S_IRWXO" => 0007,
591 "S_IROTH" => 0004,
592 "S_IWOTH" => 0002,
593 "S_IXOTH" => 0001,
594 "S_IRWXUGO" => 0777,
595 "S_IRUGO" => 0444,
596 "S_IWUGO" => 0222,
597 "S_IXUGO" => 0111,
598);
599
600#Create a search pattern for all these strings to speed up a loop below
601our $mode_perms_string_search = "";
602foreach my $entry (keys %mode_permission_string_types) {
603 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
604 $mode_perms_string_search .= $entry;
605}
Joe Perches00180462018-02-06 15:38:55 -0800606our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
607our $multi_mode_perms_string_search = qr{
608 ${single_mode_perms_string_search}
609 (?:\s*\|\s*${single_mode_perms_string_search})*
610}x;
611
612sub perms_to_octal {
613 my ($string) = @_;
614
615 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
616
617 my $val = "";
618 my $oval = "";
619 my $to = 0;
620 my $curpos = 0;
621 my $lastpos = 0;
622 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
623 $curpos = pos($string);
624 my $match = $2;
625 my $omatch = $1;
626 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
627 $lastpos = $curpos;
628 $to |= $mode_permission_string_types{$match};
629 $val .= '\s*\|\s*' if ($val ne "");
630 $val .= $match;
631 $oval .= $omatch;
632 }
633 $oval =~ s/^\s*\|\s*//;
634 $oval =~ s/\s*\|\s*$//;
635 return sprintf("%04o", $to);
636}
Joe Perchesf90774e2016-10-11 13:51:47 -0700637
Wolfram Sang7840a942010-08-09 17:20:57 -0700638our $allowed_asm_includes = qr{(?x:
639 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700640 memory|
641 time|
642 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700643)};
644# memory.h: ARM has a custom one
645
Kees Cook66b47b42014-10-13 15:51:57 -0700646# Load common spelling mistakes and build regular expression list.
647my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700648my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700649
Joe Perches36061e32014-12-10 15:51:43 -0800650if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800651 while (<$spelling>) {
652 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700653
Joe Perches36061e32014-12-10 15:51:43 -0800654 $line =~ s/\s*\n?$//g;
655 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700656
Joe Perches36061e32014-12-10 15:51:43 -0800657 next if ($line =~ m/^\s*#/);
658 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700659
Joe Perches36061e32014-12-10 15:51:43 -0800660 my ($suspect, $fix) = split(/\|\|/, $line);
661
Joe Perches36061e32014-12-10 15:51:43 -0800662 $spelling_fix{$suspect} = $fix;
663 }
664 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800665} else {
666 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700667}
Kees Cook66b47b42014-10-13 15:51:57 -0700668
Joe Perchesebfd7d62015-04-16 12:44:14 -0700669if ($codespell) {
670 if (open(my $spelling, '<', $codespellfile)) {
671 while (<$spelling>) {
672 my $line = $_;
673
674 $line =~ s/\s*\n?$//g;
675 $line =~ s/^\s*//g;
676
677 next if ($line =~ m/^\s*#/);
678 next if ($line =~ m/^\s*$/);
679 next if ($line =~ m/, disabled/i);
680
681 $line =~ s/,.*$//;
682
683 my ($suspect, $fix) = split(/->/, $line);
684
685 $spelling_fix{$suspect} = $fix;
686 }
687 close($spelling);
688 } else {
689 warn "No codespell typos will be found - file '$codespellfile': $!\n";
690 }
691}
692
693$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
694
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700695sub read_words {
696 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700697
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700698 if (open(my $words, '<', $file)) {
699 while (<$words>) {
700 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700701
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700702 $line =~ s/\s*\n?$//g;
703 $line =~ s/^\s*//g;
704
705 next if ($line =~ m/^\s*#/);
706 next if ($line =~ m/^\s*$/);
707 if ($line =~ /\s/) {
708 print("$file: '$line' invalid - ignored\n");
709 next;
710 }
711
712 $$wordsRef .= '|' if ($$wordsRef ne "");
713 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700714 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700715 close($file);
716 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700717 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700718
719 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700720}
721
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700722my $const_structs = "";
723read_words(\$const_structs, $conststructsfile)
724 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
725
726my $typeOtherTypedefs = "";
727if (length($typedefsfile)) {
728 read_words(\$typeOtherTypedefs, $typedefsfile)
729 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
730}
731$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
732
Andy Whitcroft8905a672007-11-28 16:21:06 -0800733sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700734 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
735 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700736 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700737 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700738 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700739 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700740 (?:$typeTypedefs\b)|
741 (?:${all}\b)
742 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800743 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700744 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800745 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800746 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700747 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700748 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800749 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700750 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800751 }x;
Joe Perches18130872014-08-06 16:11:22 -0700752 $NonptrTypeMisordered = qr{
753 (?:$Modifier\s+|const\s+)*
754 (?:
755 (?:${Misordered}\b)
756 )
757 (?:\s+$Modifier|\s+const)*
758 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700759 $NonptrTypeWithAttr = qr{
760 (?:$Modifier\s+|const\s+)*
761 (?:
762 (?:typeof|__typeof__)\s*\([^\)]*\)|
763 (?:$typeTypedefs\b)|
764 (?:${allWithAttr}\b)
765 )
766 (?:\s+$Modifier|\s+const)*
767 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800768 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700769 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700770 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700771 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800772 }x;
Joe Perches18130872014-08-06 16:11:22 -0700773 $TypeMisordered = qr{
774 $NonptrTypeMisordered
775 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
776 (?:\s+$Inline|\s+$Modifier)*
777 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700778 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700779 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800780}
781build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700782
Joe Perches7d2367a2011-07-25 17:13:22 -0700783our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700784
785# Using $balanced_parens, $LvalOrFunc, or $FuncArg
786# requires at least perl version v5.10.0
787# Any use must be runtime checked with $^V
788
789our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700790our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800791our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700792
Joe Perchesf8422302014-08-06 16:11:31 -0700793our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700794 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700795 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700796 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
797 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700798)};
799
Joe Perches7d2367a2011-07-25 17:13:22 -0700800sub deparenthesize {
801 my ($string) = @_;
802 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700803
804 while ($string =~ /^\s*\(.*\)\s*$/) {
805 $string =~ s@^\s*\(\s*@@;
806 $string =~ s@\s*\)\s*$@@;
807 }
808
Joe Perches7d2367a2011-07-25 17:13:22 -0700809 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700810
Joe Perches7d2367a2011-07-25 17:13:22 -0700811 return $string;
812}
813
Joe Perches34456862013-07-03 15:05:34 -0700814sub seed_camelcase_file {
815 my ($file) = @_;
816
817 return if (!(-f $file));
818
819 local $/;
820
821 open(my $include_file, '<', "$file")
822 or warn "$P: Can't read '$file' $!\n";
823 my $text = <$include_file>;
824 close($include_file);
825
826 my @lines = split('\n', $text);
827
828 foreach my $line (@lines) {
829 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
830 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
831 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800832 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
833 $camelcase{$1} = 1;
834 } 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 -0700835 $camelcase{$1} = 1;
836 }
837 }
838}
839
Joe Perches85b0ee12016-10-11 13:51:44 -0700840sub is_maintained_obsolete {
841 my ($filename) = @_;
842
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800843 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700844
Joe Perches0616efa2016-10-11 13:52:02 -0700845 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 -0700846
847 return $status =~ /obsolete/i;
848}
849
Joe Perches34456862013-07-03 15:05:34 -0700850my $camelcase_seeded = 0;
851sub seed_camelcase_includes {
852 return if ($camelcase_seeded);
853
854 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700855 my $camelcase_cache = "";
856 my @include_files = ();
857
858 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700859
Richard Genoud3645e322014-02-10 14:25:32 -0800860 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700861 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
862 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700863 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700864 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700865 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700866 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700867 @include_files = split('\n', $files);
868 foreach my $file (@include_files) {
869 my $date = POSIX::strftime("%Y%m%d%H%M",
870 localtime((stat $file)[9]));
871 $last_mod_date = $date if ($last_mod_date < $date);
872 }
873 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700874 }
Joe Perchesc707a812013-07-08 16:00:43 -0700875
876 if ($camelcase_cache ne "" && -f $camelcase_cache) {
877 open(my $camelcase_file, '<', "$camelcase_cache")
878 or warn "$P: Can't read '$camelcase_cache' $!\n";
879 while (<$camelcase_file>) {
880 chomp;
881 $camelcase{$_} = 1;
882 }
883 close($camelcase_file);
884
885 return;
886 }
887
Richard Genoud3645e322014-02-10 14:25:32 -0800888 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700889 $files = `git ls-files "include/*.h"`;
890 @include_files = split('\n', $files);
891 }
892
Joe Perches34456862013-07-03 15:05:34 -0700893 foreach my $file (@include_files) {
894 seed_camelcase_file($file);
895 }
Joe Perches351b2a12013-07-03 15:05:36 -0700896
Joe Perchesc707a812013-07-08 16:00:43 -0700897 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700898 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700899 open(my $camelcase_file, '>', "$camelcase_cache")
900 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700901 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
902 print $camelcase_file ("$_\n");
903 }
904 close($camelcase_file);
905 }
Joe Perches34456862013-07-03 15:05:34 -0700906}
907
Joe Perchesd311cd42014-08-06 16:10:57 -0700908sub git_commit_info {
909 my ($commit, $id, $desc) = @_;
910
911 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
912
913 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
914 $output =~ s/^\s*//gm;
915 my @lines = split("\n", $output);
916
Joe Perches0d7835f2015-02-13 14:38:35 -0800917 return ($id, $desc) if ($#lines < 0);
918
Joe Perchesd311cd42014-08-06 16:10:57 -0700919 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
920# Maybe one day convert this block of bash into something that returns
921# all matching commit ids, but it's very slow...
922#
923# echo "checking commits $1..."
924# git rev-list --remotes | grep -i "^$1" |
925# while read line ; do
926# git log --format='%H %s' -1 $line |
927# echo "commit $(cut -c 1-12,41-)"
928# done
929 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700930 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700931 } else {
932 $id = substr($lines[0], 0, 12);
933 $desc = substr($lines[0], 41);
934 }
935
936 return ($id, $desc);
937}
938
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700939$chk_signoff = 0 if ($file);
940
Andy Whitcroft00df3442007-06-08 13:47:06 -0700941my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800942my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700943my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700944my @fixed_inserted = ();
945my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700946my $fixlinenr = -1;
947
Du, Changbin4a593c32016-05-20 17:04:16 -0700948# If input is git commits, extract all commits from the commit expressions.
949# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
950die "$P: No git repository found\n" if ($git && !-e ".git");
951
952if ($git) {
953 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700954 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700955 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700956 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
957 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700958 } elsif ($commit_expr =~ m/\.\./) {
959 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700960 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700961 $git_range = "-1 $commit_expr";
962 }
963 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
964 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700965 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
966 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700967 my $sha1 = $1;
968 my $subject = $2;
969 unshift(@commits, $sha1);
970 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700971 }
972 }
973 die "$P: no git commits after extraction!\n" if (@commits == 0);
974 @ARGV = @commits;
975}
976
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800977my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700978for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800979 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700980 if ($git) {
981 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
982 die "$P: $filename: git format-patch failed - $!\n";
983 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800984 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700985 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800986 } elsif ($filename eq '-') {
987 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700988 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800989 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700990 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700991 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800992 if ($filename eq '-') {
993 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700994 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700995 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800996 } else {
997 $vname = $filename;
998 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800999 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001000 chomp;
1001 push(@rawlines, $_);
1002 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001003 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001004
1005 if ($#ARGV > 0 && $quiet == 0) {
1006 print '-' x length($vname) . "\n";
1007 print "$vname\n";
1008 print '-' x length($vname) . "\n";
1009 }
1010
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001011 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001012 $exit = 1;
1013 }
1014 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001015 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001016 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001017 @fixed_inserted = ();
1018 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001019 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001020 @modifierListFile = ();
1021 @typeListFile = ();
1022 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001023}
1024
Joe Perchesd8469f12015-06-25 15:03:00 -07001025if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001026 hash_show_words(\%use_type, "Used");
1027 hash_show_words(\%ignore_type, "Ignored");
1028
Joe Perchesd8469f12015-06-25 15:03:00 -07001029 if ($^V lt 5.10.0) {
1030 print << "EOM"
1031
1032NOTE: perl $^V is not modern enough to detect all possible issues.
1033 An upgrade to at least perl v5.10.0 is suggested.
1034EOM
1035 }
1036 if ($exit) {
1037 print << "EOM"
1038
1039NOTE: If any of the errors are false positives, please report
1040 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1041EOM
1042 }
1043}
1044
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001045exit($exit);
1046
1047sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001048 my ($root) = @_;
1049
1050 my @tree_check = (
1051 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1052 "README", "Documentation", "arch", "include", "drivers",
1053 "fs", "init", "ipc", "kernel", "lib", "scripts",
1054 );
1055
1056 foreach my $check (@tree_check) {
1057 if (! -e $root . '/' . $check) {
1058 return 0;
1059 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001060 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001061 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001062}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001063
Joe Perches20112472011-07-25 17:13:23 -07001064sub parse_email {
1065 my ($formatted_email) = @_;
1066
1067 my $name = "";
1068 my $address = "";
1069 my $comment = "";
1070
1071 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1072 $name = $1;
1073 $address = $2;
1074 $comment = $3 if defined $3;
1075 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1076 $address = $1;
1077 $comment = $2 if defined $2;
1078 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1079 $address = $1;
1080 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001081 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001082 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001083 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001084 $name =~ s/^\"|\"$//g;
1085 # If there's a name left after stripping spaces and
1086 # leading quotes, and the address doesn't have both
1087 # leading and trailing angle brackets, the address
1088 # is invalid. ie:
1089 # "joe smith joe@smith.com" bad
1090 # "joe smith <joe@smith.com" bad
1091 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1092 $name = "";
1093 $address = "";
1094 $comment = "";
1095 }
1096 }
1097
Joe Perches3705ce52013-07-03 15:05:31 -07001098 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001099 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001100 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001101 $address =~ s/^\<|\>$//g;
1102
1103 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1104 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1105 $name = "\"$name\"";
1106 }
1107
1108 return ($name, $address, $comment);
1109}
1110
1111sub format_email {
1112 my ($name, $address) = @_;
1113
1114 my $formatted_email;
1115
Joe Perches3705ce52013-07-03 15:05:31 -07001116 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001117 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001118 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001119
1120 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1121 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1122 $name = "\"$name\"";
1123 }
1124
1125 if ("$name" eq "") {
1126 $formatted_email = "$address";
1127 } else {
1128 $formatted_email = "$name <$address>";
1129 }
1130
1131 return $formatted_email;
1132}
1133
Joe Perchesd311cd42014-08-06 16:10:57 -07001134sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001135 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001136
Joe Perchesbd474ca2014-08-06 16:11:10 -07001137 foreach my $path (split(/:/, $ENV{PATH})) {
1138 if (-e "$path/$bin") {
1139 return "$path/$bin";
1140 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001141 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001142
Joe Perchesbd474ca2014-08-06 16:11:10 -07001143 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001144}
1145
Joe Perches000d1cc12011-07-25 17:13:25 -07001146sub which_conf {
1147 my ($conf) = @_;
1148
1149 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1150 if (-e "$path/$conf") {
1151 return "$path/$conf";
1152 }
1153 }
1154
1155 return "";
1156}
1157
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001158sub expand_tabs {
1159 my ($str) = @_;
1160
1161 my $res = '';
1162 my $n = 0;
1163 for my $c (split(//, $str)) {
1164 if ($c eq "\t") {
1165 $res .= ' ';
1166 $n++;
1167 for (; ($n % 8) != 0; $n++) {
1168 $res .= ' ';
1169 }
1170 next;
1171 }
1172 $res .= $c;
1173 $n++;
1174 }
1175
1176 return $res;
1177}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001178sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001179 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001180 return $res;
1181}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001182
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001183sub line_stats {
1184 my ($line) = @_;
1185
1186 # Drop the diff line leader and expand tabs
1187 $line =~ s/^.//;
1188 $line = expand_tabs($line);
1189
1190 # Pick the indent from the front of the line.
1191 my ($white) = ($line =~ /^(\s*)/);
1192
1193 return (length($line), length($white));
1194}
1195
Andy Whitcroft773647a2008-03-28 14:15:58 -07001196my $sanitise_quote = '';
1197
1198sub sanitise_line_reset {
1199 my ($in_comment) = @_;
1200
1201 if ($in_comment) {
1202 $sanitise_quote = '*/';
1203 } else {
1204 $sanitise_quote = '';
1205 }
1206}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001207sub sanitise_line {
1208 my ($line) = @_;
1209
1210 my $res = '';
1211 my $l = '';
1212
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001213 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001214 my $off = 0;
1215 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001216
Andy Whitcroft773647a2008-03-28 14:15:58 -07001217 # Always copy over the diff marker.
1218 $res = substr($line, 0, 1);
1219
1220 for ($off = 1; $off < length($line); $off++) {
1221 $c = substr($line, $off, 1);
1222
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001223 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001224 # and end, all to $;.
1225 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1226 $sanitise_quote = '*/';
1227
1228 substr($res, $off, 2, "$;$;");
1229 $off++;
1230 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001231 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001232 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001233 $sanitise_quote = '';
1234 substr($res, $off, 2, "$;$;");
1235 $off++;
1236 next;
1237 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001238 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1239 $sanitise_quote = '//';
1240
1241 substr($res, $off, 2, $sanitise_quote);
1242 $off++;
1243 next;
1244 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001245
1246 # A \ in a string means ignore the next character.
1247 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1248 $c eq "\\") {
1249 substr($res, $off, 2, 'XX');
1250 $off++;
1251 next;
1252 }
1253 # Regular quotes.
1254 if ($c eq "'" || $c eq '"') {
1255 if ($sanitise_quote eq '') {
1256 $sanitise_quote = $c;
1257
1258 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001259 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001260 } elsif ($sanitise_quote eq $c) {
1261 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001262 }
1263 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001264
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001265 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001266 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1267 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001268 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1269 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001270 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1271 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001272 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001273 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001274 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001275 }
1276
Daniel Walker113f04a2009-09-21 17:04:35 -07001277 if ($sanitise_quote eq '//') {
1278 $sanitise_quote = '';
1279 }
1280
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001281 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001282 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001283 my $clean = 'X' x length($1);
1284 $res =~ s@\<.*\>@<$clean>@;
1285
1286 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001287 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001288 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001289 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001290 }
1291
Joe Perchesdadf6802016-08-02 14:04:33 -07001292 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1293 my $match = $1;
1294 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1295 }
1296
Andy Whitcroft00df3442007-06-08 13:47:06 -07001297 return $res;
1298}
1299
Joe Perchesa6962d72013-04-29 16:18:13 -07001300sub get_quoted_string {
1301 my ($line, $rawline) = @_;
1302
Joe Perches478b1792018-04-10 16:33:34 -07001303 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001304 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001305 return substr($rawline, $-[0], $+[0] - $-[0]);
1306}
1307
Andy Whitcroft8905a672007-11-28 16:21:06 -08001308sub ctx_statement_block {
1309 my ($linenr, $remain, $off) = @_;
1310 my $line = $linenr - 1;
1311 my $blk = '';
1312 my $soff = $off;
1313 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001314 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001315
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001316 my $loff = 0;
1317
Andy Whitcroft8905a672007-11-28 16:21:06 -08001318 my $type = '';
1319 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001320 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001321 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001322 my $c;
1323 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001324
1325 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001326 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001327 @stack = (['', 0]) if ($#stack == -1);
1328
Andy Whitcroft773647a2008-03-28 14:15:58 -07001329 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001330 # If we are about to drop off the end, pull in more
1331 # context.
1332 if ($off >= $len) {
1333 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001334 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001335 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001336 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001337 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001338 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001339 $len = length($blk);
1340 $line++;
1341 last;
1342 }
1343 # Bail if there is no further context.
1344 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001345 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001346 last;
1347 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001348 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1349 $level++;
1350 $type = '#';
1351 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001352 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001353 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001354 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001355 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001356
Andy Whitcroft773647a2008-03-28 14:15:58 -07001357 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001358
1359 # Handle nested #if/#else.
1360 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1361 push(@stack, [ $type, $level ]);
1362 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1363 ($type, $level) = @{$stack[$#stack - 1]};
1364 } elsif ($remainder =~ /^#\s*endif\b/) {
1365 ($type, $level) = @{pop(@stack)};
1366 }
1367
Andy Whitcroft8905a672007-11-28 16:21:06 -08001368 # Statement ends at the ';' or a close '}' at the
1369 # outermost level.
1370 if ($level == 0 && $c eq ';') {
1371 last;
1372 }
1373
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001374 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001375 if ($level == 0 && $coff_set == 0 &&
1376 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1377 $remainder =~ /^(else)(?:\s|{)/ &&
1378 $remainder !~ /^else\s+if\b/) {
1379 $coff = $off + length($1) - 1;
1380 $coff_set = 1;
1381 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1382 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001383 }
1384
Andy Whitcroft8905a672007-11-28 16:21:06 -08001385 if (($type eq '' || $type eq '(') && $c eq '(') {
1386 $level++;
1387 $type = '(';
1388 }
1389 if ($type eq '(' && $c eq ')') {
1390 $level--;
1391 $type = ($level != 0)? '(' : '';
1392
1393 if ($level == 0 && $coff < $soff) {
1394 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001395 $coff_set = 1;
1396 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001397 }
1398 }
1399 if (($type eq '' || $type eq '{') && $c eq '{') {
1400 $level++;
1401 $type = '{';
1402 }
1403 if ($type eq '{' && $c eq '}') {
1404 $level--;
1405 $type = ($level != 0)? '{' : '';
1406
1407 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001408 if (substr($blk, $off + 1, 1) eq ';') {
1409 $off++;
1410 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001411 last;
1412 }
1413 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001414 # Preprocessor commands end at the newline unless escaped.
1415 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1416 $level--;
1417 $type = '';
1418 $off++;
1419 last;
1420 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001421 $off++;
1422 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001423 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001424 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001425 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001426 $line++;
1427 $remain--;
1428 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001429
1430 my $statement = substr($blk, $soff, $off - $soff + 1);
1431 my $condition = substr($blk, $soff, $coff - $soff + 1);
1432
1433 #warn "STATEMENT<$statement>\n";
1434 #warn "CONDITION<$condition>\n";
1435
Andy Whitcroft773647a2008-03-28 14:15:58 -07001436 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001437
1438 return ($statement, $condition,
1439 $line, $remain + 1, $off - $loff + 1, $level);
1440}
1441
Andy Whitcroftcf655042008-03-04 14:28:20 -08001442sub statement_lines {
1443 my ($stmt) = @_;
1444
1445 # Strip the diff line prefixes and rip blank lines at start and end.
1446 $stmt =~ s/(^|\n)./$1/g;
1447 $stmt =~ s/^\s*//;
1448 $stmt =~ s/\s*$//;
1449
1450 my @stmt_lines = ($stmt =~ /\n/g);
1451
1452 return $#stmt_lines + 2;
1453}
1454
1455sub statement_rawlines {
1456 my ($stmt) = @_;
1457
1458 my @stmt_lines = ($stmt =~ /\n/g);
1459
1460 return $#stmt_lines + 2;
1461}
1462
1463sub statement_block_size {
1464 my ($stmt) = @_;
1465
1466 $stmt =~ s/(^|\n)./$1/g;
1467 $stmt =~ s/^\s*{//;
1468 $stmt =~ s/}\s*$//;
1469 $stmt =~ s/^\s*//;
1470 $stmt =~ s/\s*$//;
1471
1472 my @stmt_lines = ($stmt =~ /\n/g);
1473 my @stmt_statements = ($stmt =~ /;/g);
1474
1475 my $stmt_lines = $#stmt_lines + 2;
1476 my $stmt_statements = $#stmt_statements + 1;
1477
1478 if ($stmt_lines > $stmt_statements) {
1479 return $stmt_lines;
1480 } else {
1481 return $stmt_statements;
1482 }
1483}
1484
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001485sub ctx_statement_full {
1486 my ($linenr, $remain, $off) = @_;
1487 my ($statement, $condition, $level);
1488
1489 my (@chunks);
1490
Andy Whitcroftcf655042008-03-04 14:28:20 -08001491 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001492 ($statement, $condition, $linenr, $remain, $off, $level) =
1493 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001494 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001495 push(@chunks, [ $condition, $statement ]);
1496 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1497 return ($level, $linenr, @chunks);
1498 }
1499
1500 # Pull in the following conditional/block pairs and see if they
1501 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001502 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001503 ($statement, $condition, $linenr, $remain, $off, $level) =
1504 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001505 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001506 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001507 #print "C: push\n";
1508 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001509 }
1510
1511 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001512}
1513
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001514sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001515 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001516 my $line;
1517 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001518 my $blk = '';
1519 my @o;
1520 my @c;
1521 my @res = ();
1522
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001523 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001524 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001525 for ($line = $start; $remain > 0; $line++) {
1526 next if ($rawlines[$line] =~ /^-/);
1527 $remain--;
1528
1529 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001530
1531 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001532 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001533 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001534 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001535 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001536 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001537 $level = pop(@stack);
1538 }
1539
Andy Whitcroft01464f32010-10-26 14:23:19 -07001540 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001541 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1542 if ($off > 0) {
1543 $off--;
1544 next;
1545 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001546
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001547 if ($c eq $close && $level > 0) {
1548 $level--;
1549 last if ($level == 0);
1550 } elsif ($c eq $open) {
1551 $level++;
1552 }
1553 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001554
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001555 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001556 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001557 }
1558
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001559 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001560 }
1561
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001562 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001563}
1564sub ctx_block_outer {
1565 my ($linenr, $remain) = @_;
1566
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001567 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1568 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001569}
1570sub ctx_block {
1571 my ($linenr, $remain) = @_;
1572
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001573 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1574 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001575}
1576sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001577 my ($linenr, $remain, $off) = @_;
1578
1579 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1580 return @r;
1581}
1582sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001583 my ($linenr, $remain) = @_;
1584
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001585 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001586}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001587sub ctx_statement_level {
1588 my ($linenr, $remain, $off) = @_;
1589
1590 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1591}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001592
1593sub ctx_locate_comment {
1594 my ($first_line, $end_line) = @_;
1595
1596 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001597 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001598 return $current_comment if (defined $current_comment);
1599
1600 # Look through the context and try and figure out if there is a
1601 # comment.
1602 my $in_comment = 0;
1603 $current_comment = '';
1604 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001605 my $line = $rawlines[$linenr - 1];
1606 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001607 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1608 $in_comment = 1;
1609 }
1610 if ($line =~ m@/\*@) {
1611 $in_comment = 1;
1612 }
1613 if (!$in_comment && $current_comment ne '') {
1614 $current_comment = '';
1615 }
1616 $current_comment .= $line . "\n" if ($in_comment);
1617 if ($line =~ m@\*/@) {
1618 $in_comment = 0;
1619 }
1620 }
1621
1622 chomp($current_comment);
1623 return($current_comment);
1624}
1625sub ctx_has_comment {
1626 my ($first_line, $end_line) = @_;
1627 my $cmt = ctx_locate_comment($first_line, $end_line);
1628
Andy Whitcroft00df3442007-06-08 13:47:06 -07001629 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001630 ##print "CMMT: $cmt\n";
1631
1632 return ($cmt ne '');
1633}
1634
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001635sub raw_line {
1636 my ($linenr, $cnt) = @_;
1637
1638 my $offset = $linenr - 1;
1639 $cnt++;
1640
1641 my $line;
1642 while ($cnt) {
1643 $line = $rawlines[$offset++];
1644 next if (defined($line) && $line =~ /^-/);
1645 $cnt--;
1646 }
1647
1648 return $line;
1649}
1650
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001651sub get_stat_real {
1652 my ($linenr, $lc) = @_;
1653
1654 my $stat_real = raw_line($linenr, 0);
1655 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1656 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1657 }
1658
1659 return $stat_real;
1660}
1661
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001662sub get_stat_here {
1663 my ($linenr, $cnt, $here) = @_;
1664
1665 my $herectx = $here . "\n";
1666 for (my $n = 0; $n < $cnt; $n++) {
1667 $herectx .= raw_line($linenr, $n) . "\n";
1668 }
1669
1670 return $herectx;
1671}
1672
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001673sub cat_vet {
1674 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001675 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001676
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001677 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001678 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1679 $res .= $1;
1680 if ($2 ne '') {
1681 $coded = sprintf("^%c", unpack('C', $2) + 64);
1682 $res .= $coded;
1683 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001684 }
1685 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001686
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001687 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001688}
1689
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001690my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001691my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001692my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001693my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001694
1695sub annotate_reset {
1696 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001697 $av_pending = '_';
1698 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001699 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001700}
1701
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001702sub annotate_values {
1703 my ($stream, $type) = @_;
1704
1705 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001706 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001707 my $cur = $stream;
1708
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001709 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001710
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001711 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001712 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001713 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001714 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001715 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001716 print "WS($1)\n" if ($dbg_values > 1);
1717 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001718 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001719 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001720 }
1721
Florian Micklerc023e4732011-01-12 16:59:58 -08001722 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001723 print "CAST($1)\n" if ($dbg_values > 1);
1724 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001725 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001726
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001727 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001728 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001729 $type = 'T';
1730
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001731 } elsif ($cur =~ /^($Modifier)\s*/) {
1732 print "MODIFIER($1)\n" if ($dbg_values > 1);
1733 $type = 'T';
1734
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001735 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001736 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001737 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001738 push(@av_paren_type, $type);
1739 if ($2 ne '') {
1740 $av_pending = 'N';
1741 }
1742 $type = 'E';
1743
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001744 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001745 print "UNDEF($1)\n" if ($dbg_values > 1);
1746 $av_preprocessor = 1;
1747 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001748
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001749 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001750 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001751 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001752
1753 push(@av_paren_type, $type);
1754 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001755 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001756
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001757 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001758 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1759 $av_preprocessor = 1;
1760
1761 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1762
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001763 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001764
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001765 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001766 print "PRE_END($1)\n" if ($dbg_values > 1);
1767
1768 $av_preprocessor = 1;
1769
1770 # Assume all arms of the conditional end as this
1771 # one does, and continue as if the #endif was not here.
1772 pop(@av_paren_type);
1773 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001774 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001775
1776 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001777 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001778
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001779 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1780 print "ATTR($1)\n" if ($dbg_values > 1);
1781 $av_pending = $type;
1782 $type = 'N';
1783
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001784 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001785 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001786 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001787 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001788 }
1789 $type = 'N';
1790
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001791 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001792 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001793 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001794 $type = 'N';
1795
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001796 } elsif ($cur =~/^(case)/o) {
1797 print "CASE($1)\n" if ($dbg_values > 1);
1798 $av_pend_colon = 'C';
1799 $type = 'N';
1800
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001801 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001802 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001803 $type = 'N';
1804
1805 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001806 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001807 push(@av_paren_type, $av_pending);
1808 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001809 $type = 'N';
1810
1811 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001812 my $new_type = pop(@av_paren_type);
1813 if ($new_type ne '_') {
1814 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001815 print "PAREN('$1') -> $type\n"
1816 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001817 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001818 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001819 }
1820
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001821 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001822 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001823 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001824 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001825
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001826 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1827 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001828 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001829 } elsif ($type eq 'E') {
1830 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001831 }
1832 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1833 $type = 'V';
1834
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001835 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001836 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001837 $type = 'V';
1838
1839 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001840 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001841 $type = 'N';
1842
Andy Whitcroftcf655042008-03-04 14:28:20 -08001843 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001844 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001845 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001846 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001847
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001848 } elsif ($cur =~/^(,)/) {
1849 print "COMMA($1)\n" if ($dbg_values > 1);
1850 $type = 'C';
1851
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001852 } elsif ($cur =~ /^(\?)/o) {
1853 print "QUESTION($1)\n" if ($dbg_values > 1);
1854 $type = 'N';
1855
1856 } elsif ($cur =~ /^(:)/o) {
1857 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1858
1859 substr($var, length($res), 1, $av_pend_colon);
1860 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1861 $type = 'E';
1862 } else {
1863 $type = 'N';
1864 }
1865 $av_pend_colon = 'O';
1866
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001867 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001868 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001869 $type = 'N';
1870
Andy Whitcroft0d413862008-10-15 22:02:16 -07001871 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001872 my $variant;
1873
1874 print "OPV($1)\n" if ($dbg_values > 1);
1875 if ($type eq 'V') {
1876 $variant = 'B';
1877 } else {
1878 $variant = 'U';
1879 }
1880
1881 substr($var, length($res), 1, $variant);
1882 $type = 'N';
1883
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001884 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001885 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001886 if ($1 ne '++' && $1 ne '--') {
1887 $type = 'N';
1888 }
1889
1890 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001891 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001892 }
1893 if (defined $1) {
1894 $cur = substr($cur, length($1));
1895 $res .= $type x length($1);
1896 }
1897 }
1898
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001899 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001900}
1901
Andy Whitcroft8905a672007-11-28 16:21:06 -08001902sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001903 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001904 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001905 ^(?:
1906 $Modifier|
1907 $Storage|
1908 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001909 DEFINE_\S+
1910 )$|
1911 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001912 goto|
1913 return|
1914 case|
1915 else|
1916 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001917 do|
1918 \#|
1919 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001920 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001921 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001922 )}x;
1923 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1924 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001925 # Check for modifiers.
1926 $possible =~ s/\s*$Storage\s*//g;
1927 $possible =~ s/\s*$Sparse\s*//g;
1928 if ($possible =~ /^\s*$/) {
1929
1930 } elsif ($possible =~ /\s/) {
1931 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001932 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001933 if ($modifier !~ $notPermitted) {
1934 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001935 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001936 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001937 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001938
1939 } else {
1940 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001941 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001942 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001943 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001944 } else {
1945 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001946 }
1947}
1948
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001949my $prefix = '';
1950
Joe Perches000d1cc12011-07-25 17:13:25 -07001951sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001952 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001953
Alexey Dobriyan522b8372017-02-27 14:30:05 -08001954 $type =~ tr/[a-z]/[A-Z]/;
1955
Joe Perchescbec18a2014-04-03 14:49:19 -07001956 return defined $use_type{$type} if (scalar keys %use_type > 0);
1957
1958 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001959}
1960
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001961sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001962 my ($level, $type, $msg) = @_;
1963
1964 if (!show_type($type) ||
1965 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001966 return 0;
1967 }
Joe Perches57230292015-06-25 15:03:03 -07001968 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07001969 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07001970 if ($level eq 'ERROR') {
1971 $output .= RED;
1972 } elsif ($level eq 'WARNING') {
1973 $output .= YELLOW;
1974 } else {
1975 $output .= GREEN;
1976 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001977 }
Joe Perches57230292015-06-25 15:03:03 -07001978 $output .= $prefix . $level . ':';
1979 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07001980 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07001981 $output .= "$type:";
1982 }
John Brooks737c0762017-07-10 15:52:24 -07001983 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07001984 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001985
1986 if ($showfile) {
1987 my @lines = split("\n", $output, -1);
1988 splice(@lines, 1, 1);
1989 $output = join("\n", @lines);
1990 }
Joe Perches57230292015-06-25 15:03:03 -07001991 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001992
Joe Perches57230292015-06-25 15:03:03 -07001993 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001994
1995 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001996}
Joe Perchescbec18a2014-04-03 14:49:19 -07001997
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001998sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001999 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002000}
Joe Perches000d1cc12011-07-25 17:13:25 -07002001
Joe Perchesd752fcc2014-08-06 16:11:05 -07002002sub fixup_current_range {
2003 my ($lineRef, $offset, $length) = @_;
2004
2005 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2006 my $o = $1;
2007 my $l = $2;
2008 my $no = $o + $offset;
2009 my $nl = $l + $length;
2010 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2011 }
2012}
2013
2014sub fix_inserted_deleted_lines {
2015 my ($linesRef, $insertedRef, $deletedRef) = @_;
2016
2017 my $range_last_linenr = 0;
2018 my $delta_offset = 0;
2019
2020 my $old_linenr = 0;
2021 my $new_linenr = 0;
2022
2023 my $next_insert = 0;
2024 my $next_delete = 0;
2025
2026 my @lines = ();
2027
2028 my $inserted = @{$insertedRef}[$next_insert++];
2029 my $deleted = @{$deletedRef}[$next_delete++];
2030
2031 foreach my $old_line (@{$linesRef}) {
2032 my $save_line = 1;
2033 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002034 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002035 $delta_offset = 0;
2036 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2037 $range_last_linenr = $new_linenr;
2038 fixup_current_range(\$line, $delta_offset, 0);
2039 }
2040
2041 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2042 $deleted = @{$deletedRef}[$next_delete++];
2043 $save_line = 0;
2044 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2045 }
2046
2047 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2048 push(@lines, ${$inserted}{'LINE'});
2049 $inserted = @{$insertedRef}[$next_insert++];
2050 $new_linenr++;
2051 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2052 }
2053
2054 if ($save_line) {
2055 push(@lines, $line);
2056 $new_linenr++;
2057 }
2058
2059 $old_linenr++;
2060 }
2061
2062 return @lines;
2063}
2064
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002065sub fix_insert_line {
2066 my ($linenr, $line) = @_;
2067
2068 my $inserted = {
2069 LINENR => $linenr,
2070 LINE => $line,
2071 };
2072 push(@fixed_inserted, $inserted);
2073}
2074
2075sub fix_delete_line {
2076 my ($linenr, $line) = @_;
2077
2078 my $deleted = {
2079 LINENR => $linenr,
2080 LINE => $line,
2081 };
2082
2083 push(@fixed_deleted, $deleted);
2084}
2085
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002086sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002087 my ($type, $msg) = @_;
2088
2089 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002090 our $clean = 0;
2091 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002092 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002093 }
Joe Perches3705ce52013-07-03 15:05:31 -07002094 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002095}
2096sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002097 my ($type, $msg) = @_;
2098
2099 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002100 our $clean = 0;
2101 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002102 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002103 }
Joe Perches3705ce52013-07-03 15:05:31 -07002104 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002105}
2106sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002107 my ($type, $msg) = @_;
2108
2109 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002110 our $clean = 0;
2111 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002112 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002113 }
Joe Perches3705ce52013-07-03 15:05:31 -07002114 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002115}
2116
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002117sub check_absolute_file {
2118 my ($absolute, $herecurr) = @_;
2119 my $file = $absolute;
2120
2121 ##print "absolute<$absolute>\n";
2122
2123 # See if any suffix of this path is a path within the tree.
2124 while ($file =~ s@^[^/]*/@@) {
2125 if (-f "$root/$file") {
2126 ##print "file<$file>\n";
2127 last;
2128 }
2129 }
2130 if (! -f _) {
2131 return 0;
2132 }
2133
2134 # It is, so see if the prefix is acceptable.
2135 my $prefix = $absolute;
2136 substr($prefix, -length($file)) = '';
2137
2138 ##print "prefix<$prefix>\n";
2139 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002140 WARN("USE_RELATIVE_PATH",
2141 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002142 }
2143}
2144
Joe Perches3705ce52013-07-03 15:05:31 -07002145sub trim {
2146 my ($string) = @_;
2147
Joe Perchesb34c6482013-09-11 14:24:01 -07002148 $string =~ s/^\s+|\s+$//g;
2149
2150 return $string;
2151}
2152
2153sub ltrim {
2154 my ($string) = @_;
2155
2156 $string =~ s/^\s+//;
2157
2158 return $string;
2159}
2160
2161sub rtrim {
2162 my ($string) = @_;
2163
2164 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002165
2166 return $string;
2167}
2168
Joe Perches52ea8502013-11-12 15:10:09 -08002169sub string_find_replace {
2170 my ($string, $find, $replace) = @_;
2171
2172 $string =~ s/$find/$replace/g;
2173
2174 return $string;
2175}
2176
Joe Perches3705ce52013-07-03 15:05:31 -07002177sub tabify {
2178 my ($leading) = @_;
2179
2180 my $source_indent = 8;
2181 my $max_spaces_before_tab = $source_indent - 1;
2182 my $spaces_to_tab = " " x $source_indent;
2183
2184 #convert leading spaces to tabs
2185 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2186 #Remove spaces before a tab
2187 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2188
2189 return "$leading";
2190}
2191
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002192sub pos_last_openparen {
2193 my ($line) = @_;
2194
2195 my $pos = 0;
2196
2197 my $opens = $line =~ tr/\(/\(/;
2198 my $closes = $line =~ tr/\)/\)/;
2199
2200 my $last_openparen = 0;
2201
2202 if (($opens == 0) || ($closes >= $opens)) {
2203 return -1;
2204 }
2205
2206 my $len = length($line);
2207
2208 for ($pos = 0; $pos < $len; $pos++) {
2209 my $string = substr($line, $pos);
2210 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2211 $pos += length($1) - 1;
2212 } elsif (substr($line, $pos, 1) eq '(') {
2213 $last_openparen = $pos;
2214 } elsif (index($string, '(') == -1) {
2215 last;
2216 }
2217 }
2218
Joe Perches91cb5192014-04-03 14:49:32 -07002219 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002220}
2221
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002222sub process {
2223 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002224
2225 my $linenr=0;
2226 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002227 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002228 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002229 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002230
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002231 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002232 my $indent;
2233 my $previndent=0;
2234 my $stashindent=0;
2235
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002236 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002237 my $signoff = 0;
2238 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002239 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002240 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002241 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches77cb8542017-02-24 15:01:28 -08002242 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002243 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002244 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002245 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002246 my $non_utf8_charset = 0;
2247
Joe Perches365dd4e2014-08-06 16:10:42 -07002248 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002249 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002250
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002251 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002252 our $cnt_lines = 0;
2253 our $cnt_error = 0;
2254 our $cnt_warn = 0;
2255 our $cnt_chk = 0;
2256
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002257 # Trace the real file/line as we go.
2258 my $realfile = '';
2259 my $realline = 0;
2260 my $realcnt = 0;
2261 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002262 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002263 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002264 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002265 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002266 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002267
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002268 my $prev_values = 'E';
2269
2270 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002271 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002272 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002273 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002274 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002275
Joe Perches7e51f192013-09-11 14:23:57 -07002276 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002277
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002278 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002279 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002280 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002281 my @setup_docs = ();
2282 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002283
Joe Perchesd8b07712013-11-12 15:10:06 -08002284 my $camelcase_file_seeded = 0;
2285
Rob Herring9f3a8992018-04-10 16:33:13 -07002286 my $checklicenseline = 1;
2287
Andy Whitcroft773647a2008-03-28 14:15:58 -07002288 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002289 my $line;
2290 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002291 $linenr++;
2292 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002293
Joe Perches3705ce52013-07-03 15:05:31 -07002294 push(@fixed, $rawline) if ($fix);
2295
Andy Whitcroft773647a2008-03-28 14:15:58 -07002296 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002297 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002298 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002299 $setup_docs = 1;
2300 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002301 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002302 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002303 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002304 $realline=$1-1;
2305 if (defined $2) {
2306 $realcnt=$3+1;
2307 } else {
2308 $realcnt=1+1;
2309 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002310 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002311
2312 # Guestimate if this is a continuing comment. Run
2313 # the context looking for a comment "edge". If this
2314 # edge is a close comment then we must be in a comment
2315 # at context start.
2316 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002317 my $cnt = $realcnt;
2318 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2319 next if (defined $rawlines[$ln - 1] &&
2320 $rawlines[$ln - 1] =~ /^-/);
2321 $cnt--;
2322 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002323 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002324 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2325 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2326 ($edge) = $1;
2327 last;
2328 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002329 }
2330 if (defined $edge && $edge eq '*/') {
2331 $in_comment = 1;
2332 }
2333
2334 # Guestimate if this is a continuing comment. If this
2335 # is the start of a diff block and this line starts
2336 # ' *' then it is very likely a comment.
2337 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002338 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002339 {
2340 $in_comment = 1;
2341 }
2342
2343 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2344 sanitise_line_reset($in_comment);
2345
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002346 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002347 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002348 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002349 $line = sanitise_line($rawline);
2350 }
2351 push(@lines, $line);
2352
2353 if ($realcnt > 1) {
2354 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2355 } else {
2356 $realcnt = 0;
2357 }
2358
2359 #print "==>$rawline\n";
2360 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002361
2362 if ($setup_docs && $line =~ /^\+/) {
2363 push(@setup_docs, $line);
2364 }
2365 }
2366
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002367 $prefix = '';
2368
Andy Whitcroft773647a2008-03-28 14:15:58 -07002369 $realcnt = 0;
2370 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002371 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002372 foreach my $line (@lines) {
2373 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002374 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002375 my $sline = $line; #copy of $line
2376 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002377
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002378 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002379
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002380#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002381 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002382 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2383 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002384 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002385 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002386 $realline=$1-1;
2387 if (defined $2) {
2388 $realcnt=$3+1;
2389 } else {
2390 $realcnt=1+1;
2391 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002392 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002393 $prev_values = 'E';
2394
Andy Whitcroft773647a2008-03-28 14:15:58 -07002395 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002396 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002397 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002398 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002399 if ($context =~ /\b(\w+)\s*\(/) {
2400 $context_function = $1;
2401 } else {
2402 undef $context_function;
2403 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002404 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002405
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002406# track the line number as we move through the hunk, note that
2407# new versions of GNU diff omit the leading space on completely
2408# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002409 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002410 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002411 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002412
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002413 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002414 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002415
2416 # Track the previous line.
2417 ($prevline, $stashline) = ($stashline, $line);
2418 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002419 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2420
Andy Whitcroft773647a2008-03-28 14:15:58 -07002421 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002422
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002423 } elsif ($realcnt == 1) {
2424 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002425 }
2426
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002427 my $hunk_line = ($realcnt != 0);
2428
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002429 $here = "#$linenr: " if (!$file);
2430 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002431
Joe Perches2ac73b42014-06-04 16:12:05 -07002432 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002433 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002434 if ($line =~ /^diff --git.*?(\S+)$/) {
2435 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002436 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002437 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002438 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002439 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002440 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002441 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002442 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002443
2444 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002445 if (!$file && $tree && $p1_prefix ne '' &&
2446 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002447 WARN("PATCH_PREFIX",
2448 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002449 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002450
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002451 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002452 ERROR("MODIFIED_INCLUDE_ASM",
2453 "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 -07002454 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002455 $found_file = 1;
2456 }
2457
Joe Perches34d88152015-06-25 15:03:05 -07002458#make up the handle for any error we report on this line
2459 if ($showfile) {
2460 $prefix = "$realfile:$realline: "
2461 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002462 if ($file) {
2463 $prefix = "$filename:$realline: ";
2464 } else {
2465 $prefix = "$filename:$linenr: ";
2466 }
Joe Perches34d88152015-06-25 15:03:05 -07002467 }
2468
Joe Perches2ac73b42014-06-04 16:12:05 -07002469 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002470 if (is_maintained_obsolete($realfile)) {
2471 WARN("OBSOLETE",
2472 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2473 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002474 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002475 $check = 1;
2476 } else {
2477 $check = $check_orig;
2478 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002479 $checklicenseline = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002480 next;
2481 }
2482
Randy Dunlap389834b2007-06-08 13:47:03 -07002483 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002484
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002485 my $hereline = "$here\n$rawline\n";
2486 my $herecurr = "$here\n$rawline\n";
2487 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002488
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002489 $cnt_lines++ if ($realcnt != 0);
2490
Joe Perchese518e9a2015-06-25 15:03:27 -07002491# Check if the commit log has what seems like a diff which can confuse patch
2492 if ($in_commit_log && !$commit_log_has_diff &&
2493 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2494 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2495 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2496 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2497 ERROR("DIFF_IN_COMMIT_MSG",
2498 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2499 $commit_log_has_diff = 1;
2500 }
2501
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002502# Check for incorrect file permissions
2503 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2504 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002505 if ($realfile !~ m@scripts/@ &&
2506 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002507 ERROR("EXECUTE_PERMISSIONS",
2508 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002509 }
2510 }
2511
Joe Perches20112472011-07-25 17:13:23 -07002512# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002513 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002514 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002515 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002516 }
2517
Joe Perchese0d975b2014-12-10 15:51:49 -08002518# Check if MAINTAINERS is being updated. If so, there's probably no need to
2519# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2520 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2521 $reported_maintainer_file = 1;
2522 }
2523
Joe Perches20112472011-07-25 17:13:23 -07002524# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002525 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002526 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002527 my $space_before = $1;
2528 my $sign_off = $2;
2529 my $space_after = $3;
2530 my $email = $4;
2531 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2532
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002533 if ($sign_off !~ /$signature_tags/) {
2534 WARN("BAD_SIGN_OFF",
2535 "Non-standard signature: $sign_off\n" . $herecurr);
2536 }
Joe Perches20112472011-07-25 17:13:23 -07002537 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002538 if (WARN("BAD_SIGN_OFF",
2539 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2540 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002541 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002542 "$ucfirst_sign_off $email";
2543 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002544 }
Joe Perches20112472011-07-25 17:13:23 -07002545 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002546 if (WARN("BAD_SIGN_OFF",
2547 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2548 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002549 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002550 "$ucfirst_sign_off $email";
2551 }
2552
Joe Perches20112472011-07-25 17:13:23 -07002553 }
2554 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002555 if (WARN("BAD_SIGN_OFF",
2556 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2557 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002558 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002559 "$ucfirst_sign_off $email";
2560 }
Joe Perches20112472011-07-25 17:13:23 -07002561 }
2562
2563 my ($email_name, $email_address, $comment) = parse_email($email);
2564 my $suggested_email = format_email(($email_name, $email_address));
2565 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002566 ERROR("BAD_SIGN_OFF",
2567 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002568 } else {
2569 my $dequoted = $suggested_email;
2570 $dequoted =~ s/^"//;
2571 $dequoted =~ s/" </ </;
2572 # Don't force email to have quotes
2573 # Allow just an angle bracketed address
2574 if ("$dequoted$comment" ne $email &&
2575 "<$email_address>$comment" ne $email &&
2576 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002577 WARN("BAD_SIGN_OFF",
2578 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002579 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002580 }
Joe Perches7e51f192013-09-11 14:23:57 -07002581
2582# Check for duplicate signatures
2583 my $sig_nospace = $line;
2584 $sig_nospace =~ s/\s//g;
2585 $sig_nospace = lc($sig_nospace);
2586 if (defined $signatures{$sig_nospace}) {
2587 WARN("BAD_SIGN_OFF",
2588 "Duplicate signature\n" . $herecurr);
2589 } else {
2590 $signatures{$sig_nospace} = 1;
2591 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002592 }
2593
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002594# Check email subject for common tools that don't need to be mentioned
2595 if ($in_header_lines &&
2596 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2597 WARN("EMAIL_SUBJECT",
2598 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2599 }
2600
Joe Perches9b3189e2014-06-04 16:12:10 -07002601# Check for old stable address
2602 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2603 ERROR("STABLE_ADDRESS",
2604 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2605 }
2606
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002607# Check for unwanted Gerrit info
2608 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2609 ERROR("GERRIT_CHANGE_ID",
2610 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2611 }
2612
Joe Perches369c8dd2015-11-06 16:31:34 -08002613# Check if the commit log is in a possible stack dump
2614 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2615 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2616 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2617 # timestamp
2618 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2619 # stack dump address
2620 $commit_log_possible_stack_dump = 1;
2621 }
2622
Joe Perches2a076f42015-04-16 12:44:28 -07002623# Check for line lengths > 75 in commit log, warn once
2624 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002625 length($line) > 75 &&
2626 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2627 # file delta changes
2628 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2629 # filename then :
2630 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2631 # A Fixes: or Link: line
2632 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002633 WARN("COMMIT_LOG_LONG_LINE",
2634 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2635 $commit_log_long_line = 1;
2636 }
2637
Joe Perchesbf4daf12015-09-09 15:37:50 -07002638# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002639 if ($in_commit_log && $commit_log_possible_stack_dump &&
2640 $line =~ /^\s*$/) {
2641 $commit_log_possible_stack_dump = 0;
2642 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002643
Joe Perches0d7835f2015-02-13 14:38:35 -08002644# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002645 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002646 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002647 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002648 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002649 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002650 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2651 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002652 my $init_char = "c";
2653 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002654 my $short = 1;
2655 my $long = 0;
2656 my $case = 1;
2657 my $space = 1;
2658 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002659 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002660 my $id = '0123456789ab';
2661 my $orig_desc = "commit description";
2662 my $description = "";
2663
Joe Perchesfe043ea2015-09-09 15:37:25 -07002664 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2665 $init_char = $1;
2666 $orig_commit = lc($2);
2667 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2668 $orig_commit = lc($1);
2669 }
2670
Joe Perches0d7835f2015-02-13 14:38:35 -08002671 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2672 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2673 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2674 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2675 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2676 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002677 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002678 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2679 defined $rawlines[$linenr] &&
2680 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2681 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002682 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002683 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2684 defined $rawlines[$linenr] &&
2685 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2686 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2687 $orig_desc = $1;
2688 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2689 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002690 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002691 }
2692
2693 ($id, $description) = git_commit_info($orig_commit,
2694 $id, $orig_desc);
2695
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002696 if (defined($id) &&
2697 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002698 ERROR("GIT_COMMIT_ID",
2699 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2700 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002701 }
2702
Joe Perches13f19372014-08-06 16:10:59 -07002703# Check for added, moved or deleted files
2704 if (!$reported_maintainer_file && !$in_commit_log &&
2705 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2706 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2707 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2708 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002709 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002710 $reported_maintainer_file = 1;
2711 WARN("FILE_PATH_CHANGES",
2712 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2713 }
2714
Andy Whitcroft00df3442007-06-08 13:47:06 -07002715# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002716 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002717 ERROR("CORRUPTED_PATCH",
2718 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002719 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002720 }
2721
2722# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2723 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002724 $rawline !~ m/^$UTF8*$/) {
2725 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2726
2727 my $blank = copy_spacing($rawline);
2728 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2729 my $hereptr = "$hereline$ptr\n";
2730
Joe Perches34d99212011-07-25 17:13:26 -07002731 CHK("INVALID_UTF8",
2732 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002733 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002734
Joe Perches15662b32011-10-31 17:13:12 -07002735# Check if it's the start of a commit log
2736# (not a header line and we haven't seen the patch filename)
2737 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002738 !($rawline =~ /^\s+(?:\S|$)/ ||
2739 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002740 $in_header_lines = 0;
2741 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002742 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002743 }
2744
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002745# Check if there is UTF-8 in a commit log when a mail header has explicitly
2746# declined it, i.e defined some charset where it is missing.
2747 if ($in_header_lines &&
2748 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2749 $1 !~ /utf-8/i) {
2750 $non_utf8_charset = 1;
2751 }
2752
2753 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002754 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002755 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002756 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2757 }
2758
Joe Perchesd6430f72016-12-12 16:46:28 -08002759# Check for absolute kernel paths in commit message
2760 if ($tree && $in_commit_log) {
2761 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2762 my $file = $1;
2763
2764 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2765 check_absolute_file($1, $herecurr)) {
2766 #
2767 } else {
2768 check_absolute_file($file, $herecurr);
2769 }
2770 }
2771 }
2772
Kees Cook66b47b42014-10-13 15:51:57 -07002773# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002774 if (defined($misspellings) &&
2775 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002776 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002777 my $typo = $1;
2778 my $typo_fix = $spelling_fix{lc($typo)};
2779 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2780 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002781 my $msg_level = \&WARN;
2782 $msg_level = \&CHK if ($file);
2783 if (&{$msg_level}("TYPO_SPELLING",
2784 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002785 $fix) {
2786 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2787 }
2788 }
2789 }
2790
Andy Whitcroft306708542008-10-15 22:02:28 -07002791# ignore non-hunk lines and lines being removed
2792 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002793
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002794#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002795 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002796 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002797 if (ERROR("DOS_LINE_ENDINGS",
2798 "DOS line endings\n" . $herevet) &&
2799 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002800 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002801 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002802 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2803 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002804 if (ERROR("TRAILING_WHITESPACE",
2805 "trailing whitespace\n" . $herevet) &&
2806 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002807 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002808 }
2809
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002810 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002811 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002812
Josh Triplett4783f892013-11-12 15:10:12 -08002813# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002814 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002815 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002816 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2817 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002818 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07002819 my $msg_level = \&ERROR;
2820 $msg_level = \&CHK if ($file);
2821 &{$msg_level}("FSF_MAILING_ADDRESS",
2822 "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 -08002823 }
2824
Andi Kleen33549572010-05-24 14:33:29 -07002825# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002826# Only applies when adding the entry originally, after that we do not have
2827# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002828 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01002829 # 'choice' is usually the last thing on the line (though
2830 # Kconfig supports named choices), so use a word boundary
2831 # (\b) rather than a whitespace character (\s)
2832 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07002833 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002834 my $cnt = $realcnt;
2835 my $ln = $linenr + 1;
2836 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002837 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002838 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002839 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002840 $f = $lines[$ln - 1];
2841 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2842 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002843
2844 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002845 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002846
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01002847 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002848 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01002849 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
2850 if ($lines[$ln - 1] =~ "---help---") {
2851 WARN("CONFIG_DESCRIPTION",
2852 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
2853 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002854 $length = -1;
2855 }
2856
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002857 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002858 $f =~ s/#.*//;
2859 $f =~ s/^\s+//;
2860 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01002861
2862 # This only checks context lines in the patch
2863 # and so hopefully shouldn't trigger false
2864 # positives, even though some of these are
2865 # common words in help texts
2866 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2867 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002868 $is_end = 1;
2869 last;
2870 }
Andi Kleen33549572010-05-24 14:33:29 -07002871 $length++;
2872 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002873 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2874 WARN("CONFIG_DESCRIPTION",
2875 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2876 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002877 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002878 }
2879
Joe Perches628f91a2017-07-10 15:52:07 -07002880# check for MAINTAINERS entries that don't have the right form
2881 if ($realfile =~ /^MAINTAINERS$/ &&
2882 $rawline =~ /^\+[A-Z]:/ &&
2883 $rawline !~ /^\+[A-Z]:\t\S/) {
2884 if (WARN("MAINTAINERS_STYLE",
2885 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2886 $fix) {
2887 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2888 }
2889 }
2890
Christoph Jaeger327953e2015-02-13 14:38:29 -08002891# discourage the use of boolean for type definition attributes of Kconfig options
2892 if ($realfile =~ /Kconfig/ &&
2893 $line =~ /^\+\s*\bboolean\b/) {
2894 WARN("CONFIG_TYPE_BOOLEAN",
2895 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2896 }
2897
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002898 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2899 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2900 my $flag = $1;
2901 my $replacement = {
2902 'EXTRA_AFLAGS' => 'asflags-y',
2903 'EXTRA_CFLAGS' => 'ccflags-y',
2904 'EXTRA_CPPFLAGS' => 'cppflags-y',
2905 'EXTRA_LDFLAGS' => 'ldflags-y',
2906 };
2907
2908 WARN("DEPRECATED_VARIABLE",
2909 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2910 }
2911
Rob Herringbff5da42014-01-23 15:54:51 -08002912# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002913 if (defined $root &&
2914 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2915 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2916
Rob Herringbff5da42014-01-23 15:54:51 -08002917 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2918
Florian Vaussardcc933192014-04-03 14:49:27 -07002919 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2920 my $vp_file = $dt_path . "vendor-prefixes.txt";
2921
Rob Herringbff5da42014-01-23 15:54:51 -08002922 foreach my $compat (@compats) {
2923 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002924 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2925 my $compat3 = $compat;
2926 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2927 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002928 if ( $? >> 8 ) {
2929 WARN("UNDOCUMENTED_DT_STRING",
2930 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2931 }
2932
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002933 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2934 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002935 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002936 if ( $? >> 8 ) {
2937 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002938 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002939 }
2940 }
2941 }
2942
Rob Herring9f3a8992018-04-10 16:33:13 -07002943# check for using SPDX license tag at beginning of files
2944 if ($realline == $checklicenseline) {
2945 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
2946 $checklicenseline = 2;
2947 } elsif ($rawline =~ /^\+/) {
2948 my $comment = "";
2949 if ($realfile =~ /\.(h|s|S)$/) {
2950 $comment = '/*';
2951 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
2952 $comment = '//';
2953 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
2954 $comment = '#';
2955 } elsif ($realfile =~ /\.rst$/) {
2956 $comment = '..';
2957 }
2958
2959 if ($comment !~ /^$/ &&
2960 $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
2961 WARN("SPDX_LICENSE_TAG",
2962 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
2963 }
2964 }
2965 }
2966
Andy Whitcroft5368df202008-10-15 22:02:27 -07002967# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08002968 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002969
Joe Perches47e0c882015-06-25 15:02:57 -07002970# line length limit (with some exclusions)
2971#
2972# There are a few types of lines that may extend beyond $max_line_length:
2973# logging functions like pr_info that end in a string
2974# lines with a single string
2975# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08002976# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07002977#
2978# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07002979# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07002980# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2981# LONG_LINE all other lines longer than $max_line_length
2982#
2983# if LONG_LINE is ignored, the other 2 types are also ignored
2984#
2985
Joe Perchesb4749e92015-07-17 16:24:01 -07002986 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07002987 my $msg_type = "LONG_LINE";
2988
2989 # Check the allowed long line types first
2990
2991 # logging functions that end in a string that starts
2992 # before $max_line_length
2993 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2994 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2995 $msg_type = "";
2996
2997 # lines with only strings (w/ possible termination)
2998 # #defines with only strings
2999 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3000 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3001 $msg_type = "";
3002
Joe Perchescc147502017-11-17 15:28:44 -08003003 # More special cases
3004 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3005 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003006 $msg_type = "";
3007
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003008 # URL ($rawline is used in case the URL is in a comment)
3009 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3010 $msg_type = "";
3011
Joe Perches47e0c882015-06-25 15:02:57 -07003012 # Otherwise set the alternate message types
3013
3014 # a comment starts before $max_line_length
3015 } elsif ($line =~ /($;[\s$;]*)$/ &&
3016 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3017 $msg_type = "LONG_LINE_COMMENT"
3018
3019 # a quoted string starts before $max_line_length
3020 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3021 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3022 $msg_type = "LONG_LINE_STRING"
3023 }
3024
3025 if ($msg_type ne "" &&
3026 (show_type("LONG_LINE") || show_type($msg_type))) {
3027 WARN($msg_type,
3028 "line over $max_line_length characters\n" . $herecurr);
3029 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003030 }
3031
Andy Whitcroft8905a672007-11-28 16:21:06 -08003032# check for adding lines without a newline.
3033 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003034 WARN("MISSING_EOF_NEWLINE",
3035 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003036 }
3037
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003038# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003039 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003040
3041# at the beginning of a line any tabs must come first and anything
3042# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003043 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3044 $rawline =~ /^\+\s* \s*/) {
3045 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003046 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003047 if (ERROR("CODE_INDENT",
3048 "code indent should use tabs where possible\n" . $herevet) &&
3049 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003050 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003051 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003052 }
3053
Alberto Panizzo08e44362010-03-05 13:43:54 -08003054# check for space before tabs.
3055 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3056 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003057 if (WARN("SPACE_BEFORE_TAB",
3058 "please, no space before tabs\n" . $herevet) &&
3059 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003060 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003061 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003062 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003063 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003064 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003065 }
3066
Joe Perches6a487212018-04-10 16:34:04 -07003067# check for assignments on the start of a line
3068 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3069 CHK("ASSIGNMENT_CONTINUATIONS",
3070 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3071 }
3072
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003073# check for && or || at the start of a line
3074 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3075 CHK("LOGICAL_CONTINUATIONS",
3076 "Logical continuations should be on the previous line\n" . $hereprev);
3077 }
3078
Joe Perchesa91e8992016-05-20 17:04:05 -07003079# check indentation starts on a tab stop
3080 if ($^V && $^V ge 5.10.0 &&
Joe Perchesbd491112018-02-06 15:39:06 -08003081 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003082 my $indent = length($1);
3083 if ($indent % 8) {
3084 if (WARN("TABSTOP",
3085 "Statements should start on a tabstop\n" . $herecurr) &&
3086 $fix) {
3087 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3088 }
3089 }
3090 }
3091
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003092# check multi-line statement indentation matches previous line
3093 if ($^V && $^V ge 5.10.0 &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003094 $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 -07003095 $prevline =~ /^\+(\t*)(.*)$/;
3096 my $oldindent = $1;
3097 my $rest = $2;
3098
3099 my $pos = pos_last_openparen($rest);
3100 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003101 $line =~ /^(\+| )([ \t]*)/;
3102 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003103
3104 my $goodtabindent = $oldindent .
3105 "\t" x ($pos / 8) .
3106 " " x ($pos % 8);
3107 my $goodspaceindent = $oldindent . " " x $pos;
3108
3109 if ($newindent ne $goodtabindent &&
3110 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003111
3112 if (CHK("PARENTHESIS_ALIGNMENT",
3113 "Alignment should match open parenthesis\n" . $hereprev) &&
3114 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003115 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003116 s/^\+[ \t]*/\+$goodtabindent/;
3117 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003118 }
3119 }
3120 }
3121
Joe Perches6ab3a972015-04-16 12:44:05 -07003122# check for space after cast like "(int) foo" or "(struct foo) bar"
3123# avoid checking a few false positives:
3124# "sizeof(<type>)" or "__alignof__(<type>)"
3125# function pointer declarations like "(*foo)(int) = bar;"
3126# structure definitions like "(struct foo) { 0 };"
3127# multiline macros that define functions
3128# known attributes or the __attribute__ keyword
3129 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3130 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003131 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003132 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003133 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003134 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003135 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003136 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003137 }
3138
Joe Perches86406b12015-09-09 15:37:41 -07003139# Block comment styles
3140# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003141 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003142 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003143 $rawline =~ /^\+[ \t]*\*/ &&
3144 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003145 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3146 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3147 }
3148
Joe Perches86406b12015-09-09 15:37:41 -07003149# Block comments use * on subsequent lines
3150 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3151 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003152 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003153 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003154 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003155 WARN("BLOCK_COMMENT_STYLE",
3156 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003157 }
3158
Joe Perches86406b12015-09-09 15:37:41 -07003159# Block comments use */ on trailing lines
3160 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003161 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3162 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3163 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003164 WARN("BLOCK_COMMENT_STYLE",
3165 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003166 }
3167
Joe Perches08eb9b82016-10-11 13:51:50 -07003168# Block comment * alignment
3169 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003170 $line =~ /^\+[ \t]*$;/ && #leading comment
3171 $rawline =~ /^\+[ \t]*\*/ && #leading *
3172 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003173 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003174 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3175 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003176 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003177 if (defined($1)) {
3178 $oldindent = expand_tabs($1);
3179 } else {
3180 $prevrawline =~ m@^\+(.*/?)\*@;
3181 $oldindent = expand_tabs($1);
3182 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003183 $rawline =~ m@^\+([ \t]*)\*@;
3184 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003185 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003186 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003187 WARN("BLOCK_COMMENT_STYLE",
3188 "Block comments should align the * on each line\n" . $hereprev);
3189 }
3190 }
3191
Joe Perches7f619192014-08-06 16:10:39 -07003192# check for missing blank lines after struct/union declarations
3193# with exceptions for various attributes and macros
3194 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3195 $line =~ /^\+/ &&
3196 !($line =~ /^\+\s*$/ ||
3197 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3198 $line =~ /^\+\s*MODULE_/i ||
3199 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3200 $line =~ /^\+[a-z_]*init/ ||
3201 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3202 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003203 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003204 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003205 if (CHK("LINE_SPACING",
3206 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3207 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003208 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003209 }
Joe Perches7f619192014-08-06 16:10:39 -07003210 }
3211
Joe Perches365dd4e2014-08-06 16:10:42 -07003212# check for multiple consecutive blank lines
3213 if ($prevline =~ /^[\+ ]\s*$/ &&
3214 $line =~ /^\+\s*$/ &&
3215 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003216 if (CHK("LINE_SPACING",
3217 "Please don't use multiple blank lines\n" . $hereprev) &&
3218 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003219 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003220 }
3221
Joe Perches365dd4e2014-08-06 16:10:42 -07003222 $last_blank_line = $linenr;
3223 }
3224
Joe Perches3b617e32014-04-03 14:49:28 -07003225# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003226 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3227 # actual declarations
3228 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003229 # function pointer declarations
3230 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003231 # foo bar; where foo is some local typedef or #define
3232 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3233 # known declaration macros
3234 $prevline =~ /^\+\s+$declaration_macros/) &&
3235 # for "else if" which can look like "$Ident $Ident"
3236 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3237 # other possible extensions of declaration lines
3238 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3239 # not starting a section or a macro "\" extended line
3240 $prevline =~ /(?:\{\s*|\\)$/) &&
3241 # looks like a declaration
3242 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003243 # function pointer declarations
3244 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003245 # foo bar; where foo is some local typedef or #define
3246 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3247 # known declaration macros
3248 $sline =~ /^\+\s+$declaration_macros/ ||
3249 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003250 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003251 # start or end of block or continuation of declaration
3252 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3253 # bitfield continuation
3254 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3255 # other possible extensions of declaration lines
3256 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3257 # indentation of previous and current line are the same
3258 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003259 if (WARN("LINE_SPACING",
3260 "Missing a blank line after declarations\n" . $hereprev) &&
3261 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003262 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003263 }
Joe Perches3b617e32014-04-03 14:49:28 -07003264 }
3265
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003266# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003267# Exceptions:
3268# 1) within comments
3269# 2) indented preprocessor commands
3270# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003271 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003272 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003273 if (WARN("LEADING_SPACE",
3274 "please, no spaces at the start of a line\n" . $herevet) &&
3275 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003276 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003277 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003278 }
3279
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003280# check we are in a valid C source file if not then ignore this hunk
3281 next if ($realfile !~ /\.(h|c)$/);
3282
Joe Perches5751a242017-11-17 15:28:52 -08003283# check for unusual line ending [ or (
3284 if ($line =~ /^\+.*([\[\(])\s*$/) {
3285 CHK("OPEN_ENDED_LINE",
3286 "Lines should not end with a '$1'\n" . $herecurr);
3287 }
3288
Joe Perches4dbed762017-05-08 15:55:39 -07003289# check if this appears to be the start function declaration, save the name
3290 if ($sline =~ /^\+\{\s*$/ &&
3291 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3292 $context_function = $1;
3293 }
3294
3295# check if this appears to be the end of function declaration
3296 if ($sline =~ /^\+\}\s*$/) {
3297 undef $context_function;
3298 }
3299
Joe Perches032a4c02014-08-06 16:10:29 -07003300# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003301# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003302# if the previous line is a break or return and is indented 1 tab more...
3303 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3304 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003305 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3306 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3307 defined $lines[$linenr] &&
3308 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003309 WARN("UNNECESSARY_ELSE",
3310 "else is not generally useful after a break or return\n" . $hereprev);
3311 }
3312 }
3313
Joe Perchesc00df192014-08-06 16:11:01 -07003314# check indentation of a line with a break;
3315# if the previous line is a goto or return and is indented the same # of tabs
3316 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3317 my $tabs = $1;
3318 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3319 WARN("UNNECESSARY_BREAK",
3320 "break is not useful after a goto or return\n" . $hereprev);
3321 }
3322 }
3323
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003324# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003325 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003326 WARN("CVS_KEYWORD",
3327 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003328 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003329
Joe Perches56e77d72013-02-21 16:44:14 -08003330# check for old HOTPLUG __dev<foo> section markings
3331 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3332 WARN("HOTPLUG_SECTION",
3333 "Using $1 is unnecessary\n" . $herecurr);
3334 }
3335
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003336# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003337 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3338 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003339#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003340 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003341 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003342 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003343 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003344 $stat =~ s/\n./\n /g;
3345 $cond =~ s/\n./\n /g;
3346
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003347#print "linenr<$linenr> <$stat>\n";
3348 # If this statement has no statement boundaries within
3349 # it there is no point in retrying a statement scan
3350 # until we hit end of it.
3351 my $frag = $stat; $frag =~ s/;+\s*$//;
3352 if ($frag !~ /(?:{|;)/) {
3353#print "skip<$line_nr_next>\n";
3354 $suppress_statement = $line_nr_next;
3355 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003356
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003357 # Find the real next line.
3358 $realline_next = $line_nr_next;
3359 if (defined $realline_next &&
3360 (!defined $lines[$realline_next - 1] ||
3361 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3362 $realline_next++;
3363 }
3364
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003365 my $s = $stat;
3366 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003367
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003368 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003369 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003370
3371 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003372 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003373
Andy Whitcroft463f2862009-09-21 17:04:34 -07003374 } elsif ($s =~ /^.\s*else\b/s) {
3375
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003376 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003377 } 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 -07003378 my $type = $1;
3379 $type =~ s/\s+/ /g;
3380 possible($type, "A:" . $s);
3381
Andy Whitcroft8905a672007-11-28 16:21:06 -08003382 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003383 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003384 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003385 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003386
3387 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003388 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003389 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003390 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003391
3392 # Check for any sort of function declaration.
3393 # int foo(something bar, other baz);
3394 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003395 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 -08003396 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003397
Andy Whitcroftcf655042008-03-04 14:28:20 -08003398 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003399 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003400 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003401
Andy Whitcroft8905a672007-11-28 16:21:06 -08003402 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003403 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003404
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003405 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003406 }
3407 }
3408 }
3409
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003410 }
3411
Andy Whitcroft653d4872007-06-23 17:16:34 -07003412#
3413# Checks which may be anchored in the context.
3414#
3415
3416# Check for switch () and associated case and default
3417# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003418 if ($line=~/\bswitch\s*\(.*\)/) {
3419 my $err = '';
3420 my $sep = '';
3421 my @ctx = ctx_block_outer($linenr, $realcnt);
3422 shift(@ctx);
3423 for my $ctx (@ctx) {
3424 my ($clen, $cindent) = line_stats($ctx);
3425 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3426 $indent != $cindent) {
3427 $err .= "$sep$ctx\n";
3428 $sep = '';
3429 } else {
3430 $sep = "[...]\n";
3431 }
3432 }
3433 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003434 ERROR("SWITCH_CASE_INDENT_LEVEL",
3435 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003436 }
3437 }
3438
3439# if/while/etc brace do not go on next line, unless defining a do while loop,
3440# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003441 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 -07003442 my $pre_ctx = "$1$2";
3443
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003444 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003445
3446 if ($line =~ /^\+\t{6,}/) {
3447 WARN("DEEP_INDENTATION",
3448 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3449 }
3450
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003451 my $ctx_cnt = $realcnt - $#ctx - 1;
3452 my $ctx = join("\n", @ctx);
3453
Andy Whitcroft548596d2008-07-23 21:29:01 -07003454 my $ctx_ln = $linenr;
3455 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003456
Andy Whitcroft548596d2008-07-23 21:29:01 -07003457 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3458 defined $lines[$ctx_ln - 1] &&
3459 $lines[$ctx_ln - 1] =~ /^-/)) {
3460 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3461 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003462 $ctx_ln++;
3463 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003464
Andy Whitcroft53210162008-07-23 21:29:03 -07003465 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3466 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003467
Joe Perchesd752fcc2014-08-06 16:11:05 -07003468 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003469 ERROR("OPEN_BRACE",
3470 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003471 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003472 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003473 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3474 $ctx =~ /\)\s*\;\s*$/ &&
3475 defined $lines[$ctx_ln - 1])
3476 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003477 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3478 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003479 WARN("TRAILING_SEMICOLON",
3480 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003481 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003482 }
3483 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003484 }
3485
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003486# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003487 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 -08003488 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3489 ctx_statement_block($linenr, $realcnt, 0)
3490 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003491 my ($s, $c) = ($stat, $cond);
3492
3493 substr($s, 0, length($c), '');
3494
Joe Perches9f5af482015-09-09 15:37:30 -07003495 # remove inline comments
3496 $s =~ s/$;/ /g;
3497 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003498
3499 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003500 my @newlines = ($c =~ /\n/gs);
3501 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003502
Joe Perches9f5af482015-09-09 15:37:30 -07003503 # Make sure we remove the line prefixes as we have
3504 # none on the first line, and are going to readd them
3505 # where necessary.
3506 $s =~ s/\n./\n/gs;
3507 while ($s =~ /\n\s+\\\n/) {
3508 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3509 }
3510
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003511 # We want to check the first line inside the block
3512 # starting at the end of the conditional, so remove:
3513 # 1) any blank line termination
3514 # 2) any opening brace { on end of the line
3515 # 3) any do (...) {
3516 my $continuation = 0;
3517 my $check = 0;
3518 $s =~ s/^.*\bdo\b//;
3519 $s =~ s/^\s*{//;
3520 if ($s =~ s/^\s*\\//) {
3521 $continuation = 1;
3522 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003523 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003524 $check = 1;
3525 $cond_lines++;
3526 }
3527
3528 # Also ignore a loop construct at the end of a
3529 # preprocessor statement.
3530 if (($prevline =~ /^.\s*#\s*define\s/ ||
3531 $prevline =~ /\\\s*$/) && $continuation == 0) {
3532 $check = 0;
3533 }
3534
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003535 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003536 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003537 while ($cond_ptr != $cond_lines) {
3538 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003539
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003540 # If we see an #else/#elif then the code
3541 # is not linear.
3542 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3543 $check = 0;
3544 }
3545
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003546 # Ignore:
3547 # 1) blank lines, they should be at 0,
3548 # 2) preprocessor lines, and
3549 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003550 if ($continuation ||
3551 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003552 $s =~ /^\s*#\s*?/ ||
3553 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003554 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003555 if ($s =~ s/^.*?\n//) {
3556 $cond_lines++;
3557 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003558 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003559 }
3560
3561 my (undef, $sindent) = line_stats("+" . $s);
3562 my $stat_real = raw_line($linenr, $cond_lines);
3563
3564 # Check if either of these lines are modified, else
3565 # this is not this patch's fault.
3566 if (!defined($stat_real) ||
3567 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3568 $check = 0;
3569 }
3570 if (defined($stat_real) && $cond_lines > 1) {
3571 $stat_real = "[...]\n$stat_real";
3572 }
3573
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003574 #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 -07003575
Joe Perches9f5af482015-09-09 15:37:30 -07003576 if ($check && $s ne '' &&
3577 (($sindent % 8) != 0 ||
3578 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003579 ($sindent == $indent &&
3580 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003581 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003582 WARN("SUSPECT_CODE_INDENT",
3583 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003584 }
3585 }
3586
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003587 # Track the 'values' across context and added lines.
3588 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003589 my ($curr_values, $curr_vars) =
3590 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003591 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003592 if ($dbg_values) {
3593 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003594 print "$linenr > .$outline\n";
3595 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003596 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003597 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003598 $prev_values = substr($curr_values, -1);
3599
Andy Whitcroft00df3442007-06-08 13:47:06 -07003600#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003601 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003602
Joe Perches11ca40a2016-12-12 16:46:31 -08003603# check for dereferences that span multiple lines
3604 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3605 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3606 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3607 my $ref = $1;
3608 $line =~ /^.\s*($Lval)/;
3609 $ref .= $1;
3610 $ref =~ s/\s//g;
3611 WARN("MULTILINE_DEREFERENCE",
3612 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3613 }
3614
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003615# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003616 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 -07003617 my $type = $1;
3618 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003619 $var = "" if (!defined $var);
3620 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003621 my $sign = $1;
3622 my $pointer = $2;
3623
3624 $pointer = "" if (!defined $pointer);
3625
3626 if (WARN("UNSPECIFIED_INT",
3627 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3628 $fix) {
3629 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003630 my $comp_pointer = $pointer;
3631 $comp_pointer =~ s/\s//g;
3632 $decl .= $comp_pointer;
3633 $decl = rtrim($decl) if ($var eq "");
3634 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003635 }
3636 }
3637 }
3638
Andy Whitcroft653d4872007-06-23 17:16:34 -07003639# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003640 if ($dbg_type) {
3641 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003642 ERROR("TEST_TYPE",
3643 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003644 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003645 ERROR("TEST_NOT_TYPE",
3646 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003647 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003648 next;
3649 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003650# TEST: allow direct testing of the attribute matcher.
3651 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003652 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003653 ERROR("TEST_ATTR",
3654 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003655 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003656 ERROR("TEST_NOT_ATTR",
3657 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003658 }
3659 next;
3660 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003661
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003662# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003663 if ($line =~ /^.\s*{/ &&
3664 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003665 if (ERROR("OPEN_BRACE",
3666 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003667 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3668 fix_delete_line($fixlinenr - 1, $prevrawline);
3669 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003670 my $fixedline = $prevrawline;
3671 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003672 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003673 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003674 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003675 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003676 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003677 }
3678
Andy Whitcroft653d4872007-06-23 17:16:34 -07003679#
3680# Checks which are anchored on the added line.
3681#
3682
3683# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003684 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003685 my $path = $1;
3686 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003687 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003688 "malformed #include filename\n" . $herecurr);
3689 }
3690 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3691 ERROR("UAPI_INCLUDE",
3692 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003693 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003694 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003695
3696# no C99 // comments
3697 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003698 if (ERROR("C99_COMMENTS",
3699 "do not use C99 // comments\n" . $herecurr) &&
3700 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003701 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003702 if ($line =~ /\/\/(.*)$/) {
3703 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003704 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003705 }
3706 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003707 }
3708 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003709 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003710 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003711
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003712# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3713# the whole statement.
3714#print "APW <$lines[$realline_next - 1]>\n";
3715 if (defined $realline_next &&
3716 exists $lines[$realline_next - 1] &&
3717 !defined $suppress_export{$realline_next} &&
3718 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3719 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003720 # Handle definitions which produce identifiers with
3721 # a prefix:
3722 # XXX(foo);
3723 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003724 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003725 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003726 $name =~ /^${Ident}_$2/) {
3727#print "FOO C name<$name>\n";
3728 $suppress_export{$realline_next} = 1;
3729
3730 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003731 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003732 ^.DEFINE_$Ident\(\Q$name\E\)|
3733 ^.DECLARE_$Ident\(\Q$name\E\)|
3734 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003735 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3736 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003737 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003738#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3739 $suppress_export{$realline_next} = 2;
3740 } else {
3741 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003742 }
3743 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003744 if (!defined $suppress_export{$linenr} &&
3745 $prevline =~ /^.\s*$/ &&
3746 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3747 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3748#print "FOO B <$lines[$linenr - 1]>\n";
3749 $suppress_export{$linenr} = 2;
3750 }
3751 if (defined $suppress_export{$linenr} &&
3752 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003753 WARN("EXPORT_SYMBOL",
3754 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003755 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003756
Joe Eloff5150bda2010-08-09 17:21:00 -07003757# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003758 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003759 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003760 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003761 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003762 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003763 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003764 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003765# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003766 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003767 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003768 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003769 $herecurr) &&
3770 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003771 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003772 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003773 }
3774
Joe Perches18130872014-08-06 16:11:22 -07003775# check for misordered declarations of char/short/int/long with signed/unsigned
3776 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3777 my $tmp = trim($1);
3778 WARN("MISORDERED_TYPE",
3779 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3780 }
3781
Joe Perchescb710ec2010-10-26 14:23:20 -07003782# check for static const char * arrays.
3783 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003784 WARN("STATIC_CONST_CHAR_ARRAY",
3785 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003786 $herecurr);
3787 }
3788
3789# check for static char foo[] = "bar" declarations.
3790 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003791 WARN("STATIC_CONST_CHAR_ARRAY",
3792 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003793 $herecurr);
3794 }
3795
Joe Perchesab7e23f2015-04-16 12:44:22 -07003796# check for const <foo> const where <foo> is not a pointer or array type
3797 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3798 my $found = $1;
3799 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3800 WARN("CONST_CONST",
3801 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3802 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3803 WARN("CONST_CONST",
3804 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3805 }
3806 }
3807
Joe Perches9b0fa602014-04-03 14:49:18 -07003808# check for non-global char *foo[] = {"bar", ...} declarations.
3809 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3810 WARN("STATIC_CONST_CHAR_ARRAY",
3811 "char * array declaration might be better as static const\n" .
3812 $herecurr);
3813 }
3814
Joe Perchesb598b672015-04-16 12:44:36 -07003815# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3816 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3817 my $array = $1;
3818 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3819 my $array_div = $1;
3820 if (WARN("ARRAY_SIZE",
3821 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3822 $fix) {
3823 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3824 }
3825 }
3826 }
3827
Joe Perchesb36190c2014-01-27 17:07:18 -08003828# check for function declarations without arguments like "int foo()"
3829 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3830 if (ERROR("FUNCTION_WITHOUT_ARGS",
3831 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3832 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003833 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003834 }
3835 }
3836
Andy Whitcroft653d4872007-06-23 17:16:34 -07003837# check for new typedefs, only function parameters and sparse annotations
3838# make sense.
3839 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003840 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003841 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003842 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02003843 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003844 WARN("NEW_TYPEDEFS",
3845 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003846 }
3847
3848# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003849 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003850 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3851 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003852 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003853
Andy Whitcroft65863862009-01-06 14:41:21 -08003854 # Should start with a space.
3855 $to =~ s/^(\S)/ $1/;
3856 # Should not end with a space.
3857 $to =~ s/\s+$//;
3858 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003859 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003860 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003861
Joe Perches3705ce52013-07-03 15:05:31 -07003862## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003863 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003864 if (ERROR("POINTER_LOCATION",
3865 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3866 $fix) {
3867 my $sub_from = $ident;
3868 my $sub_to = $ident;
3869 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003870 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003871 s@\Q$sub_from\E@$sub_to@;
3872 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003873 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003874 }
3875 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3876 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003877 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003878
Andy Whitcroft65863862009-01-06 14:41:21 -08003879 # Should start with a space.
3880 $to =~ s/^(\S)/ $1/;
3881 # Should not end with a space.
3882 $to =~ s/\s+$//;
3883 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003884 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003885 }
3886 # Modifiers should have spaces.
3887 $to =~ s/(\b$Modifier$)/$1 /;
3888
Joe Perches3705ce52013-07-03 15:05:31 -07003889## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003890 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003891 if (ERROR("POINTER_LOCATION",
3892 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3893 $fix) {
3894
3895 my $sub_from = $match;
3896 my $sub_to = $match;
3897 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003898 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003899 s@\Q$sub_from\E@$sub_to@;
3900 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003901 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003902 }
3903
Joe Perches9d3e3c72015-09-09 15:37:27 -07003904# avoid BUG() or BUG_ON()
3905 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07003906 my $msg_level = \&WARN;
3907 $msg_level = \&CHK if ($file);
3908 &{$msg_level}("AVOID_BUG",
3909 "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 -07003910 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003911
Joe Perches9d3e3c72015-09-09 15:37:27 -07003912# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003913 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003914 WARN("LINUX_VERSION_CODE",
3915 "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 -08003916 }
3917
Joe Perches17441222011-06-15 15:08:17 -07003918# check for uses of printk_ratelimit
3919 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003920 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003921 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003922 }
3923
Joe Percheseeef5732017-11-17 15:28:41 -08003924# printk should use KERN_* levels
3925 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
3926 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3927 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003928 }
3929
Joe Perches243f3802012-05-31 16:26:09 -07003930 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3931 my $orig = $1;
3932 my $level = lc($orig);
3933 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003934 my $level2 = $level;
3935 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003936 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003937 "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 -07003938 }
3939
3940 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003941 if (WARN("PREFER_PR_LEVEL",
3942 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3943 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003944 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003945 s/\bpr_warning\b/pr_warn/;
3946 }
Joe Perches243f3802012-05-31 16:26:09 -07003947 }
3948
Joe Perchesdc139312013-02-21 16:44:13 -08003949 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3950 my $orig = $1;
3951 my $level = lc($orig);
3952 $level = "warn" if ($level eq "warning");
3953 $level = "dbg" if ($level eq "debug");
3954 WARN("PREFER_DEV_LEVEL",
3955 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3956 }
3957
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003958# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3959# number of false positives, but assembly files are not checked, so at
3960# least the arch entry code will not trigger this warning.
3961 if ($line =~ /\bENOSYS\b/) {
3962 WARN("ENOSYS",
3963 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3964 }
3965
Andy Whitcroft653d4872007-06-23 17:16:34 -07003966# function brace can't be on same line, except for #defines of do while,
3967# or if closed on same line
Joe Perches2d453e32018-02-06 15:39:09 -08003968 if ($^V && $^V ge 5.10.0 &&
3969 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
3970 $sline !~ /\#\s*define\b.*do\s*\{/ &&
3971 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07003972 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08003973 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07003974 $fix) {
3975 fix_delete_line($fixlinenr, $rawline);
3976 my $fixed_line = $rawline;
3977 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3978 my $line1 = $1;
3979 my $line2 = $2;
3980 fix_insert_line($fixlinenr, ltrim($line1));
3981 fix_insert_line($fixlinenr, "\+{");
3982 if ($line2 !~ /^\s*$/) {
3983 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3984 }
3985 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003986 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003987
Andy Whitcroft8905a672007-11-28 16:21:06 -08003988# open braces for enum, union and struct go on the same line.
3989 if ($line =~ /^.\s*{/ &&
3990 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003991 if (ERROR("OPEN_BRACE",
3992 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3993 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3994 fix_delete_line($fixlinenr - 1, $prevrawline);
3995 fix_delete_line($fixlinenr, $rawline);
3996 my $fixedline = rtrim($prevrawline) . " {";
3997 fix_insert_line($fixlinenr, $fixedline);
3998 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003999 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004000 if ($fixedline !~ /^\+\s*$/) {
4001 fix_insert_line($fixlinenr, $fixedline);
4002 }
4003 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004004 }
4005
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004006# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004007 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4008 if (WARN("SPACING",
4009 "missing space after $1 definition\n" . $herecurr) &&
4010 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004011 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004012 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4013 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004014 }
4015
Joe Perches31070b52014-01-23 15:54:49 -08004016# Function pointer declarations
4017# check spacing between type, funcptr, and args
4018# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004019 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004020 my $declare = $1;
4021 my $pre_pointer_space = $2;
4022 my $post_pointer_space = $3;
4023 my $funcname = $4;
4024 my $post_funcname_space = $5;
4025 my $pre_args_space = $6;
4026
Joe Perches91f72e92014-04-03 14:49:12 -07004027# the $Declare variable will capture all spaces after the type
4028# so check it for a missing trailing missing space but pointer return types
4029# don't need a space so don't warn for those.
4030 my $post_declare_space = "";
4031 if ($declare =~ /(\s+)$/) {
4032 $post_declare_space = $1;
4033 $declare = rtrim($declare);
4034 }
4035 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004036 WARN("SPACING",
4037 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004038 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004039 }
4040
4041# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004042# This test is not currently implemented because these declarations are
4043# equivalent to
4044# int foo(int bar, ...)
4045# and this is form shouldn't/doesn't generate a checkpatch warning.
4046#
4047# elsif ($declare =~ /\s{2,}$/) {
4048# WARN("SPACING",
4049# "Multiple spaces after return type\n" . $herecurr);
4050# }
Joe Perches31070b52014-01-23 15:54:49 -08004051
4052# unnecessary space "type ( *funcptr)(args...)"
4053 if (defined $pre_pointer_space &&
4054 $pre_pointer_space =~ /^\s/) {
4055 WARN("SPACING",
4056 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4057 }
4058
4059# unnecessary space "type (* funcptr)(args...)"
4060 if (defined $post_pointer_space &&
4061 $post_pointer_space =~ /^\s/) {
4062 WARN("SPACING",
4063 "Unnecessary space before function pointer name\n" . $herecurr);
4064 }
4065
4066# unnecessary space "type (*funcptr )(args...)"
4067 if (defined $post_funcname_space &&
4068 $post_funcname_space =~ /^\s/) {
4069 WARN("SPACING",
4070 "Unnecessary space after function pointer name\n" . $herecurr);
4071 }
4072
4073# unnecessary space "type (*funcptr) (args...)"
4074 if (defined $pre_args_space &&
4075 $pre_args_space =~ /^\s/) {
4076 WARN("SPACING",
4077 "Unnecessary space before function pointer arguments\n" . $herecurr);
4078 }
4079
4080 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004081 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004082 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004083 }
4084 }
4085
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004086# check for spacing round square brackets; allowed:
4087# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004088# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4089# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004090 while ($line =~ /(.*?\s)\[/g) {
4091 my ($where, $prefix) = ($-[1], $1);
4092 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004093 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004094 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004095 if (ERROR("BRACKET_SPACE",
4096 "space prohibited before open square bracket '['\n" . $herecurr) &&
4097 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004098 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004099 s/^(\+.*?)\s+\[/$1\[/;
4100 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004101 }
4102 }
4103
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004104# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004105 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004106 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004107 my $ctx_before = substr($line, 0, $-[1]);
4108 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004109
4110 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004111 if ($name =~ /^(?:
4112 if|for|while|switch|return|case|
4113 volatile|__volatile__|
4114 __attribute__|format|__extension__|
4115 asm|__asm__)$/x)
4116 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004117 # cpp #define statements have non-optional spaces, ie
4118 # if there is a space between the name and the open
4119 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004120 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004121
4122 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004123 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004124
4125 # If this whole things ends with a type its most
4126 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004127 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004128
4129 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004130 if (WARN("SPACING",
4131 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4132 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004133 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004134 s/\b$name\s+\(/$name\(/;
4135 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004136 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004137 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004138
Andy Whitcroft653d4872007-06-23 17:16:34 -07004139# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004140 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004141 my $fixed_line = "";
4142 my $line_fixed = 0;
4143
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004144 my $ops = qr{
4145 <<=|>>=|<=|>=|==|!=|
4146 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4147 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004148 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004149 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004150 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004151 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004152
4153## print("element count: <" . $#elements . ">\n");
4154## foreach my $el (@elements) {
4155## print("el: <$el>\n");
4156## }
4157
4158 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004159 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004160
Joe Perches3705ce52013-07-03 15:05:31 -07004161 foreach my $el (@elements) {
4162 push(@fix_elements, substr($rawline, $off, length($el)));
4163 $off += length($el);
4164 }
4165
4166 $off = 0;
4167
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004168 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004169 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004170
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004171 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004172
4173 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4174
4175## print("n: <$n> good: <$good>\n");
4176
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004177 $off += length($elements[$n]);
4178
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004179 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004180 my $ca = substr($opline, 0, $off);
4181 my $cc = '';
4182 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4183 $cc = substr($opline, $off + length($elements[$n + 1]));
4184 }
4185 my $cb = "$ca$;$cc";
4186
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004187 my $a = '';
4188 $a = 'V' if ($elements[$n] ne '');
4189 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004190 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004191 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4192 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004193 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004194
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004195 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004196
4197 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004198 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004199 $c = 'V' if ($elements[$n + 2] ne '');
4200 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004201 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004202 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4203 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004204 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004205 } else {
4206 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004207 }
4208
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004209 my $ctx = "${a}x${c}";
4210
4211 my $at = "(ctx:$ctx)";
4212
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004213 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004214 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004215
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004216 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004217 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004218
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004219 # Get the full operator variant.
4220 my $opv = $op . substr($curr_vars, $off, 1);
4221
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004222 # Ignore operators passed as parameters.
4223 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004224 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004225
Andy Whitcroftcf655042008-03-04 14:28:20 -08004226# # Ignore comments
4227# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004228
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004229 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004230 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004231 if ($ctx !~ /.x[WEBC]/ &&
4232 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004233 if (ERROR("SPACING",
4234 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004235 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004236 $line_fixed = 1;
4237 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004238 }
4239
4240 # // is a comment
4241 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004242
Joe Perchesb00e4812014-04-03 14:49:33 -07004243 # : when part of a bitfield
4244 } elsif ($opv eq ':B') {
4245 # skip the bitfield test for now
4246
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004247 # No spaces for:
4248 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004249 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004250 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004251 if (ERROR("SPACING",
4252 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004253 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004254 if (defined $fix_elements[$n + 2]) {
4255 $fix_elements[$n + 2] =~ s/^\s+//;
4256 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004257 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004258 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004259 }
4260
Joe Perches23810972014-12-10 15:51:32 -08004261 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004262 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004263 my $rtrim_before = 0;
4264 my $space_after = 0;
4265 if ($ctx =~ /Wx./) {
4266 if (ERROR("SPACING",
4267 "space prohibited before that '$op' $at\n" . $hereptr)) {
4268 $line_fixed = 1;
4269 $rtrim_before = 1;
4270 }
4271 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004272 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004273 if (ERROR("SPACING",
4274 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004275 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004276 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004277 $space_after = 1;
4278 }
4279 }
4280 if ($rtrim_before || $space_after) {
4281 if ($rtrim_before) {
4282 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4283 } else {
4284 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4285 }
4286 if ($space_after) {
4287 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004288 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004289 }
4290
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004291 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004292 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004293 #warn "'*' is part of type\n";
4294
4295 # unary operators should have a space before and
4296 # none after. May be left adjacent to another
4297 # unary operator, or a cast
4298 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004299 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004300 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004301 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004302 if (ERROR("SPACING",
4303 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004304 if ($n != $last_after + 2) {
4305 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4306 $line_fixed = 1;
4307 }
Joe Perches3705ce52013-07-03 15:05:31 -07004308 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004309 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004310 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004311 # A unary '*' may be const
4312
4313 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004314 if (ERROR("SPACING",
4315 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004316 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004317 if (defined $fix_elements[$n + 2]) {
4318 $fix_elements[$n + 2] =~ s/^\s+//;
4319 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004320 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004321 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004322 }
4323
4324 # unary ++ and unary -- are allowed no space on one side.
4325 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004326 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004327 if (ERROR("SPACING",
4328 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004329 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004330 $line_fixed = 1;
4331 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004332 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004333 if ($ctx =~ /Wx[BE]/ ||
4334 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004335 if (ERROR("SPACING",
4336 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004337 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004338 $line_fixed = 1;
4339 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004340 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004341 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004342 if (ERROR("SPACING",
4343 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004344 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004345 if (defined $fix_elements[$n + 2]) {
4346 $fix_elements[$n + 2] =~ s/^\s+//;
4347 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004348 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004349 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004350 }
4351
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004352 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004353 } elsif ($op eq '<<' or $op eq '>>' or
4354 $op eq '&' or $op eq '^' or $op eq '|' or
4355 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004356 $op eq '*' or $op eq '/' or
4357 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004358 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004359 if ($check) {
4360 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4361 if (CHK("SPACING",
4362 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4363 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4364 $fix_elements[$n + 2] =~ s/^\s+//;
4365 $line_fixed = 1;
4366 }
4367 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4368 if (CHK("SPACING",
4369 "space preferred before that '$op' $at\n" . $hereptr)) {
4370 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4371 $line_fixed = 1;
4372 }
4373 }
4374 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004375 if (ERROR("SPACING",
4376 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004377 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4378 if (defined $fix_elements[$n + 2]) {
4379 $fix_elements[$n + 2] =~ s/^\s+//;
4380 }
Joe Perches3705ce52013-07-03 15:05:31 -07004381 $line_fixed = 1;
4382 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004383 }
4384
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004385 # A colon needs no spaces before when it is
4386 # terminating a case value or a label.
4387 } elsif ($opv eq ':C' || $opv eq ':L') {
4388 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004389 if (ERROR("SPACING",
4390 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004391 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004392 $line_fixed = 1;
4393 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004394 }
4395
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004396 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004397 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004398 my $ok = 0;
4399
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004400 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004401 if (($op eq '<' &&
4402 $cc =~ /^\S+\@\S+>/) ||
4403 ($op eq '>' &&
4404 $ca =~ /<\S+\@\S+$/))
4405 {
4406 $ok = 1;
4407 }
4408
Joe Perchese0df7e12015-04-16 12:44:53 -07004409 # for asm volatile statements
4410 # ignore a colon with another
4411 # colon immediately before or after
4412 if (($op eq ':') &&
4413 ($ca =~ /:$/ || $cc =~ /^:/)) {
4414 $ok = 1;
4415 }
4416
Joe Perches84731622013-11-12 15:10:05 -08004417 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004418 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004419 my $msg_level = \&ERROR;
4420 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004421
Jean Delvare0675a8f2017-09-08 16:16:07 -07004422 if (&{$msg_level}("SPACING",
4423 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004424 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4425 if (defined $fix_elements[$n + 2]) {
4426 $fix_elements[$n + 2] =~ s/^\s+//;
4427 }
Joe Perches3705ce52013-07-03 15:05:31 -07004428 $line_fixed = 1;
4429 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004430 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004431 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004432 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004433
4434## print("n: <$n> GOOD: <$good>\n");
4435
4436 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004437 }
Joe Perches3705ce52013-07-03 15:05:31 -07004438
4439 if (($#elements % 2) == 0) {
4440 $fixed_line = $fixed_line . $fix_elements[$#elements];
4441 }
4442
Joe Perches194f66f2014-08-06 16:11:03 -07004443 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4444 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004445 }
4446
4447
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004448 }
4449
Joe Perches786b6322013-07-03 15:05:32 -07004450# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004451 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004452 if (WARN("SPACING",
4453 "space prohibited before semicolon\n" . $herecurr) &&
4454 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004455 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004456 s/^(\+.*\S)\s+;/$1;/;
4457 }
4458 }
4459
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004460# check for multiple assignments
4461 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004462 CHK("MULTIPLE_ASSIGNMENTS",
4463 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004464 }
4465
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004466## # check for multiple declarations, allowing for a function declaration
4467## # continuation.
4468## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4469## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4470##
4471## # Remove any bracketed sections to ensure we do not
4472## # falsly report the parameters of functions.
4473## my $ln = $line;
4474## while ($ln =~ s/\([^\(\)]*\)//g) {
4475## }
4476## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004477## WARN("MULTIPLE_DECLARATION",
4478## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004479## }
4480## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004481
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004482#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004483 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004484 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004485 if (ERROR("SPACING",
4486 "space required before the open brace '{'\n" . $herecurr) &&
4487 $fix) {
Cyril Bur8d81ae02017-07-10 15:52:21 -07004488 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004489 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004490 }
4491
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004492## # check for blank lines before declarations
4493## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4494## $prevrawline =~ /^.\s*$/) {
4495## WARN("SPACING",
4496## "No blank lines before declarations\n" . $hereprev);
4497## }
4498##
4499
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004500# closing brace should have a space following it when it has anything
4501# on the line
4502 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004503 if (ERROR("SPACING",
4504 "space required after that close brace '}'\n" . $herecurr) &&
4505 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004506 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004507 s/}((?!(?:,|;|\)))\S)/} $1/;
4508 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004509 }
4510
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004511# check spacing on square brackets
4512 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004513 if (ERROR("SPACING",
4514 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4515 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004516 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004517 s/\[\s+/\[/;
4518 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004519 }
4520 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004521 if (ERROR("SPACING",
4522 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4523 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004524 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004525 s/\s+\]/\]/;
4526 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004527 }
4528
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004529# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004530 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4531 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004532 if (ERROR("SPACING",
4533 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4534 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004535 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004536 s/\(\s+/\(/;
4537 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004538 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004539 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004540 $line !~ /for\s*\(.*;\s+\)/ &&
4541 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004542 if (ERROR("SPACING",
4543 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4544 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004545 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004546 s/\s+\)/\)/;
4547 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004548 }
4549
Joe Perchese2826fd2014-08-06 16:10:48 -07004550# check unnecessary parentheses around addressof/dereference single $Lvals
4551# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4552
4553 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004554 my $var = $1;
4555 if (CHK("UNNECESSARY_PARENTHESES",
4556 "Unnecessary parentheses around $var\n" . $herecurr) &&
4557 $fix) {
4558 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4559 }
4560 }
4561
4562# check for unnecessary parentheses around function pointer uses
4563# ie: (foo->bar)(); should be foo->bar();
4564# but not "if (foo->bar) (" to avoid some false positives
4565 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4566 my $var = $2;
4567 if (CHK("UNNECESSARY_PARENTHESES",
4568 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4569 $fix) {
4570 my $var2 = deparenthesize($var);
4571 $var2 =~ s/\s//g;
4572 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4573 }
4574 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004575
Joe Perches63b7c732017-09-08 16:16:01 -07004576# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004577# when !drivers/staging or command-line uses --strict
4578 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
4579 $^V && $^V ge 5.10.0 && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004580 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4581 my $if_stat = $1;
4582 my $test = substr($2, 1, -1);
4583 my $herectx;
4584 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4585 my $match = $1;
4586 # avoid parentheses around potential macro args
4587 next if ($match =~ /^\s*\w+\s*$/);
4588 if (!defined($herectx)) {
4589 $herectx = $here . "\n";
4590 my $cnt = statement_rawlines($if_stat);
4591 for (my $n = 0; $n < $cnt; $n++) {
4592 my $rl = raw_line($linenr, $n);
4593 $herectx .= $rl . "\n";
4594 last if $rl =~ /^[ \+].*\{/;
4595 }
4596 }
4597 CHK("UNNECESSARY_PARENTHESES",
4598 "Unnecessary parentheses around '$match'\n" . $herectx);
4599 }
4600 }
4601
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004602#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004603 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004604 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004605 if (WARN("INDENTED_LABEL",
4606 "labels should not be indented\n" . $herecurr) &&
4607 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004608 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004609 s/^(.)\s+/$1/;
4610 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004611 }
4612
Joe Perches5b9553a2014-04-03 14:49:21 -07004613# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004614 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004615 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004616 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004617 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4618 my $value = $1;
4619 $value = deparenthesize($value);
4620 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4621 ERROR("RETURN_PARENTHESES",
4622 "return is not a function, parentheses are not required\n" . $herecurr);
4623 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004624 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004625 ERROR("SPACING",
4626 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004627 }
4628 }
Joe Perches507e5142013-11-12 15:10:13 -08004629
Joe Perchesb43ae212014-06-23 13:22:07 -07004630# unnecessary return in a void function
4631# at end-of-function, with the previous line a single leading tab, then return;
4632# and the line before that not a goto label target like "out:"
4633 if ($sline =~ /^[ \+]}\s*$/ &&
4634 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4635 $linenr >= 3 &&
4636 $lines[$linenr - 3] =~ /^[ +]/ &&
4637 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004638 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004639 "void function return statements are not generally useful\n" . $hereprev);
4640 }
Joe Perches9819cf22014-06-04 16:12:09 -07004641
Joe Perches189248d2014-01-23 15:54:47 -08004642# if statements using unnecessary parentheses - ie: if ((foo == bar))
4643 if ($^V && $^V ge 5.10.0 &&
4644 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4645 my $openparens = $1;
4646 my $count = $openparens =~ tr@\(@\(@;
4647 my $msg = "";
4648 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4649 my $comp = $4; #Not $1 because of $LvalOrFunc
4650 $msg = " - maybe == should be = ?" if ($comp eq "==");
4651 WARN("UNNECESSARY_PARENTHESES",
4652 "Unnecessary parentheses$msg\n" . $herecurr);
4653 }
4654 }
4655
Joe Perchesc5595fa2015-09-09 15:37:58 -07004656# comparisons with a constant or upper case identifier on the left
4657# avoid cases like "foo + BAR < baz"
4658# only fix matches surrounded by parentheses to avoid incorrect
4659# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4660 if ($^V && $^V ge 5.10.0 &&
4661 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4662 my $lead = $1;
4663 my $const = $2;
4664 my $comp = $3;
4665 my $to = $4;
4666 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004667 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004668 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4669 WARN("CONSTANT_COMPARISON",
4670 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4671 $fix) {
4672 if ($comp eq "<") {
4673 $newcomp = ">";
4674 } elsif ($comp eq "<=") {
4675 $newcomp = ">=";
4676 } elsif ($comp eq ">") {
4677 $newcomp = "<";
4678 } elsif ($comp eq ">=") {
4679 $newcomp = "<=";
4680 }
4681 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4682 }
4683 }
4684
Joe Perchesf34e4a42015-04-16 12:44:19 -07004685# Return of what appears to be an errno should normally be negative
4686 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004687 my $name = $1;
4688 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004689 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004690 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004691 }
4692 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004693
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004694# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004695 if ($line =~ /\b(if|while|for|switch)\(/) {
4696 if (ERROR("SPACING",
4697 "space required before the open parenthesis '('\n" . $herecurr) &&
4698 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004699 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004700 s/\b(if|while|for|switch)\(/$1 \(/;
4701 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004702 }
4703
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004704# Check for illegal assignment in if conditional -- and check for trailing
4705# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004706 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004707 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4708 ctx_statement_block($linenr, $realcnt, 0)
4709 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004710 my ($stat_next) = ctx_statement_block($line_nr_next,
4711 $remain_next, $off_next);
4712 $stat_next =~ s/\n./\n /g;
4713 ##print "stat<$stat> stat_next<$stat_next>\n";
4714
4715 if ($stat_next =~ /^\s*while\b/) {
4716 # If the statement carries leading newlines,
4717 # then count those as offsets.
4718 my ($whitespace) =
4719 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4720 my $offset =
4721 statement_rawlines($whitespace) - 1;
4722
4723 $suppress_whiletrailers{$line_nr_next +
4724 $offset} = 1;
4725 }
4726 }
4727 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004728 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004729 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004730 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004731
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004732 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004733 ERROR("ASSIGN_IN_IF",
4734 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004735 }
4736
4737 # Find out what is on the end of the line after the
4738 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004739 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004740 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004741 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004742 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4743 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004744 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004745 # Find out how long the conditional actually is.
4746 my @newlines = ($c =~ /\n/gs);
4747 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004748 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004749
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004750 $stat_real = raw_line($linenr, $cond_lines)
4751 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004752 if (defined($stat_real) && $cond_lines > 1) {
4753 $stat_real = "[...]\n$stat_real";
4754 }
4755
Joe Perches000d1cc12011-07-25 17:13:25 -07004756 ERROR("TRAILING_STATEMENTS",
4757 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004758 }
4759 }
4760
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004761# Check for bitwise tests written as boolean
4762 if ($line =~ /
4763 (?:
4764 (?:\[|\(|\&\&|\|\|)
4765 \s*0[xX][0-9]+\s*
4766 (?:\&\&|\|\|)
4767 |
4768 (?:\&\&|\|\|)
4769 \s*0[xX][0-9]+\s*
4770 (?:\&\&|\|\||\)|\])
4771 )/x)
4772 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004773 WARN("HEXADECIMAL_BOOLEAN_TEST",
4774 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004775 }
4776
Andy Whitcroft8905a672007-11-28 16:21:06 -08004777# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004778 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4779 my $s = $1;
4780 $s =~ s/$;//g; # Remove any comments
4781 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004782 ERROR("TRAILING_STATEMENTS",
4783 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004784 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004785 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004786# if should not continue a brace
4787 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004788 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004789 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004790 $herecurr);
4791 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004792# case and default should not have general statements after them
4793 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4794 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004795 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004796 \s*return\s+
4797 )/xg)
4798 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004799 ERROR("TRAILING_STATEMENTS",
4800 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004801 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004802
4803 # Check for }<nl>else {, these must be at the same
4804 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004805 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4806 $previndent == $indent) {
4807 if (ERROR("ELSE_AFTER_BRACE",
4808 "else should follow close brace '}'\n" . $hereprev) &&
4809 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4810 fix_delete_line($fixlinenr - 1, $prevrawline);
4811 fix_delete_line($fixlinenr, $rawline);
4812 my $fixedline = $prevrawline;
4813 $fixedline =~ s/}\s*$//;
4814 if ($fixedline !~ /^\+\s*$/) {
4815 fix_insert_line($fixlinenr, $fixedline);
4816 }
4817 $fixedline = $rawline;
4818 $fixedline =~ s/^(.\s*)else/$1} else/;
4819 fix_insert_line($fixlinenr, $fixedline);
4820 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004821 }
4822
Joe Perches8b8856f2014-08-06 16:11:14 -07004823 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4824 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004825 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4826
4827 # Find out what is on the end of the line after the
4828 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004829 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004830 $s =~ s/\n.*//g;
4831
4832 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004833 if (ERROR("WHILE_AFTER_BRACE",
4834 "while should follow close brace '}'\n" . $hereprev) &&
4835 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4836 fix_delete_line($fixlinenr - 1, $prevrawline);
4837 fix_delete_line($fixlinenr, $rawline);
4838 my $fixedline = $prevrawline;
4839 my $trailing = $rawline;
4840 $trailing =~ s/^\+//;
4841 $trailing = trim($trailing);
4842 $fixedline =~ s/}\s*$/} $trailing/;
4843 fix_insert_line($fixlinenr, $fixedline);
4844 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004845 }
4846 }
4847
Joe Perches95e2c602013-07-03 15:05:20 -07004848#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004849 while ($line =~ m{($Constant|$Lval)}g) {
4850 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004851
4852#gcc binary extension
4853 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004854 if (WARN("GCC_BINARY_CONSTANT",
4855 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4856 $fix) {
4857 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004858 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004859 s/\b$var\b/$hexval/;
4860 }
Joe Perches95e2c602013-07-03 15:05:20 -07004861 }
4862
4863#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004864 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004865 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004866#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004867 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004868#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004869 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4870#Ignore some three character SI units explicitly, like MiB and KHz
4871 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004872 while ($var =~ m{($Ident)}g) {
4873 my $word = $1;
4874 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004875 if ($check) {
4876 seed_camelcase_includes();
4877 if (!$file && !$camelcase_file_seeded) {
4878 seed_camelcase_file($realfile);
4879 $camelcase_file_seeded = 1;
4880 }
4881 }
Joe Perches7e781f62013-09-11 14:23:55 -07004882 if (!defined $camelcase{$word}) {
4883 $camelcase{$word} = 1;
4884 CHK("CAMELCASE",
4885 "Avoid CamelCase: <$word>\n" . $herecurr);
4886 }
Joe Perches34456862013-07-03 15:05:34 -07004887 }
Joe Perches323c1262012-12-17 16:02:07 -08004888 }
4889 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004890
4891#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004892 if ($line =~ /\#\s*define.*\\\s+$/) {
4893 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4894 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4895 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004896 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004897 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004898 }
4899
Fabian Frederick0e212e02015-04-16 12:44:25 -07004900# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4901# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004902 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004903 my $file = "$1.h";
4904 my $checkfile = "include/linux/$file";
4905 if (-f "$root/$checkfile" &&
4906 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004907 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004908 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004909 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4910 if ($asminclude > 0) {
4911 if ($realfile =~ m{^arch/}) {
4912 CHK("ARCH_INCLUDE_LINUX",
4913 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4914 } else {
4915 WARN("INCLUDE_LINUX",
4916 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4917 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004918 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004919 }
4920 }
4921
Andy Whitcroft653d4872007-06-23 17:16:34 -07004922# multi-statement macros should be enclosed in a do while loop, grab the
4923# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004924# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004925 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4926 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004927 my $ln = $linenr;
4928 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004929 my ($off, $dstat, $dcond, $rest);
4930 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004931 my $has_flow_statement = 0;
4932 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004933 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004934 ctx_statement_block($linenr, $realcnt, 0);
4935 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004936 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004937 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004938
Joe Perches08a28432014-10-13 15:51:55 -07004939 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004940 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004941
Joe Perchesf59b64b2016-10-11 13:52:08 -07004942 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4943 my $define_args = $1;
4944 my $define_stmt = $dstat;
4945 my @def_args = ();
4946
4947 if (defined $define_args && $define_args ne "") {
4948 $define_args = substr($define_args, 1, length($define_args) - 2);
4949 $define_args =~ s/\s*//g;
4950 @def_args = split(",", $define_args);
4951 }
4952
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004953 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004954 $dstat =~ s/\\\n.//g;
4955 $dstat =~ s/^\s*//s;
4956 $dstat =~ s/\s*$//s;
4957
4958 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004959 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4960 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004961 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004962 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004963 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004964
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004965 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004966 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4967 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004968 {
4969 }
4970
Joe Perches42e15292016-03-15 14:58:01 -07004971 # Make asm volatile uses seem like a generic function
4972 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4973
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004974 my $exceptions = qr{
4975 $Declare|
4976 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004977 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004978 DECLARE_PER_CPU|
4979 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004980 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004981 union|
4982 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004983 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004984 ^\"|\"$|
4985 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004986 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004987 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07004988
4989 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07004990 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07004991 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07004992
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004993 if ($dstat ne '' &&
4994 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4995 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004996 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004997 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004998 $dstat !~ /$exceptions/ &&
4999 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005000 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005001 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005002 $dstat !~ /^for\s*$Constant$/ && # for (...)
5003 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5004 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005005 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005006 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005007 {
Joe Perchese7955562017-05-08 15:55:48 -07005008 if ($dstat =~ /^\s*if\b/) {
5009 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5010 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5011 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005012 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5013 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5014 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005015 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005016 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005017 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005018
5019 }
Joe Perches52076492016-10-11 13:52:14 -07005020
5021 # Make $define_stmt single line, comment-free, etc
5022 my @stmt_array = split('\n', $define_stmt);
5023 my $first = 1;
5024 $define_stmt = "";
5025 foreach my $l (@stmt_array) {
5026 $l =~ s/\\$//;
5027 if ($first) {
5028 $define_stmt = $l;
5029 $first = 0;
5030 } elsif ($l =~ /^[\+ ]/) {
5031 $define_stmt .= substr($l, 1);
5032 }
5033 }
5034 $define_stmt =~ s/$;//g;
5035 $define_stmt =~ s/\s+/ /g;
5036 $define_stmt = trim($define_stmt);
5037
Joe Perchesf59b64b2016-10-11 13:52:08 -07005038# check if any macro arguments are reused (ignore '...' and 'type')
5039 foreach my $arg (@def_args) {
5040 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005041 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005042 my $tmp_stmt = $define_stmt;
5043 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
5044 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5045 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005046 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005047 if ($use_cnt > 1) {
5048 CHK("MACRO_ARG_REUSE",
5049 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005050 }
5051# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005052 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005053 ((defined($1) && $1 ne ',') ||
5054 (defined($2) && $2 ne ','))) {
5055 CHK("MACRO_ARG_PRECEDENCE",
5056 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005057 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005058 }
Joe Perches5023d342012-12-17 16:01:47 -08005059
Joe Perches08a28432014-10-13 15:51:55 -07005060# check for macros with flow control, but without ## concatenation
5061# ## concatenation is commonly a macro that defines a function so ignore those
5062 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005063 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005064 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005065
Joe Perches08a28432014-10-13 15:51:55 -07005066 WARN("MACRO_WITH_FLOW_CONTROL",
5067 "Macros with flow control statements should be avoided\n" . "$herectx");
5068 }
5069
Joe Perches481eb482012-12-17 16:01:56 -08005070# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005071
5072 } else {
5073 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005074 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5075 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005076 $line =~ /^\+.*\\$/) {
5077 WARN("LINE_CONTINUATIONS",
5078 "Avoid unnecessary line continuations\n" . $herecurr);
5079 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005080 }
5081
Joe Perchesb13edf72012-07-30 14:41:24 -07005082# do {} while (0) macro tests:
5083# single-statement macros do not need to be enclosed in do while (0) loop,
5084# macro should not end with a semicolon
5085 if ($^V && $^V ge 5.10.0 &&
5086 $realfile !~ m@/vmlinux.lds.h$@ &&
5087 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5088 my $ln = $linenr;
5089 my $cnt = $realcnt;
5090 my ($off, $dstat, $dcond, $rest);
5091 my $ctx = '';
5092 ($dstat, $dcond, $ln, $cnt, $off) =
5093 ctx_statement_block($linenr, $realcnt, 0);
5094 $ctx = $dstat;
5095
5096 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005097 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005098
5099 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5100 my $stmts = $2;
5101 my $semis = $3;
5102
5103 $ctx =~ s/\n*$//;
5104 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005105 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005106
Joe Perchesac8e97f2012-08-21 16:15:53 -07005107 if (($stmts =~ tr/;/;/) == 1 &&
5108 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005109 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5110 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5111 }
5112 if (defined $semis && $semis ne "") {
5113 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5114 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5115 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005116 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5117 $ctx =~ s/\n*$//;
5118 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005119 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005120
5121 WARN("TRAILING_SEMICOLON",
5122 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005123 }
5124 }
5125
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005126# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005127 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5128 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005129 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005130 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005131 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5132 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005133 my @allowed = ();
5134 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005135 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005136 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005137 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005138 for my $chunk (@chunks) {
5139 my ($cond, $block) = @{$chunk};
5140
Andy Whitcroft773647a2008-03-28 14:15:58 -07005141 # If the condition carries leading newlines, then count those as offsets.
5142 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5143 my $offset = statement_rawlines($whitespace) - 1;
5144
Joe Perchesaad4f612012-03-23 15:02:19 -07005145 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005146 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5147
5148 # We have looked at and allowed this specific line.
5149 $suppress_ifbraces{$ln + $offset} = 1;
5150
5151 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005152 $ln += statement_rawlines($block) - 1;
5153
Andy Whitcroft773647a2008-03-28 14:15:58 -07005154 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005155
5156 $seen++ if ($block =~ /^\s*{/);
5157
Joe Perchesaad4f612012-03-23 15:02:19 -07005158 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005159 if (statement_lines($cond) > 1) {
5160 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005161 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005162 }
5163 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005164 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005165 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005166 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005167 if (statement_block_size($block) > 1) {
5168 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005169 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005170 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005171 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005172 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005173 if ($seen) {
5174 my $sum_allowed = 0;
5175 foreach (@allowed) {
5176 $sum_allowed += $_;
5177 }
5178 if ($sum_allowed == 0) {
5179 WARN("BRACES",
5180 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5181 } elsif ($sum_allowed != $allow &&
5182 $seen != $allow) {
5183 CHK("BRACES",
5184 "braces {} should be used on all arms of this statement\n" . $herectx);
5185 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005186 }
5187 }
5188 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005189 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005190 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005191 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005192
Andy Whitcroftcf655042008-03-04 14:28:20 -08005193 # Check the pre-context.
5194 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5195 #print "APW: ALLOWED: pre<$1>\n";
5196 $allowed = 1;
5197 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005198
5199 my ($level, $endln, @chunks) =
5200 ctx_statement_full($linenr, $realcnt, $-[0]);
5201
Andy Whitcroftcf655042008-03-04 14:28:20 -08005202 # Check the condition.
5203 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005204 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005205 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005206 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005207 }
5208 if (statement_lines($cond) > 1) {
5209 #print "APW: ALLOWED: cond<$cond>\n";
5210 $allowed = 1;
5211 }
5212 if ($block =~/\b(?:if|for|while)\b/) {
5213 #print "APW: ALLOWED: block<$block>\n";
5214 $allowed = 1;
5215 }
5216 if (statement_block_size($block) > 1) {
5217 #print "APW: ALLOWED: lines block<$block>\n";
5218 $allowed = 1;
5219 }
5220 # Check the post-context.
5221 if (defined $chunks[1]) {
5222 my ($cond, $block) = @{$chunks[1]};
5223 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005224 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005225 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005226 if ($block =~ /^\s*\{/) {
5227 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5228 $allowed = 1;
5229 }
5230 }
5231 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005232 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005233 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005234
Joe Perches000d1cc12011-07-25 17:13:25 -07005235 WARN("BRACES",
5236 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005237 }
5238 }
5239
Joe Perchese4c5bab2017-02-24 15:01:41 -08005240# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005241 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5242 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005243 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5244 }
5245
Joe Perches0979ae62012-12-17 16:01:59 -08005246# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005247 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005248 if (CHK("BRACES",
5249 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5250 $fix && $prevrawline =~ /^\+/) {
5251 fix_delete_line($fixlinenr - 1, $prevrawline);
5252 }
Joe Perches0979ae62012-12-17 16:01:59 -08005253 }
Joe Perches77b9a532013-07-03 15:05:29 -07005254 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005255 if (CHK("BRACES",
5256 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5257 $fix) {
5258 fix_delete_line($fixlinenr, $rawline);
5259 }
Joe Perches0979ae62012-12-17 16:01:59 -08005260 }
5261
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005262# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005263 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5264 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005265 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005266 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005267 }
5268
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005269# Check for user-visible strings broken across lines, which breaks the ability
5270# to grep for the string. Make exceptions when the previous string ends in a
5271# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5272# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005273 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005274 $prevline =~ /"\s*$/ &&
5275 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5276 if (WARN("SPLIT_STRING",
5277 "quoted string split across lines\n" . $hereprev) &&
5278 $fix &&
5279 $prevrawline =~ /^\+.*"\s*$/ &&
5280 $last_coalesced_string_linenr != $linenr - 1) {
5281 my $extracted_string = get_quoted_string($line, $rawline);
5282 my $comma_close = "";
5283 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5284 $comma_close = $1;
5285 }
5286
5287 fix_delete_line($fixlinenr - 1, $prevrawline);
5288 fix_delete_line($fixlinenr, $rawline);
5289 my $fixedline = $prevrawline;
5290 $fixedline =~ s/"\s*$//;
5291 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5292 fix_insert_line($fixlinenr - 1, $fixedline);
5293 $fixedline = $rawline;
5294 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5295 if ($fixedline !~ /\+\s*$/) {
5296 fix_insert_line($fixlinenr, $fixedline);
5297 }
5298 $last_coalesced_string_linenr = $linenr;
5299 }
5300 }
5301
5302# check for missing a space in a string concatenation
5303 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5304 WARN('MISSING_SPACE',
5305 "break quoted strings at a space character\n" . $hereprev);
5306 }
5307
Joe Perchese4b7d302017-05-08 15:55:51 -07005308# check for an embedded function name in a string when the function is known
5309# This does not work very well for -f --file checking as it depends on patch
5310# context providing the function name or a single line form for in-file
5311# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005312 if ($line =~ /^\+.*$String/ &&
5313 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005314 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5315 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005316 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005317 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005318 }
5319
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005320# check for spaces before a quoted newline
5321 if ($rawline =~ /^.*\".*\s\\n/) {
5322 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5323 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5324 $fix) {
5325 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5326 }
5327
5328 }
5329
Joe Perchesf17dba42014-10-13 15:51:51 -07005330# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005331 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005332 CHK("CONCATENATED_STRING",
5333 "Concatenated strings should use spaces between elements\n" . $herecurr);
5334 }
5335
Joe Perches90ad30e2014-12-10 15:51:59 -08005336# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005337 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005338 WARN("STRING_FRAGMENTS",
5339 "Consecutive strings are generally better as a single string\n" . $herecurr);
5340 }
5341
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005342# check for non-standard and hex prefixed decimal printf formats
5343 my $show_L = 1; #don't show the same defect twice
5344 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005345 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005346 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005347 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005348 # check for %L
5349 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005350 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005351 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5352 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005353 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005354 # check for %Z
5355 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5356 WARN("PRINTF_Z",
5357 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5358 $show_Z = 0;
5359 }
5360 # check for 0x<decimal>
5361 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5362 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005363 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5364 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005365 }
5366
5367# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005368 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005369 WARN("LINE_CONTINUATIONS",
5370 "Avoid line continuations in quoted strings\n" . $herecurr);
5371 }
5372
Andy Whitcroft00df3442007-06-08 13:47:06 -07005373# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005374 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005375 CHK("REDUNDANT_CODE",
5376 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005377 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005378 }
5379
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005380# check for needless "if (<foo>) fn(<foo>)" uses
5381 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005382 my $tested = quotemeta($1);
5383 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5384 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5385 my $func = $1;
5386 if (WARN('NEEDLESS_IF',
5387 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5388 $fix) {
5389 my $do_fix = 1;
5390 my $leading_tabs = "";
5391 my $new_leading_tabs = "";
5392 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5393 $leading_tabs = $1;
5394 } else {
5395 $do_fix = 0;
5396 }
5397 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5398 $new_leading_tabs = $1;
5399 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5400 $do_fix = 0;
5401 }
5402 } else {
5403 $do_fix = 0;
5404 }
5405 if ($do_fix) {
5406 fix_delete_line($fixlinenr - 1, $prevrawline);
5407 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5408 }
5409 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005410 }
5411 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005412
Joe Perchesebfdc402014-08-06 16:10:27 -07005413# check for unnecessary "Out of Memory" messages
5414 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5415 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5416 (defined $1 || defined $3) &&
5417 $linenr > 3) {
5418 my $testval = $2;
5419 my $testline = $lines[$linenr - 3];
5420
5421 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5422# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5423
Joe Perchesfb0d0e02017-07-10 15:52:04 -07005424 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 -07005425 WARN("OOM_MESSAGE",
5426 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5427 }
5428 }
5429
Joe Perchesf78d98f2014-10-13 15:52:01 -07005430# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005431 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005432 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5433 my $level = $1;
5434 if (WARN("UNNECESSARY_KERN_LEVEL",
5435 "Possible unnecessary $level\n" . $herecurr) &&
5436 $fix) {
5437 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5438 }
5439 }
5440
Joe Perches45c55e92017-02-24 15:01:31 -08005441# check for logging continuations
5442 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5443 WARN("LOGGING_CONTINUATION",
5444 "Avoid logging continuation uses where feasible\n" . $herecurr);
5445 }
5446
Joe Perchesabb08a52014-12-10 15:51:46 -08005447# check for mask then right shift without a parentheses
5448 if ($^V && $^V ge 5.10.0 &&
5449 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5450 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5451 WARN("MASK_THEN_SHIFT",
5452 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5453 }
5454
Joe Perchesb75ac612014-12-10 15:52:02 -08005455# check for pointer comparisons to NULL
5456 if ($^V && $^V ge 5.10.0) {
5457 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5458 my $val = $1;
5459 my $equal = "!";
5460 $equal = "" if ($4 eq "!=");
5461 if (CHK("COMPARISON_TO_NULL",
5462 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5463 $fix) {
5464 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5465 }
5466 }
5467 }
5468
Joe Perches8716de32013-09-11 14:24:05 -07005469# check for bad placement of section $InitAttribute (e.g.: __initdata)
5470 if ($line =~ /(\b$InitAttribute\b)/) {
5471 my $attr = $1;
5472 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5473 my $ptr = $1;
5474 my $var = $2;
5475 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5476 ERROR("MISPLACED_INIT",
5477 "$attr should be placed after $var\n" . $herecurr)) ||
5478 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5479 WARN("MISPLACED_INIT",
5480 "$attr should be placed after $var\n" . $herecurr))) &&
5481 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005482 $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 -07005483 }
5484 }
5485 }
5486
Joe Perchese970b8842013-11-12 15:10:10 -08005487# check for $InitAttributeData (ie: __initdata) with const
5488 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5489 my $attr = $1;
5490 $attr =~ /($InitAttributePrefix)(.*)/;
5491 my $attr_prefix = $1;
5492 my $attr_type = $2;
5493 if (ERROR("INIT_ATTRIBUTE",
5494 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5495 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005496 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005497 s/$InitAttributeData/${attr_prefix}initconst/;
5498 }
5499 }
5500
5501# check for $InitAttributeConst (ie: __initconst) without const
5502 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5503 my $attr = $1;
5504 if (ERROR("INIT_ATTRIBUTE",
5505 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5506 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005507 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005508 /(^\+\s*(?:static\s+))/;
5509 $lead = rtrim($1);
5510 $lead = "$lead " if ($lead !~ /^\+$/);
5511 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005512 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005513 }
5514 }
5515
Joe Perchesc17893c2015-04-16 12:44:42 -07005516# check for __read_mostly with const non-pointer (should just be const)
5517 if ($line =~ /\b__read_mostly\b/ &&
5518 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5519 if (ERROR("CONST_READ_MOSTLY",
5520 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5521 $fix) {
5522 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5523 }
5524 }
5525
Joe Perchesfbdb8132014-04-03 14:49:14 -07005526# don't use __constant_<foo> functions outside of include/uapi/
5527 if ($realfile !~ m@^include/uapi/@ &&
5528 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5529 my $constant_func = $1;
5530 my $func = $constant_func;
5531 $func =~ s/^__constant_//;
5532 if (WARN("CONSTANT_CONVERSION",
5533 "$constant_func should be $func\n" . $herecurr) &&
5534 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005535 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005536 }
5537 }
5538
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005539# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005540 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005541 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005542 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005543 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005544 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005545 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5546 }
5547 if ($delay > 2000) {
5548 WARN("LONG_UDELAY",
5549 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005550 }
5551 }
5552
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005553# warn about unexpectedly long msleep's
5554 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5555 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005556 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005557 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005558 }
5559 }
5560
Joe Perches36ec1932013-07-03 15:05:25 -07005561# check for comparisons of jiffies
5562 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5563 WARN("JIFFIES_COMPARISON",
5564 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5565 }
5566
Joe Perches9d7a34a2013-07-03 15:05:26 -07005567# check for comparisons of get_jiffies_64()
5568 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5569 WARN("JIFFIES_COMPARISON",
5570 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5571 }
5572
Andy Whitcroft00df3442007-06-08 13:47:06 -07005573# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005574# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005575# print "#ifdef in C files should be avoided\n";
5576# print "$herecurr";
5577# $clean = 0;
5578# }
5579
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005580# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005581 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005582 if (ERROR("SPACING",
5583 "exactly one space required after that #$1\n" . $herecurr) &&
5584 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005585 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005586 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5587 }
5588
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005589 }
5590
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005591# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005592 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5593 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005594 my $which = $1;
5595 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005596 CHK("UNCOMMENTED_DEFINITION",
5597 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005598 }
5599 }
5600# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005601
5602 my $barriers = qr{
5603 mb|
5604 rmb|
5605 wmb|
5606 read_barrier_depends
5607 }x;
5608 my $barrier_stems = qr{
5609 mb__before_atomic|
5610 mb__after_atomic|
5611 store_release|
5612 load_acquire|
5613 store_mb|
5614 (?:$barriers)
5615 }x;
5616 my $all_barriers = qr{
5617 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005618 smp_(?:$barrier_stems)|
5619 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005620 }x;
5621
5622 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005623 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005624 WARN("MEMORY_BARRIER",
5625 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005626 }
5627 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005628
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005629 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5630
5631 if ($realfile !~ m@^include/asm-generic/@ &&
5632 $realfile !~ m@/barrier\.h$@ &&
5633 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5634 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5635 WARN("MEMORY_BARRIER",
5636 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5637 }
5638
Joe Perchescb426e92015-06-25 15:02:46 -07005639# check for waitqueue_active without a comment.
5640 if ($line =~ /\bwaitqueue_active\s*\(/) {
5641 if (!ctx_has_comment($first_line, $linenr)) {
5642 WARN("WAITQUEUE_ACTIVE",
5643 "waitqueue_active without comment\n" . $herecurr);
5644 }
5645 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005646
Paul E. McKenney91db2592017-11-27 09:37:35 -08005647# check for smp_read_barrier_depends and read_barrier_depends
5648 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5649 WARN("READ_BARRIER_DEPENDS",
5650 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5651 }
5652
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005653# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005654 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005655 CHK("ARCH_DEFINES",
5656 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005657 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005658
Joe Perches596ed452017-07-12 14:37:02 -07005659# check that the storage class is not after a type
5660 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005661 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005662 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5663 }
5664# Check that the storage class is at the beginning of a declaration
5665 if ($line =~ /\b$Storage\b/ &&
5666 $line !~ /^.\s*$Storage/ &&
5667 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5668 $1 !~ /[\,\)]\s*$/) {
5669 WARN("STORAGE_CLASS",
5670 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005671 }
5672
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005673# check the location of the inline attribute, that it is between
5674# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005675 if ($line =~ /\b$Type\s+$Inline\b/ ||
5676 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005677 ERROR("INLINE_LOCATION",
5678 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005679 }
5680
Andy Whitcroft8905a672007-11-28 16:21:06 -08005681# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005682 if ($realfile !~ m@\binclude/uapi/@ &&
5683 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005684 if (WARN("INLINE",
5685 "plain inline is preferred over $1\n" . $herecurr) &&
5686 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005687 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005688
5689 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005690 }
5691
Joe Perches3d130fd2011-01-12 17:00:00 -08005692# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005693 if ($realfile !~ m@\binclude/uapi/@ &&
5694 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005695 WARN("PREFER_PACKED",
5696 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005697 }
5698
Joe Perches39b7e282011-07-25 17:13:24 -07005699# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005700 if ($realfile !~ m@\binclude/uapi/@ &&
5701 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005702 WARN("PREFER_ALIGNED",
5703 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005704 }
5705
Joe Perches5f14d3b2012-01-10 15:09:52 -08005706# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005707 if ($realfile !~ m@\binclude/uapi/@ &&
5708 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005709 if (WARN("PREFER_PRINTF",
5710 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5711 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005712 $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 -07005713
5714 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005715 }
5716
Joe Perches6061d942012-03-23 15:02:16 -07005717# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005718 if ($realfile !~ m@\binclude/uapi/@ &&
5719 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005720 if (WARN("PREFER_SCANF",
5721 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5722 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005723 $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 -07005724 }
Joe Perches6061d942012-03-23 15:02:16 -07005725 }
5726
Joe Perches619a9082014-12-10 15:51:35 -08005727# Check for __attribute__ weak, or __weak declarations (may have link issues)
5728 if ($^V && $^V ge 5.10.0 &&
5729 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5730 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5731 $line =~ /\b__weak\b/)) {
5732 ERROR("WEAK_DECLARATION",
5733 "Using weak declarations can have unintended link defects\n" . $herecurr);
5734 }
5735
Tomas Winklerfd39f902016-12-12 16:46:34 -08005736# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005737 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005738 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005739 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5740 my $type = $1;
5741 if ($type =~ /\b($typeC99Typedefs)\b/) {
5742 $type = $1;
5743 my $kernel_type = 'u';
5744 $kernel_type = 's' if ($type =~ /^_*[si]/);
5745 $type =~ /(\d+)/;
5746 $kernel_type .= $1;
5747 if (CHK("PREFER_KERNEL_TYPES",
5748 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5749 $fix) {
5750 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5751 }
5752 }
5753 }
5754
Joe Perches938224b2016-01-20 14:59:15 -08005755# check for cast of C90 native int or longer types constants
5756 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5757 my $cast = $1;
5758 my $const = $2;
5759 if (WARN("TYPECAST_INT_CONSTANT",
5760 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5761 $fix) {
5762 my $suffix = "";
5763 my $newconst = $const;
5764 $newconst =~ s/${Int_type}$//;
5765 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5766 if ($cast =~ /\blong\s+long\b/) {
5767 $suffix .= 'LL';
5768 } elsif ($cast =~ /\blong\b/) {
5769 $suffix .= 'L';
5770 }
5771 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5772 }
5773 }
5774
Joe Perches8f53a9b2010-03-05 13:43:48 -08005775# check for sizeof(&)
5776 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005777 WARN("SIZEOF_ADDRESS",
5778 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005779 }
5780
Joe Perches66c80b62012-07-30 14:41:22 -07005781# check for sizeof without parenthesis
5782 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005783 if (WARN("SIZEOF_PARENTHESIS",
5784 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5785 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005786 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005787 }
Joe Perches66c80b62012-07-30 14:41:22 -07005788 }
5789
Joe Perches88982fe2012-12-17 16:02:00 -08005790# check for struct spinlock declarations
5791 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5792 WARN("USE_SPINLOCK_T",
5793 "struct spinlock should be spinlock_t\n" . $herecurr);
5794 }
5795
Joe Perchesa6962d72013-04-29 16:18:13 -07005796# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005797 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005798 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005799 $fmt =~ s/%%//g;
5800 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005801 if (WARN("PREFER_SEQ_PUTS",
5802 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5803 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005804 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005805 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005806 }
5807 }
5808
Joe Perches478b1792018-04-10 16:33:34 -07005809# check for vsprintf extension %p<foo> misuses
Joe Perches0b523762017-05-08 15:55:36 -07005810 if ($^V && $^V ge 5.10.0 &&
5811 defined $stat &&
5812 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5813 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005814 my $specifier;
5815 my $extension;
5816 my $bad_specifier = "";
5817 my $stat_real;
5818
Joe Perches0b523762017-05-08 15:55:36 -07005819 my $lc = $stat =~ tr@\n@@;
5820 $lc = $lc + $linenr;
5821 for (my $count = $linenr; $count <= $lc; $count++) {
5822 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5823 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07005824
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005825 while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5826 $specifier = $1;
5827 $extension = $2;
5828 if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
5829 $bad_specifier = $specifier;
5830 last;
5831 }
5832 if ($extension eq "x" && !defined($stat_real)) {
5833 if (!defined($stat_real)) {
5834 $stat_real = get_stat_real($linenr, $lc);
5835 }
5836 WARN("VSPRINTF_SPECIFIER_PX",
5837 "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");
5838 }
5839 }
5840 if ($bad_specifier ne "") {
5841 my $stat_real = get_stat_real($linenr, $lc);
5842 my $ext_type = "Invalid";
5843 my $use = "";
5844 if ($bad_specifier =~ /p[Ff]/) {
5845 $ext_type = "Deprecated";
5846 $use = " - use %pS instead";
5847 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
5848 }
5849
5850 WARN("VSPRINTF_POINTER_EXTENSION",
5851 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
5852 }
Joe Perches0b523762017-05-08 15:55:36 -07005853 }
5854 }
5855
Andy Whitcroft554e1652012-01-10 15:09:57 -08005856# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005857 if ($^V && $^V ge 5.10.0 &&
5858 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005859 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005860
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005861 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005862 my $ms_val = $7;
5863 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005864
Andy Whitcroft554e1652012-01-10 15:09:57 -08005865 if ($ms_size =~ /^(0x|)0$/i) {
5866 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005867 "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 -08005868 } elsif ($ms_size =~ /^(0x|)1$/i) {
5869 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005870 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5871 }
5872 }
5873
Joe Perches98a9bba2014-01-23 15:54:52 -08005874# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perchesf333195d2016-10-11 13:51:53 -07005875# if ($^V && $^V ge 5.10.0 &&
5876# defined $stat &&
5877# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5878# if (WARN("PREFER_ETHER_ADDR_COPY",
5879# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5880# $fix) {
5881# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5882# }
5883# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005884
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005885# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perchesf333195d2016-10-11 13:51:53 -07005886# if ($^V && $^V ge 5.10.0 &&
5887# defined $stat &&
5888# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5889# WARN("PREFER_ETHER_ADDR_EQUAL",
5890# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5891# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005892
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005893# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5894# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perchesf333195d2016-10-11 13:51:53 -07005895# if ($^V && $^V ge 5.10.0 &&
5896# defined $stat &&
5897# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5898#
5899# my $ms_val = $7;
5900#
5901# if ($ms_val =~ /^(?:0x|)0+$/i) {
5902# if (WARN("PREFER_ETH_ZERO_ADDR",
5903# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5904# $fix) {
5905# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5906# }
5907# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5908# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5909# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5910# $fix) {
5911# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5912# }
5913# }
5914# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005915
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005916# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005917 if ($^V && $^V ge 5.10.0 &&
5918 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005919 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005920 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005921 my $call = $1;
5922 my $cast1 = deparenthesize($2);
5923 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005924 my $cast2 = deparenthesize($7);
5925 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005926 my $cast;
5927
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005928 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005929 $cast = "$cast1 or $cast2";
5930 } elsif ($cast1 ne "") {
5931 $cast = $cast1;
5932 } else {
5933 $cast = $cast2;
5934 }
5935 WARN("MINMAX",
5936 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005937 }
5938 }
5939
Joe Perches4a273192012-07-30 14:41:20 -07005940# check usleep_range arguments
5941 if ($^V && $^V ge 5.10.0 &&
5942 defined $stat &&
5943 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5944 my $min = $1;
5945 my $max = $7;
5946 if ($min eq $max) {
5947 WARN("USLEEP_RANGE",
5948 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5949 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5950 $min > $max) {
5951 WARN("USLEEP_RANGE",
5952 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5953 }
5954 }
5955
Joe Perches823b7942013-11-12 15:10:15 -08005956# check for naked sscanf
5957 if ($^V && $^V ge 5.10.0 &&
5958 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005959 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005960 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5961 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5962 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5963 my $lc = $stat =~ tr@\n@@;
5964 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07005965 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08005966 WARN("NAKED_SSCANF",
5967 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5968 }
5969
Joe Perchesafc819a2014-06-04 16:12:08 -07005970# check for simple sscanf that should be kstrto<foo>
5971 if ($^V && $^V ge 5.10.0 &&
5972 defined $stat &&
5973 $line =~ /\bsscanf\b/) {
5974 my $lc = $stat =~ tr@\n@@;
5975 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07005976 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07005977 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5978 my $format = $6;
5979 my $count = $format =~ tr@%@%@;
5980 if ($count == 1 &&
5981 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5982 WARN("SSCANF_TO_KSTRTO",
5983 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5984 }
5985 }
5986 }
5987
Joe Perches70dc8a42013-09-11 14:23:58 -07005988# check for new externs in .h files.
5989 if ($realfile =~ /\.h$/ &&
5990 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005991 if (CHK("AVOID_EXTERNS",
5992 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005993 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005994 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005995 }
5996 }
5997
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005998# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005999 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006000 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006001 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006002 my $function_name = $1;
6003 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006004
6005 my $s = $stat;
6006 if (defined $cond) {
6007 substr($s, 0, length($cond), '');
6008 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006009 if ($s =~ /^\s*;/ &&
6010 $function_name ne 'uninitialized_var')
6011 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006012 WARN("AVOID_EXTERNS",
6013 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006014 }
6015
6016 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006017 WARN("FUNCTION_ARGUMENTS",
6018 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006019 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006020
6021 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6022 $stat =~ /^.\s*extern\s+/)
6023 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006024 WARN("AVOID_EXTERNS",
6025 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006026 }
6027
Joe Perchesa0ad7592017-07-10 15:52:19 -07006028# check for function declarations that have arguments without identifier names
6029 if (defined $stat &&
Miles Chen25bdda22017-11-17 15:28:34 -08006030 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Joe Perchesca0d8922016-10-11 13:52:16 -07006031 $1 ne "void") {
6032 my $args = trim($1);
6033 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6034 my $arg = trim($1);
6035 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6036 WARN("FUNCTION_ARGUMENTS",
6037 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6038 }
6039 }
6040 }
6041
Joe Perchesa0ad7592017-07-10 15:52:19 -07006042# check for function definitions
6043 if ($^V && $^V ge 5.10.0 &&
6044 defined $stat &&
6045 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6046 $context_function = $1;
6047
6048# check for multiline function definition with misplaced open brace
6049 my $ok = 0;
6050 my $cnt = statement_rawlines($stat);
6051 my $herectx = $here . "\n";
6052 for (my $n = 0; $n < $cnt; $n++) {
6053 my $rl = raw_line($linenr, $n);
6054 $herectx .= $rl . "\n";
6055 $ok = 1 if ($rl =~ /^[ \+]\{/);
6056 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6057 last if $rl =~ /^[ \+].*\{/;
6058 }
6059 if (!$ok) {
6060 ERROR("OPEN_BRACE",
6061 "open brace '{' following function definitions go on the next line\n" . $herectx);
6062 }
6063 }
6064
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006065# checks for new __setup's
6066 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6067 my $name = $1;
6068
6069 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006070 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006071 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006072 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006073 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006074
6075# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006076 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006077 WARN("UNNECESSARY_CASTS",
6078 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006079 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006080
Joe Perchesa640d252013-07-03 15:05:21 -07006081# alloc style
6082# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6083 if ($^V && $^V ge 5.10.0 &&
6084 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6085 CHK("ALLOC_SIZEOF_STRUCT",
6086 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6087 }
6088
Joe Perches60a55362014-06-04 16:12:07 -07006089# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
6090 if ($^V && $^V ge 5.10.0 &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006091 defined $stat &&
6092 $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 -07006093 my $oldfunc = $3;
6094 my $a1 = $4;
6095 my $a2 = $10;
6096 my $newfunc = "kmalloc_array";
6097 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006098 my $r1 = $a1;
6099 my $r2 = $a2;
6100 if ($a1 =~ /^sizeof\s*\S/) {
6101 $r1 = $a2;
6102 $r2 = $a1;
6103 }
6104 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6105 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006106 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006107 my $herectx = get_stat_here($linenr, $cnt, $here);
6108
Joe Perches60a55362014-06-04 16:12:07 -07006109 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006110 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6111 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006112 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006113 $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 -07006114 }
6115 }
6116 }
6117
Joe Perches972fdea2013-04-29 16:18:12 -07006118# check for krealloc arg reuse
6119 if ($^V && $^V ge 5.10.0 &&
6120 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6121 WARN("KREALLOC_ARG_REUSE",
6122 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6123 }
6124
Joe Perches5ce59ae2013-02-21 16:44:18 -08006125# check for alloc argument mismatch
6126 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6127 WARN("ALLOC_ARRAY_ARGS",
6128 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6129 }
6130
Joe Perchescaf2a542011-01-12 16:59:56 -08006131# check for multiple semicolons
6132 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006133 if (WARN("ONE_SEMICOLON",
6134 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6135 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006136 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006137 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006138 }
6139
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006140# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6141 if ($realfile !~ m@^include/uapi/@ &&
6142 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006143 my $ull = "";
6144 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6145 if (CHK("BIT_MACRO",
6146 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6147 $fix) {
6148 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6149 }
6150 }
6151
Joe Perches2d632742016-05-20 17:04:00 -07006152# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6153 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6154 my $config = $1;
6155 if (WARN("PREFER_IS_ENABLED",
6156 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6157 $fix) {
6158 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6159 }
6160 }
6161
Joe Perchese81f2392014-08-06 16:11:25 -07006162# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006163 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6164 my $has_break = 0;
6165 my $has_statement = 0;
6166 my $count = 0;
6167 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006168 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006169 $prevline--;
6170 my $rline = $rawlines[$prevline - 1];
6171 my $fline = $lines[$prevline - 1];
6172 last if ($fline =~ /^\@\@/);
6173 next if ($fline =~ /^\-/);
6174 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6175 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6176 next if ($fline =~ /^.[\s$;]*$/);
6177 $has_statement = 1;
6178 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006179 $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 -08006180 }
6181 if (!$has_break && $has_statement) {
6182 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006183 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006184 }
6185 }
6186
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006187# check for switch/default statements without a break;
6188 if ($^V && $^V ge 5.10.0 &&
6189 defined $stat &&
6190 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006191 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006192 my $herectx = get_stat_here($linenr, $cnt, $here);
6193
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006194 WARN("DEFAULT_NO_BREAK",
6195 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006196 }
6197
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006198# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006199 if ($line =~ /\b__FUNCTION__\b/) {
6200 if (WARN("USE_FUNC",
6201 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6202 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006203 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006204 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006205 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006206
Joe Perches62ec8182015-02-13 14:38:18 -08006207# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6208 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6209 ERROR("DATE_TIME",
6210 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6211 }
6212
Joe Perches2c924882012-03-23 15:02:20 -07006213# check for use of yield()
6214 if ($line =~ /\byield\s*\(\s*\)/) {
6215 WARN("YIELD",
6216 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6217 }
6218
Joe Perches179f8f42013-07-03 15:05:30 -07006219# check for comparisons against true and false
6220 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6221 my $lead = $1;
6222 my $arg = $2;
6223 my $test = $3;
6224 my $otype = $4;
6225 my $trail = $5;
6226 my $op = "!";
6227
6228 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6229
6230 my $type = lc($otype);
6231 if ($type =~ /^(?:true|false)$/) {
6232 if (("$test" eq "==" && "$type" eq "true") ||
6233 ("$test" eq "!=" && "$type" eq "false")) {
6234 $op = "";
6235 }
6236
6237 CHK("BOOL_COMPARISON",
6238 "Using comparison to $otype is error prone\n" . $herecurr);
6239
6240## maybe suggesting a correct construct would better
6241## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6242
6243 }
6244 }
6245
Joe Perches5d430902018-04-10 16:34:25 -07006246# check for bool bitfields
6247 if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6248 WARN("BOOL_BITFIELD",
6249 "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6250 }
6251
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006252# check for semaphores initialized locked
6253 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006254 WARN("CONSIDER_COMPLETION",
6255 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006256 }
Joe Perches6712d852012-03-23 15:02:20 -07006257
Joe Perches67d0a072011-10-31 17:13:10 -07006258# recommend kstrto* over simple_strto* and strict_strto*
6259 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006260 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006261 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006262 }
Joe Perches6712d852012-03-23 15:02:20 -07006263
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006264# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006265 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006266 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006267 "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 -07006268 }
Joe Perches6712d852012-03-23 15:02:20 -07006269
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006270# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006271# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006272 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006273 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006274 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006275 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006276 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006277
6278# use of NR_CPUS is usually wrong
6279# ignore definitions of NR_CPUS and usage to define arrays as likely right
6280 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006281 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6282 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006283 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6284 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6285 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006286 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006287 WARN("NR_CPUS",
6288 "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 -07006289 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006290
Joe Perches52ea8502013-11-12 15:10:09 -08006291# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6292 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6293 ERROR("DEFINE_ARCH_HAS",
6294 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6295 }
6296
Joe Perchesacd93622015-02-13 14:38:38 -08006297# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6298 if ($^V && $^V ge 5.10.0 &&
6299 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6300 WARN("LIKELY_MISUSE",
6301 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6302 }
6303
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006304# whine mightly about in_atomic
6305 if ($line =~ /\bin_atomic\s*\(/) {
6306 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006307 ERROR("IN_ATOMIC",
6308 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006309 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006310 WARN("IN_ATOMIC",
6311 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006312 }
6313 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006314
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006315# check for mutex_trylock_recursive usage
6316 if ($line =~ /mutex_trylock_recursive/) {
6317 ERROR("LOCKING",
6318 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6319 }
6320
Peter Zijlstra1704f472010-03-19 01:37:42 +01006321# check for lockdep_set_novalidate_class
6322 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6323 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6324 if ($realfile !~ m@^kernel/lockdep@ &&
6325 $realfile !~ m@^include/linux/lockdep@ &&
6326 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006327 ERROR("LOCKDEP",
6328 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006329 }
6330 }
Dave Jones88f88312011-01-12 16:59:59 -08006331
Joe Perchesb392c642015-04-16 12:44:16 -07006332 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6333 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006334 WARN("EXPORTED_WORLD_WRITABLE",
6335 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006336 }
Joe Perches24358802014-04-03 14:49:13 -07006337
Joe Perches00180462018-02-06 15:38:55 -08006338# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6339# and whether or not function naming is typical and if
6340# DEVICE_ATTR permissions uses are unusual too
6341 if ($^V && $^V ge 5.10.0 &&
6342 defined $stat &&
6343 $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*\)/) {
6344 my $var = $1;
6345 my $perms = $2;
6346 my $show = $3;
6347 my $store = $4;
6348 my $octal_perms = perms_to_octal($perms);
6349 if ($show =~ /^${var}_show$/ &&
6350 $store =~ /^${var}_store$/ &&
6351 $octal_perms eq "0644") {
6352 if (WARN("DEVICE_ATTR_RW",
6353 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6354 $fix) {
6355 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6356 }
6357 } elsif ($show =~ /^${var}_show$/ &&
6358 $store =~ /^NULL$/ &&
6359 $octal_perms eq "0444") {
6360 if (WARN("DEVICE_ATTR_RO",
6361 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6362 $fix) {
6363 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6364 }
6365 } elsif ($show =~ /^NULL$/ &&
6366 $store =~ /^${var}_store$/ &&
6367 $octal_perms eq "0200") {
6368 if (WARN("DEVICE_ATTR_WO",
6369 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6370 $fix) {
6371 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6372 }
6373 } elsif ($octal_perms eq "0644" ||
6374 $octal_perms eq "0444" ||
6375 $octal_perms eq "0200") {
6376 my $newshow = "$show";
6377 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6378 my $newstore = $store;
6379 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6380 my $rename = "";
6381 if ($show ne $newshow) {
6382 $rename .= " '$show' to '$newshow'";
6383 }
6384 if ($store ne $newstore) {
6385 $rename .= " '$store' to '$newstore'";
6386 }
6387 WARN("DEVICE_ATTR_FUNCTIONS",
6388 "Consider renaming function(s)$rename\n" . $herecurr);
6389 } else {
6390 WARN("DEVICE_ATTR_PERMS",
6391 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6392 }
6393 }
6394
Joe Perches515a2352014-04-03 14:49:24 -07006395# Mode permission misuses where it seems decimal should be octal
6396# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006397# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6398# specific definition of not visible in sysfs.
6399# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6400# use the default permissions
Joe Perches515a2352014-04-03 14:49:24 -07006401 if ($^V && $^V ge 5.10.0 &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006402 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006403 $line =~ /$mode_perms_search/) {
6404 foreach my $entry (@mode_permission_funcs) {
6405 my $func = $entry->[0];
6406 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006407
Joe Perches459cf0a2016-10-11 13:52:19 -07006408 my $lc = $stat =~ tr@\n@@;
6409 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006410 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006411
Joe Perches515a2352014-04-03 14:49:24 -07006412 my $skip_args = "";
6413 if ($arg_pos > 1) {
6414 $arg_pos--;
6415 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6416 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006417 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006418 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006419 my $val = $1;
6420 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006421 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6422 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6423 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006424 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006425 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006426 }
6427 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006428 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006429 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006430 }
Joe Perches24358802014-04-03 14:49:13 -07006431 }
6432 }
6433 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006434
Joe Perches459cf0a2016-10-11 13:52:19 -07006435# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006436 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006437 my $oval = $1;
6438 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006439 if (WARN("SYMBOLIC_PERMS",
6440 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6441 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006442 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006443 }
6444 }
6445
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006446# validate content of MODULE_LICENSE against list from include/linux/module.h
6447 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6448 my $extracted_string = get_quoted_string($line, $rawline);
6449 my $valid_licenses = qr{
6450 GPL|
6451 GPL\ v2|
6452 GPL\ and\ additional\ rights|
6453 Dual\ BSD/GPL|
6454 Dual\ MIT/GPL|
6455 Dual\ MPL/GPL|
6456 Proprietary
6457 }x;
6458 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6459 WARN("MODULE_LICENSE",
6460 "unknown module license " . $extracted_string . "\n" . $herecurr);
6461 }
6462 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006463 }
6464
6465 # If we have no input at all, then there is nothing to report on
6466 # so just keep quiet.
6467 if ($#rawlines == -1) {
6468 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006469 }
6470
Andy Whitcroft8905a672007-11-28 16:21:06 -08006471 # In mailback mode only produce a report in the negative, for
6472 # things that appear to be patches.
6473 if ($mailback && ($clean == 1 || !$is_patch)) {
6474 exit(0);
6475 }
6476
6477 # This is not a patch, and we are are in 'no-patch' mode so
6478 # just keep quiet.
6479 if (!$chk_patch && !$is_patch) {
6480 exit(0);
6481 }
6482
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006483 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006484 ERROR("NOT_UNIFIED_DIFF",
6485 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006486 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006487 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006488 ERROR("MISSING_SIGN_OFF",
6489 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006490 }
6491
Andy Whitcroft8905a672007-11-28 16:21:06 -08006492 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006493 if ($summary && !($clean == 1 && $quiet == 1)) {
6494 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006495 print "total: $cnt_error errors, $cnt_warn warnings, " .
6496 (($check)? "$cnt_chk checks, " : "") .
6497 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006498 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006499
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006500 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006501 # If there were any defects found and not already fixing them
6502 if (!$clean and !$fix) {
6503 print << "EOM"
6504
6505NOTE: For some of the reported defects, checkpatch may be able to
6506 mechanically convert to the typical style using --fix or --fix-inplace.
6507EOM
6508 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006509 # If there were whitespace errors which cleanpatch can fix
6510 # then suggest that.
6511 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006512 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006513 print << "EOM"
6514
6515NOTE: Whitespace errors detected.
6516 You may wish to use scripts/cleanpatch or scripts/cleanfile
6517EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006518 }
6519 }
6520
Joe Perchesd752fcc2014-08-06 16:11:05 -07006521 if ($clean == 0 && $fix &&
6522 ("@rawlines" ne "@fixed" ||
6523 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006524 my $newfile = $filename;
6525 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006526 my $linecount = 0;
6527 my $f;
6528
Joe Perchesd752fcc2014-08-06 16:11:05 -07006529 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6530
Joe Perches3705ce52013-07-03 15:05:31 -07006531 open($f, '>', $newfile)
6532 or die "$P: Can't open $newfile for write\n";
6533 foreach my $fixed_line (@fixed) {
6534 $linecount++;
6535 if ($file) {
6536 if ($linecount > 3) {
6537 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006538 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006539 }
6540 } else {
6541 print $f $fixed_line . "\n";
6542 }
6543 }
6544 close($f);
6545
6546 if (!$quiet) {
6547 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006548
Joe Perches3705ce52013-07-03 15:05:31 -07006549Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6550
6551Do _NOT_ trust the results written to this file.
6552Do _NOT_ submit these changes without inspecting them for correctness.
6553
6554This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6555No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006556EOM
6557 }
6558 }
6559
Joe Perchesd8469f12015-06-25 15:03:00 -07006560 if ($quiet == 0) {
6561 print "\n";
6562 if ($clean == 1) {
6563 print "$vname has no obvious style problems and is ready for submission.\n";
6564 } else {
6565 print "$vname has style problems, please review.\n";
6566 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006567 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006568 return $clean;
6569}